<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2010 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Форма добаления/редактирования сопутствующих товаров.
 * 
 * Файл: /admin/shop/action/form_edit_add_tying_products.php
 * 
 * @author Hostmake LLC
 * @version 5.x
 */
if (isset($id))
{
	$edit_tying_products = $id;
}
else
{
	$edit_tying_products = 0;
}
$datetime = & singleton('DateClass');

$form_params = array();
$form_params['title'] = $edit_tying_products==0?$GLOBALS['MSG_shops']['tying_products_add_form_title']:$GLOBALS['MSG_shops']['tying_products_edit_form_title'];
$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/shop/shop.php';
$form_params['form_attribs']['name'] = 'EditAddTyingProducts';

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

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

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

$DataBase = & singleton('DataBase');
$shop = & singleton('shop');

$admin_forms_fields = new admin_forms_fields();

// Получаем имя товара
$res = $shop->GetItem($shop_items_catalog_item_id);

$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);
	}
}

// Если мы пришли сюда из формы списка модификаций
if (isset($_REQUEST['shop_items_catalog_modification_id']))
{
	$shop_items_catalog_modification_id = to_int($_REQUEST['shop_items_catalog_modification_id']);

	$row = $shop->GetItem($shop_items_catalog_modification_id);

	$new_path_array[] = array("name" => sprintf($GLOBALS['MSG_shops']['item_modification_title'], $row['shop_items_catalog_name']),
	"link" => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&shop_items_catalog_modification_id=$shop_items_catalog_modification_id", 81, 'load_data'),
	"onclick" => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&shop_items_catalog_modification_id=$shop_items_catalog_modification_id", 81, 'load_data'));
}

$new_path_array[] = array("name" => sprintf($GLOBALS['MSG_shops']['show_tying_products_title'], $res['shop_items_catalog_name']),
"link" => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&shop_items_catalog_item_id=$shop_items_catalog_item_id", 79, 'load_data'),
"onclick" => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&shop_items_catalog_item_id=$shop_items_catalog_item_id", 79, 'load_data')
);

$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

// Редактировние сопутствующего товара
if ($edit_tying_products)
{
	$type = 1;

	$result = $shop->GetTyingProducts($edit_tying_products);

	$shop_items_catalog_item_id = to_int($result['shop_items_catalog_item_id']);

	$sho_shop_items_catalog_item_id = to_int($result['sho_shop_items_catalog_item_id']);
}
// Добавление сопутствующего товара
else
{
	$sho_shop_items_catalog_item_id = 0;
}

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


// Получаем список сопутствующих товаров текущему товару
$arr_s = array();

$arr = array();

$arr[0] = ' ... ';

$query = "SELECT *,
	shop_items_catalog_table.shop_items_catalog_name AS name 
	FROM shop_intermediate_table LEFT JOIN shop_items_catalog_table 
	ON shop_intermediate_table.shop_items_catalog_item_id = $shop_items_catalog_item_id 
	AND shop_intermediate_table.sho_shop_items_catalog_item_id = 
	shop_items_catalog_table.shop_items_catalog_item_id 
	WHERE shop_items_catalog_table.shop_items_catalog_name IS NOT NULL ";

$res = $DataBase->query($query);

while ($row = mysql_fetch_assoc($res)) 
{
	$arr_s[] = $row['shop_items_catalog_item_id'];
}

$query = "SELECT * FROM `shop_items_catalog_table` WHERE `shop_shops_id` = '$shop_shops_id'";

$res = $DataBase->query($query);

while ($row = mysql_fetch_assoc($res))
{
	if (in_array($row['shop_items_catalog_item_id'], $arr_s) || in_array($edit_tying_products, $arr_s)) 
	{
		continue;
	}
	$arr[$row['shop_items_catalog_item_id']] = $row['shop_items_catalog_name'];
}

// Список сопутствующих товаров
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'tying_products_name';
$params['caption'] = $GLOBALS['MSG_shops']['tying_products_name'];
$params['type'] = 2; // Выпадающий список.
$params['items']  = $arr;
$params['value'] = $sho_shop_items_catalog_item_id;
$admin_forms_fields->AddField($params);

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

// передеам скрытое поле с идентификатором адреса если он есть
if ($edit_tying_products)
{
	$params = array();
	$params['tab_id'] = $tab_id;
	$params['name'] = 'edit_tying_products';
	$params['caption'] = '';
	$params['type'] = 4; // hidden.
	$params['value'] = $edit_tying_products;
	$admin_forms_fields->AddField($params);
}

// Если пришли из формы модификаций
if (isset($shop_items_catalog_modification_id))
{
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'shop_items_catalog_modification_id';
	$param['caption'] = '';
	$param['type'] = 4; // hidden.
	$param['value'] = $shop_items_catalog_modification_id;
	$admin_forms_fields->AddField($param);
}


$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'action';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = 'show_tying_products';
$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'] = 'shop_items_catalog_item_id';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $shop_items_catalog_item_id;
$admin_forms_fields->AddField($params);

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

// Кнопка "Применить".
$params = array();
$params['name'] = 'add_tying_products';
$params['caption'] = $GLOBALS['MSG_shops']['add_city_area_form_button'];
$params['image'] = '/admin/images/add.gif';
$params['attributes']['onclick'] = "return SendForm('{$admin_forms_fields->GetWindowId()}', '{$admin_forms_fields->AAction}', '{$admin_forms_fields->AAdditionalParams}', this, ".to_int($_REQUEST['admin_forms_id']).")";
$admin_forms_fields->AddButton($params);

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