<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2010 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Обработчик добавления/редактирования свойства.
 * 
 * Файл: /admin/shop/action/add_list_of_properties.php
 * 
 * @author Hostmake LLC
 * @version 5.x
 */
$param = array();

$shop = & singleton('shop');

$date = new DateClass();

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

if (isset($_REQUEST['edit_list_of_properties']))
{
	$param['id'] = to_int($_REQUEST['edit_list_of_properties']);
}

// Формируем массив с параметрами для дополнительного свойства товара 
$param['name'] = to_str($_REQUEST['list_of_properties_name']);
$param['xml_name'] = to_str($_REQUEST['list_of_properties_xml_name']);
$param['order'] = to_str($_REQUEST['list_of_properties_order']);
$param['type'] = to_int($_REQUEST['list_of_properties_type']);
$param['prefics'] = to_str($_REQUEST['list_of_properties_prefics']);
$param['shop_properties_items_dir_id'] = to_int($_REQUEST['shop_properties_items_dir_id']);

$param['shop_list_of_properties_default_big_width'] = to_int($_REQUEST['shop_list_of_properties_default_big_width']);
$param['shop_list_of_properties_default_big_height'] = to_int($_REQUEST['shop_list_of_properties_default_big_height']);
$param['shop_list_of_properties_default_small_width'] = to_int($_REQUEST['shop_list_of_properties_default_small_width']);
$param['shop_list_of_properties_default_small_height'] = to_int($_REQUEST['shop_list_of_properties_default_small_height']);

$param['shop_list_of_properties_description'] = to_str($_REQUEST['shop_list_of_properties_description']);

$shop_list_of_properties_cml_id = to_str($_REQUEST['shop_list_of_properties_cml_id']);

if ($param['type'] == 7)
{
	//$param['default_value'] = isset($_REQUEST['list_of_properties_default_value']) ? 1 : 0;
	$param['default_value'] = isset($_REQUEST['checkbox']) ? 1 : 0;
}
// тип данных - дата
elseif ($param['type'] == 5)
{
	$param['default_value'] = $date->date_format_sql($_REQUEST['date_without_time']);
}
// тип данных - датавремя
elseif ($param['type'] == 6)
{
	$param['default_value'] = $date->datetime_format_sql($_REQUEST['date_with_time']);
}
else 
{
	$param['default_value'] = to_str($_REQUEST['list_of_properties_default_value']);
}

$param['shop_shops_id'] = to_int($_REQUEST['shop_shops_id']);
$param['mesures_id'] = to_int($_REQUEST['list_of_properties_mesures']);
$param['list_id'] = to_int($_REQUEST['list_of_properties_lists_id']);

$param['shop_list_of_properties_show_kind'] = to_int($_REQUEST['shop_list_of_properties_show_kind']);

$shop = new shop();

// Если идет вставка,
if (!isset($param['id']))
{
	// Проверяем есть ли дополнительное свойство с таким shop_list_of_properties_cml_id
	if (!empty($shop_list_of_properties_cml_id)
	&& $shop->GetItemPropertyIdByCmlId($shop_list_of_properties_cml_id, $shop_shops_id))
	{
		show_error_message($GLOBALS['MSG_shops']['error_insert_property_cml']);
	}
	else
	{
		$param['shop_list_of_properties_cml_id'] = $shop_list_of_properties_cml_id;
		
		// Осуществляем вставку дополнительного свойства 
		if ($prop = $shop->InsertPropretyOfItems($param))
		{
			$admin_forms = new admin_forms();
			$window_id = $admin_forms->GetWindowId();
			?><script type="text/javascript">
			$.appendInput('<?php echo $window_id?>', 'EditAddProperty', 'edit_list_of_properties', '<?php echo $prop?>');
			</script><?php

			if (isset($_REQUEST['edit_list_of_properties']))
			{
				show_message($GLOBALS['MSG_shops']['success_edit_list_of_properties']);
			}
			else
			{
				show_message($GLOBALS['MSG_shops']['success_add_list_of_properties']);
			}
		}
		else
		{
			if (isset($_REQUEST['edit_list_of_properties']))
			{
				show_error_message($GLOBALS['MSG_shops']['error_edit_list_of_properties']);
			}
			else
			{
				show_error_message($GLOBALS['MSG_shops']['error_add_list_of_properties']);
			}
		}
	}
}
else
{
//	 Осуществляем вставку дополнительного свойства 
	$param['shop_list_of_properties_cml_id'] = $shop_list_of_properties_cml_id;
	if ($prop = $shop->InsertPropretyOfItems($param))
	{
		$admin_forms = new admin_forms();
		$window_id = $admin_forms->GetWindowId();
		?><script type="text/javascript">
		$.appendInput('<?php echo $window_id?>', 'EditAddProperty', 'edit_list_of_properties', '<?php echo $prop?>');
		</script><?php

		if (isset($_REQUEST['edit_list_of_properties']))
		{
			show_message($GLOBALS['MSG_shops']['success_edit_list_of_properties']);
		}
		else
		{
			show_message($GLOBALS['MSG_shops']['success_add_list_of_properties']);
		}
	}
	else
	{
		if (isset($_REQUEST['edit_list_of_properties']))
		{
			show_error_message($GLOBALS['MSG_shops']['error_edit_list_of_properties']);
		}
		else
		{
			show_error_message($GLOBALS['MSG_shops']['error_add_list_of_properties']);
		}
	}
}

?>