<?php
class User_DeleteController 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();
		}
		
		//vyhodnoceni id objektu
		if (strtolower($objectInfo["id"]) == "list") {
			$this->_list($objectInfo["format"]);
			return;
		}
		
		//vytvoreni odpovedi
		$response = new Library_Response();
		
		//nacteni informaci o uzivateli
		/* @var $user Application_Model_Row_User */
		$user = $this->_tableUsers->fetchRow($this->_tableUsers->select(false)
			->where("username like ?", $objectInfo["id"])->orWhere("id = ?", $objectInfo["id"]));
		
		if (!$user) {
			$response->setStatus("USER_NOT_FOUND", 404);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		//kontorla jestli se nejedna o systemove uzivatele
		if ($user->id == 1 || $user->id == 2) {
			$response->setStatus("SYSTEM_USER_CANT_BE_DELETED", 404);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		//kontorla opraveneni
		
		//nacteni roli
		$tableRoles = new Application_Model_UsersRoles();
		$roleList = $user->findManyToManyRowset($tableRoles, "Application_Model_UsersHasManyUsersRoles");
		$roles = array();
		
		foreach ($roleList as $role) {
			$roles[] = $role->role_name;
		}
		
		//vlastni kontrola opravneni
		if ($this->_user->id == $user->id || in_array("USER_MANAGER", $roles) || $this->_user->id != 1) {
			//uzivatel je opravne mazat
			
			//vyhodnoceni uzivatele k prepsani vlastnictvi dokumentu a adresaru
			if ($user->id == $this->_user->id) {
				//uzivatel maze sam sebe, dokumenty se prepisi na root uzivatele
				$rewriteTo = 1;
			} else {
				//uzivatel maze jineho uzivatele, dokumenty se pripisi na aktualniho uzivatele
				$rewriteTo = $this->_user->id;
			}
		} else {
			//uzivatel neni opravnen smazat uzivatele
			$response->setStatus("NO_PERMISION_TO_DELETE_USER", 403);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		//odebrani asociaci roli a skupin
		$tableUsersGroups = new Application_Model_UsersHasManyUsersGroups();
		$tableUsersRoles = new Application_Model_UsersHasManyUsersRoles();
		
		$tableUsersGroups->delete(array("user_id" => $user->id));
		$tableUsersRoles->delete(array("user_id" => $user->id));
		
		//prevedeni dokumentu a adresaru na noveho uzivatele
		$tableDocuments = new Application_Model_Documents();
		$tableDirectories = new Application_Model_DocumentsDirectories();
		$tableTrash = new Application_Model_DocumentsUnassigned();
		
		$updated = array("user_id" => $rewriteTo);
		$where = array("user_id" => $user->id);
		
		//vlastni update dat
		$tableDocuments->update($updated, $where);
		$tableDirectories->update($updated, $where);
		$tableTrash->update($updated, $where);
		
		//smazani uzivatele
		$user->delete();
		
		//zapis odpovedi
		$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("delete-html");
				break;
			
			case "json":
			
			default:
				$this->_helper->viewRenderer("delete-json");
		}
	}
}
