<?php
class Document_GetController extends Library_Controller {
	public function __call($methodName, $args) {
		//inicializace objektu
		$objectInfo = $this->_parseActionName($methodName);
		
		//kontorla formatu
		if (empty($objectInfo["format"])) {
			$this->_download($objectInfo);
			return;
		}
		
		$response = new Library_Response();

		//nacteni dokumentu
		$tableDocuments = new Application_Model_Documents();

		//kontrola jeslti se dokument nacita pomoci zkratky
		if (strlen($objectInfo["id"]) == 7) {
			$tableShortcuts = new Application_Model_DocumentsShortcuts();

			$shortcut = $tableShortcuts->fetchRow($tableShortcuts->select(false)
			->where("shortcut like ?", $_REQUEST["shortcut"]));

			if ($shortcut) {
				$document = $shortcut->findParentRow("Application_Model_Documents", "document", $select);
			} else {
				$document = false;
			}
		} else {
			$document = $tableDocuments->find($objectInfo["id"])->current();
		}

		if (!$document) {
			$response->setStatus("DOCUMENT_NOT_FOUND", 404);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}

		//nacteni opravneni
		$user = $this->_user;
		
		$groupList = $user->findDependentRowset("Application_Model_UsersHasManyUsersGroups", "user");
		$groups = array();

		foreach ($groupList as $group) {
			$groups[] = $group->user_group_id;
		}
		
		$permisions = $document->getPermisions($user->id, $groups);

		//nacteni zaznamu z historie
		$tableHistory = new Application_Model_DocumentsHistory();
		
		if ($document->is_latest) {
			//dokument je aktualni verzi, nacte se historie
			$master = $document;
		} else {
			//dokument neni aktualni, nalezne se master document
			$historyRow = $tableHistory->fetchRow($tableHistory->select(false)->where("document_old_uuid like ?", $document->uuid));
			$master = $historyRow->findParentRow($tableDocuments, "last");
		}
		
		$history = $master->findDependentRowset($tableHistory, "last", $tableHistory->select(false)->order("created_at"));

		//nacteni adresaru ve kterych je dokument ulozen
		$directories = $master->findManyToManyRowset("Application_Model_DocumentsDirectories", "Application_Model_DocumentsDirectoriesHasManyDocuments", "document", "directory");
		
		//zapsani odpovedi
		$response->setOk();
		$response["document"] = $document;
		$response["master"] = $master;
		$response["history"] = $history;
		$response["directories"] = $directories;
		
		Zend_Registry::set("_user", $this->_user);
		Zend_Registry::set("_groups", $groups);

		$this->_sendToRender($response, $objectInfo["format"]);
	}
	
	protected function _download($objectInfo) {
		//nacteni dokumentu
		$tableDocuments = new Application_Model_Documents();

		//kontrola jeslti se dokument nacita pomoci zkratky
		if (strlen($objectInfo["id"]) == 7) {
			$tableShortcuts = new Application_Model_DocumentsShortcuts();

			$shortcut = $tableShortcuts->fetchRow($tableShortcuts->select(false)
			->where("shortcut like ?", $_REQUEST["shortcut"]));

			if ($shortcut) {
				$document = $shortcut->findParentRow("Application_Model_Documents", "document", $select);
			} else {
				$document = false;
			}
		} else {
			$document = $tableDocuments->find($objectInfo["id"])->current();
		}

		if (!$document) {
			$response->setStatus("DOCUMENT_NOT_FOUND", 404);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}

		//nacteni opravneni
		$user = $this->_user;
		
		$groupList = $user->findDependentRowset("Application_Model_UsersHasManyUsersGroups", "user");
		$groups = array();

		foreach ($groupList as $group) {
			$groups[] = $group->user_group_id;
		}
		
		$permisions = $document->getPermisions($user->id, $groups);
		
		if (!$permisions->read) {
		    $response = new Library_Response();
		    $response->setStatus("NO_PERMISION_TO_READ", 403);
		    $this->_sendToRender($response, $objectInfo["format"]);
		    return;
		}
		
		//nacteni obsahu dokumentu
		$contentFileName = CONTENT_PATH . "/" .$document->uuid;
		
		//odeslani hlavicek
		header("Content-Type: ".$document->mime_type);
		header("Content-Length: ".  filesize($contentFileName));
		header("Content-Transfer-Encoding: binary");
		header("Content-Disposition: attachment; filename=" . urlencode($document->document_name));
		
		print @readfile($contentFileName);
		exit();
	}

	protected function _sendToRender(Library_Response $response, $format) {
		//zapsani do view
		$this->view->response = $response;
		$response->sendHeader();

		if ($format != "html") {
			$this->_helper->layout()->disableLayout();
		}

		//vyhodnoceni formatu
		switch ($format) {
			case "html":
				$this->_helper->viewRenderer("get-html");
				break;
					
			case "json":
					
			default:
				$this->_helper->viewRenderer("get-json");
		}
	}
}