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

$form_params = array();
$form_params['title'] = $shop_eitem_id==0?$GLOBALS['MSG_shops']['eitems_add_title']:$GLOBALS['MSG_shops']['eitems_edit_title'];
$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/shop/shop.php';
$form_params['form_attribs']['name'] = 'EditAddEitem';

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

$shop = & singleton('shop');

$kernel = & singleton('kernel');

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

$admin_forms_fields = new admin_forms_fields();

$DataBase = & singleton('DataBase');

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

// Получаем имя товара
$item_name_row = $shop->GetItem($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['modification']))
{
	$modification_id = intval($_REQUEST['modification']);

	$modification_row = $shop->GetItem($modification_id, array('cache_off' => true));

	$new_path_array[] = array (
	"name" => sprintf($GLOBALS['MSG_shops']['item_modification_title'], $modification_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=$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=$modification_id", 81, 'load_data'));

	$new_path_array[] = array ("name" => sprintf($GLOBALS['MSG_shops']['shop_eitems_form_title'], $item_name_row['shop_items_catalog_name']),
	"link" => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$shop_groups_parent_id&shop_shops_id=$shop_shops_id&item_id=$item_id&modification=$modification_id", 132, 'load_data'),
	"onclick" => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$shop_groups_parent_id&shop_shops_id=$shop_shops_id&item_id=$item_id&modification=$modification_id", 132, 'load_data')
	);
}
else
{
	$new_path_array[] = array ("name" => sprintf($GLOBALS['MSG_shops']['shop_eitems_form_title'], $item_name_row['shop_items_catalog_name']),
	"link" => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$shop_groups_parent_id&shop_shops_id=$shop_shops_id&item_id=$item_id", 132, 'load_data'),
	"onclick" => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$shop_groups_parent_id&shop_shops_id=$shop_shops_id&item_id=$item_id", 132, 'load_data')
	);
}

$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

// Редактировние электронного товара
if ($shop_eitem_id)
{
	$result = $shop->GetEItem($shop_eitem_id);

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

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

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

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

	$shop_eitem_count = to_int($result['shop_eitem_count']);
}
// Добавление электронного товара
else
{
	$shop_items_catalog_item_id = $item_id;

	$shop_eitem_name = '';

	$shop_eitem_value = '';

	$shop_eitem_filename = '';

	$shop_eitem_count = 1;
}

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

// Описание электронного товара
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_eitem_name';
$params['caption'] = $GLOBALS['MSG_shops']['eitems_add_edit_form_name'];
$params['type'] = 0; // Поле ввода
$params['value'] = $shop_eitem_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_eitem_value';
$params['caption'] = $GLOBALS['MSG_shops']['eitems_add_edit_form_text'];
$params['type'] = 5; // Поле ввода
$params['value'] = $shop_eitem_value;
$params['attributes']['rows'] = 10;
$admin_forms_fields->AddField($params);

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

// Файл электронного товара
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_eitem_filename';
$params['caption'] = $GLOBALS['MSG_shops']['eitems_add_edit_form_file'];
$params['type'] = 9; // Поле загрузки файла


if ($shop_eitem_filename != '')
{
	// Получаем расширение файла
	$exp = $kernel->GetExtension($shop_eitem_filename);

	$params['options']['big_image_path'] = '/admin/shop/shop.php?action=download_eitem&shop_id='.$shop_shops_id.'&shop_eitem_id='.$shop_eitem_id;

	$params['options']['href_delete_big_image'] = "/admin/shop/shop.php?action=delete_eitem_image&shop_eitem_id=$shop_eitem_id&shop_shops_id=$shop_shops_id&item_id=$item_id";
	// Значение для onclick
	$params['options']['onclick_delete_big_image'] = "sendRequest('{$params['options']['href_delete_big_image']}', 'get', callback_function_exec_from_return); return false;";
}
else
{
	$params['options']['big_image_path'] = '';
	$params['options']['href_delete_big_image'] = "";
	// Значение для onclick
	$params['options']['onclick_delete_big_image'] = "";
}


$params['value'] = $shop_eitem_filename;
$params['options']['show_big_image_params'] = 0;
$params['options']['load_small_image_show'] = 0;
$admin_forms_fields->AddField($params);

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

// Количество копий электронного товара
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_eitem_count';
$params['caption'] = $GLOBALS['MSG_shops']['eitems_add_edit_form_count'];
$params['type'] = 0; // Поле ввода
$params['value'] = $shop_eitem_count;
//$params['format']['lib']['value'] = 'integer';
$params['format']['maxlen']['value'] = 10;
$params['attributes']['style'] = 'width: 100px;';
$admin_forms_fields->AddField($params);

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

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

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

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

if ($shop_eitem_id)
{
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'shop_eitem_id';
	$param['caption'] = '';
	$param['type'] = 4; // hidden.
	$param['value'] = $shop_eitem_id;
	$admin_forms_fields->AddField($param);
}

// Кнопка "Сохранить".
$param = array();
$param['name'] = 'save_eitem';
$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_eitem';
$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();


?>