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

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

$param['admin_forms_id'] = to_int($_POST['admin_forms_edit_id']);
$param['admin_forms_events_function'] = to_str($_POST['admin_forms_events_function']);
$param['admin_forms_events_picture'] = to_str($_POST['admin_forms_events_picture']);
$param['admin_forms_events_show_button'] = isset($_POST['admin_forms_events_show_button']) ? 1 : 0;
$param['admin_forms_events_group_operation'] = isset($_POST['admin_forms_events_group_operation']) ? 1 : 0;
$param['admin_forms_events_ask'] = isset($_POST['admin_forms_events_ask']) ? 1 : 0;
$param['admin_forms_events_order'] = to_int($_POST['admin_forms_events_order']);
$param['admin_forms_events_dataset_id'] = to_int($_POST['admin_forms_events_dataset_id']);

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

if (!$admin_forms_events_id)
{	 
	// Редактирование события
	if ($_POST['admin_forms_events_id'])
	{
		show_error_message($GLOBALS['MSG_admin_forms']['form_forms_event_edit_error']);
	}
	else // Добавление события
	{
		show_error_message($GLOBALS['MSG_admin_forms']['form_forms_event_add_error']);
	}	
}
else
{
	$admin_forms = new admin_forms();
	$window_id = $admin_forms->GetWindowId();
	?><script type="text/javascript">
	$.appendInput('<?php echo $window_id?>', 'EditAddAdminFormEvent', 'admin_forms_events_id', '<?php echo $admin_forms_events_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_events_id'] = $admin_forms_events_id;
		$param['admin_words_id'] = $admin_words_id;
		$admin_forms->InsertAdminFormsEvent($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_events_id'])
	{
		show_message($GLOBALS['MSG_admin_forms']['form_forms_event_edit_success']);
	}
	else // Добавление события
	{
		show_message($GLOBALS['MSG_admin_forms']['form_forms_event_add_success']);
	}	
}
?>