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

$constant = new Constants();

if ($constants_id)
{
	$row_constant = $constant->GetConstant($constants_id);
	
	if ($row_constant)
	{
		$constants_name = to_str($row_constant['constants_name']);
	}
	else
	{
		$constants_name = '';
	}
}
else 
{
	$constants_name = '';
}

$form_params = array();
$form_params['title'] = $constants_id == 0
? $GLOBALS['MSG_constants']['constants_add_header']
: sprintf($GLOBALS['MSG_constants']['constants_edit_header'], $constants_name);

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

$admin_forms_fields = new admin_forms_fields();

$new_path_array[] = array(
'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/constants/constants.php', '', 26, 'load_data'),
'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/constants/constants.php', '', 26, 'load_data'),
'name' => $GLOBALS['MSG_constants']['constants_link']);

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

$admin_forms_fields->admin_forms_fields($form_params);

$DataBase = & singleton('DataBase');

if (!isset($error))
{
	// Редактирование константы
	if ($constants_id)
	{
		$type = 1;

		$row = $constant->GetConstant($constants_id);

		if ($row)
		{
			$const_name = $row['constants_name'];
			$const_value = $row['constants_value'];
			$const_description = $row['constants_description'];
			$const_activity = $row['constants_activity'];
		}
	}
	else // Добавление константы
	{
		$const_name = '';
		$const_value = '';
		$const_description = '';
		$const_activity = 0;
	}
}
else // Возникла ошибка при добавлении/редактировании информации о константе 
{
	show_error_message($GLOBALS['MSG_constants']['constants_add_edit_error_message']);
	$const_name = to_str($_REQUEST['constants_name']);
	$const_value = to_str($_REQUEST['constants_value']);
	$const_description = to_str($_REQUEST['constants_description']);
	
	$const_activity = isset($_REQUEST['constants_activity']) ? 1 : 0;
}

// Параметры.
$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_constants']['parameters']);

// название константы
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'constants_name';
$param['caption'] = $GLOBALS['MSG_constants']['constants_add_edit_name'];
$param['type'] = 0; // Поле ввода.
$param['attributes']['class'] = 'large';
$param['value'] = $const_name;
$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'] = 'constants_value';
$param['caption'] = $GLOBALS['MSG_constants']['constants_add_edit_value'];
$param['type'] = 0; // textbox
$param['value'] = $const_value;
$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'] = 'constants_description';
$param['caption'] = $GLOBALS['MSG_constants']['constants_add_edit_description'];
$param['type'] = 5; // textbox
$param['attributes']['rows'] = 5;
$param['value'] = $const_description;

$admin_forms_fields->AddField($param);

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


// активность константы
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'constants_activity';
$param['html_id'] = 'id_constant_active';
$param['caption'] = $GLOBALS['MSG_constants']['constants_add_edit_action'];
$param['type'] = 1; // checkbox
$param['value'] = $const_activity;

$admin_forms_fields->AddField($param);

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

// передеам скрытое поле с идентификатором константы если он есть
if ($constants_id)
{
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'constants_id';
	$param['caption'] = '';
	$param['type'] = 4; // hidden.

	$param['value'] = $constants_id;

	$admin_forms_fields->AddField($param);
}

// Кнопка "Сохранить".
$param = array();
$param['name'] = 'save_constants';
$param['caption'] = $GLOBALS['MSG_constants']['constants_save_button_name'];
$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_constants';
$param['caption'] = $GLOBALS['MSG_constants']['constants_add_edit_button_name'];
$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();

if (isset($error))
{
	do_html_footer_admin();
}
?>