<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2010 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Форма добавления/редактирования комментария.
 * 
 * Файл: /admin/InformationSystems/action/form_edit_add_comments.php
 * 
 * @author Hostmake LLC
 * @version 5.x
 */
$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']
);

$information_systems_dir_parent_id = to_int($_REQUEST['information_systems_dir_parent_id']);

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_systems_dir_parent_id={$information_systems_dir_parent_id}&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_systems_dir_parent_id={$information_systems_dir_parent_id}&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_systems_dir_parent_id = to_int($_REQUEST['information_systems_dir_parent_id']);
	
// Путь по группам инфосистем
if ($information_systems_dir_parent_id)
{
	$information_systems_dir_path_array = $InformationSystem->GetInformationSystemsDirPathArray($information_systems_dir_parent_id);
	
	// Меняем порядок следования элементов в массиве на обратный
	$information_systems_dir_path_array = array_reverse($information_systems_dir_path_array, true);
	
	foreach ($information_systems_dir_path_array as $key => $value)
	{
		$new_path_array[] = array (
		'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/InformationSystems/InformationSystems.php', "&information_systems_dir_parent_id={$key}", 11, 'load_data'),
		'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/InformationSystems/InformationSystems.php', "&information_systems_dir_parent_id={$key}", 11, 'load_data'),
		'name' => $value['information_systems_dir_name']);
	}
}	

// Получаем данные об информационной системе, группу которой сейчас добавляем/редактируем
$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_systems_dir_parent_id={$information_systems_dir_parent_id}&information_system_id={$information_system_id}", 12, 'load_data'),
			'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/InformationSystems/InformationSystems.php', "&information_groups_parent_id={$key}&information_systems_dir_parent_id={$information_systems_dir_parent_id}&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_systems_dir_parent_id={$information_systems_dir_parent_id}&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_systems_dir_parent_id={$information_systems_dir_parent_id}&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'], $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'];
	$comment_phone = $row['comment_phone'];
	$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;
	$comment_phone = '';
	$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);

// Тема комментария
$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;
//$param['attributes']['style'] = 'width: 320px;';
//$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

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

// 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';
$param['attributes']['style'] = 'width: 320px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

// Номер телефона
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'comment_phone';
$param['caption'] = $GLOBALS['MSG_information_systems']['comment_add_form_phone'];
$param['type'] = 0; // textbox
$param['value'] = $comment_phone;
$param['attributes']['style'] = 'width: 320px;';
$param['div_attributes']['style'] = 'float: left;';
$param['format']['maxlen']['value'] = 100;
$admin_forms_fields->AddField($param);

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

// Текст комментария
$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['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['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;
$param['attributes']['style'] = 'width: 110px;';
$param['div_attributes']['style'] = 'float: left;';
$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';
$param['attributes']['style'] = 'width: 110px;';
$param['div_attributes']['style'] = 'float: left;';
$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';
//$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

// Идентификатор родительского комментария
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'comment_parent_id';
$param['caption'] = $GLOBALS['MSG_information_systems']['comment_add_form_parent_id'];
$param['type'] = 0; // textbox
$param['value'] = $comment_parent_id;
$param['format']['lib']['value'] = 'integer';
$param['attributes']['style'] = 'width: 220px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

// Идентификатор пользователя
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'site_users_id';
$param['caption'] = $GLOBALS['MSG_information_systems']['comment_add_form_site_user_id'];
$param['type'] = 0; // Поле ввода
$param['value'] = $site_users_id;
$param['format']['lib']['value'] = 'integer';
$param['format']['lib']['message'] = $GLOBALS['MSG_information_systems']['show_invalid_characters_integer'];
$param['format']['minlen']['value'] = 1;
$param['format']['maxlen']['value'] = 11;
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 150px;';
$admin_forms_fields->AddField($param);

// Если указан ID пользователя и есть модуль "Пользователи сайта"
if (!empty($site_users_id) && class_exists('SiteUsers'))
{
	$SiteUsers = & singleton('SiteUsers');
	$site_user_row = $SiteUsers->GetSiteUser($site_users_id);

	if ($site_user_row)
	{
		ob_start();
		?><div class="large item_div item_div_as_is" style="width: 150px"><img src="/admin/images/new_window.gif" /> <a href="<?php echo "/admin/Site_users/Site_users.php?admin_forms_id=30&operation=edit_user&check_0_{$site_user_row['site_users_id']}=1"?>" target="_blank"><?php echo $site_user_row['site_users_login']?></a></div><?php
		$site_users_link = ob_get_clean();

		// Ссылка на пользователя
		$param = array();
		$param['tab_id'] = $tab_id;
		$param['name'] = 'site_users_id_link';
		$param['caption'] = '&nbsp;';
		$param['type'] = 15; // Как есть
		$param['value'] = $site_users_link;
		$admin_forms_fields->AddField($param);
	}
}

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

// Оценка
$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'] = '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_systems_dir_parent_id';
$param['type'] = 4; // Скрытое поле.
$param['value'] = $information_systems_dir_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'] = '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 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_item_comment';
$param['caption'] = $GLOBALS['MSG_information_systems']['comment_add_form_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();
?>