<?php
/**
 * Documents.
 *
 * @package HostCMS
 * @version 6.x
 * @author Hostmake LLC
 * @copyright © 2005-2013 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 */
require_once('../../../bootstrap.php');

Core_Auth::authorization('document');

// Код формы
$iAdmin_Form_Id = 115;
$sAdminFormAction = '/admin/document/version/index.php';

$oAdmin_Form = Core_Entity::factory('Admin_Form', $iAdmin_Form_Id);

$document_id = intval(Core_Array::getGet('document_id', 0));

$oDocument = Core_Entity::factory('Document', $document_id);

// Контроллер формы
$oAdmin_Form_Controller = new Admin_Form_Controller($oAdmin_Form);
$oAdmin_Form_Controller
	->setUp()
	->path($sAdminFormAction)
	->title(Core::_('Document_Version.title', $oDocument->name))
	->pageTitle(Core::_('Document_Version.title', $oDocument->name));

// Меню формы
$oAdmin_Form_Entity_Menus = new Admin_Form_Entity_Menus();

$sDocumentPath = '/admin/document/index.php';

// Элементы меню
$oAdmin_Form_Entity_Menus->add(
	Core::factory('Admin_Form_Entity_Menu')
		->name(Core::_('Document_Version.menu'))
		->add(
			Core::factory('Admin_Form_Entity_Menu')
				->name(Core::_('Document_Version.add'))
				->img('/admin/images/page_add.gif')
				->href(
					$oAdmin_Form_Controller->getAdminActionLoadHref($oAdmin_Form_Controller->getPath(), 'edit', NULL, 0, 0)
				)
				->onclick(
					$oAdmin_Form_Controller->getAdminActionLoadAjax($oAdmin_Form_Controller->getPath(), 'edit', NULL, 0, 0)
				)
		)->add(
			Core::factory('Admin_Form_Entity_Menu')
				->name(Core::_('Document_Version.deleteAllOldVersion'))
				->img('/admin/images/page_delete.gif')
				->href(
					$oAdmin_Form_Controller->getAdminActionLoadHref($oAdmin_Form_Controller->getPath(), 'deleteOldDocumentVersions', NULL, 0, 0)
				)
				->onclick(
					$oAdmin_Form_Controller->getAdminActionLoadAjax($oAdmin_Form_Controller->getPath(), 'deleteOldDocumentVersions', NULL, 0, 0)
				)
		)
);

// Добавляем все меню контроллеру
$oAdmin_Form_Controller->addEntity($oAdmin_Form_Entity_Menus);

// Элементы строки навигации
$oAdmin_Form_Entity_Breadcrumbs = new Admin_Form_Entity_Breadcrumbs();

// Строка навигации

// Элементы строки навигации
$oAdmin_Form_Entity_Breadcrumbs->add(
	Core::factory('Admin_Form_Entity_Breadcrumb')
		->name(Core::_('Document.title'))
		->href(
			$oAdmin_Form_Controller->getAdminLoadHref($sDocumentPath, NULL, NULL, '')
		)
		->onclick(
			$oAdmin_Form_Controller->getAdminLoadAjax($sDocumentPath, NULL, NULL, '')
	)
);

$document_dir_id = $oDocument->document_dir_id;
if ($document_dir_id)
{
	// Если передана родительская группа - строим хлебные крошки
	$oDocument_Dir = Core_Entity::factory('Document_Dir')->find($document_dir_id);

	if (!is_null($oDocument_Dir->id))
	{
		$aBreadcrumbs = array();

		do
		{
			$additionalParams = 'document_dir_id=' . intval($oDocument_Dir->id);

			$aBreadcrumbs[] = Core::factory('Admin_Form_Entity_Breadcrumb')
				->name($oDocument_Dir->name)
				->href(
					$oAdmin_Form_Controller->getAdminLoadHref($sDocumentPath, NULL, NULL, $additionalParams)
				)
				->onclick(
					$oAdmin_Form_Controller->getAdminLoadAjax($sDocumentPath, NULL, NULL, $additionalParams)
				);
		} while($oDocument_Dir = $oDocument_Dir->getParent());

		$aBreadcrumbs = array_reverse($aBreadcrumbs);

		foreach ($aBreadcrumbs as $oAdmin_Form_Entity_Breadcrumb)
		{
			$oAdmin_Form_Entity_Breadcrumbs->add(
				$oAdmin_Form_Entity_Breadcrumb
			);
		}
	}
}

$oAdmin_Form_Entity_Breadcrumbs->add(
	Core::factory('Admin_Form_Entity_Breadcrumb')
		->name(Core::_('Document_Version.title', $oDocument->name))
		->href(
			$oAdmin_Form_Controller->getAdminLoadHref($oAdmin_Form_Controller->getPath())
		)
		->onclick(
			$oAdmin_Form_Controller->getAdminLoadAjax($oAdmin_Form_Controller->getPath())
	)
);

// Добавляем все хлебные крошки контроллеру
$oAdmin_Form_Controller->addEntity($oAdmin_Form_Entity_Breadcrumbs);

// Действие редактирования
$oAdmin_Form_Action = Core_Entity::factory('Admin_Form', $iAdmin_Form_Id)
	->Admin_Form_Actions
	->getByName('edit');

if ($oAdmin_Form_Action && $oAdmin_Form_Controller->getAction() == 'edit')
{
	$oDocument_Controller_Edit = new Document_Version_Controller_Edit(
		$oAdmin_Form_Action
	);

	$oDocument_Controller_Edit
		->addEntity($oAdmin_Form_Entity_Breadcrumbs);

	// Добавляем типовой контроллер редактирования контроллеру формы
	$oAdmin_Form_Controller->addAction($oDocument_Controller_Edit);
}

// Действие "Удалить нетекущие версии документа"
$oAdmin_Form_Action = Core_Entity::factory('Admin_Form', $iAdmin_Form_Id)
	->Admin_Form_Actions
	->getByName('deleteOldDocumentVersions');

if ($oAdmin_Form_Action && $oAdmin_Form_Controller->getAction() == 'deleteOldDocumentVersions')
{
	$oDocument_Controller_deleteOldDocumentVersions = new Document_Version_Controller_Document_Oldversions(
		$oAdmin_Form_Action
	);

	$oDocument_Controller_deleteOldDocumentVersions
		->document_id(Core_Array::getGet('document_id'));

	// Добавляем типовой контроллер редактирования контроллеру формы
	$oAdmin_Form_Controller->addAction($oDocument_Controller_deleteOldDocumentVersions);
}

// Действие "Применить"
$oAdminFormActionApply = Core_Entity::factory('Admin_Form', $iAdmin_Form_Id)
	->Admin_Form_Actions
	->getByName('apply');

if ($oAdminFormActionApply && $oAdmin_Form_Controller->getAction() == 'apply')
{
	$oControllerApply = new Admin_Form_Action_Controller_Type_Apply
	(
		$oAdminFormActionApply
	);

	// Добавляем типовой контроллер редактирования контроллеру формы
	$oAdmin_Form_Controller->addAction($oControllerApply);
}

// Действие "Копировать"
$oAdminFormActionCopy = Core_Entity::factory('Admin_Form', $iAdmin_Form_Id)
	->Admin_Form_Actions
	->getByName('copy');

if ($oAdminFormActionCopy && $oAdmin_Form_Controller->getAction() == 'copy')
{
	$oControllerCopy = new Admin_Form_Action_Controller_Type_Copy(
		$oAdminFormActionCopy
	);

	// Добавляем типовой контроллер редактирования контроллеру формы
	$oAdmin_Form_Controller->addAction($oControllerCopy);
}

// Источник данных 0
$oAdmin_Form_Dataset = new Admin_Form_Dataset_Entity(
	Core_Entity::factory('Document_Version')
);

// Ограничение источника 0 по родительской группе
$oAdmin_Form_Dataset->addCondition(
		array('select' => array('document_versions.*', array('users.login', 'user_name')))
)->addCondition(
	array('leftJoin' => array('users', 'users.id', '=', 'document_versions.user_id'))
)
->addCondition(
	array('where' =>
		array('document_id', '=', $document_id)
	)
);

// Добавляем источник данных контроллеру формы
$oAdmin_Form_Controller->addDataset(
	$oAdmin_Form_Dataset
);

// Показ формы
$oAdmin_Form_Controller->execute();
