<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2008 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Обработчик добавления/редактирования свойства.
 * 
 * Файл: /admin/shop/action/add_list_of_properties.php
 * 
 * @author Hostmake LLC
 * @version 5.2
 */
$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']);

$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))
		{
		?>
	<script>
	var ElementInput = document.createElement("input");
	ElementInput.setAttribute("type", "hidden");
	ElementInput.setAttribute("name", "edit_list_of_properties");
	ElementInput.setAttribute("value", "<?php echo $prop?>");

	document.getElementById('EditAddProperty').appendChild(ElementInput);
	</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))
	{
		?>
	<script>
	var ElementInput = document.createElement("input");
	ElementInput.setAttribute("type", "hidden");
	ElementInput.setAttribute("name", "edit_list_of_properties");
	ElementInput.setAttribute("value", "<?php echo $prop?>");

	document.getElementById('EditAddProperty').appendChild(ElementInput);
	</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']);
		}
	}
}

?>