<?php
class Documentation_PutController extends Library_Controller {
	public function __call($methodName, $args) {
		//priprava dat
		$response = new Library_Response();
		$objectInfo = $this->_parseActionName($methodName);
		$tableDoc = new Application_Model_Documentations();
		
		//kontorla dat
		try {
			//kontorla odeslanych dat
			if (!isset($_REQUEST["documentation"])) {
				//data nebyla odeslana
				$response->setStatus("DATA_NOT_SEND", 400);
				throw new Zend_Exception("Data not sent to server");
			}
			
			$data = $_REQUEST["documentation"];
			
			if (!isset($data["comment"])) {
				//data nesjou kompletni
				$response->setStatus("DATA_NOT_COMPLETE", 400);
				return;
			}
			
			//konotrla existence objektu
			$doc = $tableDoc->find($objectInfo["id"])->current();
			
			if (!$doc) {
				//objekt nebyl nalezen
				$response->setStatus("DOCUMENTATION_NOT_FOUND", 404);
				throw new Zend_Exception("Documentation id " . $objectInfo["id"] . " not found");
			}
			
			//kontorla opravneni
			if ($doc->user_id != $this->_user->id && $this->user_id != 1 && !in_array("DOCUMENTATION_MANAGER", $this->_user->getRoles())) {
				//uzivatel neni opravnen menit dokuemntaci
				$response->setStatus("NO_PERSION_TO_CHANGE_DOCUMENTATION", 403);
				throw new Zend_Exception("User id " . $this->_user->id . " is not permited do change documentation id " . $doc->id);
			}
		} catch (Zend_Exception $e) {
			$this->_sendToRender($response, $objectInfo["format"], "put");
			return;
		}
		
		//uprava hodnot
		$doc->comment = $data["comment"];
		$doc->save();
		
		//zapis odpovedi
		$response->setOk();
		$response["documentaion"] = $doc;
		
		$this->_sendToRender($response, $objectInfo["format"], "put");
	}
}