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

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

$param['admin_forms_id'] = to_int($_POST['admin_forms_edit_id']);
$param['admin_forms_field_name'] = to_str($_POST['admin_forms_field_name']);
$param['admin_forms_field_order'] = to_int($_POST['admin_forms_field_order']);
$param['admin_forms_field_type'] = to_int($_POST['admin_forms_field_type']);
$param['admin_forms_field_format'] = to_str($_POST['admin_forms_field_format']);
$param['admin_forms_field_allow_order'] = isset($_POST['admin_forms_field_allow_order']) ? 1 : 0;
$param['admin_forms_field_allow_filter'] = isset($_POST['admin_forms_field_allow_filter']) ? 1 : 0;
$param['admin_forms_field_align_title'] = to_str($_POST['admin_forms_field_align_title']);
$param['admin_forms_field_align'] = to_str($_POST['admin_forms_field_align']);
$param['admin_forms_field_width'] = to_str($_POST['admin_forms_field_width']);
$param['admin_forms_field_style'] = to_str($_POST['admin_forms_field_style']);
$param['admin_forms_field_attrib'] = to_str($_POST['admin_forms_field_attrib']);
$param['admin_forms_field_image'] = to_str($_POST['admin_forms_field_image']);
$param['admin_forms_field_link'] = to_str($_POST['admin_forms_field_link']);
$param['admin_forms_field_onclick'] = to_str($_POST['admin_forms_field_onclick']);
$param['admin_forms_field_list'] = to_str($_POST['admin_forms_field_list']);

// Вставляем/обновляем поле формы.
$admin_forms_field_id = $admin_forms->InsertAdminFormsField($param);

if (!$admin_forms_field_id)
{ 
	// Редактирование поля
	if ($_POST['admin_forms_field_id'])
	{
		show_error_message($GLOBALS['MSG_admin_forms']['form_forms_field_edit_error']);
	}
	else 
	{
		show_error_message($GLOBALS['MSG_admin_forms']['form_forms_field_add_error']);
	}
}
else
{
	$admin_forms = new admin_forms();
	$window_id = $admin_forms->GetWindowId();
	?><script type="text/javascript">
	$.appendInput('<?php echo $window_id?>', 'EditAddAdminFormField', 'admin_forms_field_id', '<?php echo $admin_forms_field_id?>');
	</script><?php

	$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_field_id'] = $admin_forms_field_id;
		$param['admin_words_id'] = $admin_words_id;
		$admin_forms->InsertAdminFormsField($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);
				}
			}
		}
	}
	
	// Редактирование поля
	if ($_POST['admin_forms_field_id'])
	{
		show_message($GLOBALS['MSG_admin_forms']['form_forms_field_edit_success']);
	}
	else 
	{
		show_message($GLOBALS['MSG_admin_forms']['form_forms_field_add_success']);
	}
}
?>