<?php 

$DataBase = & singleton('DataBase');

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

$admin_forms_fields = new admin_forms_fields();

$shop_type_of_delivery_id = to_int($_REQUEST['shop_type_of_delivery_id']);

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

$type_of_delivery_row = $shop->GetTypeOfDelivery($shop_type_of_delivery_id);

?>                
<h1><?php echo $GLOBALS['MSG_shops']['coupon_group_link']?></h1>
<p><a
	href="<?php echo $admin_forms->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&shop_type_of_delivery_id=$shop_type_of_delivery_id", 74, 'load_data')?>"onclick="<?php echo $admin_forms->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&shop_type_of_delivery_id=$shop_type_of_delivery_id", 74, 'load_data')?>"><?php echo $GLOBALS['MSG_shops']['coupon_group_link']?></a></p>
<?php

if($_FILES['import_price_file']['tmp_name'] != '')
{
	$fp = fopen($_FILES['import_price_file']['tmp_name'], "rb");

	if(!$fp)
	{
		return true;
	}
}
else
{
	return true;
}

$counter = 0;

// Цикл по строкам CSV-файла
while(!feof($fp))
{
	$current_csv_line_array = fgetcsv($fp, 10000, ';', '"');
	
	// Если пустая строка - пропускаем
	if (!is_array($current_csv_line_array) || (count($current_csv_line_array) == 1 && empty ($current_csv_line_array[0])))
	{
		continue;
	}
	
	if($current_csv_line_array)
	{
		$param = array();
	
		foreach($current_csv_line_array as $code => $data)
		{
			switch($code)
			{
				case 0:
				{
					// Название купона
					$param['shop_coupon_name'] = $data;
					
					break;
				}
				case 1:
				{
					// Скидка
					$param['shop_order_discount_id'] = $data;
					
					break;
				}
				case 2:
				{
					// Активность
					$param['shop_coupon_active'] = $data;
					
					break;
				}
				case 3:
				{
					// Количество
					$param['shop_coupon_count'] = $data;
					
					break;
				}
				case 4:
				{
					// Текст купона
					$param['shop_coupon_text'] = $data;
					
					break;
				}
				case 5:
				{
					// Идентификатор купона (для обновления)
					
					if(trim($data) != '')
					{
						$param['shop_coupon_id'] = $data;
					}
					
					break;
				}
				default:
				{
				}
			}
		}
		
		$param['shop_shops_id'] = $shop_shops_id;
		
		$shop->InsertCoupon($param);
		
		$counter++;
		
		// Очищаем массив
		$param_array = array();
	}
}

// Закрываем файл
@ fclose($fp);
 
?>
<p>
	<?php 
	show_message(sprintf($GLOBALS['MSG_shops']['import_coupons_result'], $counter));
	?>
</p>
<?php

return true;
?>