<?php
class Addressary_GetController extends Library_Controller {
	public function __call($methodName, $arguments) {
		//vyhodnoceni metody
		if (substr($methodName, -6) !== "Action") {
			parent::__call($methodName, $arguments);
			return;
		}
		
		//separace akce (formatu)
		$action = substr($methodName, 0, strlen($methodName) - 6);
		$format = strtolower($action);
		
		//nacteni dat z tabulky adresare
		$tableAddressary = new Application_Model_MessagesAddressary();
		
		$addressary = $tableAddressary->fetchAll(array("user_id" => $this->_user->id), "target_user_login");

		//vytvoreni odpovedi
		$response = new Library_Response();
		$response->setOk();
		
		$response["addressary"] = $addressary;
		
		$this->_sendToRender($response, $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("get-html");
				break;
			
			case "json":
			
			default:
				$this->_helper->viewRenderer("get-json");
		}
	}
}