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

$form_params = array();

$form_name = '';

if ($admin_forms_id)
{
	$admin_forms = & singleton('admin_forms');
	
	$form_row = $admin_forms->GetAdminForm($admin_forms_id);
		
	if ($form_row)
	{
		$form_word_row = $admin_forms->GetAdminFormsWord(to_int($form_row['admin_words_id']));
	}
	
	$form_name = to_str($form_word_row['name']);
}

$form_params['title'] = $admin_forms_id == 0
? $GLOBALS['MSG_admin_forms']['form_add_forms_title']
: sprintf($GLOBALS['MSG_admin_forms']['form_edit_forms_title'], $form_name);

$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/admin_forms/admin_forms.php';
$form_params['form_attribs']['enctype'] = 'multipart/form-data';
$form_params['form_attribs']['name'] = 'EditAddAdminForm';

$admin_forms_fields = new admin_forms_fields();

// Формируем "хлебные крошки"
$new_path_array[] = array(
'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/admin_forms/admin_forms.php', '', 1, 'load_data'),
'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/admin_forms/admin_forms.php', '', 1, 'load_data'),
'name' => $GLOBALS['MSG_admin_forms']['show_form_fields_menu_admin_forms']);

// Передаем в массив для констурктора хлебные крошки
$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

$admin_forms = & singleton('admin_forms');

if ($admin_forms_id)
{
	$form_row = $admin_forms->GetAdminForm($admin_forms_id);
}
else
{
	$form_row = false;
}

// Закладка с названием/описанием.
$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_admin_forms']['form_forms_tab_1']);

// Извлекаем информацию о названии и описание на всех языках.
$languages = $admin_forms->GetAllLanguages();

if ($languages)
{
	foreach ($languages as $lng)
	{
		$name = '';
		$description = '';

		if ($form_row)
		{
			$word = $admin_forms->GetAdminFormsWord(to_int($form_row['admin_words_id']), $lng['admin_language_id']);
			if ($word)
			{
				$name = to_str($word['name']);
				$description = to_str($word['description']);
			}
		}

		// Название формы.
		$param = array();
		$param['tab_id'] = $tab_id;
		$param['name'] = 'admin_forms_word_name_lng_' . $lng['admin_language_id'];
		$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_lng_name'] . ' (' . $lng['admin_language_short_name'] . ')';
		$param['type'] = 0; // Поле ввода.
		$param['value'] = $name;
		$param['attributes']['class'] = 'large';
		$param['format']['minlen']['value'] = 1;
		$param['format']['maxlen']['value'] = 255;
		$admin_forms_fields->AddField($param);

		// Разделитель.
		$admin_forms_fields->AddSeparator($tab_id);

		// Описание формы.
		$param = array();
		$param['tab_id'] = $tab_id;
		$param['name'] = 'admin_forms_word_description_lng_' . $lng['admin_language_id'];
		$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_lng_description'] . ' (' . $lng['admin_language_short_name'] . ')';
		$param['type'] = 5; // Textarea.
		$param['value'] = $description;
		$param['attributes'] = array();
		$param['attributes']['rows'] = 7;
		$param['attributes']['style'] = 'width: 100%;';
		$admin_forms_fields->AddField($param);
	}
}

if ($form_row)
{
	if ($admin_forms->WordExists($form_row['admin_words_id']))
	{
		// Добавляем скрытое поле с идентификатором слова для имени и описания формы.
		$param = array();
		$param['tab_id'] = $tab_id;
		$param['name'] = 'admin_words_id';
		$param['type'] = 4; // Сркытое поле.
		$param['value'] = to_int($form_row['admin_words_id']);
		$admin_forms_fields->AddField($param);
	}
}

// Закладка с параметрами.
$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_admin_forms']['form_forms_tab_2']);

// Количество элементов на страницу.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'admin_forms_on_page_field';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_on_page'];
$param['type'] = 0; // Поле ввода.

if ($form_row)
{
	$param['value'] = to_int($form_row['admin_forms_on_page']);
}
else
{
	$param['value'] = ON_PAGE;
}

$admin_forms_fields->AddField($param);

// Ключевое поле.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'admin_forms_key_field';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_key_field'];
$param['type'] = 0; // Поле ввода.
$param['value'] = to_str($form_row['admin_forms_key_field']);
$param['format']['minlen']['value'] = 1;
$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Отображать действия.
$param = array();
$param['tab_id'] = $tab_id;
$param['type'] = 1;
$param['name'] = 'admin_forms_show_operations';
$param['html_id'] = 'id_admin_forms_show_operations';
$param['value'] = to_int($form_row['admin_forms_show_operations']);
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_show_operations'];

$admin_forms_fields->AddField($param);

// Отображать групповые операции.
$param = array();
$param['tab_id'] = $tab_id;
$param['type'] = 1;
$param['name'] = 'admin_forms_show_group_operations';
$param['html_id'] = 'id_admin_forms_show_group_operations';
$param['value'] = to_int($form_row['admin_forms_show_group_operations']);
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_show_group_operations'];

$admin_forms_fields->AddField($param);

// Отображать групповые операции как картинки.
$param = array();
$param['tab_id'] = $tab_id;
$param['type'] = 1;
$param['name'] = 'admin_forms_group_operations_as_images';
$param['html_id'] = 'id_admin_forms_group_operations_as_images';
$param['value'] = isset($form_row['admin_forms_group_operations_as_images']) ?  intval($form_row['admin_forms_group_operations_as_images']) : 1;
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_show_group_operations_as_images'];

$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Поле сортировки по умолчанию.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'admin_forms_default_order_field';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_order_field'];
$param['type'] = 0; // Поле ввода.
$param['value'] = to_str($form_row['admin_forms_default_order_field']);

$admin_forms_fields->AddField($param);

// Поле направление сортировки.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'admin_forms_default_order_direction';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_order_direction'];
$param['type'] = 2; // Поле ввода.
$value = to_int($form_row['admin_forms_default_order_direction']);

if ($value == 0)
{
	$value = 1;
}

$param['value'] = $value;

$items = array();
$items[1] = $GLOBALS['MSG_admin_forms']['form_forms_order_direction_asc'];
$items[2] = $GLOBALS['MSG_admin_forms']['form_forms_order_direction_desc'];

$param['items'] = $items;
$admin_forms_fields->AddField($param);

if ($form_row)
{
	// Добавляем скрытое поле с идентификатором формы.
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'admin_forms_edit_id';
	$param['type'] = 4; // Скрытое поле.
	$param['value'] = to_int($form_row['admin_forms_id']);
	$admin_forms_fields->AddField($param);
}

// Кнопка "Сохранить".
$param = array();
$param['name'] = 'save_admin_form';
$param['caption'] = $GLOBALS['MSG_admin_forms']['save'];
$param['attributes']['onclick'] = "return SendForm('{$admin_forms_fields->GetWindowId()}', '{$admin_forms_fields->AAction}', '{$admin_forms_fields->AAdditionalParams}', this, ".to_int($_REQUEST['admin_forms_id']).", 'load_data', " . to_int($admin_forms_fields->form_params['current_page']) . ", " . to_int($admin_forms_fields->form_params['on_page']) . " )";
$param['attributes']['type'] = 'button';
$admin_forms_fields->AddButton($param);

// Кнопка "Применить".
$param = array();
$param['name'] = 'add_admin_forms';
$param['caption'] = $GLOBALS['MSG_admin_forms']['apply'];
$param['image'] = '/admin/images/add.gif';
$param['attributes']['onclick'] = "return SendForm('{$admin_forms_fields->GetWindowId()}', '{$admin_forms_fields->AAction}', '{$admin_forms_fields->AAdditionalParams}', this, ".to_int($_REQUEST['admin_forms_id']).", 'load_data', " . to_int($admin_forms_fields->form_params['current_page']) . ", " . to_int($admin_forms_fields->form_params['on_page']) . " )";
$param['attributes']['type'] = 'submit';
$admin_forms_fields->AddButton($param);

// Отображение формы.
$admin_forms_fields->ShowForm();
?>