<?php
class Directory_GetController extends Library_Controller {
	/**
	 * pretizena funkce kontrolujici pristup k adresarum
	 * @see Zend_Controller_Action::__call()
	 */
	public function __call($methodName, $args) {
		//inicializace objektu
		$objectInfo = $this->_parseActionName($methodName);
		$response = new Library_Response();
		
		//vytvoreni instance tabulky adresaru a nacteni adresare
		$tableDirectories = new Application_Model_DocumentsDirectories();
		
		/**
		 * @var Application_Model_Row_Directory $directory
		 */
		$directory = $tableDirectories->find($objectInfo["id"])->current();
		
		//kontrola existence adresare
		if (!$directory) {
		    $response->setStatus("DIRECTORY_NOT_FOUND", 404);
		    $this->_sendToRender($response, $objectInfo["format"]);
		    return;
		}
		
		//kontrola opravneni pristupu
		$user = $this->_user;
		
		//nacteni skupin
		$groupList = $user->findDependentRowset("Application_Model_UsersHasManyUsersGroups", "user");
		$groups = array();
		
		foreach ($groupList as $group) {
			$groups[] = $group->user_group_id;
		}
		
		$permisions = $directory->getPermisions($user->id, $groups);
		
		//zapsani seznamu skupin do registru pro pripad potreby
		Zend_Registry::set("groups", $groups);
		
		//pokud nema uzivatel opravneni cist, pripoji se chybove hlaseni
		if (!$permisions->read) {
		    $response->setStatus("NO_READ_PERMISIONS", 403);
		    $this->_sendToRender($response, $objectInfo["format"]);
		    return;
		}
		
		//nacteni obsahu adresare
		
		//podrizene adresare
		$subDirs = $directory->findDependentRowset($tableDirectories, "parent", $tableDirectories->select(false)
		        ->order("directory_name"));
		
		//podrizene soubory
		$tableDocuments = new Application_Model_Documents();
		$tableDocumentsDirectories = new Application_Model_DocumentsDirectoriesHasManyDocuments;
		
		$files = $directory->findManyToManyRowset($tableDocuments, $tableDocumentsDirectories, null, null, $tableDocuments->select(false)
			->order("document_name"));
		
		//nacteni lokalnich jmen podrizenych souboru
		$localNameList = $directory->findDependentRowset($tableDocumentsDirectories, "directory");
		$localNames = array();
		
		foreach ($localNameList as $localName) {
			$localNames[$localName->document_uuid] = $localName->document_name;
		}
		
		//nacteni primeho rodice
		$directParent = $directory->findParentRow($tableDirectories);
		
		if (!$directParent)
		    $directParent = $directory;
		
		//nacteni plne cesty
		$path = array();
		
		$thisParent = $directParent;
		
		if ($thisParent->id != $directory->id)
		    $path[] = $thisParent;
		
		while ($thisParent->depth) {
		    $thisParent = $thisParent->findParentRow("Application_Model_DocumentsDirectories");
		
		    $path[] = $thisParent;
		}
		
		$path = array_reverse($path);
		
		//zapsani informaci do smarty
		$response["directory"] = $directory;
		$response["subdirs"] = $subDirs;
		$response["documents"] = $files;
		$response["documentsNames"] = $localNames;
		$response["permisions"] = $permisions;
		$response["directParent"] = $directParent;
		$response["path"] = $path;
		
		$response->setOk();
		$this->_sendToRender($response, $objectInfo["format"]);
	}
	
	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");
		}
	}
}
?>