<?php
class Sended_PutController extends Library_Controller {
public function __call($methodName, $args) {
		//vytvoreni odpovedi a instance tabulky
		$response = new Library_Response();
		$tableSended = new Application_Model_MessagesSended();
		
		//nacteni objektu
		$objectInfo = $this->_parseActionName($methodName);
		
		//nacitani a kontrola dat
		try {
			//kontrola uzivatele
			if ($this->_user->id == 2) {
				//uzivatel je guest, ketery nemuze posilat zpravy
				$response->setStatus("GUEST_CANT_SEND_MESSAGES", 403);
				throw new Zend_Exception("User guest is not permited to send messages");
			}
			
			//nacteni radku
			$row = $tableSended->find($objectInfo["id"])->current();
			
			if (!$row) {
				//zprava neexistuje
				$response->setStatus(404, "SENDED_MESSAGE_NOT_FOUND");
				throw new Zend_Exception("Message id " . $objectInfo["id"] . " not found");
			}
			
			if ($row->user_id != $this->_user->id) {
				//uzivatel neni vlastnikem zpravy
				$response->setStatus(403, "USER_IS_NOT_OWNER");
				throw new Zend_Exception("User id " . $this->_user->id . " is not owner of sended message id " . $row->id);
			}
			
			//pokud je zprava uz odeslana vyhodi se chyba
			if ($row->sended_at != "0000-00-00 00:00:00") {
				//zprava uz byla odeslana
				$response->setStatus("MESSAGE_IS_SENDED", 400);
				throw new Zend_Exception("Message id " . $row->id . " is already sended");
			}
		} catch (Zend_Exception $e) {
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		//kontrola odeslanych dat
		if (isset($_REQUEST["sended"])) {
			//data byla odeslana
			$data = $_REQUEST["sended"];
			
			//odebrani potencialne nebezpecnych hodnot
			unset($data["id"], $data["user_id"], $data["created_at"], $data["sended_at"]);
			
			$row->setFromArray($data);
		}
		
		//nastaveni majitele zpravy
		$row->user_id = $this->_user->id;
		
		//konotrla jestli se ma zprava odeslat
		if (isset($_REQUEST["send"])) {
			//zprava byla oznacena k odeslani
			$this->_send($row);
		}
		
		//ulozeni zpravy
		$row->save();
		
		//zapsani dat
		$response->setOk();
		$response["sended"] = $row;
		
		$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("put-html");
				break;
			
			case "json":
			
			default:
				$this->_helper->viewRenderer("put-json");
		}
	}
	
	protected function _send($messageRow) {
		//naparsovani cilu
		$targetList = explode(" ", $messageRow->targets);
		
		//nacteni id uzivatelu
		$users = $this->_tableUsers->fetchAll($this->_tableUsers->select(false)->where("username in (?)", $targetList));
		
		//vytvoreni pole prototypu zpravy
		$data = array(
			"user_id" => 0,
			"from_user_id" => $this->_user->id,
			"subject" => $messageRow->subject,
			"content" => $messageRow->content 
		);
		
		//ukladani dat
		$tableRecieved = new Application_Model_MessagesRecieved();
		
		foreach ($users as $user) {
			$data["user_id"] = $user->id;
			
			$tableRecieved->insert($data);
		}
		
		$messageRow->sended_at = new Zend_Db_Expr("NOW()");
	}
}