<?php
class Plan_DeleteController extends Library_Controller {
	public function __call($methodName, $args) {
		//priprava dat
		$objectInfo = $this->_parseActionName($methodName);
		$response = new Library_Response();
		$tableTasks = new Application_Model_PlansTasks();
		
		//kontorla dat
		try {
			//kontrola existence ukolu
			$task = $tableTasks->find($objectInfo["id"])->current();
			
			if (!$task) {
				//ukol neexistuje
				$response->setStatus("TASK_NOT_FOUND", 404);
				throw new Zend_Exception("Task id " . $objectInfo["id"] . " not found");
			}
			
			//kontorla opravneni
			$userId = $this->_user->id;
			
			if ($task->user_id != $userId && $userId != 1 && !in_array("PLAN_MANAGER", $this->_user->id)) {
				//uzivatel neni opravnen smazat ukol
				$response->setStatus("NOT_PERMITED", 403);
				throw new Zend_Exception("User id " . $userId . " is not permited to delete task id " . $task->id);
			}
		} catch (Zend_Exception $e) {
			$this->_sendToRender($response, $objectInfo["format"], "delete");
			return;
		}
		
		//smazani asociovane historie
		$tableHistory = new Application_Model_PlansTasksHistory();
		$tableHistory->delete("task_id = " . $task->id);
		
		//smazani ukolu
		$task->delete();
		
		//nastaveni a zapsani odpovedi
		$response->setOk();
		$this->_sendToRender($response, $objectInfo["format"], "delete");
	}
}