<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2008 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Обработчик добавления/редактирования информационной системы.
 * 
 * Файл: /admin/InformationSystems/action/add_information_systems.php
 * 
 * @author Hostmake LLC
 * @version 5.2
 */
$InformationSystem = & singleton('InformationSystem');

$uploaddir = CMS_FOLDER . UPLOADDIR . 'private/';

// Обновление информационной системы
if (isset($_REQUEST["information_system_id"]))
{
	$type = 1;
	$information_system_id = to_int($_REQUEST["information_system_id"]);
}
else
{
	$type = 0;
	$information_system_id = 0;
}

// Задали watermark
if (!empty($_FILES['information_systems_watermark_file']['tmp_name']))
{
	$file_name = $_FILES['information_systems_watermark_file']['tmp_name'];
	
	$watermark_file_extension = $kernel->GetExtension($_FILES['information_systems_watermark_file']['name']);
	
	// Файл ватермарка не является png-файлом
	if (exif_imagetype($file_name) != 3)
	{
		$watermark_extention_error = 1;
		
		show_error_message($GLOBALS['MSG_information_systems']['error_watermark_extention_add_edit_information_system']);
	}
}
else
{
	$file_name = '';
	$watermark_file_extension = '';
}

if (isset($_POST['information_systems_default_used_watermark']))
{
	$information_system_watermark_default_used = 1;
}
else
{
	$information_system_watermark_default_used = 0;
}

if (isset($_POST['information_systems_default_used_small_watermark']))
{
	$information_system_watermark_default_used_small = 1;
}
else
{
	$information_system_watermark_default_used_small = 0;
}

// Не было ошибки, связанной с типом файла "водяного знака"
if (!isset($watermark_extention_error))
{
	$param = array();

	$param['type'] = $type;
	$param['id'] = $information_system_id;
	$param['site_id'] = to_int($_POST['site_id']);
	$param['name'] =  to_str($_POST['information_systems_name']);
	$param['description'] = to_str($_POST['information_systems_description']);
	$param['items_order_field'] = to_str($_POST['information_systems_items_order_field']);
	$param['items_order_type'] = to_str($_POST['information_systems_items_order_type']);
	$param['information_systems_access'] = to_int($_POST['information_systems_access']);
	$param['information_systems_captcha_used'] = ((isset($_POST['information_systems_captcha_used']))?1:0);
	$param['watermark_file'] = $file_name;
	$param['watermark_file_expantion'] = $watermark_file_extension;
	$param['watermark_default_used'] = $information_system_watermark_default_used;
	$param['watermark_default_used_small'] = $information_system_watermark_default_used_small;
	$param['watermark_default_position_x'] = to_str($_POST['information_systems_watermark_default_position_x']);
	$param['watermark_default_position_y'] = to_str($_POST['information_systems_watermark_default_position_y']);
	$param['structure_id'] = to_int($_POST['structure_id']);
	$param['items_on_page'] = to_int($_POST['information_systems_items_on_page']);
	$param['information_systems_group_items_order_field'] = to_int($_POST['information_systems_group_items_order_field']);
	$param['information_systems_group_items_order_type'] = to_int($_POST['information_systems_group_items_order_type']);
	$param['information_systems_format_date'] = to_str($_POST['information_systems_format_date']);
	$param['information_systems_format_datetime'] = to_str($_POST['information_systems_format_datetime']);

	// Картинки.
	$param['information_systems_image_big_max_width'] = to_int($_POST['information_systems_image_big_max_width']);
	$param['information_systems_image_big_max_height'] = to_int($_POST['information_systems_image_big_max_height']);
	$param['information_systems_image_small_max_width'] = to_int($_POST['information_systems_image_small_max_width']);
	$param['information_systems_image_small_max_height'] = to_int($_POST['information_systems_image_small_max_height']);

	$param['information_systems_url_type'] = to_int($_POST['information_systems_url_type']);
	
	if (isset($_POST['information_systems_typograph_item']))
	{
		$param['information_systems_typograph_item'] = 1;
	}
	else 
	{
		$param['information_systems_typograph_item'] = 0;
	}
	
	if (isset($_POST['information_systems_typograph_group']))
	{
		$param['information_systems_typograph_group'] = 1;
	}
	else 
	{
		$param['information_systems_typograph_group'] = 0;
	}
	
	if (isset($_POST['information_systems_apply_tags_automatic']))
	{
		$param['information_systems_apply_tags_automatic'] = 1;
	}
	else 
	{
		$param['information_systems_apply_tags_automatic'] = 0;
	}
	
	if (isset($_POST['information_systems_file_name_conversion']))
	{
		$param['information_systems_file_name_conversion'] = 1;
	}
	else 
	{
		$param['information_systems_file_name_conversion'] = 0;
	}
	
	$param['information_systems_apply_keywords_automatic'] = isset($_POST['information_systems_apply_keywords_automatic'])?1:0;	
	
	if ($insert_information_system_id = $InformationSystem->InsertInfotmationSystem($param))
	{
		// Добавим скрытое поле к форме с ID созданного элемента
	?>
	<script>
	var ElementInput = document.createElement("input");
	ElementInput.setAttribute("type", "hidden");
	ElementInput.setAttribute("name", "information_system_id");
	ElementInput.setAttribute("value", "<?php echo $insert_information_system_id?>");

	document.getElementById('EditAddInformationSystem').appendChild(ElementInput);
	</script>
	<?php 

	// Редактирование информационной системы
	if ($type)
	{
		show_message($GLOBALS['MSG_information_systems']['edit_information_systems_success']);
	}
	else
	{
		show_message($GLOBALS['MSG_information_systems']['add_information_systems_success']);
	}
	}
	else
	{
		show_error_message($GLOBALS['MSG_information_systems']['error_add_edit_information_system']);
	}
}
?>