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

$shop = & singleton('shop');

$admin_forms_fields = new admin_forms_fields();

$shop_shops_id = to_int($_REQUEST['shop_shops_id']);
$shop_groups_parent_id = to_int($_REQUEST['shop_groups_parent_id']);
$resource = $shop->GetComment($shop_comment_id, array('cache_off' => true));

if (isset($_REQUEST['sort_catalog_item_id']) && intval($_REQUEST['sort_catalog_item_id']) != 0)
{
	$sort_catalog_item_id = to_int($_REQUEST['sort_catalog_item_id']);
}
else
{
	$sort_catalog_item_id = $resource['shop_items_catalog_item_id'];
}

// Если был передан идентификатор товара
if ($sort_catalog_item_id)
{
	$res = $shop->GetItem($sort_catalog_item_id);

	$shop_name_hc = sprintf($GLOBALS['MSG_shops']['show_item_comment_title'], $res['shop_items_catalog_name']);
}
else
{
	$shop_name_hc =$GLOBALS['MSG_shops']['items_catalog_add_form_comment_link'];
}

$datetime = & singleton('DateClass');
$DataBase = & singleton('DataBase');

if ($shop_shops_id)
{
	$row = $shop->GetShop($shop_shops_id);
}
else
{
	// Получаеи идентификатор магазина по идентификатору комментария
	$shop_data_rows = $shop->GetComment($shop_comment_id, array('cache_off' => true));
	$shop_items_catalog_item_id = $shop_data_rows['shop_items_catalog_item_id'];
	$shop_data_rows = $shop->GetItem($shop_items_catalog_item_id, array('cache_off' => true));
	$shop_shops_id = $shop_data_rows['shop_shops_id'];
	$row = $shop->GetShop($shop_shops_id);
}

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

if ((isset($_REQUEST['all_comments_all_shops']) || isset($_REQUEST['all_comments_this_shop']))  && $sort_catalog_item_id)
{
	$item_row = $shop->GetItem($sort_catalog_item_id, array('cache_off' => true));

	$shop_groups_parent_id = $item_row['shop_groups_id'];
}

// Строим хлебные крошки.
$path_array = $shop->GetShopPathArray($shop_groups_parent_id, false);

// Преобразуем идентификаторы групп в ссылки на группы.
$new_path_array = array();

$new_path_array[0]['name'] = $GLOBALS['MSG_shops']['header_admin__list_forms'];
$new_path_array[0]['link'] = $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', '', 54, 'load_data');
$new_path_array[0]['onclick'] = $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', '', 54, 'load_data');

$new_path_array[] = array('name' => $row['shop_shops_name'],
'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=0&shop_shops_id=$shop_shops_id", 65, 'load_data'),
'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=0&shop_shops_id=$shop_shops_id", 65, 'load_data'));

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

// Если мы пришли сюда из формы списка модификаций
if (isset($_REQUEST['shop_items_catalog_modification_id']))
{
	$shop_items_catalog_modification_id = to_int($_REQUEST['shop_items_catalog_modification_id']);

	// Получаем имя товара от которого пришли

	$row = $shop->GetItem($shop_items_catalog_modification_id);

	$new_path_array[] = array("name" => sprintf($GLOBALS['MSG_shops']['item_modification_title'], $row['shop_items_catalog_name']),
	"link" => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&shop_items_catalog_modification_id=$shop_items_catalog_modification_id", 81, 'load_data'),
	"onclick" => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&shop_items_catalog_modification_id=$shop_items_catalog_modification_id", 81, 'load_data'));

	$new_path_array[] = array('name' => $shop_name_hc,
	'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_items_catalog_modification_id=$shop_items_catalog_modification_id&shop_groups_parent_id=$shop_groups_parent_id&sort_catalog_item_id=$sort_catalog_item_id", 66, 'load_data'),
	'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_items_catalog_modification_id=$shop_items_catalog_modification_id&shop_groups_parent_id=$shop_groups_parent_id&sort_catalog_item_id=$sort_catalog_item_id", 66, 'load_data')
	);

}
else
{

	if (isset($_REQUEST['all_comments_all_shops']))
	{
		$new_path_array[] = array('name' => $GLOBALS['MSG_shops']['all_list_of_comments'],
		'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&all_comments_all_shops=1", 66, 'load_data'),
		'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&all_comments_all_shops=1", 66, 'load_data')
		);
	}

	if (isset($_REQUEST['all_comments_this_shop']))
	{

		$new_path_array[] = array('name' => $GLOBALS['MSG_shops']['items_catalog_add_form_comment_link'],
		'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&all_comments_this_shop=1", 66, 'load_data'),
		'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&all_comments_this_shop=1", 66, 'load_data')
		);

		$additional_param = "&all_comments_this_shop=1";
	}
	else
	{
		$additional_param = "";
	}

	$new_path_array[] = array('name' => $shop_name_hc,
	'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&sort_catalog_item_id=$sort_catalog_item_id".$additional_param, 66, 'load_data'),
	'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&sort_catalog_item_id=$sort_catalog_item_id".$additional_param, 66, 'load_data')
	);
}

$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

// Редактирование отзыва
if ($shop_comment_id)
{
	$type = 1;
	$result = $shop->GetComment($shop_comment_id);
	$site_users_id = $result['site_users_id'];
	$shop_comment_user_name = $result['shop_comment_user_name'];
	$shop_comment_subject = $result['shop_comment_subject'];
	$shop_comment_text = $result['shop_comment_text'];
	$shop_comment_grade = $result['shop_comment_grade'];
	$shop_comment_user_ip = $result['shop_comment_user_ip'];
	$shop_comment_date_time = $datetime->datetime_format($result['shop_comment_date_time']);
	$shop_comment_active = $result['shop_comment_active'];
	$shop_comment_is_comment = $result['shop_comment_is_comment'];
	$shop_items_catalog_item_id = $result['shop_items_catalog_item_id'];
	$site_users_id = $result['site_users_id'];
	$users_id = $result['users_id'];
	$shop_comment_user_email = $result['shop_comment_user_email'];
}
// Добавление отзыва
else
{
	$site_users_id = 0;
	$shop_items_catalog_item_id = to_int($_REQUEST['sort_catalog_item_id']);
	$shop_comment_user_name = '';
	$shop_comment_subject = '';
	$shop_comment_text = '';
	$shop_comment_grade = 0;
	$shop_comment_user_ip = $_SERVER['REMOTE_ADDR'];
	$shop_comment_date_time = date("d.m.Y H:i:s");
	$shop_comment_active = 0;
	$shop_comment_is_comment = 0;
	$site_users_id = 0;
	$shop_comment_user_email = '';
}

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

// Автор
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_comment_user_name';
$param['caption'] = $GLOBALS['MSG_shops']['comment_form_user_name'];
$param['type'] = 0; // Поле ввода
$param['value'] = $shop_comment_user_name;
//$param['format']['minlen']['value'] = 1;
//$param['format']['maxlen']['value'] = 255;
$param['attributes']['class'] = 'large';

$admin_forms_fields->AddField($param);

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

// E-mail
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_comment_user_email';
$param['caption'] = $GLOBALS['MSG_shops']['shop_form_comment_add_edit_email'];
$param['type'] = 0; // Поле ввода
$param['value'] = $shop_comment_user_email;

$admin_forms_fields->AddField($param);

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

// Тема
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_comment_subject';
$param['caption'] = $GLOBALS['MSG_shops']['comment_form_subject'];
$param['type'] = 0; // Поле ввода
$param['value'] = $shop_comment_subject;
$param['format']['minlen']['value'] = 1;
$param['format']['maxlen']['value'] = 255;
$admin_forms_fields->AddField($param);

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

// Дата/время
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_comment_date_time';
$param['html_id'] = 'shop_comment_date_time';
$param['caption'] = $GLOBALS['MSG_shops']['comment_form_date_time'];
$param['type'] = 11; // Дата/время
$param['value'] =  $shop_comment_date_time;
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

// IP-адрес компьютера автора комментария
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_comment_user_ip';
$param['caption'] = $GLOBALS['MSG_shops']['comment_form_ip'];
$param['type'] = 0;
$param['value'] = $shop_comment_user_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'] = 'shop_items_catalog_item_id';
$param['caption'] = $GLOBALS['MSG_shops']['comment_form_id'];
$param['type'] = 0; // Поле ввода
$param['value'] = $shop_items_catalog_item_id;
$param['format']['minlen']['value'] = 1;
$param['format']['maxlen']['value'] = 11;
$param['format']['lib']['value'] = 'integer';
$param['format']['lib']['message'] = $GLOBALS['MSG_shops']['show_invalid_characters_integer'];
$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'] = 'site_users_id';
$param['caption'] = $GLOBALS['MSG_shops']['comment_form_site_users_id'];
$param['type'] = 0; // Поле ввода
$param['value'] = $site_users_id;
$param['attributes']['style'] = 'width: 110px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

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

// Показывать комментарий
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_comment_active';
$param['caption'] = $GLOBALS['MSG_shops']['comment_form_active'];
$param['type'] = 1; // Флажок
$param['value'] = $shop_comment_active;
$admin_forms_fields->AddField($param);

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

// Текст
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_comment_text';
$param['caption'] = $GLOBALS['MSG_shops']['comment_form_text'];
$param['type'] = 7; // Визуальный редактор.

// ID шаблона
$param['template'] = $template_id;

$param['value'] = $shop_comment_text;
$param['attributes']['rows'] = 10;
//$param['format']['minlen']['value'] = 1;
$admin_forms_fields->AddField($param);

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

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

$admin_forms_fields->AddField($param);

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


// передеам скрытое поле с идентификатором адреса если он есть
if ($shop_comment_id)
{
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'shop_comment_id';
	$param['caption'] = '';
	$param['type'] = 4; // hidden.
	$param['value'] = $shop_comment_id;
	$admin_forms_fields->AddField($param);
}

$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'action';
$param['caption'] = '';
$param['type'] = 4; // hidden.
$param['value'] = 'show_comment';
$admin_forms_fields->AddField($param);

// Если пришли из формы модификаций
if (isset($shop_items_catalog_modification_id))
{
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'shop_comment_id';
	$param['caption'] = '';
	$param['type'] = 4; // hidden.
	$param['value'] = $shop_comment_id;
	$admin_forms_fields->AddField($param);
}

$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_shops_id';
$param['caption'] = '';
$param['type'] = 4; // hidden.
$param['value'] = $shop_shops_id;
$admin_forms_fields->AddField($param);

$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_groups_parent_id';
$param['caption'] = '';
$param['type'] = 4; // hidden.
$param['value'] = $shop_groups_parent_id;
$admin_forms_fields->AddField($param);

$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'sort_catalog_item_id';
$param['caption'] = '';
$param['type'] = 4; // hidden.
$param['value'] = $sort_catalog_item_id;
$admin_forms_fields->AddField($param);

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