<?php
class Documentation_GetController extends Library_Controller {
	public function __call($methodName, $args) {
		//priprava dat
		$objectInfo = $this->_parseActionName($methodName);

		//vyhodnoceni typu pozadavku
		if (strlen($objectInfo["id"]) == 40 || $objectInfo["id"][0] == "d") {
			//pozadavek na vypis adresare nebo dokumentu
			$this->_list($objectInfo);
			return;
		}
		
		//dokonceni pripravy
		$response = new Library_Response();
		$tableDoc = new Application_Model_Documentations();

		//nacteni a kontrola dat
		try {
			$doc = $tableDoc->find($objectInfo["id"])->current();
			
			if (!$doc) {
				//dokumentace nebyla nalezena
				$response->setStatus("DOCUMENTATION_NOT_FOUND", 404);
				throw new Zend_Exception("Documentation id " . $objectInfo["id"] . " not found");
			}
		} catch (Zend_Exception $e) {
			$this->_sendToRender($response, $objectInfo["format"], "get");
			return;
		}
		
		//vraceni odpovedi
		$response->setOk();
		$response["documentation"] = $doc;
		
		$this->_sendToRender($response, $objectInfo["format"], "get");
	}

	protected function _list($objectInfo) {
		//vytvoreni tabulky a odpovedi
		$response = new Library_Response();
		$tableDoc = new Application_Model_Documentations();
		
		//nacteni seznamu
		if (strlen($objectInfo["id"]) == 40) {
			//zkoumany objekt je dokument
			
			//nacteni dokumentace k objektu
			$documentedBy = $tableDoc->fetchAll($tableDoc->select(false)
				->where("documented_document_uuid like ?", $objectInfo["id"])
				->order("id"));
			
			//nacteni sokumentovanych objektu
			$documentationFor = $tableDoc->fetchAll($tableDoc->select(false)
				->where("documentation_document_uuid like ?", $objectInfo["id"])
				->order("id"));
			
			$this->view->type = "document";
		} else {
			//zkoumany objekt je adresar
			
			//separace id
			$id = substr($objectInfo["id"], 1);
			
			//nacteni dokumentace
			$documentedBy = $tableDoc->fetchAll($tableDoc->select(false)
				->where("documented_directory_id = ?", $id)
				->order("id"));
			
			//vytvopreni prazdne instance dokumentovanych objektu
			$documentationFor = $tableDoc->find(0);
			
			$this->view->type = "directory";
		}
		
		$response->setOk();
		$response["documented_by"] = $documentedBy;
		$response["documentationFor"] = $documentationFor;
		
		$this->_sendToRender($response, $objectInfo["format"], "list");
	}
}