<?php
class Note_GetController extends Library_Controller {
	public function __call($methodName, $args) {
		//priprava dat
		$objectInfo = $this->_parseActionName($methodName);
		
		//vyhodnoceni typu identifikatoru
		if (strlen($objectInfo["id"]) == 40 || substr($objectInfo["id"], 0, 1) == "d") {
			//jedna se o dotaz na vypis
			$this->_list($objectInfo);
			return;
		}
		
		//priprava tabulky a odpovedi
		$tableNotes = new Application_Model_Notes();
		$response = new Library_Response();

		//nacteni dat
		try {
			$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");
			}
		} catch (Zend_Exception $e) {
			$this->_sendToRender($response, $objectInfo["format"], "get");
			return;
		}
		
		//nastaveni odpovedi
		$response->setOk();
		$response["note"] = $note;
		
		//odeslani odpovedi
		$this->_sendToRender($response, $objectInfo["format"], "get");
	}
	
	protected function _list($objectInfo) {
		//vytvoreni instanci
		$response = new Library_Response();
		$tableNotes = new Application_Model_Notes();
		
		//vyhodnoceni typu objektu a nacteni pozadovanych dat
		$select = $tableNotes->select()->order("created_at desc");
		
		if (strlen($objectInfo["id"]) == 40) {
			//jedna se o identifikator dokumentu
			$select->where("document_uuid like ?", $objectInfo["id"]);
		} else {
			//jedna se o identifikator adresare - musi se preskocit prvni znak
			$select->where("directory_id = ?", substr($objectInfo["id"], 1));
		}
		
		//nacteni polozek a zapsani vysledku
		$notes = $tableNotes->fetchAll($select);
		$response["note"] = $notes;
		$response->setOk();
		
		$this->_sendToRender($response, $objectInfo["format"], "list");
	}
}