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

// Title для страницы
$form_params['title'] = $tag_id == 0
? $GLOBALS['MSG_tag']['form_edit_add_title_add']
: $GLOBALS['MSG_tag']['form_edit_add_title_edit'];

// Метод отправки данных
$form_params['form_attribs']['method'] = 'post';

// Адрес всех обработчиков модуля – главный файл формы
$form_params['form_attribs']['action'] = '/admin/tag/tag.php';

// Метод отправки данных
$form_params['form_attribs']['name'] = 'EditAddTag';

$admin_forms_fields = & singleton('admin_forms_fields');

$i = 0;

$current_tags_group_parent_id = to_int($_REQUEST['tags_group_parent_id']);

//Хлебные крошки
$new_path_array = array();
$new_path_array[$i]['name'] = $GLOBALS['MSG_tag']['title'];
$new_path_array[$i]['link'] = $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/tag/tag.php', '&tags_group_parent_id=0', 173, 'load_data');
$new_path_array[$i]['onclick'] = $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/tag/tag.php', '&tags_group_parent_id=0', 173, 'load_data');

// Строим "хлебные крошки"
$path_array = $Tag->GetTagsGroupsPathArray($current_tags_group_parent_id);
		
unset($path_array[0]);

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

// Преобразуем идентификаторы групп в ссылки на группы.
if ($path_array)
{
	foreach ($path_array as $group_parent_id => $value)
	{
		$new_path_array[] = array (
		'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/tag/tag.php', "&tags_group_parent_id={$group_parent_id}", 173, 'load_data'),
		'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/tag/tag.php', "&tags_group_parent_id={$group_parent_id}", 173, 'load_data'),
		'name' => $value['tags_group_name']);
	}
}

$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);


// Редактировние
if ($tag_id)
{	
	$row = $Tag->GetTag($tag_id);
		
	$tag_name = $row['tag_name'];
	$tag_path = $row['tag_path'];
	$tag_description = $row['tag_description'];
	$tags_group_id = $row['tags_group_id'];
	$users_id = $row['users_id'];
}
// Добавление
else
{
	$kernel = & singleton('kernel');

	$users_id = $kernel->GetCurrentUser();

	$tag_name = '';
	$tag_path = '';
	$tag_description = '';
	$tags_group_id = $current_tags_group_parent_id;
}

$tab_id = $admin_forms_fields->AddTab('');

$param = array();

// Ключевое слово.

if ($tag_id)
{
	$param['tab_id'] = $tab_id;
	$param['name'] = 'tag_name';
	$param['caption'] = $GLOBALS['MSG_tag']['form_edit_tag_name'];
	$param['type'] = 0; // Поле ввода
	$param['value'] = $tag_name;
	$param['attributes']['class'] = 'large';
	$param['format']['minlen']['value'] = 1;
	$param['format']['maxlen']['value'] = 255;
}
else
{	
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'tag_name';
	$param['caption'] = $GLOBALS['MSG_tag']['form_add_tag_name'];
	
	$param['type'] = 5; // textarea
	$param['attributes'] = array();
	$param['attributes']['rows'] = 5;
		
	$param['value'] = '';
}
$admin_forms_fields->AddField($param);

// Список групп
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'new_tags_group_parent_id';
$param['caption'] = $GLOBALS['MSG_tag']['form_edit_add_tags_group_parent_name'];
$param['type'] = 2; // Выпадающий список.

$param['value'] = $tags_group_id;

$items = array();
$mas = array();
$param_tags_group = array();

$param_tags_group['tags_group_parent_id'] = 0; 
$param_tags_group['separator'] = '&nbsp;';
//$param_tags_group['tags_group_id'] = $tags_group_id; 

$mas = $Tag->GetTagsGroupsTree($param_tags_group);

$items[0] = '..';
$count_groups = count($mas);

for ($i = 0; $i < $count_groups; $i++)
{
	$items[$mas[$i]['tags_group_id']] = $mas[$i]['separator'] . $mas[$i]['tags_group_name'];
}

$param['items'] = $items;

$admin_forms_fields->AddField($param);


$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'tag_path';
$param['caption'] = $GLOBALS['MSG_tag']['form_add_tag_path'];
$param['type'] = 0; // Поле ввода
$param['value'] = $tag_path;
$param['format']['lib']['value'] = 'path';
$param['format']['lib']['message'] = $GLOBALS['MSG_tag']['show_invalid_characters_path'];
$param['format']['maxlen']['value'] = 255;
$param['attributes']['style'] = 'font-weight: bold; width: 100%';


$admin_forms_fields->AddField($param);


if ($tag_id)
{
	$tag_description_caption = $GLOBALS['MSG_tag']['form_add_tag_description']; 
}
else
{
	$tag_description_caption = $GLOBALS['MSG_tag']['form_add_tag_or_tags_description'];
}


$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'tag_description';
$param['caption'] = $tag_description_caption;

$param['type'] = 7; // Визуальный редактор
$param['value'] = $tag_description;

$admin_forms_fields->AddField($param);

// Если редактирование
if ($tag_id)
{
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'tag_id';
	$param['caption'] = '';
	$param['type'] = 4; // hidden.
	$param['value'] = $tag_id;

	$admin_forms_fields->AddField($param);
}

$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'tags_group_parent_id';
$param['caption'] = '';
$param['type'] = 4; // hidden.
$param['value'] = $current_tags_group_parent_id;

$admin_forms_fields->AddField($param);

$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'users_id';
$param['caption'] = '';
$param['type'] = 4; // hidden.
$param['value'] = $users_id;

$admin_forms_fields->AddField($param);

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