<?php
class Recieved_GetController extends Library_Controller {
	public function __call($methodName, $args) {
		//priprava operace
		$response = new Library_Response();
		$objectInfo = $this->_parseActionName($methodName);
		$tableRecieved = new Application_Model_MessagesRecieved();
		
		//nacitani a kontrola dat
		try {
			//nacteni zpravy
			$recieved = $tableRecieved->find($objectInfo["id"])->current();
			
			if (!$recieved) {
				//zprava nebyla nalezena
				$response->setStatus("MESSAGE_NOT_FOUND", 404);
				throw new Zend_Exception("Sended message id " . $objectInfo["id"] . " not found");
			}
			
			//kontorla vlastnika
			if ($recieved->user_id != $this->_user->id) {
				//zprava neni vlastni
				$response->setStatus("MESSAGE_IS_NOT_OWN", 403);
				throw new Zend_Exception("User id " . $this->_user->id . " is not owner of sended message id " . $sended->id);
			}
		} catch (Zend_Exception $e) {
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		//nastaveni odpovedi
		$response["recieved"] = $recieved;
		$response->setOk();
		
		$this->_sendToRender($response, $objectInfo["format"]);
	}
	
	public function indexAction() {
		$this->_forward("list.json");
	}
	
	public function listAction() {
		//presmerovani na format Json
		$this->_forward("list.json");
	}
	
	public function listHtmlAction() {
		//vytvoreni odpovedi
		$response = new Library_Response();
		$response->setOk();
		$response["recieved"] = $this->_list();
		
		$this->view->response = $response;
	}
	
	public function listJsonAction() {
		//vytvoreni odpovedi
		$response = new Library_Response();
		$response->setOk();
		$response["recieved"] = $this->_list();
		
		$this->view->response = $response;
	}
	
	protected function _list() {
		//vytvoreni zakladniho selectu
		$tableRecieved = new Application_Model_MessagesRecieved();
		
		$select = $tableRecieved->select(false)->where("user_id = ?", $this->_user->id);
		
		//kontorla jestli je odeslan parametr $without
		if (isset($_REQUEST["without"])) {
			//parametr byl odeslan
			$without = $_REQUEST["without"];
			
			//pokud parametr neni pole, zmeni se na nej
			if (!is_array($without)) {
				//parametr neni pole
				$without = array($without);
			}
			
			//vyhodnoceni omezeni
			foreach ($without as $param) {
				switch (strtolower($param)) {
					case "readed":
						$select->where("is_readed = 0");
						break;
						
					case "unreaded":
						$select->where("is_readed <> 0");
						break;
				}
			}
		}
		
		//kontrola jeslti jsou odslany strankovaci parametry
		if (isset($_REQUEST["itemsPerPage"], $_REQUEST["page"])) {
			//strankovani je nastaveno
			$select->limitPage($_REQUEST["page"], $_REQUEST["limitPerPage"]);
		}
		
		//kontrola odeslani razeni
		if (isset($_REQUEST["order"])) {
			//razeni je nastaveno
			$select->order($_REQUEST["order"]);
		} else {
			//razeni neni nastaveno
			$select->order("id desc");
		}
		
		//nastaveni nacitanych hodnot
		$select->from($tableRecieved, array("id", "user_id", "from_user_id", "subject", "recieved_at", "is_readed"));
		
		//nacteni seznamu vlastnich zprav
		return $tableRecieved->fetchAll($select);
	}
	
	protected function _sendToRender(Library_Response $response, $format) {
		//zapsani do view
		$this->view->response = $response;
		$response->sendHeader();
		
		if ($format != "html") {
			$this->_helper->layout()->disableLayout();
		}
		
		//vyhodnoceni formatu
		switch ($format) {
			case "html":
				$this->_helper->viewRenderer("get-html");
				break;
			
			case "json":
			
			default:
				$this->_helper->viewRenderer("get-json");
		}
	}
}