<?php
class Note_DeleteController extends Library_Controller {
	public function __call($methodName, $args) {
		//priprava objektu a dat
		$tableNotes = new Application_Model_Notes();
		$response = new Library_Response();
		$objectInfo = $this->_parseActionName($methodName);
		
		//kontrola dat
		try {
			//nacteni dat z databaze
			$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");
			}
			
			//konotrla opravneni poznamku smazat
			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_DELETE", 403);
				throw new Zend_Exception("User id " . $this->_user->id . " is not permited to delete note id " . $note->id);
			}
		} catch (Zend_Exception $e) {
			$this->_sendToRender($response, $objectInfo["format"], "delete");
			return;
		}
		
		//smazani poznamky
		$note->delete();
		
		//nastaveni a odeslani odpovedi
		$response->setOk();
		$this->_sendToRender($response, $objectInfo["format"], "delete");
	}
}