<?php
class Documentation_PostController extends Library_Controller {
	public function __call($methodName, $args) {
		//priprava odpovedi
		$response = new Library_Response();
		$objectInfo = $this->_parseActionName($methodName);
		
		$tableDoc = new Application_Model_Documentations();
		$tableDocuments = new Application_Model_Documents();
		
		//kontorla dat
		try {
			//kontorla jestli uzivatel neni guest
			if ($this->_user->id == 2) {
				$response->setStatus("NOT_PERMITED", 403);
				throw new Zend_Exception("Guest is not permited to create documentation");
			}
			
			//konotrla dat
			if (!isset($_REQUEST["documentation"])) {
				//data nebyla odeslana na server
				$response->setStatus("DATA_NOT_SEND", 400);
				throw new Zend_Exception("Data not send to server");
			}
			
			$data = $_REQUEST["documentation"];
			
			if (!isset($data["documentation_document_uuid"], $data["comment"])) {
				//data nejsou kompletni
				$response->setStatus("DATA_NOT_COMPLETE", 400);
				throw new Zend_Exception("No complete data sent to server");
			}
			
			//konotrla existence dokumentace a jeji validity
			$documentation = $tableDocuments->find($data["documentation_document_uuid"])->current();
			
			if (!$documentation) {
				//objekt dokumentace nebyl nalezen
				$response->setStatus("DOCUMENTATION_NOT_FOUND", 404);
				throw new Zend_Exception("Documentation document id " . $data["documentation_document_uuid"] . " not found");
			}
			
			//kontorla aktualnosti
			if (!$documentation->is_latest) {
				//dokumentace neni aktualni
				$response->setStatus("DOCUMENTATION_NOT_ACTUAL", 400);
				throw new Zend_Exception("Documentation document id " . $data["documentation_document_uuid"] . " not actual");
			}
			
			//kontorla ciloveho objektu
			$isDir = false;
			
			if (strlen($objectInfo["id"]) == 40) {
				//cilovy objekt je dokument
				$target = $tableDocuments->find($objectInfo["id"]);
				
				if (!$target) {
					//cilovy dokument nebyl nalezen
					$response->setStatus("TARGET_NOT_FOUND", 404);
					throw new Zend_Exception("Target document uuid " . $objectInfo["id"] . " not found");
				}
				
				if (!$target->is_latest) {
					//cilovy dokument neni aktualni
					$response->setStatus("TARGET_IS_NOT_ACTUAL", 400);
					throw new Zend_Exception("Target document uuid " . $objectInfo["id"] . " not actual");
				}
			} else {
				//cilovy objekt je adresar
				$tableDirectory = new Application_Model_DocumentsDirectories();
				$target = $tableDirectory->find($objectInfo["id"]);
				
				if (!$target) {
					//adresar neexistuje
					$response->setStatus("TARGET_NOT_FOUND", 404);
					throw new Zend_Exception("Target directory id " . $objectInfo["id"] . " not found");
				}
				
				$isDir = true;
			}
		} catch (Zend_Exception $e) {
			$this->_sendToRender($response, $objectInfo["format"], "post");
			return;
		}
		
		//preventivni odstraneni potencionalne nebezpecnych dat
		unset($data["created_at"], $data["documented_directory_id"], $data["documented_document_uuid"], $data["user_id"], $data["id"]);
		
		//vytvoreni dokumentace
		$row = $tableDoc->createRow($data);
		$row->user_id = $this->_user_id;
		
		//nastaveni identifikatoru dokumentovaneho objektu
		if ($isDir) {
			$row->documented_directory_id = $objectInfo["id"];
		} else {
			$row->documented_document_uuid = $objectInfo["id"];
		}
		
		//ulozeni a zapsani odpovedi
		$row->save();
		
		$response->setOk();
		$response["documentation"] = $row;
		
		$this->_sendToRender($response, $objectInfo["format"], "post");
	}
}