<?php
class Note_PostController extends Library_Controller {
	public function __call($methodName, $args) {
		//priprava dat
		$objectInfo = $this->_parseActionName($methodName);
		$response = new Library_Response();
		$tableNotes = new Application_Model_Notes();
		
		//vytvoreni tabulky ciloveho objketu
		if (strlen($objectInfo["id"]) == 40) {
			//objekt je dokument
			$isDocument = true;
			$tableObject = new Application_Model_Documents();
		} else {
			//objekt je adresar
			$isDocument = false;
			$tableObject = new Application_Model_DocumentsDirectories();
		}
		
		//kontrola dat
		try {
			//kontrla odeslanych dat
			if (!isset($_REQUEST["note"]["note"])) {
				//data poznamky nebyla odeslana
				$response->setStatus("DATA_NOT_SEND", 403);
				throw new Zend_Exception("Data sent to server is not complete");
			}
			
			//kontorla exitence objektu
			$object = $tableObject->find($objectInfo["id"])->current();
			
			if (!$object) {
				//objekt nebyl nalezen
				$response->setStatus("OBJECT_NOT_FOUND", 404);
				throw new Zend_Exception("Object id " . $objectInfo["id"] . " not found");
			}
			
		} catch (Zend_Exception $e) {
			$this->_sendToRender($response, $objectInfo["format"], "post");
			return;
		}
		
		//data jsou v poradku, pripravi se radek
		$data = array(
			"user_id" => $this->_user->id
			, "note" => $_REQUEST["note"]["note"]
		);
		
		//vyhodnoceni typu
		if ($isDocument) {
			//objekt je dokument
			$data["document_uuid"] = $objectInfo["id"];
		} else {
			//objekt je adresar
			$data["directory_id"] = $objectInfo["id"];
		};
		
		//zapsani dat
		$row = $tableNotes->createRow($data);
		$row->save();
		
		//zapsani odpovedi
		$response->setOk();
		$response["note"] = $row;
		
		$this->_sendToRender($response, $objectInfo["format"], "post");
	}
}
?>