<?php
class Plan_GetController extends Library_Controller {
	public function __call($methodName, $args) {
		//priprava dat
		$objectInfo = $this->_parseActionName($methodName);
		
		//kontrola pozadavku na vypis
		if (empty($objectInfo["format"])) {
			//format je ulozen v identifikacnim, cisle, je to pozadavek na vypis
			$objectInfo["format"] = $objectInfo["id"];
			$this->_list($objectInfo);
			return;
		}
		
		//priprava dat
		$response = new Library_Response();
		$tableTasks = new Application_Model_PlansTasks();
		
		try {
			$task = $tableTasks->find($objectInfo["id"])->current();
			
			if (!$task) {
				//ukol nebyl nalezen
				$response->setStatus("TASK_NOT_FOUND", 404);
				throw new Zend_Exception("Task id " . $objectInfo["id"]);
			}
		} catch (Zend_Exception $e) {
			$this->_sendToRender($response, $objectInfo["format"], "get");
			return;
		}
		
		//nastaveni odpovedi
		$response->setOk();
		$response["task"] = $task;
		
		$this->_sendToRender($response, $objectInfo["format"], "get");
	}
	
	protected function _list($objectInfo) {
		//priprava dat
		$response = new Library_Response();
		$tableTasks = new Application_Model_PlansTasks();
		
		//nacteni dat prijatych ukolu
		$recieved = $tableTasks->fetchAll("for_user_id = " . $this->_user->id);
		
		//nacteni dat vytvorenych ukolu
		$sent = $tableTasks->fetchAll("user_id = " . $this->_user->id);
		
		//zapsani do odpovedi a odeslani na render
		$response->setOk();
		$response["sent"] = $sent;
		$response["recieved"] = $recieved;
		
		$this->_sendToRender($response, $objectInfo["format"], "list");
	}
}