<?php
class User_PostController extends Library_Controller {
	public function __call($methodName, $args) {
		//naparsovani objektu
		$objectInfo = $this->_parseActionName($methodName);

		//kontorla formatu
		if ($objectInfo["format"] != "html") {
			//pozadovany format neni html, vypne se layout
			$this->_helper->layout()->disableLayout();
		}
		
		//vytvoreni odpovedi
		$response = new Library_Response();
		
		//vyhodnoceni id objektu
		if (strtolower($objectInfo["id"]) == "list") {
			$response->setStatus("LIST_IS_RESERVED_NAME", 403);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		//kontorla jestli uzivatel existuje
		/* @var $user Application_Model_Row_User */
		$user = $this->_tableUsers->fetchRow($this->_tableUsers->select(false)
			->where("username like ?", $objectInfo["id"]));
		
		if ($user) {
			//uzivatel s prihlasovacim jmenem existuje
			$response->setStatus("USERNAME_EXISTS", 409);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		//kontorla delky a znaku prihlasovacoho jmena
		$alnum = new Zend_Validate_Alnum();
		$length = new Zend_Validate_StringLength(array("min" => 5));
		$empty = new Zend_Validate_NotEmpty();
		$email = new Zend_Validate_EmailAddress();
		
		if (!($alnum->isValid($objectInfo["id"]) || $length->isValid($objectInfo["id"]))) {
			//prihlasovaci jmeno neni validni
			$response->setStatus("USERNAME_IS_INVALID", 400);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		//kontrola odeslanych dat
		if (!isset($_REQUEST["user"]["email"], $_REQUEST["user"]["password"])) {
			//data nejsou kompletni
			$response->setStatus("POST_DATA_NOT_COMPLETE", 400);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		$data = $_REQUEST["user"];
		
		if (!($email->isValid($data["email"]) && $empty->isValid($data["password"]) && $length->isValid($data["password"]))) {
			$response->setStatus("POST_DATA_IS_INVALID", 400);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		//zapsani dat
		$user = $this->_tableUsers->createRow();
		$user->username = $objectInfo["id"];
		$user->email = $data["email"];
		$user->salt = sha1(time() . microtime());
		$user->password = $user->hashPassword($data["password"]);
		$user->setCookie()->save();
		
		//vytvoreni domovskeho adresare
		$tableDirectory = new Application_Model_DocumentsDirectories();
		$directory = $tableDirectory->createRow(array(
			"directory_name" => $user->username,
			"user_id" => $user->id,
			"group_id" => 0,
			"parent_id" => 1,
			"depth" => 1
		));
		
		$directory->save();
		
		$user->root_directory_id = $directory->id;
		$user->save();
		
		//zapsani odpovedi
		$response->setOk();
		$response["user"] = $user;
		
		$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");
		}
	}
}
