<?php
class Plan_PostController extends Library_Controller {
	public function __call($methodName, $args) {
		//priprava dat
		$response = new Library_Response();
		$objectInfo = $this->_parseActionName($methodName);
		$tableProjects = new Application_Model_Projects();
		$tableManagers = new Application_Model_PlansManagers();
		
		//kontorla dat
		try {
			//kontrola odeslanych dat
			if (!isset($_REQUEST["task"])) {
				//data nebyla odeslana
				$response->setStatus("DATA_NOT_SEND", 400);
				throw new Zend_Exception("Data not sent to server");
			}
			
			$data = $_REQUEST["task"];
			
			if (!isset($data["project_id"], $data["task_content"])) {
				//data nejsou kompletni
				$response->setStatus("DATA_NOT_COMPLETE");
				throw new Zend_Exception("Data sent to server is not complete");
			}
			
			//konotrla existence projektu
			$project = $tableProjects->find($data["project_id"])->current();
			
			if (!$project) {
				//projekt neexistuje
				$response->setStatus("PROJECT_NOT_FOUND", 404);
				throw new Zend_Exception("Project id " . $data["project_id"] . " not found");
			}
			
			//kontorla jestli je uzivatel opravnen zadavat ukoly
			if ($this->_user->id != 1 && !in_array("PLAN_MANAGER", $this->_user->getRoles())) {
				//uzivatel neni root, ani PLAN_MANAGER, zkontroluje se opravneni
				
				$user = $tableManagers->fetchRow(array("user_id = " . $this->_user->id, "project_id = " . $project->directory_id));
				
				if (!$user) {
					//uzivatel neni opravnen
					$response->setStatus("NOT_PERMITED", 403);
					throw new Zend_Exception("User id " . $this->_user->id . " is not permited to create tasks");
				}
			}
			
			//kontorla existence ciloveho uzivatele
			$targetUser = $this->_tableUsers->find($objectInfo["id"])->current();
			
			if (!$targetUser) {
				$response->setStatus("TARGET_NOT_FOUND", 404);
				throw new Zend_Exception("Target user id " . $objectInfo["id"] . " not found");
			}
			
			//konotrla jeslti je cilovy uzivatel spolupracovnik na projektu
			$tableColabours = new Application_Model_ProjectsColabours();
			$user = $tableColabours->fetchRow(array("directory_id = " . $project->directory_id, "user_id = " . $targetUser->id));
			
			if (!$user && $project->user_id != $targetUser->id) {
				$response->setStatus("TARGET_USER_IS_NOT_COLABOUR", 403);
				throw new Zend_Exception("Target user id " . $targetUser->id . " is not colabour in project id " . $project->directory_id);
			}
		} catch (Zend_Exception $e) {
			$this->_sendToRender($response, $objectInfo["format"], "post");
			return;
		}
		
		//vytvoreni zaznamu o ukolu a prvnoho zaznamu do historie
		$tableTasks = new Application_Model_PlansTasks();
		$taskRow = $tableTasks->createRow();
		
		$tableHistory = new Application_Model_PlansTasksHistory();
		$historyRow = $tableHistory->createRow();
		
		$taskRow->setFromArray(array(
			"user_id" => $this->_user->id
			, "for_user_id" => $targetUser->id
			, "task_content" => $data["task_content"]
			, "project_id" => $project->directory_id
		));
		
		//doplneni nepovinnych hodnot
		if (isset($data["deadline_at"])) {
			$taskRow->deadline_at = $data["deadline_at"];
		}
		
		if (isset($data["current_comment"])) {
			$taskRow->current_comment = $data["current_comment"];
			$historyRow->comment = $data["current_comment"];
		}
		
		//kontorla odeslani statusu
		if (isset($data["current_status"])) {
			$statusList = array(
				"DONE"
				, "DELAY"
				, "INPROGRESS"
				, "NOTSTARTED"
				, "PUTOFF"
				, "NEVER"
			);
			
			$data["current_status"] = strtoupper($data["current_status"]);
			
			if (in_array($data["current_status"], $statusList)) {
				$taskRow->current_status = $data["current_status"];
				$historyRow->status = $data["current_status"];
			} else {
				$historyRow->status = "NOTSTARTED";
			}
		} else {
			$historyRow->status = "NOTSTARTED";
		}
		
		//ulozeni dat
		$taskRow->save();
		
		$historyRow->task_id = $taskRow->id;
		$historyRow->save();
		
		//zapsani odpovedi
		$response->setOk();
		$response["task"] = $taskRow;
		
		$this->_sendToRender($response, $objectInfo["format"], "post");
	}
}