<?php
class Note_PutController extends Library_Controller {
	public function __call($methodName, $args) {
		//priprava dat
		$tableNotes = new Application_Model_Notes();
		$response = new Library_Response();
		$objectInfo = $this->_parseActionName($methodName);
		
		//nacitani a kontrola dat
		try {
			//kontrola odeslanych dat
			if (!isset($_REQUEST["note"]["note"])) {
				//data nebyla odeslana
				$response->setStatus("DATA_NOT_SEND", 400);
				throw new Zend_Exception("No sent data for update of note");
			}
			
			//nacteni dat
			$note = $tableNotes->find($objectInfo["id"])->current();
			
			if (!$note) {
				//poznamka nebyla nalezena
				$response->setStatus("NOTE_NOT_FOUND", 404);
				throw new Zend_Exception("Note id " . $objectInfo["id"] . " not found");
			}
			
			//konotrola opravneni
			if ($note->user_id != $this->_user->id && !in_array("NOTE_MANAGER", $this->_user->getRoles()) && $this->_user->id != 1) {
				//uzivatel neni opravnen smazat poznamku
				$response->setStatus("NOT_PERMITED_TO_UPDATE", 403);
				throw new Zend_Exception("User id " . $this->_user->id . " is not permited to update note id " . $note->id);
			}
		} catch (Zend_Exception $e) {
			$this->_sendToRender($response, $objectInfo["format"], "put");
			return;
		}
		
		//update dat
		$note->note = $_REQUEST["note"]["note"];
		$note->save();
		
		//zapsani odpovedi
		$response->setOk();
		$response["note"] = $note;
		
		$this->_sendToRender($response, $objectInfo["format"], "put");
	}
}