<?php
class Directory_PostController extends Library_Controller {
	public function __call($methodName, $args) {
		//priprava objektu
		$response = new Library_Response();
		$objectInfo = $this->_parseActionName($methodName);
		
		//hack formatu
		$objectInfo["format"] = $objectInfo["id"];
		
		//validace dat
		if (!isset($_REQUEST["directory"]["directory_name"], $_REQUEST["directory"]["parent_id"])) {
			//data nebyla odeslana
			$response->setStatus("DATA_NOT_SEND", 400);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		//kontorla jestli jmeno adredsare neni prazdne
		$data = $_REQUEST["directory"];
		$data["directory_name"] = trim($data["directory_name"]);
		
		if (empty($data["directory_name"])) {
			//jmeno adredare je prazdne
			$response->setStatus("NAME_IS_EMPTY", 400);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		//kontorla existence rodicovskeho adresare a opravneni k nemu pristupovat
		$tableDirectories = new Application_Model_DocumentsDirectories();
		/* @var $parent Application_Model_Row_directory */
		$parent = $tableDirectories->find($data["parent_id"])->current();
		
		if (!$parent) {
			//rodicovsky adresar neexistuje
			$response->setStatus("PARENT_NOT_FOUND", 404);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		//nacteni skupin uzivatele
		$groupList = $this->_user->findDependentRowset("Application_Model_UsersHasManyUsersGroups", "user");
		$groups = array();
		
		foreach ($groupList as $group) {
			$groups[] = $group->user_group_id;
		}
		
		//kontrola opravneni zapisovat
		if (!($parent->getPermisions($this->_user->id, $groups)->write || $this->_user->id == 1)) {
			//uzivatel nema opravneni zapisovat ani neni root, zkotnroluji se role
			$tableRoles = new Application_Model_UsersRoles();
			$role = $this->_user->findManyToManyRowset("Application_Model_UsersRoles", "Application_Model_UsersHasManyUsersRoles", "user", "role", $tableRoles->select(false)->where("role_name like ?", "DOCUMENT_MANAGER"));
			
			if (!$role->count()) {
				//uzivatel nema potrebnou roli
				$response->setStatus("NO_PERMISIONS", 403);
				$this->_sendToRender($response, $objectInfo["format"]);
				return;
			}
		}
		
		//kontorla duplicity jmena
		$siblings = $parent->findDependentRowset($tableDirectories, "parent", $tableDirectories->select(false)->where("directory_name like ?", $data["directory_name"]));
		
		if ($siblings->count()) {
			//jmeno uz existuje
			$response->setStatus("DIRECTORY_NAME_EXISTS", 409);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		//zapis je mozny, provede se vytvoreni adresare
		$directory = $tableDirectories->createRow(array(
			"directory_name" => $data["directory_name"],
			"user_id" => $this->_user->id,
			"group_id" => 0,
			"depth" => $parent->depth + 1,
			"parent_id" => $parent->id
		));
		
		$directory->save();
		
		//nastaveni odpovedi
		$response["directory"] = $directory;
		$response->setOk();
		$this->_sendToRender($response, $objectInfo["format"]);
	}
	
	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("post-html");
				break;
			
			case "json":
			
			default:
				$this->_helper->viewRenderer("post-json");
		}
	}
}