<?php
class Document_DeleteController extends Library_Controller {
	public function __call($methodName, $args) {
		//inicializace objektu
		$objectInfo = $this->_parseActionName($methodName);
		$response = new Library_Response();

		//nacteni dokumentu
		$tableDocuments = new Application_Model_Documents();

		//kontrola jeslti se dokument nacita pomoci zkratky
		if (strlen($objectInfo["id"]) == 7) {
			$tableShortcuts = new Application_Model_DocumentsShortcuts();

			$shortcut = $tableShortcuts->fetchRow($tableShortcuts->select(false)
			->where("shortcut like ?", $_REQUEST["shortcut"]));

			if ($shortcut) {
				$document = $shortcut->findParentRow("Application_Model_Documents", "document", $select);
			} else {
				$document = false;
			}
		} else {
			$document = $tableDocuments->find($objectInfo["id"])->current();
		}

		if (!$document) {
			$response->setStatus("DOCUMENT_NOT_FOUND", 404);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		if (!$document->is_latest) {
			//dokument neni master
			$response->setStatus("DOCUMENT_IS_NOT_MASTER", 405);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}

		//nacteni opravneni
		$user = $this->_user;
		
		//nactnei role
		$tableRoles = new Application_Model_UsersRoles();
		$role = $user->findManyToManyRowset($tableRoles, "Application_Model_UsersHasManyUsersRoles", "user", "role", $tableRoles->select(false)->where("role_name like ?", "DOCUMENT_MANAGER"));
		
		//konotrla opravneni smazat dokument
		if (!($user->id == 1 || $role->count() || $document->user_id == $user->id)) {
			$response->setStatus("NO_PERMISION_TO_DELETE_DOCUMENT", 403);
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		//vytvoreni tabulky trash a asociaci k adresarum
		$tableTrash = new Application_Model_DocumentsUnassigned();
		$tableAssocs = new Application_Model_DocumentsDirectoriesHasManyDocuments();
		
		//nacteni vsech verzi
		$history = $document->findDependentRowset("Application_Model_DocumentsHistory", "last");
		
		//smazani vsech asociaci
		foreach ($history as $hisotryRow) {
			$oldDocument = $hisotryRow->findParentRow($tableDocuments, "old");
			
			$tableAssocs->delete(array("document_uuid" => $oldDocument->uuid));
			$tableTrash->delete(array("uuid" => $document->uuid));
			
			//smazani dokumentu
			$oldDocument->delete();
		}
		
		//smazani master dokumentu
		$tableAssocs->delete(array("document_uuid" => $document->uuid));
		$tableTrash->delete(array("uuid" => $document->uuid));
		
		$document->delete();
		
		//odeslani 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");
		}
	}
}