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

$form_params = array();

$form_params['title'] = ($xsl_id && !isset($error)) ? $GLOBALS['MSG_xsl']['XSL_edit_form_title'] : $GLOBALS['MSG_xsl']['XSL_add_form_title'];
$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/xsl/xsl.php';
$form_params['form_attribs']['name'] = 'EditAddXsl';

$admin_forms_fields = new admin_forms_fields();

$xsl = new xsl();

// Формируем "хлебные крошки"
$xsl->GetMasXslGroupPath($xsl_dir_parent_id, $xsl_dir_parent_id);

$path_array = $xsl->mas_xsl_group_path[$xsl_dir_parent_id];

$new_path_array = array();

if (count($path_array) > 0)
{
	// Меняем порядок следования элементов в массиве на обратный
	$path_array = array_reverse($path_array, true);

	if ($path_array)
	{
		foreach ($path_array as $key => $value)
		{
			$new_path_array[] = array (
			'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/xsl/xsl.php', "&xsl_dir_parent_id=$key", 22, 'load_data'),
			'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/xsl/xsl.php', "&xsl_dir_parent_id=$key", 22, 'load_data'),
			'name' => $value['xsl_dir_name']);
		}
	}
}

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

$admin_forms_fields->admin_forms_fields($form_params);

$DataBase = & singleton('DataBase');

if (!isset($error_edit_add_xsl))
{
	// Редактирование XSL-шаблона
	if ($xsl_id)
	{
		$File = new File();

		$result_xsl = $xsl->select_xsl($xsl_id);

		$row_xsl = mysql_fetch_assoc($result_xsl);

		$xsl_name = $row_xsl['xsl_name'];

		$fname = $xsl->GetXslPath($row_xsl["xsl_id"]);

		if (is_file($fname))
		{
			$xsl_value = $File->GetFileContent($fname);
		}
		else
		{
			$xsl_value = '';
		}
		
		$xsl_comment = $row_xsl['xsl_comment'];
		$xsl_order = $row_xsl['xsl_order'];
		$xsl_format = $row_xsl['xsl_format'];
	}
	// Добавление XSL-шаблона
	else
	{
		$xsl_name = '';
		$xsl_value = '';
		$xsl_comment = '';
		$xsl_order = 0;
		$xsl_format = 1;
	}
}
else
{
	show_error_message($GLOBALS['MSG_xsl']['XSL_add_form_message']);
	
	if (isset($_POST['xsl_dir_id']))
	{
		$xsl_dir_id = $_POST['xsl_dir_id'];
	}
	
	$xsl_name = to_str($_POST['xsl_name']);
	$xsl_value = to_str($_POST['xsl_value']);
	$xsl_comment = to_str($_POST['xsl_comment']);
	$xsl_order = to_int($_POST['xsl_order']);
	$xsl_format = isset($_POST['xsl_format']) ? 1 : 0;
}

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

// Название XSL-шаблона.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'xsl_name';
$param['caption'] = $GLOBALS['MSG_xsl']['XSL_add_form_name'];
$param['type'] = 0; // Поле ввода.
$param['attributes'] = array();
$param['attributes']['class'] = 'large';
$param['value'] = $xsl_name;
$param['format']['minlen']['value'] = 1;
$param['format']['maxlen']['value'] = 255;

$admin_forms_fields->AddField($param);

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

// Структура каталогов XSL.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'xsl_dir_id';
$param['caption'] = $GLOBALS['MSG_xsl']['XSL_add_form_dir'];
$param['type'] = 2; // Выпадающий список.

$value = $xsl_dir_id;

$param['value'] = $value;

$items = array();

$mas = array();

// Формируем дерево разделов XSL-шаблонов
$mas = $xsl->fill_xsl_dir(0,"&nbsp;&nbsp;");

$mas_count = count($mas);

$items[0] = "..";

if ($mas_count > 0)
{
	for($i = 0; $i < $mas_count; $i++)
	{
		$items[$mas[$i][0]] = $mas[$i][1];
	}
}

$param['items'] = $items;

$admin_forms_fields->AddField($param);

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

// Текст XSL-шаблона.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'xsl_value';
$param['caption'] = $GLOBALS['MSG_xsl']['XSL_add_form_value'];
$param['type'] = 5; // Поле ввода.
$param['attributes'] = array();
$param['attributes']['rows'] = 25;
$param['value'] = $xsl_value;
$param['attributes']['style'] = 'height: 400px; width: 100%';
$param['attributes']['class'] = 'codepress xsl';
$admin_forms_fields->AddField($param);

// CodePress для textarea
$admin_forms_fields->AddExternalHtml('<script type="text/javascript">CodePressAction("load");</script>');

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

// Форматировать XSL-шаблон.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'xsl_format';
$param['caption'] = $GLOBALS['MSG_xsl']['XSL_add_form_format'];
$param['type'] = 1; // Флажок
$param['value'] = $xsl_format;

$admin_forms_fields->AddField($param);

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

// Порядковый номер XSL-шаблона.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'xsl_order';
$param['caption'] = $GLOBALS['MSG_xsl']['XSL_add_form_order'];
$param['type'] = 0; // Поле ввода.
$param['value'] = $xsl_order;

$admin_forms_fields->AddField($param);

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

// Комментарий к XSL-шаблону.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'xsl_comment';
$param['caption'] = $GLOBALS['MSG_xsl']['XSL_add_form_comment'];
$param['type'] = 5; // Поле ввода.
$param['attributes'] = array();
$param['attributes']['rows'] = 15;
$param['value'] = $xsl_comment;

$admin_forms_fields->AddField($param);

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

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

// Добавляем скрытое поле с идентификатором исходного раздела XSL.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'xsl_dir_parent_id';
$param['type'] = 4; // Скрытое поле.
$param['value'] = $xsl_dir_parent_id;

$admin_forms_fields->AddField($param);

// Кнопка "Сохранить".
$param = array();
$param['name'] = 'save_xsl';
$param['caption'] = $GLOBALS['MSG_xsl']['XSL_save_dir_form_button'];
$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_xsl';
$param['caption'] = $GLOBALS['MSG_xsl']["XSL_add_form_button"];
$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();
?>