<?php
/* --------------------------------------------------------------
   google_shopping.php 2013-07-31 gm
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2013 Gambio GmbH
   Released under the GNU General Public License
   --------------------------------------------------------------
*/

require_once(DIR_FS_CATALOG . 'inc/xtc_get_countries.inc.php');

class Google_Shopping
{
	var $v_partnerlink;
	var $v_export_type;
	var $v_module_name;
	var $v_module_homepage;
	var $v_module_format;
	var $v_module_export_filename;
	var $v_filename;
	var $v_keyname;
	var $v_linkname;
	var $v_delimiter;
	var	$v_enclosure;

	var $v_field_filename;
	var $v_field_customers_groups;
	var $v_field_currency;
	var $v_field_shipping_costs;
	var $v_field_shipping_costs_free;
	var $v_field_attributes;
	var $v_field_campaign;
	var $v_field_export;
	var $v_field_additional_fields_array = array();
	var $v_shipping_country_id;

	var $v_category_file_path;
	var	$v_shipping_availability_array = array();
	var	$v_availability_array = array();
	var $v_allowed_units_array = array();
	var $v_allowed_unit_values_array = array();
	var $v_allowed_variants_array = array();
	var $v_use_variants = true;
	
	var $v_coo_additional_fields_controller;

	function Google_Shopping()
	{
		$this->save_config();
		$this->load_availabilities();
		$this->load_shipping_country();
		$this->formSettings();
		
		$this->v_allowed_units_array = array('mg', 'g', 'kg', 'ml', 'cl', 'l', 'cm', 'm', 'm2', 'cbm');
		$this->v_allowed_unit_values_array = array(1, 10, 100, 75, 50, 1000);
		
		$t_installed = gm_get_conf('GM_GOOGLE_SHOPPING_STATUS', 'ASSOC', true);
		
		if($t_installed === '1')
		{
			$this->v_coo_additional_fields_controller = MainFactory::create_object('AdditionalFieldControl');
			$this->load_additional_field_conf();
			$this->load_variants_fields_conf();			
		}		
	}

	function formSettings()
	{
		$this->v_partnerlink='';
		$this->v_export_type='comparison';
		$this->v_module_name='Google Shopping';
		$this->v_module_homepage='www.google.de/prdhp';
		$this->v_module_format='txt';
		$this->v_module_export_filename='google_shopping.txt';
		$this->v_filename='google_shopping.php';
		$this->v_keyname='GOOGLE_SHOPPING';
		$this->v_linkname='google_shopping';
		$this->v_delimiter="\t";
		$this->v_enclosure='';

		$this->v_field_filename=true;
		$this->v_field_customers_groups=true;
		$this->v_field_currency=true;
		$this->v_field_shipping_costs=true;
		$this->v_field_shipping_costs_free=true;
		$this->v_field_attributes=true;
		$this->v_field_campaign=true;
		$this->v_field_export=true;
		
		$this->v_category_file_path = 'http://www.google.com/basepages/producttype/taxonomy.de-DE.txt';
	}

	function formAddOn()
	{
		foreach(xtc_get_countriesList('', true, true) as $t_country)
		{
			$t_country_array[] = array('id' => $t_country['countries_id'], 'text' => $t_country['countries_name'] . ': ' . $t_country['countries_iso_code_2']);
		}
		
		$t_addOns = '<tr style="vertical-align:top; background-color:#d6e6f3;">
				   <td class="dataTableContent_gm"><strong>Google Kategorie URL</strong></td>
				   <td class="dataTableContent_gm">'.xtc_draw_input_field('categorie_file_path', gm_get_conf('GM_GOOGLE_SHOPPING_CATEGORY_FILE_PATH', 'ASSOC', true), 'style="width: 450px;"')
				   .'<br />Geben Sie hier den Pfad zur Taxonomy Datei f&uuml;r die Google Produkt-Kategorien an.';

		$t_addOns .= '<tr style="vertical-align:top; background-color:#d6e6f3;">
				   <td class="dataTableContent_gm"><strong>fehlende MPN generieren</strong></td>
				   <td class="dataTableContent_gm">'.xtc_draw_radio_field('auto_mpn', '0', ((int)gm_get_conf('GM_GOOGLE_SHOPPING_AUTO_MPN', 'ASSOC', true)) ? false : true).'nein'
				   .'<br />'.xtc_draw_radio_field('auto_mpn', '1', ((int)gm_get_conf('GM_GOOGLE_SHOPPING_AUTO_MPN', 'ASSOC', true)) ? true : false).'ja'
					.'<br />Sollen fehlende MPN automatisch aus der Artikelnummer oder, falls nicht vorhanden, per Zufall generiert und gespeichert werden?<br /><br />'
					.'<input type="checkbox" name="mpn_attribute_model" value="1"' . ((gm_get_conf('GM_GOOGLE_SHOPPING_MPN_ATTRIBUTE_MODEL') == '1') ? ' checked="checked"' : '') . ' /> Attributartikel-MPN nicht aus Artikelnummer und Attribut-Artikelnummer, sondern nur aus Attribut-Artikelnummer generieren.';

		$t_addOns .= '<tr style="vertical-align:top; background-color:#d6e6f3;">
				   <td class="dataTableContent_gm"><strong>fehlende Marken generieren</strong></td>
				   <td class="dataTableContent_gm">'.xtc_draw_radio_field('auto_brand', '0', ((int)gm_get_conf('GM_GOOGLE_SHOPPING_AUTO_BRAND', 'ASSOC', true)) ? false : true).'nein'
				   .'<br />'.xtc_draw_radio_field('auto_brand', '1', ((int)gm_get_conf('GM_GOOGLE_SHOPPING_AUTO_BRAND', 'ASSOC', true)) ? true : false).'ja'
					.'<br />Sollen fehlende Marken aus dem Herstellernamen generiert und gespeichert werden?';

		$t_addOns .= '<tr style="vertical-align:top; background-color:#d6e6f3;">
				   <td class="dataTableContent_gm"><strong>Versandland</strong></td>
				   <td class="dataTableContent_gm">' . xtc_draw_pull_down_menu('shipping_country', $t_country_array, $this->v_shipping_country_id)
					.'<br /> Versandland ausw&auml;hlen';

		$t_addOns .= '<tr style="vertical-align:top; background-color:#d6e6f3;">
				   <td class="dataTableContent_gm"><strong>Farbe-Bezeichnungen</strong></td>
				   <td class="dataTableContent_gm">'.xtc_draw_input_field('GM_GOOGLE_SHOPPING_COLOR', gm_get_conf('GM_GOOGLE_SHOPPING_COLOR', 'ASSOC', true), 'style="width: 450px;"')
				   .'<br />Kommagetrennte Bezeichnungen f&uuml;r Farbe.';
		
		$t_addOns .= '<tr style="vertical-align:top; background-color:#d6e6f3;">
				   <td class="dataTableContent_gm"><strong>Gr&ouml;&szlig;e-Bezeichnungen</strong></td>
				   <td class="dataTableContent_gm">'.xtc_draw_input_field('GM_GOOGLE_SHOPPING_SIZE', gm_get_conf('GM_GOOGLE_SHOPPING_SIZE', 'ASSOC', true), 'style="width: 450px;"')
				   .'<br />Kommagetrennte Bezeichnungen f&uuml;r Gr&ouml;&szlig;e.';
		
		$t_addOns .= '<tr style="vertical-align:top; background-color:#d6e6f3;">
				   <td class="dataTableContent_gm"><strong>Material-Bezeichnungen</strong></td>
				   <td class="dataTableContent_gm">'.xtc_draw_input_field('GM_GOOGLE_SHOPPING_MATERIAL', gm_get_conf('GM_GOOGLE_SHOPPING_MATERIAL', 'ASSOC', true), 'style="width: 450px;"')
				   .'<br />Kommagetrennte Bezeichnungen f&uuml;r Material.';
		
		$t_addOns .= '<tr style="vertical-align:top; background-color:#d6e6f3;">
				   <td class="dataTableContent_gm"><strong>Muster-Bezeichnungen</strong></td>
				   <td class="dataTableContent_gm">'.xtc_draw_input_field('GM_GOOGLE_SHOPPING_PATTERN', gm_get_conf('GM_GOOGLE_SHOPPING_PATTERN', 'ASSOC', true), 'style="width: 450px;"')
				   .'<br />Kommagetrennte Bezeichnungen f&uuml;r Muster.';
				
		$t_addOns .= '<tr style="vertical-align:top; background-color:#d6e6f3;">
				   <td class="dataTableContent_gm"><strong>Zusatzfelder</strong></td>
				   <td class="dataTableContent_gm">'.xtc_draw_input_field('additional_fields', gm_get_conf('GM_GOOGLE_SHOPPING_ADDITIONAL_FIELDS', 'ASSOC', true), 'style="width: 800px;"')
				   .'<br />Kommagetrennte Liste der Zusatzfelder, die beim Export berücksichtigt werden sollen.';
		
		// return addon setup
		return($t_addOns);
	}
	
	function load_availabilities()
	{
		$t_availability_sql = "SELECT
									s.shipping_status_id,
									a.google_export_availability_id,
									a.availability
								FROM
									shipping_status_to_google_availability s,
									google_export_availability a
								WHERE s.google_export_availability_id = a.google_export_availability_id";
		$t_availability_result = xtc_db_query($t_availability_sql);
		while($t_availability_result_array = xtc_db_fetch_array($t_availability_result))
		{
			$this->v_shipping_availability_array[$t_availability_result_array['shipping_status_id']] = array('ID' => $t_availability_result_array['google_export_availability_id'],
																									'NAME' => $t_availability_result_array['availability']);
		}

		$t_availability_sql = "SELECT
									google_export_availability_id,
									availability
								FROM google_export_availability";
		$t_availability_result = xtc_db_query($t_availability_sql);
		while($t_availability_result_array = xtc_db_fetch_array($t_availability_result))
		{
			$this->v_availability_array[$t_availability_result_array['google_export_availability_id']] = $t_availability_result_array['availability'];
		}
	}
	
	function load_shipping_country()
	{
		$this->v_shipping_country_id = gm_get_conf('GM_GOOGLE_SHOPPING_SHIPPING_COUNTRY', 'ASSOC', true) ? gm_get_conf('GM_GOOGLE_SHOPPING_SHIPPING_COUNTRY', 'ASSOC', true) : STORE_COUNTRY;
	}

	function save_config()
	{
		if(isset($_POST['shipping_country']))
		{
			gm_set_conf('GM_GOOGLE_SHOPPING_SHIPPING_COUNTRY', (int)$_POST['shipping_country']);
		}
		
		if(isset($_POST['additional_fields']))
		{
			gm_set_conf('GM_GOOGLE_SHOPPING_ADDITIONAL_FIELDS', xtc_db_input($_POST['additional_fields']));
		}
		
		if(isset($_POST['GM_GOOGLE_SHOPPING_COLOR']))
		{
			gm_set_conf('GM_GOOGLE_SHOPPING_COLOR', trim($_POST['GM_GOOGLE_SHOPPING_COLOR']));
			gm_set_conf('GM_GOOGLE_SHOPPING_SIZE', trim($_POST['GM_GOOGLE_SHOPPING_SIZE']));
			gm_set_conf('GM_GOOGLE_SHOPPING_MATERIAL', trim($_POST['GM_GOOGLE_SHOPPING_MATERIAL']));
			gm_set_conf('GM_GOOGLE_SHOPPING_PATTERN', trim($_POST['GM_GOOGLE_SHOPPING_PATTERN']));
		}
		
		if(isset($_POST['auto_mpn']) && isset($_POST['auto_brand']))
		{
			gm_set_conf('GM_GOOGLE_SHOPPING_AUTO_MPN', (int)$_POST['auto_mpn']);
			gm_set_conf('GM_GOOGLE_SHOPPING_AUTO_BRAND', (int)$_POST['auto_brand']);

			if(isset($_POST['mpn_attribute_model']) && $_POST['mpn_attribute_model'] == '1')
			{
				gm_set_conf('GM_GOOGLE_SHOPPING_MPN_ATTRIBUTE_MODEL', 1);
			}
			else
			{
				gm_set_conf('GM_GOOGLE_SHOPPING_MPN_ATTRIBUTE_MODEL', 0);
			}

			if((int)$_POST['auto_mpn'] == 1)
			{
				xtc_db_query("INSERT INTO products_item_codes (products_id)
								SELECT
									p.products_id AS products_id
								FROM
									products AS p LEFT JOIN products_item_codes AS pic USING (products_id)
								WHERE
									google_export_condition IS NULL");

				xtc_db_query("UPDATE
									products AS p,
									products_item_codes AS pic
								SET
									code_mpn = products_model
								WHERE
									pic.products_id = p.products_id AND
									(pic.code_mpn = '' OR pic.code_mpn IS NULL)");

				xtc_db_query("UPDATE
									products_item_codes AS pic
								SET
									code_mpn = UPPER(LEFT(CAST(MD5(pic.products_id) AS CHAR), 5))
								WHERE
									pic.code_mpn = '' OR
									pic.code_mpn IS NULL");

			}

			if((int)$_POST['auto_brand'] == 1)
			{
				xtc_db_query("INSERT INTO products_item_codes (products_id)
								SELECT
									p.products_id AS products_id
								FROM
									products AS p LEFT JOIN products_item_codes AS pic USING (products_id)
								WHERE
									google_export_condition IS NULL");

				xtc_db_query("UPDATE
									products AS p,
									products_item_codes AS pic,
									manufacturers AS m
								SET
									pic.brand_name = m.manufacturers_name
								WHERE
									p.products_id = pic.products_id AND
									p.manufacturers_id = m.manufacturers_id AND
									(pic.brand_name = '' OR pic.brand_name IS NULL)");
			}

			return true;
		}
		
		return false;
	}

	function formatResults($p_products_array)
	{
		// ignore vouchers
		if(strpos($p_products_array['products_model'], 'GIFT_') === 0)
		{
			return false;
		}
		
		$c_products_id = (int)$p_products_array['products_id'];
		if(isset($p_products_array['products_id_copy']))
		{
			$c_products_id = (int)$p_products_array['products_id_copy'];
		}
		
		$t_google_categories_array = array();

		$t_google_sql = "SELECT google_category
							FROM products_google_categories
							WHERE products_id = '" . $c_products_id . "'";
		$t_google_result = xtc_db_query($t_google_sql);
		while($t_google_result_array = xtc_db_fetch_array($t_google_result))
		{
			$t_google_categories_array[] = $t_google_result_array['google_category'];
		}

		$p_products_array['google_category'] = '';
		if(!empty($t_google_categories_array))
		{
			$p_products_array['google_category'] = implode('","', $t_google_categories_array);
		}

		$p_products_array['products_price'] = number_format((double)$p_products_array['products_price'], 2, ',', '') . ' ' . gm_get_conf('GM_GOOGLE_SHOPPING_CURRENCY', 'ASSOC', true);
		
//		 special_price_period
		if( (isset($p_products_array['specials_id']) && !empty($p_products_array['specials_id']) ) &&  ( isset( $p_products_array['special_status']) && $p_products_array['special_status'] == 1) )
		{
			$t_start_timestamp = $p_products_array['specials_date_added'];
			if(isset($p_products_array['specials_last_modified']) && !empty($p_products_array['specials_last_modified']))
			{
				$t_start_timestamp = $p_products_array['specials_last_modified'];
			}
			
			$t_end_timestamp = $p_products_array['expires_date'];
			if(empty($p_products_array['expires_date']))
			{
				$t_end_timestamp = strtotime('+1 year', $t_start_timestamp);
			}
			
			$p_products_array['expires_date'] = date('c', $t_start_timestamp) . '/' . date('c', $t_end_timestamp);
		}
		else
		{
			$p_products_array['expires_date'] = '';
		}
		 
		// FSK18
		$p_products_array['products_fsk18'] = $p_products_array['products_fsk18'] == 1 ? 'TRUE ' : 'FALSE';
		
		// shipping_costs
		if(trim($_POST['shipping_costs']) != '')
		{
			$t_country_result = xtc_db_query('SELECT countries_iso_code_2 FROM ' . TABLE_COUNTRIES . ' WHERE countries_id = ' . $this->v_shipping_country_id);
			$t_row = xtc_db_fetch_array($t_country_result);
			$t_country_iso = $t_row['countries_iso_code_2'];
			$p_products_array['products_shipping_costs'] = $t_country_iso . ':::' . number_format((double)$p_products_array['products_shipping_costs'], 2, '.', '') . ' ' . gm_get_conf('GM_GOOGLE_SHOPPING_CURRENCY', 'ASSOC', true);
		}
		else
		{
			$p_products_array['products_shipping_costs'] = number_format((double)$p_products_array['products_shipping_costs'], 2, '.', '');
		}
		
		$p_products_array['products_description'] = trim(no_html($p_products_array['products_description']));
		if(!empty($p_products_array['products_description']) && strlen($p_products_array['products_description']) > 1500)
		{
			$desc = $p_products_array['products_description'];
			$length = strpos($desc, " ", 1500);
			if($length !== false)
			{
				$p_products_array['products_description'] = substr($p_products_array['products_description'], 0, $length) . '[...]';
			}
		}

		$t_images_array = array();
		// 1-9 additional images
		for($i = 2; $i <= (int)MO_PICS+1 && $i < 11; $i++)
		{
			if(isset($p_products_array['products_image_' . $i]) && !empty($p_products_array['products_image_' . $i]))
			{
				$t_images_array[] = $p_products_array['products_image_' . $i];
			}
		}
		$p_products_array['products_images'] = implode(',', $t_images_array);

		$p_products_array['google_export_gtin'] = '';
		if(!empty($p_products_array['products_ean']))
		{
			$p_products_array['google_export_gtin'] = trim($p_products_array['products_ean']);
		}
		elseif(!empty($p_products_array['code_isbn']))
		{
			$p_products_array['google_export_gtin'] = trim($p_products_array['code_isbn']);
		}
		elseif(!empty($p_products_array['code_upc']))
		{
			$p_products_array['google_export_gtin'] = trim($p_products_array['code_upc']);
		}
		elseif(!empty($p_products_array['code_jan']))
		{
			$p_products_array['google_export_gtin'] = trim($p_products_array['code_jan']);
		}

		if(empty($p_products_array['google_export_condition']))
		{
			$p_products_array['google_export_condition'] = 'neu';
		}

		if(isset($p_products_array['products_attributes_id']) && isset($p_products_array['products_id_copy']))
		{
			$p_products_array['products_id'] = $p_products_array['products_id_copy'] . 'a' . $p_products_array['products_attributes_id'];
		}

		if(empty($p_products_array['google_export_availability_id']) 
				&& !empty($p_products_array['shipping_status_id'])
				&& isset($this->v_shipping_availability_array[(int)$p_products_array['shipping_status_id']]))
		{
			$p_products_array['google_export_availability_id'] = $this->v_shipping_availability_array[(int)$p_products_array['shipping_status_id']]['ID'];
			$p_products_array['google_export_availability'] = $this->v_shipping_availability_array[(int)$p_products_array['shipping_status_id']]['NAME'];
		}
		elseif(!empty($p_products_array['google_export_availability_id']) && isset($this->v_availability_array[(int)$p_products_array['google_export_availability_id']]))
		{
			$p_products_array['google_export_availability'] = $this->v_availability_array[(int)$p_products_array['google_export_availability_id']];
		}
		
		if (!empty($p_products_array['identifier_exists']) && !empty($p_products_array['google_export_gtin']))
		{
			$p_products_array['identifier_exists'] = 'TRUE';
		}
		else
		{
			$p_products_array['identifier_exists'] = 'FALSE';
		}

		if(!empty($p_products_array['code_mpn']) && isset($p_products_array['products_attributes_id']))
		{
			if(!empty($p_products_array['attributes_model']))
			{
				if(gm_get_conf('GM_GOOGLE_SHOPPING_MPN_ATTRIBUTE_MODEL', 'ASSOC', true) == '1')
				{
					$p_products_array['code_mpn'] = $p_products_array['attributes_model'];
				}
				else
				{
					$p_products_array['code_mpn'] .= $p_products_array['attributes_model'];
				}
			}
			else
			{
				$p_products_array['code_mpn'] .= '-' . $p_products_array['products_attributes_id'];
			}
		}
		elseif(isset($p_products_array['products_attributes_id']) && !empty($p_products_array['attributes_model']) && gm_get_conf('GM_GOOGLE_SHOPPING_MPN_ATTRIBUTE_MODEL', 'ASSOC', true) == '1')
		{
			$p_products_array['code_mpn'] = $p_products_array['attributes_model'];
		}
		
		$p_products_array['products_name'] = str_replace("\t", ' ', $p_products_array['products_name']);
		$p_products_array['products_description'] = str_replace("\t", ' ', $p_products_array['products_description']);
		$p_products_array['products_weight'] = (double)$p_products_array['products_weight'] . ' kg';
		
		
		$p_products_array['expiration_date'] = str_replace('0000-00-00', '', $p_products_array['expiration_date']);
		
		
		// addtional fields
		$this->set_additional_field_data($p_products_array);

		$this->set_vpe_data($p_products_array);		
		
		if(!empty($p_products_array['specials_id']))
		{
			$p_products_array['specials_new_products_price'] = $p_products_array['products_price'];
		}		
		
		if(isset($p_products_array['retail_price']))
		{
			$p_products_array['products_price'] = number_format((double)$p_products_array['retail_price'], 2, ',', '') . ' ' . gm_get_conf('GM_GOOGLE_SHOPPING_CURRENCY', 'ASSOC', true);;
		}
		
		$this->set_variants_data($p_products_array);
		
		return $p_products_array;
	}

	function exportScheme()
	{
		$t_csv_fields_array=array();
		$t_csv_fields_array['products_id'] = 'ID';
		$t_csv_fields_array['products_name'] = 'Titel';
		$t_csv_fields_array['products_description'] = 'Beschreibung';
		$t_csv_fields_array['google_category'] = 'Google Produktkategorie';
		$t_csv_fields_array['products_categories'] = 'Produkttyp';
		$t_csv_fields_array['products_link'] = 'Link';
		$t_csv_fields_array['products_image_1'] = 'Bildlink';
		$t_csv_fields_array['products_images'] = html_entity_decode_wrapper('Zus&auml;tzlicher Bildlink');
		$t_csv_fields_array['google_export_condition'] = 'Zustand';
		$t_csv_fields_array['google_export_availability'] = html_entity_decode_wrapper('Verf&uuml;gbarkeit');
		$t_csv_fields_array['products_price'] = 'Preis';
		$t_csv_fields_array['specials_new_products_price'] = 'Sonderangebotspreis';
		$t_csv_fields_array['expires_date'] = 'Sonderangebotszeitraum';
		$t_csv_fields_array['brand_name'] = 'Marke';
		$t_csv_fields_array['google_export_gtin'] = 'GTIN';
		$t_csv_fields_array['code_mpn']= 'MPN';
		$t_csv_fields_array['identifier_exists']= 'Kennzeichnung existiert';
		$t_csv_fields_array['gender']= 'Geschlecht';
		$t_csv_fields_array['age_group']= 'Altersgruppe';
		$t_csv_fields_array['products_shipping_costs']= 'Versand';
		$t_csv_fields_array['products_weight']= 'Versandgewicht';
		$t_csv_fields_array['products_fsk18']= 'Erwachsene';
		$t_csv_fields_array['unit_price_measure']= html_entity_decode_wrapper('Grundpreis Ma&szlig;');
		$t_csv_fields_array['unit_pricing_base_measure']= html_entity_decode_wrapper('Grundpreis Einheitsma&szlig;');
		$t_csv_fields_array['expiration_date']= 'Verfallsdatum';
		
		$t_csv_fields_array['Farbe']= 'Farbe'; 
		$t_csv_fields_array[html_entity_decode_wrapper('Gr&ouml;&szlig;e')]= html_entity_decode_wrapper('Gr&ouml;&szlig;e'); 
		$t_csv_fields_array['Material']= 'Material'; 
		$t_csv_fields_array['Muster']= 'Muster';
		$t_csv_fields_array['item_group_id']= 'Produktgruppe';
		
		// additional fields
		$t_additional_fields = $this->v_coo_additional_fields_controller->get_field_names_by_item_type('product');
		if(is_array($t_additional_fields) && count($t_additional_fields))
		{
			foreach($t_additional_fields as $t_coo_additional_field)
			{
				$t_field_name_array = $t_coo_additional_field->get_name_array();
				$t_field_name = $t_field_name_array[$_SESSION['languages_id']];
				if($this->is_field_selected($t_field_name))
					$t_csv_fields_array[$t_field_name] = $t_field_name;
			}
		}
		
		return($t_csv_fields_array);
	}
	
	// $p_variant_array = array('name' => 'Farbe', 'value' => 'rot')
	function get_variant_name($p_property_name)
	{
		$t_color = trim(gm_get_conf('GM_GOOGLE_SHOPPING_COLOR', 'ASSOC', true));
		$t_size = trim(gm_get_conf('GM_GOOGLE_SHOPPING_SIZE', 'ASSOC', true));
		$t_material = trim(gm_get_conf('GM_GOOGLE_SHOPPING_MATERIAL', 'ASSOC', true));
		$t_pattern = trim(gm_get_conf('GM_GOOGLE_SHOPPING_PATTERN', 'ASSOC', true));
		$t_variants_array = array();
		
		$t_variants_array['Farbe'] = array($t_color);
		if(strpos($t_color, ',') !== false)
		{
			$t_variants_array['Farbe'] = preg_split('/\s*,\s*/', $t_color);
		}
		
		$t_variants_array[html_entity_decode_wrapper('Gr&ouml;&szlig;e')] = array($t_size);
		if(strpos($t_size, ',') !== false)
		{
			$t_variants_array[html_entity_decode_wrapper('Gr&ouml;&szlig;e')] = preg_split('/\s*,\s*/', $t_size);
		}
		
		$t_variants_array['Material'] = array($t_material);
		if(strpos($t_material, ',') !== false)
		{
			$t_variants_array['Material'] = preg_split('/\s*,\s*/', $t_material);
		}
		
		$t_variants_array['Muster'] = array($t_pattern);
		if(strpos($t_pattern, ',') !== false)
		{
			$t_variants_array['Muster'] = preg_split('/\s*,\s*/', $t_pattern);
		}
		
		foreach($t_variants_array AS $t_variant_name => $t_values_array)
		{
			foreach($t_values_array AS $t_alternative_name)
			{
				if($p_property_name == $t_alternative_name)
				{
					return $t_variant_name;
				}
			}
		}
		
		return false;
	}
	
	function load_additional_field_conf()
	{
		$t_additional_fields = gm_get_conf('GM_GOOGLE_SHOPPING_ADDITIONAL_FIELDS', 'ASSOC', true);
		if($t_additional_fields === null || $t_additional_fields === false)
		{
			gm_set_conf('GM_GOOGLE_SHOPPING_ADDITIONAL_FIELDS', 'Farbe,Größe,Material,Muster,Adwords Gruppierung,Adwords Label,Adwords Weiterleitung,Energieeffizienzklasse,Ausgeschlossenes Ziel');
			$this->load_additional_field_conf();
		}
			
		if(strpos($t_additional_fields, ',') !== false)
		{
			$t_additional_fields = explode(',', $t_additional_fields);
			if(is_array($t_additional_fields))
			{
				foreach($t_additional_fields as $t_additional_field)
				{
					$this->v_field_additional_fields_array[] = trim($t_additional_field);
				}
			}
		}
		elseif(!empty($t_additional_fields))
		{
			$this->v_field_additional_fields_array[] = trim($t_additional_fields);
		}
	}
	
	function is_field_selected($p_additional_field_name)
	{
		if(in_array($p_additional_field_name, $this->v_field_additional_fields_array))
			return true;
			
		return false;
	}
	
	function set_additional_field_data(&$p_products_array)
	{
		if(!isset($p_products_array['products_id'])) return false;
		
		$c_products_id = $p_products_array['products_id'];
		
		$t_additional_fields = $this->v_coo_additional_fields_controller->get_fields_by_item_id_and_item_type($c_products_id, 'product');
		if(is_array($t_additional_fields) && count($t_additional_fields))
		{
			foreach($t_additional_fields as $t_coo_additional_field)
			{
				$t_field_name_array = $t_coo_additional_field->get_name_array();
				$t_field_name = $t_field_name_array[$_SESSION['languages_id']];
				if($this->is_field_selected($t_field_name))
				{
					$t_field_value = '';
					$t_coo_field_value_array = $t_coo_additional_field->get_field_value_array();
					foreach($t_coo_field_value_array as $t_coo_field_value)
					{
						$t_field_values_array = $t_coo_field_value->get_value_array();
						$t_field_value = '';
						if(count($t_field_values_array) != 0)
						{
							if($t_coo_additional_field->is_multilingual())
							{
								$t_field_value = $t_field_values_array[$_SESSION['languages_id']];
							}
							else
							{
								$t_field_value = $t_field_values_array[0];
							}
						}
					}
					$p_products_array[$t_field_name] = $t_field_value;
				}
			}
		}
	}
	
	function set_vpe_data(&$p_products_array)
	{
		if($p_products_array['products_vpe_name'] != '')
		{
			$t_vpe_name = trim($p_products_array['products_vpe_name']);
			preg_match('/([0-9]*)(.+)/', $t_vpe_name, $t_matches_array);
			
			$t_unit_value = (double)$t_matches_array[1];
			if(empty($t_unit_value))
			{
				$t_unit_value = 1;
			}
			$t_unit_name = trim($t_matches_array[2]);
			
			$p_products_array['unit_pricing_base_measure'] = '';
			$p_products_array['unit_price_measure'] = '';
			
			if(in_array($t_unit_name, $this->v_allowed_units_array) && in_array($t_unit_value, $this->v_allowed_unit_values_array))
			{
				$t_continue = true;
				
				switch($t_unit_value)
				{
					case 50:
					case 1000:
						if($t_unit_name != 'kg')
						{
							$t_continue = false;
						}
						break;
					case 75:
						if($t_unit_name != 'cl')
						{
							$t_continue = false;
						}
				}			
				
				if($t_continue === true)
				{
					$p_products_array['unit_pricing_base_measure'] = $t_unit_value . ' ' . $t_unit_name;

					$t_unit_price_measure_value = (string)((double)$p_products_array['vpe_multiplier'] * $t_unit_value);
					$t_unit_price_measure_value = str_replace('.', ',', $t_unit_price_measure_value);
					$p_products_array['unit_price_measure'] = $t_unit_price_measure_value . ' ' . $t_unit_name;
				}
			}		
		}
	}
	
	function set_variants_data(&$p_products_array)
	{
		$t_products_name_suffix_array = array();
						
		if((isset($p_products_array['properties']) && !empty($p_products_array['properties'])) || (isset($p_products_array['options']) && !empty($p_products_array['options'])))
		{
			$p_products_array['item_group_id'] = $p_products_array['products_id_copy'];
		}
		
		if(isset($p_products_array['properties']) && is_array($p_products_array['properties']))
		{
			foreach($p_products_array['properties'] AS $t_property_array)
			{
				$t_variant_name = $this->get_variant_name($t_property_array['properties_name']);
				
				if($t_variant_name !== false)
				{
					$p_products_array[$t_variant_name] = $t_property_array['values_name'];
				}
				else
				{
					$p_products_array['item_group_id'] .= 'P' . $t_property_array['properties_id'] . 'P' . $t_property_array['properties_values_id'];
					
					$t_products_name_suffix_array[] = $t_property_array['properties_name'] . ': ' . $t_property_array['values_name'];
				}
			}
		}
		
		if(isset($p_products_array['options']['names']) && is_array($p_products_array['options']['names']) && !empty($p_products_array['options']['names']))
		{
			foreach($p_products_array['options']['names'] AS $t_key => $t_names_array)
			{
				$t_variant_name = $this->get_variant_name($t_names_array['option_name']);

				if($t_variant_name !== false)
				{
					$p_products_array[$t_variant_name] = $t_names_array['value_name'];
				}
				else
				{
					$p_products_array['item_group_id'] .= 'A' . str_replace('_', 'A', $t_key);
					
					$t_products_name_suffix_array[] = $t_names_array['option_name'] . ': ' . $t_names_array['value_name'];
				}
			}
		}
		
		if(!empty($t_products_name_suffix_array))
		{
			$p_products_array['products_name'] .= ' (' . implode(', ', $t_products_name_suffix_array) . ')';
		}
	}
	
	function load_variants_fields_conf()
	{
		$t_color = gm_get_conf('GM_GOOGLE_SHOPPING_COLOR', 'ASSOC', true);
		if($t_color === null || $t_color === false)
		{
			gm_set_conf('GM_GOOGLE_SHOPPING_COLOR', 'Farbe');			
		}

		$t_size = gm_get_conf('GM_GOOGLE_SHOPPING_SIZE', 'ASSOC', true);
		if($t_size === null || $t_size === false)
		{
			gm_set_conf('GM_GOOGLE_SHOPPING_SIZE', html_entity_decode_wrapper('Gr&ouml;&szlig;e'));			
		}

		$t_material = gm_get_conf('GM_GOOGLE_SHOPPING_MATERIAL', 'ASSOC', true);
		if($t_material === null || $t_material === false)
		{
			gm_set_conf('GM_GOOGLE_SHOPPING_MATERIAL', 'Material');			
		}

		$t_pattern = gm_get_conf('GM_GOOGLE_SHOPPING_PATTERN', 'ASSOC', true);
		if($t_pattern === null || $t_pattern === false)
		{
			gm_set_conf('GM_GOOGLE_SHOPPING_PATTERN', 'Muster');			
		}
	}
}
