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

$form_params = array();
$form_params['title'] = $shop_coupon_id==0?$GLOBALS['MSG_shops']['coupon_form_table_title_add']:$GLOBALS['MSG_shops']['coupon_form_table_title_edit'];
$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/shop/shop.php';
$form_params['form_attribs']['name'] = 'EditAddCoupon';

$shop_shops_id = to_int($_REQUEST['shop_shops_id']);
$shop = & singleton('shop');

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

$admin_forms_fields = new admin_forms_fields();

$DataBase = & singleton('DataBase');

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

$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

// Редактировние купона
if ($shop_coupon_id)
{
	$type = 1;

	$result = $shop->GetCoupon($shop_coupon_id);

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

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

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

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

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

	$users_id = to_int($result['users_id']);
}
// Добавление купона
else
{
	$shop_order_discount_id = 0;

	$shop_coupon_name = '';

	$shop_coupon_active = 0;

	$shop_coupon_count = -1;

	$shop_coupon_text = sprintf("%03d-%03d-%03d-%03d", rand(0, 999), rand(0, 999),
	rand(0, 999), rand(0, 999));

	$users_id = false;
}

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

// Заполняем поле значения "Скидка"
$arr = array();
$arr[0] = ' ... ';
$shop = & singleton('shop');
$order_discount_resource = $shop->GetAllOrderDiscounts($shop_shops_id);

while ($row = mysql_fetch_assoc($order_discount_resource))
{
	$arr[$row['shop_order_discount_id']] = $row['shop_order_discount_name'];
}

// Название купона
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_coupon_name';
$params['caption'] = $GLOBALS['MSG_shops']['coupon_form_name'];
$params['type'] = 0; // Поле ввода
$params['value'] = $shop_coupon_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);

// Скидка, которую предоставляет купон
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_order_discount_id';
$params['caption'] = $GLOBALS['MSG_shops']['coupon_form_discount'];
$params['type'] = 2; // Выпадающий список
$params['items'] = $arr;
$params['value'] = $shop_order_discount_id;

$admin_forms_fields->AddField($params);

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

// Активность
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_coupon_active';
$params['caption'] = $GLOBALS['MSG_shops']['coupon_form_active'];
$params['type'] = 1; // Флажок
$params['value'] = $shop_coupon_active;
$admin_forms_fields->AddField($params);

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

// Количество
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_coupon_count';
$params['caption'] = $GLOBALS['MSG_shops']['coupon_form_count'];
$params['type'] = 0; // Поле ввода
$params['value'] = $shop_coupon_count;
$params['format']['lib']['value'] = 'decimal';
$params['format']['lib']['message'] = $GLOBALS['MSG_shops']['show_invalid_characters_integer'];
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 11;
$admin_forms_fields->AddField($params);

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

// Текст купона
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_coupon_text';
$params['caption'] = $GLOBALS['MSG_shops']['coupon_form_text'];
$params['type'] = 0; // Поле ввода
$params['value'] = $shop_coupon_text;
$params['format']['lib']['message'] = $GLOBALS['MSG_shops']['show_invalid_characters_integer'];
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 255;
$admin_forms_fields->AddField($params);

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

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


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

// Кнопка "Сохранить".
$param = array();
$param['name'] = 'save_coupon';
$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_coupon';
$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']).")";
$params['attributes']['type'] = 'submit';
$admin_forms_fields->AddButton($params);

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