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

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

$form_params = array();
$form_params['title'] = ($document_id == 0)
? $GLOBALS['MSG_documents']['documents_add_form_title']
: $GLOBALS['MSG_documents']['documents_edit_form_title'];

$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/documents/documents.php';
$form_params['form_attribs']['name'] = 'EditAddDocument';

$admin_forms_fields = new admin_forms_fields();

// Масив для хранения "хлебных крошек"
$new_path_array = array();

$documents = new documents();
$path_array = $documents->GetDocumentsPathArray($documents_dir_parent_id);

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

$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

$document_row = false;

if ($document_id)
{
	// Получаем информацию о документе
	$document_row = $documents->GetDocument($document_id);

	$site_id = $document_row['site_id'];

	if (!$document_row)
	{
		show_error_message($GLOBALS['MSG_documents']['error_documents_id']);
		return false;
	}
}
else
{
	$site_id = CURRENT_SITE;
}

$version_row = false;

if ($document_row)
{
	// Получаем список версий документа
	$resource = $documents->select_version($document_id,
	$documents_version_id == 0 ? - 1 : $documents_version_id,
	$documents_version_id == 0 ? 1 : 0 );

	if ($resource && mysql_num_rows($resource) == 1)
	{
		$version_row = mysql_fetch_assoc($resource);
	}

	if ($documents_version_id && !$version_row)
	{
		show_error_message($GLOBALS['MSG_documents']['error_documents_version_id']);
		return false;
	}
}

// Получаем содержимое документа.
if ($version_row)
{
	$file_name = $documents->GetDocumentVersionPath(to_int($version_row['documents_version_id']));

	$File = new File();
	$documents_text = $File->GetFileContent($file_name);
}
else
{
	$documents_text = '';
}

$templates = new templates();
$all_templates = $templates->GetAllTemplates(false);

if (to_int($version_row['documents_version_id']))
{
	$template_id = to_int($version_row['templates_id']);
}
elseif ($all_templates) // Добавление документа
{
	$template_id = $all_templates[0]['templates_id'];
}
else 
{
	$template_id = 0;
}

// Закладка с названием/описанием.
$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_documents']['documents_form_tab_2']);

// Название документа.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'documents_name';
$param['caption'] = $GLOBALS['MSG_documents']['documents_add_form_name'];
$param['type'] = 0; // Поле ввода.
if (to_int($document_row['documents_id']))
{
	$param['value'] = to_str($document_row['documents_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'] = 'documents_text';
$param['caption'] = $GLOBALS['MSG_documents']['documents_add_form_text'];

if (!defined('USE_WYSIWYG')
|| (defined('USE_WYSIWYG')
	&& USE_WYSIWYG))
{
	$param['type'] = 7; // Визуальный редактор.
	$param['template'] = $template_id; // ID шаблона
}
else
{
	$param['type'] = 5; // textarea
	$param['attributes'] = array();
	$param['attributes']['rows'] = 10;
}

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

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

if (class_exists('typograph'))
{
	// Типографировать.
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['type'] = 1; // Флажок.
	$param['name'] = 'use_typograph';
	$param['html_id'] = 'id_use_typograph';
	$param['value'] = 1;
	$param['caption'] = $GLOBALS['MSG_documents']['exec_typograph'];
	$param['div_attributes']['style'] = 'float: left;';
	$admin_forms_fields->AddField($param);

	// Оптическое выравнивание.
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['type'] = 1; // Флажок.
	$param['name'] = 'trailing_punctuation';
	$param['html_id'] = 'id_trailing_punctuation';
	$param['value'] = 1;
	$param['caption'] = $GLOBALS['MSG_documents']['use_trailing_punctuation'];
	$param['div_attributes']['style'] = 'float: left;';
	$admin_forms_fields->AddField($param);
}

// Содержание документа.
$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_documents']['documents_form_tab_1']);

// Родительская группа.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'documents_dir_id';
$param['caption'] = $GLOBALS['MSG_documents']['documents_add_form_dir'];
$param['type'] = 2; // Поле ввода.

// Редактирование документа
if (to_int($document_row['documents_id']))
{
	$param['value'] = to_int($document_row['documents_dir_id']);
}
else // Добавление документа
{
	$param['value'] = $documents_dir_parent_id;
}

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

$tree = $documents->GetDocumentsDirTree(0, "&nbsp;&nbsp;");

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

$param['attributes']['style'] = 'width: 320px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

$sites_array = array();

// Получаем данные о пользователе
$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_documents']['documents_site'];
$param['type'] = 2; // Выпадающий список.
$param['items'] = $sites_array;
$param['value'] = $site_id;
$param['attributes']['style'] = 'width: 320px;';
$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'] = 'templates_id';
$param['caption'] = $GLOBALS['MSG_documents']['documents_add_form_templates'];
$param['type'] = 2; // Список.

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

if ($all_templates)
{
	foreach ($all_templates as $template_row)
	{
		$param['items'][to_int($template_row['templates_id'])] = to_str($template_row['templates_name']);
	}
}
$param['value'] = $template_id;
$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'] = 'documents_status_id';
$param['caption'] = $GLOBALS['MSG_documents']['documents_add_form_status'];
$param['type'] = 2; // Список.

if (to_int($document_row['documents_id']))
{
	$param['value'] = to_int($document_row['documents_status_id']);
}

$param['items'] = array();
$param['items'][0] = '&hellip;';
$resource = $documents->select_status(false);
if ($resource && mysql_num_rows($resource) > 0)
{
	while ($status_row = mysql_fetch_assoc($resource))
	{
		$param['items'][to_int($status_row['documents_status_id'])] = to_str($status_row['documents_status_name']);
	}
}
$param['attributes']['style'] = 'width: 320px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

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

// При добавлении документа версия текущая
if (!isset($version_row['documents_version_current']))
{
	$version_row['documents_version_current'] = 1;
}

// Является ли версия текущей.
$param = array();
$param['tab_id'] = $tab_id;
$param['type'] = 1; // Флажок.
$param['name'] = 'documents_version_current';
$param['html_id'] = 'id_documents_version_current';
$param['value'] = to_int($version_row['documents_version_current']);
$param['caption'] = $GLOBALS['MSG_documents']['documents_add_form_current'];
$admin_forms_fields->AddField($param);

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

// Описание документа.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'documents_version_comment';
$param['caption'] = $GLOBALS['MSG_documents']['documents_add_form_comment'];
$param['type'] = 5; // Textarea.
$param['attributes'] = array();
$param['attributes']['rows'] = 5;
$param['value'] = to_str($version_row['documents_version_comment']);
$admin_forms_fields->AddField($param);

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

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

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

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