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

// Проверяем существует ли идентификатор.
if (to_int($_POST['admin_forms_edit_id']) > 0)
{
	$param['admin_forms_id'] = to_int($_POST['admin_forms_edit_id']);
}

$param['admin_forms_on_page'] = to_int($_POST['admin_forms_on_page_field']);
$param['admin_forms_key_field'] = to_str($_POST['admin_forms_key_field']);
$param['admin_forms_show_operations'] = isset($_POST['admin_forms_show_operations']) ? 1 : 0;
$param['admin_forms_show_group_operations'] = isset($_POST['admin_forms_show_group_operations']) ? 1 : 0;
$param['admin_forms_group_operations_as_images'] = isset($_POST['admin_forms_group_operations_as_images']) ? 1 : 0;
$param['admin_forms_default_order_field'] = to_str($_POST['admin_forms_default_order_field']);
$param['admin_forms_default_order_direction'] = to_int($_POST['admin_forms_default_order_direction']);

// Вставляем/обновляем форму.
$admin_forms_id = $admin_forms->InsertAdminForm($param);

if (!$admin_forms_id)
{	 
	// Редактирование формы 
	if ($_POST['admin_forms_edit_id'])
	{
		show_error_message($GLOBALS['MSG_admin_forms']['form_forms_edit_error']);
	}
	else // Добавление формы 
	{
		show_error_message($GLOBALS['MSG_admin_forms']['form_forms_add_error']);
	}
}
else
{
	$word_params = array();
	$admin_words_id = to_int($_POST['admin_words_id']);
	if ($admin_forms->WordExists($admin_words_id))
	{
		$word_params['admin_words_id'] = $admin_words_id;
	}

	// Получаем идентификатор слова.
	$admin_words_id = $admin_forms->InsertWord($word_params);
	
	if ($admin_words_id)
	{
		// Обновляем слово у формы.
		$param = array();
		$param['admin_forms_id'] = $admin_forms_id;
		$param['admin_words_id'] = $admin_words_id;
		$admin_forms->InsertAdminForm($param);
		
		// Извлекаем информацию на всех языках.
		$languages = $admin_forms->GetAllLanguages();
		if ($languages)
		{
			foreach ($languages as $lng)
			{
				if (isset($_POST['admin_forms_word_name_lng_'.$lng['admin_language_id']]))
				{
					$word = $admin_forms->GetAdminFormsWord($admin_words_id, $lng['admin_language_id']);
					
					$word_value_param = array();
					
					if ($word)
					{
						$word_value_param['admin_words_value_id'] = to_int($word['admin_words_value_id']);
					}
					
					$word_value_param['admin_words_id'] = $admin_words_id;
					$word_value_param['admin_language_id'] = $lng['admin_language_id'];
					$word_value_param['admin_words_value_name'] = to_str($_POST['admin_forms_word_name_lng_'.$lng['admin_language_id']]);
					$word_value_param['admin_words_value_description'] = to_str($_POST['admin_forms_word_description_lng_'.$lng['admin_language_id']]);
					
					$admin_forms->InsertWordsValue($word_value_param);
				}
			}
		}
	}
	
	$admin_forms = new admin_forms();
	$window_id = $admin_forms->GetWindowId();
	?><script type="text/javascript">
	$.appendInput('<?php echo $window_id?>', 'EditAddAdminForm', 'admin_forms_edit_id', '<?php echo $admin_forms_id?>');
	</script><?php
	
	// Редактирование формы 
	if ($_POST['admin_forms_edit_id'])
	{
		show_message($GLOBALS['MSG_admin_forms']['form_forms_edit_success']);
	}
	else // Добавление формы 
	{
		show_message($GLOBALS['MSG_admin_forms']['form_forms_add_success']);
	}
}
?>