<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2010 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Форма для загрузки прайс-листов (2 из 2) csv.
 * 
 * Файл: /admin/shop/action/import_price_list_load_form_step_two_csv.php
 * 
 * @author Hostmake LLC
 * @version 5.x
 */
// Открываем файл для импорта
if ($fp = @fopen($floadfile, 'rb'))
{
	mb_internal_encoding("UTF-8");
	
	// Если импорт не в UTF-8
	if (strtoupper($_REQUEST['import_price_encoding']) != 'UTF-8'
	&& defined('ALT_SITE_LOCALE'))
	{
		setlocale(LC_ALL, ALT_SITE_LOCALE);
	}
	
	// Читаем и разбираем первую строку файла
	$row_field = fgetcsv($fp, 3000, $import_price_separator_value, $import_price_stop_value);

	setlocale(LC_ALL, SITE_LOCAL);
	// Локаль для чисел
	setlocale(LC_NUMERIC, 'POSIX');

	$row_field = CorrectToEncoding($row_field, 'UTF-8', $_REQUEST['import_price_encoding']);
	
	// Закрываем файл
	fclose($fp);
}

// Запоминаем количество элементов (количество полей)
$count_field = count($row_field);

// Максимальное время выполнения
$import_price_max_time = to_int($_REQUEST['import_price_max_time']);
$import_price_max_count = to_int($_REQUEST['import_price_max_count']);

$form_params = array();
$form_params['title'] = $GLOBALS['MSG_shops']['import_price_list_link'];
$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/shop/shop.php';

$admin_forms_fields = new admin_forms_fields();

$shop_shops_id = to_int($_REQUEST['shop_shops_id']);
$shop_groups_parent_id = to_int($_REQUEST['shop_groups_parent_id']);

$shop = & singleton('shop');
$warehouse = & singleton('warehouse');
$row = $shop->GetShop($shop_shops_id);

$path_array = $shop->GetShopPathArray($shop_groups_parent_id, false);

// Преобразуем идентификаторы групп в ссылки на группы.
$new_path_array = array();
$new_path_array[0]['name'] = $GLOBALS['MSG_shops']['header_admin__list_forms'];
$new_path_array[0]['link'] = $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', ' ', 54, 'load_data');
$new_path_array[0]['onclick'] = $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', ' ', 54, 'load_data');

$new_path_array[1]['name'] = $row['shop_shops_name'];
$new_path_array[1]['link'] = $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=0&shop_shops_id=$shop_shops_id", 65, 'load_data');
$new_path_array[1]['onclick'] = $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=0&shop_shops_id=$shop_shops_id", 65, 'load_data');

if ($path_array)
{
	foreach ($path_array as $key => $value)
	{
		$new_path_array[] = array (
		'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$key&shop_shops_id=$shop_shops_id", 65, 'load_data'),
		'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$key&shop_shops_id=$shop_shops_id", 65, 'load_data'),
		'name' => $value);
	}
}

$new_path_array[] = array("name" => $GLOBALS['MSG_shops']['import_price_list_title'],
"link" => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&action=import_price_list&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id", 0, 'load_data'),
"onclick" => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&action=import_price_list&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id", 0, 'load_data')
);

$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

// Параметры.
$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_shops']['tab']);

// Заполняем необходимые массивы данных для формировния компонента
$array_of_data = array(0 => $GLOBALS['MSG_shops']['!download'],
1 => $GLOBALS['MSG_shops']['groups_id'],
2 => $GLOBALS['MSG_shops']['name_groups'],
3 => $GLOBALS['MSG_shops']['groups_path'],
4 => $GLOBALS['MSG_shops']['shop_group_order_import_field'],
5 => $GLOBALS['MSG_shops']['groups_description'],
6 => $GLOBALS['MSG_shops']['groups_add_form_seo_title_import_field'],
7 => $GLOBALS['MSG_shops']['groups_add_form_seo_description_import_field'],
8 => $GLOBALS['MSG_shops']['groups_add_form_seo_keywords_import_field'],
9 => $GLOBALS['MSG_shops']['catalog_image_group'],
10 => $GLOBALS['MSG_shops']['catalog_small_image_group'],
11 => $GLOBALS['MSG_shops']['shop_groups_cml_id'],
12 => $GLOBALS['MSG_shops']['shop_groups_parent_cml_id'],
13 => $GLOBALS['MSG_shops']['currency_id'],
14 => $GLOBALS['MSG_shops']['tax_id'],
15 => $GLOBALS['MSG_shops']['producers_id'],
16 => $GLOBALS['MSG_shops']['producers_name'],
17 => $GLOBALS['MSG_shops']['sellers_id'], 
18 => $GLOBALS['MSG_shops']['sellers_name'],
19 => $GLOBALS['MSG_shops']['mesures_id'],
20 => $GLOBALS['MSG_shops']['mesures_value'],
21 => $GLOBALS['MSG_shops']['item_id'],
22 => $GLOBALS['MSG_shops']['catalog_value'],
23 => $GLOBALS['MSG_shops']['catalog_marking'],
24 => $GLOBALS['MSG_shops']['catalog_date'],
25 => $GLOBALS['MSG_shops']['catalog_description'],
26 => $GLOBALS['MSG_shops']['catalog_text'],
27 => $GLOBALS['MSG_shops']['catalog_image'],
28 => $GLOBALS['MSG_shops']['catalog_small_image'],
29 => $GLOBALS['MSG_shops']['catalog_label'],
30 => $GLOBALS['MSG_shops']['catalog_weight'],
31 => $GLOBALS['MSG_shops']['catalog_price'],
32 => $GLOBALS['MSG_shops']['catalog_is_active'],
33 => $GLOBALS['MSG_shops']['catalog_order'],
34 => $GLOBALS['MSG_shops']['catalog_path'],
35 => $GLOBALS['MSG_shops']['catalog_seo_title'],
36 => $GLOBALS['MSG_shops']['catalog_seo_description'],
37 => $GLOBALS['MSG_shops']['catalog_seo_keywords'],
38 => $GLOBALS['MSG_shops']['catalog_indexation'],
39 => $GLOBALS['MSG_shops']['export_yandex_market'],
40 => $GLOBALS['MSG_shops']['export_rambler_pokupki'],
41 => $GLOBALS['MSG_shops']['yandex_market_base_price'],
42 => $GLOBALS['MSG_shops']['yandex_market_card_of_models'],
43 => $GLOBALS['MSG_shops']['item_parent_mark'],
44 => $GLOBALS['MSG_shops']['eitem_import_plain_text'],
45 => $GLOBALS['MSG_shops']['eitem_import_file'],
46 => $GLOBALS['MSG_shops']['eitem_import_count'],
47 => $GLOBALS['MSG_shops']['catalog_item_put_end'],
48 => $GLOBALS['MSG_shops']['catalog_item_put_off'],
49 => $GLOBALS['MSG_shops']['shop_items_catalog_type'],
50 => $GLOBALS['MSG_shops']['import_additional_item_group'],
51 => $GLOBALS['MSG_shops']['import_special_price_count_item_from'],
52 => $GLOBALS['MSG_shops']['import_special_price_count_item_to'],
53 => $GLOBALS['MSG_shops']['import_special_price_price_value'],
54 => $GLOBALS['MSG_shops']['import_special_price_price_percent'],
55 => $GLOBALS['MSG_shops']['import_item_cml_id'],
56 => $GLOBALS['MSG_shops']['import_field_group_activity'],
57 => $GLOBALS['MSG_shops']['shop_parent_mark_id_soput'],
58 => $GLOBALS['MSG_shops']['shop_upload_csv_site_user_id'],
59 => $GLOBALS['MSG_shops']['shop_upload_csv_eitem_name'],
60 => $GLOBALS['MSG_shops']['shop_import_item_sales_notes']
);

$array_of_color = array(0 => '#999999',
1 => '#4E8400',
2 => '#4E8400',
3 => '#4E8400',
4 => '#4E8400',
5 => '#4E8400',
6 => '#4E8400',
7 => '#4E8400',
8 => '#4E8400',
9 => '#4E8400',
10 => '#4E8400',
11 => '#4E8400',
12 => '#4E8400',
13 => '#E39600',
14 => '#555555',
15 => '#7D00AC',
16 => '#7D00AC',
17 => '#0F10DE',
18 => '#0F10DE',
19 => '#BAAB00',
20 => '#BAAB00',
21 => '#0085B1',
22 => '#0085B1',
23 => '#0085B1',
24 => '#0085B1',
25 => '#0085B1',
26 => '#0085B1',
27 => '#0085B1',
28 => '#0085B1',
29 => '#0085B1',
30 => '#0085B1',
31 => '#0085B1',
32 => '#0085B1',
33 => '#0085B1',
34 => '#0085B1',
35 => '#0085B1',
36 => '#0085B1',
37 => '#0085B1',
38 => '#0085B1',
39 => '#0085B1',
40 => '#0085B1',
41 => '#0085B1',
42 => '#0085B1',
43 => '#0085B1',
44 => '#0085B1',
45 => '#0085B1',
46 => '#0085B1',
47 => '#0085B1',
48 => '#0085B1',
49 => '#0085B1',
50 => '#CC4488',
51 => '#CC4488',
52 => '#CC4488',
53 => '#CC4488',
54 => '#CC4488',
55 => '#CC4488',
56 => '#CC4488',
57 => '#CC1100',
58 => '#CC1100',
59 => '#CC1100',
60 => '#CD853F'
);

$array_of_values = array(0 => '',
1 => 'shop_groups_id',
2 => 'shop_groups_value',
3 => 'shop_groups_path',
4 => 'shop_groups_order',
5 => 'shop_groups_description',
6 => 'shop_groups_seo_title',
7 => 'shop_groups_seo_description',
8 => 'shop_groups_seo_keywords',
9 => 'shop_groups_image',
10 => 'shop_groups_small_image',
11 => 'shop_groups_cml_id',
12 => 'shop_shop_groups_parent_cml_id',
13 => 'shop_currency_id',
14 => 'shop_tax_id',
15 => 'shop_producers_list_id',
16 => 'shop_producers_list_value',
17 => 'shop_shop_sallers_id',
18 => 'shop_sallers_name',
19 => 'shop_mesures_id',
20 => 'shop_mesures_value',
21 => 'shop_items_catalog_item_id',
22 => 'shop_items_catalog_name',
23 => 'shop_items_catalog_marking',
24 => 'shop_shop_items_catalog_date_time',
25 => 'shop_items_catalog_description',
26 => 'shop_items_catalog_text',
27 => 'shop_items_catalog_image',
28 => 'shop_items_catalog_small_image',
29 => 'shop_items_catalog_label',
30 => 'shop_items_catalog_weight',
31 => 'shop_items_catalog_price',
32 => 'shop_items_catalog_is_active',
33 => 'shop_items_catalog_order',
34 => 'shop_items_catalog_path',
35 => 'shop_items_catalog_seo_title',
36 => 'shop_items_catalog_seo_description',
37 => 'shop_items_catalog_seo_keywords',
38 => 'shop_items_catalog_indexation',
39 => 'shop_shop_items_catalog_yandex_market_allow',
40 => 'shop_shop_items_catalog_rambler_pokupki_allow',
41 => 'shop_shop_items_catalog_yandex_market_bid',
42 => 'shop_shop_items_catalog_yandex_market_cid',
43 => 'shop_item_parent_mark',
44 => 'shop_eitems_text',
45 => 'shop_eitems_file',
46 => 'shop_eitem_count',
47 => 'shop_shop_items_catalog_putend_date',
48 => 'shop_shop_items_catalog_putoff_date',
49 => 'shop_shop_items_catalog_type',
50 => 'additional_group',
51 => 'shop_special_prices_from',
52 => 'shop_special_prices_to',
53 => 'shop_special_prices_price',
54 => 'shop_special_prices_percent',
55 => 'shop_items_cml_id',
56 => 'shop_groups_activity',
57 => 'shop_item_parent_soput',
58 => 'site_users_id',
59 => 'shop_eitem_name',
60 => 'shop_items_catalog_yandex_market_sales_notes'
);

// Выбираем дополнительные свойства для товара
// Выбираем дополнительные свойства для товара с учетом сортировки по группам дополнительных свойств и дополнительно получим имя группы
$query = "SELECT * FROM `shop_list_of_properties_table` LEFT JOIN `shop_properties_items_dir_table` USING(shop_properties_items_dir_id)
 WHERE shop_list_of_properties_table.shop_shops_id = '$shop_shops_id' 
 ORDER BY `shop_properties_items_dir_table`.`shop_properties_items_dir_id`, `shop_list_of_properties_order`";

$DataBase = & singleton('DataBase');

$result = $DataBase->query($query);
					
$count = mysql_num_rows($result);

for ($i = 0; $i < $count; $i++)
{
	$row = mysql_fetch_assoc($result);
	$array_of_data[] = $row['shop_list_of_properties_name'] . '&nbsp;[' . ((to_int($row['shop_properties_items_dir_id']) != 0)?$row['shop_properties_items_dir_name']:$GLOBALS['MSG_shops']['import_property_group_root']) . ']';
	$array_of_values[] = 'prop-'.$row['shop_list_of_properties_id'];
	$array_of_color[] = '#7FB8D6';
	
	 if(to_int($row['shop_list_of_properties_type']) == 1)
	{
		$array_of_data[] = $GLOBALS['MSG_shops']['import_small_images'] . $row['shop_list_of_properties_name'] . '&nbsp;[' . ((to_int($row['shop_properties_items_dir_id']) != 0)?$row['shop_properties_items_dir_name']:$GLOBALS['MSG_shops']['import_property_group_root']) . ']';
		$array_of_values[] = 'propsmall-'.$row['shop_list_of_properties_id'];
		$array_of_color[] = '#7FB8D6';
	} 
}

// Необходимо вывести все дополнительные свойства групп товаров
//$group_property_res = $shop->GetAllPropretyOfGroup($shop_shops_id);

// Получаем дополнительные свойства запросом вместе с группами
$sql = "SELECT *, IF (shop_properties_group_table.shop_properties_groups_dir_id = 0, 'Корневая группа', shop_properties_groups_dir_table.shop_properties_groups_dir_name) AS group_name FROM `shop_properties_group_table`
LEFT JOIN `shop_properties_groups_dir_table` USING(`shop_properties_groups_dir_id`)
        WHERE shop_properties_group_table.shop_shops_id = '$shop_shops_id'
				
				GROUP BY shop_properties_group_id
        ORDER BY `shop_properties_group_order` ASC"; 
				
$group_property_res = $DataBase->query($sql);

if($group_property_res)
{
	while($group_property_row = mysql_fetch_assoc($group_property_res))
	{
		$array_of_data[] = $group_property_row['shop_properties_group_name'] . " [" . $group_property_row['group_name'] . "]";
		$array_of_values[] = 'prop_group-'.$group_property_row['shop_properties_group_id'];
		$array_of_color[] = '#EBAB16';
	}
}

// Выбираем возможные цены для товара
$result = $shop->GetAllPricesForShop($shop_shops_id);

if ($result) 
{
	$count = mysql_num_rows($result);
}
else 
{
	$count = 0;
}


for ($i = 0; $i < $count; $i++)
{
	$row = mysql_fetch_assoc($result);
	$array_of_data[] = $row['shop_list_of_prices_name'];
	$array_of_values[] = 'price-'.$row['shop_list_of_prices_id'];
	$array_of_color[] = '#B03434';
}

// получаем список всех складов магазина
$aWarehouses = $warehouse->GetAllWarehousesForShop($shop_shops_id);

if($aWarehouses)
{
	foreach ($aWarehouses as $warehouse_row)
	{
		$array_of_data[] = sprintf($GLOBALS['MSG_shops']['warehouse_import_field'], $warehouse_row['shop_warehouse_name']);
		$array_of_values[] = 'warehouse-'.$warehouse_row['shop_warehouse_id'];
		$array_of_color[] = '#EB0016';
	}
}

// импортируемые поля
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'field';
$param['caption'] = $GLOBALS['MSG_shops']['import_price_list_import_param'];
$param['type'] = 14; // Выпадающие списки
$param['options']['count'] = count($row_field);
$param['options']['data_array'] = $array_of_data;
$param['options']['color_array'] = $array_of_color;
$param['options']['labels_array'] = $row_field;
$param['options']['values_array'] = $array_of_values;
$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

/////////////////
$import_price_action_items = to_int($_REQUEST['import_price_action_items']);

$import_price_action_delete_image = (isset($_REQUEST['import_price_action_delete_image']) === false
? 0 : 1);

$search_event_indexation = (isset($_REQUEST['search_event_indexation']))?true:false;

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'file_name';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $floadfile;
$admin_forms_fields->AddField($params);


$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'import_price_list_images_path';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $import_price_list_images_path;
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'alternative_file_pointer';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = to_str($_REQUEST['alternative_file_pointer']);
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'count_field';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $count_field;
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'import_price_action_items';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $import_price_action_items;
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'import_price_action_delete_image';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $import_price_action_delete_image;
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'search_event_indexation';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $search_event_indexation;
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_shops_id';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $shop_shops_id;
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'import_price_name_field_f';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $import_price_name_field_f;
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'import_price_parent_group_id';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $shop_groups_parent_id;
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'import_price_separator_value';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $import_price_separator_value;
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'import_price_stop_value';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $import_price_stop_value;
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'import_price_type';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $import_price_type;
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'import_price_max_count';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $import_price_max_count; 
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'import_price_max_time';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $import_price_max_time; 
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'import_price_encoding';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $_REQUEST['import_price_encoding']; 
$admin_forms_fields->AddField($params);

// Кнопка "Применить".
$params = array();
$params['name'] = 'import_data';
$params['caption'] = $GLOBALS['MSG_shops']['import_price_list_button_load'];
$params['attributes']['onclick'] = "return SendForm('{$admin_forms_fields->GetWindowId()}', '/admin/shop/shop.php', '&action=import_data', this, 0)";
$admin_forms_fields->AddButton($params);

// Отображение формы.
$admin_forms_fields->ShowForm();
