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

if (!isset($admin_forms_id))
{
	$admin_forms_id = to_int($_REQUEST['admin_forms_edit_id']);
}

$form_params = array();

$admin_forms = & singleton('admin_forms');

$form_name = '';

if ($admin_forms_id)
{
	$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_field_id == 0
? $GLOBALS['MSG_admin_forms']['form_add_forms_field_title']
: sprintf($GLOBALS['MSG_admin_forms']['form_edit_forms_field_title'], $form_name);

$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/admin_forms/admin_forms.php';
$form_params['form_attribs']['name'] = 'EditAddAdminFormField';

$admin_forms_fields = new admin_forms_fields();

// Данные о форме.
$form_row = $admin_forms->GetAdminForm($admin_forms_id);

if ($form_row)
{
	$form_word_row = $admin_forms->GetAdminFormsWord(to_int($form_row['admin_words_id']));
}

// Формируем "хлебные крошки"
$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']);

$new_path_array[] = array(
'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/admin_forms/admin_forms.php', "&admin_forms_edit_id=$admin_forms_id", 2, 'load_data'),
'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/admin_forms/admin_forms.php', "&admin_forms_edit_id=$admin_forms_id", 2, 'load_data'),
'name' => sprintf($GLOBALS['MSG_admin_forms']['form_fields_menu_admin_form_fields'], to_str($form_word_row['name'])));

$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

$admin_forms = & singleton('admin_forms');

if ($admin_forms_field_id)
{
	$field_row = $admin_forms->GetAdminFormsField($admin_forms_field_id);
}
else
{
	$field_row = false;
}

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

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

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

		if ($field_row)
		{
			$word = $admin_forms->GetAdminFormsWord(to_int($field_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_field_lng_name'] . ' (' . $lng['admin_language_short_name'] . ')';
		$param['type'] = 0; // Поле ввода.
		$param['value'] = $name;
		$param['attributes']['class'] = 'large';
		$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_field_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 ($field_row)
{
	if ($admin_forms->WordExists($field_row['admin_words_id']))
	{
		// Добавляем скрытое поле с идентификатором слова для имени и описания формы.
		$param = array();
		$param['tab_id'] = $tab_id;
		$param['name'] = 'admin_words_id';
		$param['type'] = 4; // Сркытое поле.
		$param['value'] = to_int($field_row['admin_words_id']);
		
		$admin_forms_fields->AddField($param);
	}
}

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

// Имя ключевого поля.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'admin_forms_field_name';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_field_key_field_name'];
$param['type'] = 0; // Поле ввода.

if ($field_row)
{
	$param['value'] = to_str($field_row['admin_forms_field_name']);
}
$param['attributes']['style'] = 'width: 320px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

// Порядок сортировки.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'admin_forms_field_order';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_field_order'];
$param['type'] = 0; // Поле ввода.

if ($field_row)
{
	$param['value'] = to_int($field_row['admin_forms_field_order']);
}
$param['attributes']['style'] = 'width: 320px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

$admin_forms_fields->AddSeparator($tab_id);

$admin_forms = new admin_forms();
$window_id = $admin_forms->GetWindowId();

// Тип поля.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'admin_forms_field_type';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_field_type'];
$param['type'] = 2; // Выпадающий список.
$param['attributes']['onchange'] = "ShowRowsAdminForm('{$window_id}', this.value)";

$value = to_int($field_row['admin_forms_field_type']);
if ($value == 0)
{
	$value = 1;
}
$param['value'] = $value;

$param['items'] = array();
$param['items'][1] = $GLOBALS['MSG_admin_forms']['field_type_text'];
$param['items'][2] = $GLOBALS['MSG_admin_forms']['field_type_input'];
$param['items'][3] = $GLOBALS['MSG_admin_forms']['field_type_checkbox'];
$param['items'][4] = $GLOBALS['MSG_admin_forms']['field_type_link'];
$param['items'][5] = $GLOBALS['MSG_admin_forms']['field_type_date_time'];
$param['items'][6] = $GLOBALS['MSG_admin_forms']['field_type_date'];
$param['items'][7] = $GLOBALS['MSG_admin_forms']['field_type_image_link'];
$param['items'][8] = $GLOBALS['MSG_admin_forms']['field_type_image_list'];
$param['items'][9] = $GLOBALS['MSG_admin_forms']['field_type_text_as_is'];
$param['items'][10] = $GLOBALS['MSG_admin_forms']['field_type_image_callback_function'];
$param['attributes']['style'] = 'width: 320px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

// Формат данных.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'admin_forms_field_format';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_field_format'];
$param['type'] = 0; // Поле ввода.

if ($field_row)
{
	$param['value'] = to_str($field_row['admin_forms_field_format']);
}
$param['attributes']['style'] = 'width: 320px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

$admin_forms_fields->AddSeparator($tab_id);

// Путь к изображению.
$param = array();
$param['tab_id'] = $tab_id;
$param['div_id'] = 'list_image_path_id';
$param['name'] = 'admin_forms_field_image';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_field_image'];
$param['type'] = 5; // Textarea.

if ($field_row)
{
	$param['value'] = to_str($field_row['admin_forms_field_image']);
}

$param['attributes']['rows'] = 7;
$param['attributes']['style'] = 'width: 100%;';

$admin_forms_fields->AddField($param);

$admin_forms_fields->AddSeparator($tab_id);

// Значения списка.
$param = array();
$param['tab_id'] = $tab_id;
$param['div_id'] = 'list_values_id';
$param['name'] = 'admin_forms_field_list';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_field_list'];
$param['type'] = 5; // Textarea.

if ($field_row)
{
	$param['value'] = to_str($field_row['admin_forms_field_list']);
}

$param['attributes']['rows'] = 7;
$param['attributes']['style'] = 'width: 100%;';

$admin_forms_fields->AddField($param);

$admin_forms_fields->AddSeparator($tab_id);

// Ссылка.
$param = array();
$param['tab_id'] = $tab_id;
$param['div_id'] = 'link_id';
$param['name'] = 'admin_forms_field_link';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_field_link'];
$param['type'] = 0; // Поле ввода.

if ($field_row)
{
	$param['value'] = to_str($field_row['admin_forms_field_link']);
}

$admin_forms_fields->AddField($param);

$admin_forms_fields->AddSeparator($tab_id);

// Onclick.
$param = array();
$param['tab_id'] = $tab_id;
$param['div_id'] = 'onclick_id';
$param['name'] = 'admin_forms_field_onclick';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_field_onclick'];
$param['type'] = 0; // Поле ввода.

if ($field_row)
{
	$param['value'] = to_str($field_row['admin_forms_field_onclick']);
}

$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_field_allow_order';
$param['html_id'] = 'id_admin_forms_field_allow_order';
$param['value'] = to_int($field_row['admin_forms_field_allow_order']);
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_field_allow_order'];

$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_field_allow_filter';
$param['html_id'] = 'id_admin_forms_field_allow_filter';
$param['value'] = to_int($field_row['admin_forms_field_allow_filter']);
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_field_allow_filter'];

$admin_forms_fields->AddField($param);

// Вид
$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_admin_forms']['form_forms_field_tab_3']);

// Выравнивание заголовка.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'admin_forms_field_align_title';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_field_align_title'];
$param['type'] = 2; // Поле ввода.

$items = array();
$items['left'] = $GLOBALS['MSG_admin_forms']['form_forms_field_align_left'];
$items['center'] = $GLOBALS['MSG_admin_forms']['form_forms_field_align_center'];
$items['right'] = $GLOBALS['MSG_admin_forms']['form_forms_field_align_right'];

$param['items'] = $items;

if ($field_row)
{
	$param['value'] = to_str($field_row['admin_forms_field_align_title']);
}
$param['attributes']['style'] = 'width: 320px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

// Выравнивание данных.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'admin_forms_field_align';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_field_align'];
$param['type'] = 2; // Поле ввода.
$param['items'] = $items;

if ($field_row)
{
	$param['value'] = to_str($field_row['admin_forms_field_align']);
}
$param['attributes']['style'] = 'width: 320px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

$admin_forms_fields->AddSeparator($tab_id);

// Ширина поля.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'admin_forms_field_width';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_field_width'];
$param['type'] = 0; // Поле ввода.

if ($field_row)
{
	$param['value'] = to_str($field_row['admin_forms_field_width']);
}
$param['attributes']['style'] = 'width: 320px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

// CSS стиль.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'admin_forms_field_style';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_field_style'];
$param['type'] = 0; // Поле ввода.

if ($field_row)
{
	$param['value'] = to_str($field_row['admin_forms_field_style']);
}
$param['attributes']['style'] = 'width: 320px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

$admin_forms_fields->AddSeparator($tab_id);

// Строка с атрибутами поля.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'admin_forms_field_attrib';
$param['caption'] = $GLOBALS['MSG_admin_forms']['form_forms_field_attrib'];
$param['type'] = 0; // Поле ввода.

if ($field_row)
{
	$param['value'] = to_str($field_row['admin_forms_field_attrib']);
}
$param['attributes']['style'] = 'width: 650px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);


// Добавляем скрытое поле с идентификатором редактируемой формы.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'admin_forms_edit_id';
$param['type'] = 4; // Сркытое поле.
$param['value'] = to_int($_REQUEST['admin_forms_edit_id']);

$admin_forms_fields->AddField($param);

// Отобразить список полей формы.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'action';
$param['type'] = 4; // Скрытое поле.
$param['value'] = 'show_admin_form_fields';

$admin_forms_fields->AddField($param);

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

// Кнопка "Сохранить".
$param = array();
$param['name'] = 'save_admin_forms_field';
$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_field';
$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);

ob_start();
?><script language="JavaScript">
ShowRowsAdminForm('<?php echo $window_id?>', <?php echo (isset($field_row['admin_forms_field_type'])) ? intval($field_row['admin_forms_field_type']) : 1?>)
</script><?php
$external_html = ob_get_clean();

$admin_forms_fields->SetExternalHtml($external_html);

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