<?php
class Documentation_DeleteController extends Library_Controller {
	public function __call($methodName, $args) {
		//priprava dat
		$response = new Library_Response();
		$tableDoc = new Application_Model_Documentations();
		$objectInfo = $this->_parseActionName($methodName);
		
		//kontrola dat
		try {
			//nacteni dat
			$row = $tableDoc->find($objectInfo["id"])->current();
			
			if (!$row) {
				// dokumentace nebyla nalezena
				$response->setStatus("DOC_NOT_FOUND", 404);
				throw new Zend_Exception("Documentation id " . $objectInfo["id"] . " not found");
			}
			
			//kontrola opravneni
			if ($row->user_id != $this->_user->id && $this->_user->id != 1 && !in_array("NOTE_MANAGER", $this->_user->getRoles())) {
				//uzivatel neni opravnen smazat dokumentaci
				$response->setStatus("NO_PERMISION_TO_DELETE", 403);
				throw new Zend_Exception("User id " . $this->_user->id . " is not permited to delete documentation id " . $row->id);
			}
		} catch (Zend_Exception $e) {
			$this->_sendToRender($response, $objectInfo["format"], "delete");
			return;
		}
		
		//smazani dokumentace
		$row->delete();
		
		//nastaveni a odeslani odpovedi
		$response->setOk();
		$this->_sendToRender($response, $objectInfo["format"], "delete");
	}
}