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

$DateClass = new DateClass();

$structure = & singleton('Structure');

$row_information_item = $InformationSystem->GetInformationSystemItem($information_item_id);

$form_params = array();

$item_name = $row_information_item['information_items_name'];

$form_params['title'] = $information_comment_id == 0
? $GLOBALS['MSG_information_systems']['comment_add_form_title']
: $GLOBALS['MSG_information_systems']['comment_edit_form_title'] . ' "' . $item_name . '"';
$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/InformationSystems/InformationSystems.php';
$form_params['form_attribs']['name'] = 'EditAddItemComment';

$admin_forms_fields = new admin_forms_fields();

// Формируем "хлебные крошки"
$new_path_array = array();

$new_path_array[] = array(
'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/InformationSystems/InformationSystems.php', "&all_comments_all_systems=1", 52, 'load_data'),
'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/InformationSystems/InformationSystems.php', "&all_comments_all_systems=1", 52, 'load_data'),
'name' => $GLOBALS['MSG_information_systems']['show_comments_all_systems_link']
);

if (isset($row_information_item['information_systems_id']))
{
	$information_system_id = $row_information_item['information_systems_id'];

	$inf_sys_row = $InformationSystem->GetInformationSystem($information_system_id);

	$new_path_array[] = array(
	'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/InformationSystems/InformationSystems.php', "&action=show_comments&all_comments_system=1&information_system_id={$information_system_id}", 52, 'load_data'),
	'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/InformationSystems/InformationSystems.php', "&action=show_comments&all_comments_system=1&information_system_id={$information_system_id}", 52, 'load_data'),
	'name' => sprintf($GLOBALS['MSG_information_systems']['show_comments_system_title'], $inf_sys_row['information_systems_name']));
}

$new_path_array[] = array(
'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/InformationSystems/InformationSystems.php', '', 11, 'load_data'),
'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/InformationSystems/InformationSystems.php', '', 11, 'load_data'),
'name' => $GLOBALS['MSG_information_systems']['show_information_systems_title']
);

// Получаем данные об информационной системе, группу которой сейчас добавляем/редактируем
$information_system_row = $InformationSystem->GetInformationSystem($information_system_id);

// Определим ID макета по структуре, связанной с инфосистемой
$structure = & singleton('Structure');
$structure_row = $structure->GetStructureItem(to_int($information_system_row['structure_id']));
$template_id = to_int($structure_row['templates_id']);

// Отображаем список всех комментариев всех информационных систем
if ($information_system_id == 0)
{
	$information_system_id = $row_information_item['information_systems_id'];
}

$path_array = $InformationSystem->GetInformationGroupsPathArray($information_groups_parent_id, $information_system_id);

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/InformationSystems/InformationSystems.php', "&information_groups_parent_id={$key}&information_system_id={$information_system_id}", 12, 'load_data'),
			'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/InformationSystems/InformationSystems.php', "&information_groups_parent_id={$key}&information_system_id={$information_system_id}", 12, 'load_data'),
			'name' => $value['information_groups_name']);
		}
	}
}

$new_path_array[] = array(
'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/InformationSystems/InformationSystems.php', "&action=show_comments&information_system_id={$information_system_id}&information_groups_parent_id={$information_groups_parent_id}&information_item_id={$information_item_id}", 52, 'load_data'),
'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/InformationSystems/InformationSystems.php', "&action=show_comments&information_system_id={$information_system_id}&information_groups_parent_id={$information_groups_parent_id}&information_item_id={$information_item_id}", 52, 'load_data'),
'name' => sprintf($GLOBALS['MSG_information_systems']['show_comments_title'], htmlspecialchars($item_name)));

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


$row = $InformationSystem->GetComment($information_comment_id);

// Редактирование комментария
if ($information_comment_id && $row)
{
	$comment_parent_id = $row['comment_parent_id'];
	$comment_fio = $row['comment_fio'];
	$comment_email = $row['comment_email'];
	$comment_text = $row['comment_text'];
	$comment_status = $row['comment_status'];
	$comment_subject = $row['comment_subject'];
	$comment_ip = $row['comment_ip'];
	$comment_date = date('d.m.Y H:i:s', $DateClass->DateSqlToUnix($row['comment_date']));
	$comment_grade = $row['comment_grade'];
	$site_users_id = $row['site_users_id'];
}
else // Добавление комментария к информационному элементу
{
	$comment_parent_id = 0;
	$comment_fio = '';
	$comment_email = '';
	$comment_text = '';
	$comment_status = 1;
	$comment_subject = '';
	$comment_ip = $_SERVER['REMOTE_ADDR'];
	$comment_date = date('d.m.Y H:i:s');
	$comment_grade = 0;
	$site_users_id = 0;
}

$admin_forms_fields->admin_forms_fields($form_params);

$DataBase = & singleton('DataBase');

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

// ФИО автора комментария
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'comment_fio';
$param['caption'] = $GLOBALS['MSG_information_systems']['comment_add_form_fio'];
$param['type'] = 0; // textbox
$param['attributes']['class'] = 'large';
$param['value'] = $comment_fio;
$param['format']['maxlen']['value'] = 255;

$admin_forms_fields->AddField($param);

// E-mail автор комментария
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'comment_email';
$param['caption'] = $GLOBALS['MSG_information_systems']['comment_add_form_email'];
$param['type'] = 0; // textbox
$param['value'] = $comment_email;
$param['format']['lib']['value'] = 'email';

$admin_forms_fields->AddField($param);

// Текст комментария
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'comment_text';
$param['caption'] = $GLOBALS['MSG_information_systems']['comment_add_form_text'];

// Если не существует константа для отображения wysywig или
// она равна true, то отображаем wysiwyg.
if (!defined('ALLOW_WYSIWYG_COMMENT_IS_ITEM') || (defined('ALLOW_WYSIWYG_COMMENT_IS_ITEM')  && ALLOW_WYSIWYG_COMMENT_IS_ITEM))
{
	$param['type'] = 7; // Визуальный редактор.
	$param['template'] = $template_id;
}
else
{
	$param['type'] = 5; // textarea
	$param['attributes'] = array();
	$param['attributes']['rows'] = 7;
}

// Получаем данные об информационной системе, элемент которой сейчас добавляем/редактируем
$information_system_row = $InformationSystem->GetInformationSystem($information_system_id);

$information_system_structure_id = $information_system_row['structure_id'];

$row_structure = $structure->GetStructureItem($information_system_structure_id);

$param['template'] = $row_structure['templates_id'];

$param['value'] = $comment_text;

$admin_forms_fields->AddField($param);

// Добавляем чекбоксы типографики для описания.
if (class_exists("typograph"))
{
	// Оптическое выравнивание
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['type'] = 1;
	$param['name'] = 'use_typograph_for_comment_text';
	$param['html_id'] = 'use_typograph_for_comment_text';
	$param['caption'] = $GLOBALS['MSG_information_systems']['exec_typograph_for_comment'];
	$param['lable'] = $GLOBALS['MSG_information_systems']["yes"];
	$param['value'] = 1;

	$admin_forms_fields->AddField($param);

	// Висячая пунктуация
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['type'] = 1;
	$param['name'] = 'trailing_punctuation_for_comment_text';
	$param['html_id'] = 'id_trailing_punctuation_for_comment_text';
	$param['caption'] = $GLOBALS['MSG_information_systems']['use_trailing_punctuation_for_comment'];
	$param['lable'] = $GLOBALS['MSG_information_systems']["yes"];
	$param['value'] = 1;
	$admin_forms_fields->AddField($param);
}

// Статус активности комментария
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'comment_status';
$param['caption'] = $GLOBALS['MSG_information_systems']['comment_add_form_status'];
$param['type'] = 2; // Выпадающий список.
$param['value'] = $comment_status;

$items = array();

$items[0] = $GLOBALS['MSG_information_systems']['comment_add_form_status_false'];
$items[1] = $GLOBALS['MSG_information_systems']['comment_add_form_status_true'];

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

// Тема комментария
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'comment_subject';
$param['caption'] = $GLOBALS['MSG_information_systems']['comment_add_form_subject'];
$param['type'] = 0; // textbox
$param['value'] = $comment_subject;

$admin_forms_fields->AddField($param);

// IP-адрес компьютера автора комментария
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'comment_ip';
$param['caption'] = $GLOBALS['MSG_information_systems']['comment_add_form_ip'];
$param['type'] = 0; // textbox
$param['value'] = $comment_ip;
$param['format']['lib']['value'] = 'ip';

$admin_forms_fields->AddField($param);

// Дата-время добавления комментария
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'comment_date';
$param['caption'] = $GLOBALS['MSG_information_systems']['comment_add_form_date'];
$param['type'] = 11; // дата-время
$param['value'] = $comment_date;
$param['format']['lib']['value'] = 'datetime';

$admin_forms_fields->AddField($param);

// Оценка
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'comment_grade';
$param['caption'] = $GLOBALS['MSG_information_systems']['comment_add_form_grade'];
$param['type'] = 12; // textarea
$param['value'] = $comment_grade;
$param['options']['max_grade'] = 5;

$admin_forms_fields->AddField($param);

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

	$admin_forms_fields->AddField($param);
}

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

$admin_forms_fields->AddField($param);
// Добавляем скрытое поле с идентификатор информационного элемента, к которому принадлежит комментарий
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'information_item_id';
$param['type'] = 4; // Скрытое поле.
$param['value'] = $information_item_id;

$admin_forms_fields->AddField($param);

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

$admin_forms_fields->AddField($param);

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

$admin_forms_fields->AddField($param);

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

$admin_forms_fields->AddField($param);

$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'all_comments_system';
$param['type'] = 4; // Скрытое поле.
$param['value'] = to_int($_REQUEST['all_comments_system']);

$admin_forms_fields->AddField($param);

$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'site_users_id';
$param['type'] = 4; // Скрытое поле.
$param['value'] = $site_users_id;

$admin_forms_fields->AddField($param);

$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'all_comments_all_systems';
$param['type'] = 4; // Скрытое поле.
$param['value'] = to_int($_REQUEST['all_comments_all_systems']);
$admin_forms_fields->AddField($param);

// Кнопка "Сохранить".
$param = array();
$param['name'] = 'save_item_comment';
$param['caption'] = $GLOBALS['MSG_information_systems']['comment_save_form_button_name'];
$param['image'] = '/admin/images/add.gif';
$param['attributes']['onclick'] = "return doSendForm('{$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_item_comment';
$param['caption'] = $GLOBALS['MSG_information_systems']['comment_add_form_button_name'];
$param['image'] = '/admin/images/add.gif';
$param['attributes']['onclick'] = "return doSendForm('{$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();
?>