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

$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/shop/shop.php';
$form_params['form_attribs']['name'] = 'EditAddCondOfDelivery';

$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_type_of_delivery_id = to_int($_REQUEST['shop_type_of_delivery_id']);
$DataBase = & singleton('DataBase');

$row = $shop->GetShop($shop_shops_id);

//Получаем информацию о единице измерения веса товаров магазина
$shop_mesure_info = $shop->GetMesure($row['shop_mesures_id']);

$row2 = $shop->GetTypeOfDelivery($shop_type_of_delivery_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']['show_type_of_delivery_link'],
"link" => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id", 77, '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", 77, 'load_data')
);

$new_path_array[] = array("name" => sprintf($GLOBALS['MSG_shops']['show_cond_of_delivery_title'], $row2['shop_type_of_delivery_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_type_of_delivery_id=$shop_type_of_delivery_id", 78, '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_type_of_delivery_id=$shop_type_of_delivery_id", 78, 'load_data')
);
$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

// Редактировние условия доставки
if ($edit_cond_of_delivery)
{
	$type = 1;

	$result = $shop->GetCondOfDelivery($edit_cond_of_delivery);

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

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

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

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

	$shop_cond_of_delivery_name = to_str($result['shop_cond_of_delivery_name']);

	$shop_cond_of_delivery_weight_from = to_float($result['shop_cond_of_delivery_weight_from']);

	$shop_cond_of_delivery_weight_to = to_float($result['shop_cond_of_delivery_weight_to']);

	$shop_cond_of_delivery_price_from = to_float($result['shop_cond_of_delivery_price_from']);

	$shop_cond_of_delivery_price_to = to_float($result['shop_cond_of_delivery_price_to']);

	$shop_cond_of_delivery_description = to_str($result['shop_cond_of_delivery_description']);

	$shop_cond_of_delivery_price = to_str($result['shop_cond_of_delivery_price']);

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

	$users_id = to_int($result['users_id']);
	
	$shop_tax_id = to_int($result['shop_tax_id']);
}
// Добавление условия доставки
else
{
	$shop_country_id = 0;

	$shop_location_id = 0;

	$shop_city_id = 0;

	$shop_city_area_id = 0;

	$shop_cond_of_delivery_name = '';

	$shop_cond_of_delivery_weight_from = 0;

	$shop_cond_of_delivery_weight_to = 0;

	$shop_cond_of_delivery_price_from = 0;

	$shop_cond_of_delivery_price_to = 0;

	$shop_cond_of_delivery_description = '';

	$shop_cond_of_delivery_price = 0;

	$shop_currency_id = 0;

	$users_id = false;
	
	$shop_tax_id = 0;
}

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

// Название условия доставки
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'cond_of_delivery_name';
$params['caption'] = $GLOBALS['MSG_shops']['cond_of_delivery_add_form_name'];
$params['type'] = 0; // Поле ввода.
$params['value'] = $shop_cond_of_delivery_name;
$params['attributes']['class'] = 'large';
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 255;
$admin_forms_fields->AddField($params);

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

// Заполняем поле "Страна доставки"
$arr = array();
$arr[0] = ' ... ';

$res = $shop->GetAllCountries();

if ($res)
{
	while ($row = mysql_fetch_assoc($res))
	{
		$arr[$row['shop_country_id']] = $row['shop_country_name'];
	}
}

// Страна доставки
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'country_id';
$params['caption'] = $GLOBALS['MSG_shops']['cond_of_delivery_add_form_country'];
$params['type'] = 2; // Выпадающий список
$params['items'] = $arr;
$params['attributes']['onchange'] = "doSetLocation(this.options[this.selectedIndex].value)";
$params['value'] = $shop_country_id;

$admin_forms_fields->AddField($params);

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

// Заполняем поле "Местоположение доставки"
$arr = array();
$arr[0] = ' ... ';

// Расположения заполняем только тогда, когда страна указана
if ($shop_country_id)
{
	$res = $shop->GetAllLocation($shop_country_id);

	if ($res)
	{
		while ($row = mysql_fetch_assoc($res))
		{
			$arr[$row['shop_location_id']] = $row['shop_location_name'];
		}
	}
}

// Местоположение доставки
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'location_id';
$params['caption'] = $GLOBALS['MSG_shops']['location_id'];
$params['type'] = 2; // Выпадающий список
$params['items'] = $arr;
$params['attributes']['onchange'] = "doSetCity(this.options[this.selectedIndex].value)";
$params['value'] = $shop_location_id;
$admin_forms_fields->AddField($params);

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

// Заполняем поле "Город доставки"
$arr = array();
$arr[0] = ' ... ';

// Города заполняем только тогда, когда расположение указано
if ($shop_location_id)
{
	$res = $shop->GetAllCity($shop_location_id);

	if ($res)
	{
		while ($row = mysql_fetch_assoc($res))
		{
			$arr[$row['shop_city_id']] = $row['shop_city_name'];
		}
	}
}

// Город доставки
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'city_id';
$params['caption'] = $GLOBALS['MSG_shops']['cond_of_delivery_add_form_city'];
$params['type'] = 2; // Выпадающий список
$params['items'] = $arr;
$params['attributes']['onchange'] = "doSetCityArea(this.options[this.selectedIndex].value)";
$params['value'] = $shop_city_id;
$admin_forms_fields->AddField($params);

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

// Заполняем поле "Район доставки"
$arr = array();
$arr[0] = ' ... ';

// Города заполняем только тогда, когда расположение указано
if ($shop_city_id)
{
	$res = $shop->GetAllCityArea($shop_city_id);

	if ($res)
	{
		while ($row = mysql_fetch_assoc($res))
		{
			$arr[$row['shop_city_area_id']] = $row['shop_city_area_name'];
		}
	}
}

// Район доставки
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'city_area_id';
$params['caption'] = $GLOBALS['MSG_shops']['cond_of_delivery_add_form_city_area'];
$params['type'] = 2; // Выпадающий список
$params['items'] = $arr;
$params['value'] = $shop_city_area_id;
$admin_forms_fields->AddField($params);

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

// Заполняем поле "Название типа доставки"
$arr = array();
$arr[0] = ' ... ';

$res = $shop->GetAllTypeOfDelivery($shop_shops_id);

if ($res)
{
	while ($row = mysql_fetch_assoc($res))
	{
		$arr[$row['shop_type_of_delivery_id']] = $row['shop_type_of_delivery_name'];
	}
}

// Название типа доставки
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'type_of_delivery_id';
$params['caption'] = $GLOBALS['MSG_shops']['type_of_delivery_id'];
$params['type'] = 2; // Выпадающий список
$params['items'] = $arr;
$params['value'] = $shop_type_of_delivery_id;

$admin_forms_fields->AddField($params);

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

// Описание условия доставки
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'cond_of_delivery_description';
$params['caption'] = $GLOBALS['MSG_shops']['cond_of_delivery_add_form_description'];
$params['type'] = 5; // Текстовое поле
$params['value'] = $shop_cond_of_delivery_description;
$params['attributes']['rows'] = 5;
$admin_forms_fields->AddField($params);

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

$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_shops']['tab_cond_of_delivery_settings']);


// Вес заказа (от)
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'cond_of_delivery_weight_from';
$params['caption'] = sprintf($GLOBALS['MSG_shops']['show_cond_of_delivery_weight_from'], $shop_mesure_info['shop_mesures_name']);
$params['type'] = 0; // Текстовое поле
$params['value'] = $shop_cond_of_delivery_weight_from;
$params['format']['lib']['value'] = 'decimal';
$params['format']['lib']['message'] = $GLOBALS['MSG_shops']['show_invalid_characters_decimal'];
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 15;
$admin_forms_fields->AddField($params);

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

// Вес заказа (до)
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'cond_of_delivery_weight_to';
$params['caption'] = sprintf($GLOBALS['MSG_shops']['show_cond_of_delivery_weight_to'], $shop_mesure_info['shop_mesures_name']);
$params['type'] = 0; // Текстовое поле
$params['value'] = $shop_cond_of_delivery_weight_to;
$params['format']['lib']['value'] = 'decimal';
$params['format']['lib']['message'] = $GLOBALS['MSG_shops']['show_invalid_characters_decimal'];
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 15;
$admin_forms_fields->AddField($params);

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

// Сумма заказанных товаров (от)
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'cond_of_delivery_price_from';
$params['caption'] = $GLOBALS['MSG_shops']['show_cond_of_delivery_price_from'];
$params['type'] = 0; // Текстовое поле
$params['value'] = $shop_cond_of_delivery_price_from;
$params['format']['lib']['value'] = 'decimal';
$params['format']['lib']['message'] = $GLOBALS['MSG_shops']['show_invalid_characters_decimal'];
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 15;
$admin_forms_fields->AddField($params);

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

// Сумма заказанных товаров (до)
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'cond_of_delivery_price_to';
$params['caption'] = $GLOBALS['MSG_shops']['show_cond_of_delivery_price_to'];
$params['type'] = 0; // Текстовое поле
$params['value'] = $shop_cond_of_delivery_price_to;
$params['format']['lib']['value'] = 'decimal';
$params['format']['lib']['message'] = $GLOBALS['MSG_shops']['show_invalid_characters_decimal'];
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 15;
$admin_forms_fields->AddField($params);

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

// Цена доставки
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'cond_of_delivery_price';
$params['caption'] = $GLOBALS['MSG_shops']['cond_of_delivery_add_form_price'];
$params['type'] = 0; // Текстовое поле
$params['value'] = $shop_cond_of_delivery_price;
$params['format']['lib']['value'] = 'decimal';
$params['format']['lib']['message'] = $GLOBALS['MSG_shops']['show_invalid_characters_decimal'];
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 15;
$params['attributes']['style'] = 'width: 120px;';
$params['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($params);

// Заполняем поле "Тип валюты"
$arr = array();
$arr[0] = ' ... ';
$res = $shop->GetAllCurrency();

if ($res)
{
	while ($row = mysql_fetch_assoc($res))
	{
		$arr[$row['shop_currency_id']] = $row['shop_currency_name'];
	}
}

// Валюта доставки
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'currency_id';
$params['caption'] = '&nbsp;';
$params['type'] = 2; // Выпадающий список
$params['items'] = $arr;
$params['value'] = $shop_currency_id;
$params['attributes']['style'] = 'width: 100px;';
$params['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($params);

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

$shop_tax_array = array(0 => " ... ");

// Получаем список налогов
$shop_tax_res = $shop->GetAllTax();

if($shop_tax_res)
{
	while($shop_tax_row = mysql_fetch_assoc($shop_tax_res))
	{
		$shop_tax_array[$shop_tax_row['shop_tax_id']] = $shop_tax_row['shop_tax_name'];
	}
}

// Идентификатор налога
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_tax_id';
$params['caption'] = $GLOBALS['MSG_shops']['table_nds_for_delivery'];
$params['type'] = 2; // Выпадающий список
$params['items'] = $shop_tax_array;
$params['value'] = $shop_tax_id;
$params['attributes']['style'] = 'width: 100px;';
$params['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($params);

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

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

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

// Кнопка "Сохранить".
$param = array();
$param['name'] = 'save_cond_of_delivery';
$param['caption'] = $GLOBALS['MSG_shops']['save'];
$param['attributes']['onclick'] = "return SendForm('{$admin_forms_fields->GetWindowId()}', '{$admin_forms_fields->AAction}', '{$admin_forms_fields->AAdditionalParams}', this, ".to_int($_REQUEST['admin_forms_id']).", 'load_data', " . to_int($admin_forms_fields->form_params['current_page']) . ", " . to_int($admin_forms_fields->form_params['on_page']) . " )";
$param['attributes']['type'] = 'button';
$admin_forms_fields->AddButton($param);

// Кнопка "Применить".
$params = array();
$params['name'] = 'add_cond_of_delivery';
$params['caption'] = $GLOBALS['MSG_shops']['apply'];
$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']).", 'load_data', " . to_int($admin_forms_fields->form_params['current_page']) . ", " . to_int($admin_forms_fields->form_params['on_page']) . " )";
$params['attributes']['type'] = 'submit';
$admin_forms_fields->AddButton($params);

ob_start();
?>
<script type="text/javascript">
var location_select_id = "field_id_4";
var city_select_id = "field_id_6";
var cityarea_select_id = "field_id_8";
</script>
<?php

$js_code = ob_get_clean();

$admin_forms_fields->SetExternalHtml($js_code);

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