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

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

$form_params = array();
$form_params['title'] = ($document_version_id == 0)
? $GLOBALS['MSG_documents']['documents_version_add_form_title']
: $GLOBALS['MSG_documents']['documents_version_edit_form_title'];

$document_info = $documents->GetDocument($document_id);

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

$admin_forms_fields = new admin_forms_fields($form_params);

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

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

if (count($path_array) > 0)
{
	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);
	}
}

$new_path_array[] = array (
'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax($admin_forms_fields->AAction, "&document_id=$document_id&documents_dir_parent_id=$documents_dir_parent_id", 115, "load_data"),

'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax($admin_forms_fields->AAction, "&document_id=$document_id&documents_dir_parent_id=$documents_dir_parent_id", 115, "load_data"),

'name' =>$GLOBALS['MSG_documents']['documents_version_link'].' "'.htmlspecialchars($document_info['documents_name']).'"');

$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

$document_row = false;

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

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

$version_row = false;

// Редактирование версии документа
if ($document_version_id)
{
	// Извлекаем информацию о текущей версии документа.
	$resource = $documents->select_version($document_id,
	$document_version_id == 0 ? - 1 : $document_version_id,
	$document_version_id == 0 ? 1 : 0 );

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

	if ($document_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 = '';
}

// Закладка с названием/описанием.
$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; // Поле ввода.
$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'];
$param['type'] = 7; // Визуальный редактор.
$param['value'] = $documents_text;
$param['template'] = to_int($version_row['templates_id']);
$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;
}

$items = array();
$items[0] = '&hellip;';
$tree = $documents->GetDocumentsDirTree(0, "&nbsp;&nbsp;");
if ($tree)
{
	foreach ($tree as $key => $value)
	{
		$items[$value[0]] = $value[1];
	}
}
$param['items'] = $items;
$admin_forms_fields->AddField($param);

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

// Статус документа.
$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']);
}

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

$resource = $documents->select_status(-1);

if ($resource && mysql_num_rows($resource) > 0)
{
	while ($status_row = mysql_fetch_assoc($resource))
	{
		$items[to_int($status_row['documents_status_id'])] = to_str($status_row['documents_status_name']);
	}
}
$param['items'] = $items;

$admin_forms_fields->AddField($param);

// Макет.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'templates_id';
$param['caption'] = $GLOBALS['MSG_documents']['documents_add_form_templates'];
$param['type'] = 2; // Список.

if (to_int($version_row['documents_version_id']))
{
	$param['value'] = to_int($version_row['templates_id']);
}

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

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

if ($all_templates)
{
	foreach ($all_templates as $template_row)
	{
		$items[to_int($template_row['templates_id'])] = to_str($template_row['templates_name']);
	}
}

$param['items'] = $items;

$admin_forms_fields->AddField($param);

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

// Является ли версия текущей.
$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'] = 10;
$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_version';
$param['caption'] = $GLOBALS['MSG_documents']['save'];
// $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_document_version';
$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();
?>