<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2010 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Форма добавления/редактирования макетов.
 * 
 * Файл: /admin/templates/templates/action/form_edit_add_template.php
 * 
 * @author Hostmake LLC
 * @version 5.x
 */
$form_params = array();
$form_params['title'] = $templates_id == 0 ? $GLOBALS['MSG_templates']['templates_add_form_title'] : $GLOBALS['MSG_templates']['templates_edit_form_title'];
$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/templates/templates/templates.php';
$form_params['form_attribs']['name'] = 'EditAddTemplate';

$admin_forms_fields = new admin_forms_fields();

$templates = new templates();

$path_array = $templates->GetTemplatePathArray($templates_group_id);

$new_path_array = array();

// Формируем "хлебные крошки"
if ($path_array)
{
	foreach ($path_array as $key => $value)
	{
		$new_path_array[] = array (
		'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/templates/templates/templates.php', "&templates_group_parent_id={$key}", 6, 'load_data'),
		'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/templates/templates/templates.php', "&templates_group_parent_id={$key}", 6, 'load_data'),
		'name' => $value);
	}
}

$param['path_array'] = $new_path_array;

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

$admin_forms_fields->admin_forms_fields($form_params);

$admin_forms = & singleton('admin_forms');

$template_row = false;

if ($templates_id)
{
	$param = array();
	$param['cache_off'] = true;
	$template_row = $templates->GetTemplate($templates_id, $param);
	
	$site_id = to_int($template_row['site_id']);
}
else 
{
	$site_id = CURRENT_SITE;
}

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

// Название макета.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'templates_name';
$param['caption'] = $GLOBALS['MSG_templates']['templates_add_edit_form_name'];
$param['type'] = 0; // Поле ввода.
if ($template_row)
{
	$param['value'] = to_str($template_row['templates_name']);
}
$param['attributes']['class'] = 'large';
$param['format']['minlen']['value'] = 1;
$param['format']['maxlen']['value'] = 255;
$admin_forms_fields->AddField($param);


// Родительская группа.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'templates_group_id';
$param['caption'] = '';
$param['type'] = 4; // Поле ввода.
$param['value'] = $templates_group_id;
$admin_forms_fields->AddField($param);

// Текущая родительская группа.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'templates_parent_group_id';
$param['caption'] = $GLOBALS['MSG_templates']['templates_add_form_group'];
$param['type'] = 2; // Выпадающий список
$value = $templates_group_id;
$param['value'] = $value;

$items = array();
$items[0] = '&hellip;';

$tree = $templates->GetTemplateDirTree(0, "&nbsp;&nbsp;", false, true, $site_id);

if ($tree)
{
	foreach ($tree as $key => $value)
	{
		$items[$value[0]] = $value[1];
	}
}

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

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

$sites_array = array();

// Получаем список доступных сайтов
$site = & singleton ('site');

// Получаем данные о пользователе
$UserAccess = new user_access();
$user_row = $UserAccess->GetUserByName($_SESSION['valid_user']);

// Выбираем доступные сайты
$res = $site->SelectSites(array('users_type_id' => to_int($user_row['users_type_id'])));

while ($rows = mysql_fetch_assoc($res)) 
{
	$sites_array[$rows['site_id']] = $rows['site_name'];
}

// Список сайтов.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'site_id';
$param['caption'] = $GLOBALS['MSG_templates']['templates_site'];
$param['type'] = 2; // Выпадающий список.
$param['items'] = $sites_array;
$param['value'] = $site_id;
$admin_forms_fields->AddField($param);

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

if ($template_row)
{
	$param['value'] = to_str($template_row['templates_order']);
}

$param['format']['lib']['value'] = 'integer';
$admin_forms_fields->AddField($param);

// Макет.
$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_templates']['templates_form_tab_2']);

// Данные макета.
if (!to_int($template_row['templates_id']))
{
	$templates_value = '';
}
else
{
	$File = new File();
	
	$file_name = CMS_FOLDER . 'templates/template' . to_int($template_row['templates_id']) . '/template.htm';
	
	if (is_file($file_name))
	{
		$templates_value = $File->GetFileContent($file_name);
	}
	else
	{
		$templates_value = '';
	}
}

// Код макета.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'templates_value';
$param['caption'] = $GLOBALS['MSG_templates']['templates_add_edit_form_template'];
$param['type'] = 5; // Textarea.
$param['attributes'] = array();
$param['attributes']['rows'] = 20;
$param['value'] = $templates_value;
$param['attributes']['style'] = 'height: 400px; width: 100%';
$param['attributes']['class'] = 'codepress php';
$admin_forms_fields->AddField($param);

// CSS.
$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_templates']['templates_form_tab_3']);

// Данные макета.
if (!to_int($template_row['templates_id']))
{
	$css_value = '';
}
else
{
	$File = new File();
	
	$file_name = CMS_FOLDER . 'templates/template' . to_int($template_row['templates_id']) . '/style.css';
	
	if (is_file($file_name))
	{
		$css_value = $File->GetFileContent($file_name);
	}
	else
	{
		$css_value = '';
	}
}

$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'css_value';
$param['caption'] = $GLOBALS['MSG_templates']['templates_add_edit_form_CSS'];
$param['type'] = 5; // Textarea.
$param['attributes'] = array();
$param['attributes']['rows'] = 20;
$param['value'] = $css_value;
$param['attributes']['style'] = 'height: 400px; width: 100%';
$param['attributes']['class'] = 'codepress css';
$admin_forms_fields->AddField($param);

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

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

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