<?php

class Eppe_Frontend_Controller_Action extends Eppe_Controller_Action {

	protected $_seoConfig = null;

	protected $isAjax = false;
	protected $resolveAllowed = true;
	protected $forceLayout;
	
	protected $_authStorage = 'Frontend';
	
	public function init() {
		if ($this->_request->isXmlHttpRequest() || $this->looksLikeAjaxOrIframe()) {
			$this->isAjax = true;
			$this->ajaxResponse = new Rtn_Ajax_Response();
			$this->_disableLayout();
			$this->_disableView();
			$this->resolveAllowed && $this->_resolveAllowed();
		}
		else {
			$this->_initHeadStyles();
			$this->_initHeadScripts();

			// parent (problem z dziedziczeniem bezposrednio od dziadka)
			$this->_initViewScriptsPath();
			$this->_initDefaultFormAndValidateTranslator();
			$this->_initCustomHeadScriptClass();
			$this->_initCustomHeadStyleClass();

			$this->_initLayoutScript();
			$this->resolveAllowed && $this->_resolveAllowed();
			$this->_initMainMenu();
			$this->_initPaginationPartial();
			$this->_initGlobalViewHelpersPath();
			$this->_initMainMeta();
		}
	}

	/**
	 * Ustalenie nazwy skryptu z layoutem
	 */
	protected function _initLayoutScript() {
		if(!empty($this->forceLayout)){
			$this->_helper->layout->setLayout($this->forceLayout);
		}
		else{
			$this->_helper->layout->setLayout('layout');
		}
	}

	/**
	 * Sprawdza, czy dana rola ma uprawnienia do zasobu. Jeśli nie ma, następuje przekierowanie na stronę logowania.
	 *
	 */
	protected function _resolveAllowed() {
		if (!Eppe_Acl::checkAllowed($this->_request,$this->_authStorage)) {
			$this->_redirectUnauthorized($this->view->url(array('redir' => base64_encode(urlencode($_SERVER['REQUEST_URI']))), 'login'));
		}
	}

	/**
	 * Przekierowuje do wskazanego url w przypadku niezalogownych użytkowników,
	 * lub na stronę główną, jeśli uzytkownik jest zalogowany
	 */
	protected function _redirectUnauthorized($url) {
		if (Eppe_Auth::_()->hasIdentity()) {
			$url = $this->view->url(array(), 'homepage');
		}
		
		if($this->isAjax){
			$this->ajaxResponse->evalJS = 'window.location = "'.$url.'"';
			// Ominięcie wykonania akcji - Przejście od razu do postDispatch
			// i wysłanie odpowiedzi.
			$this->postDispatch();
			$this->getResponse()->sendResponse();
			exit; 
		}
		else{
			$this->_redirect($url);
		}
	}

	/**
	 * Odświeża sesję przy przeładowaniu
	 */
	protected function _refreshSession() {
		if (Eppe_Auth::_()->hasIdentity()) {
			$namespace = new Zend_Session_Namespace('Eppe_Auth_Front');
			$namespace->setExpirationSeconds(3600 * 3);
		}
	}

	/**
	 * @todo informacje meta powinny być pobierane z seo.ini
	 */
	protected function _initMainMeta() {
		if (!Zend_Registry::isRegistered('metaOn')) {
			$this->view->headTitle($this->_getSeoConfig()->defaultHead->title)
				->setSeparator($this->_getSeoConfig()->defaultHead->separator);
			$this->view->headMeta()
				->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8')
				->appendHttpEquiv('Content-Language', substr(Eppe_Localization::getCurrent(), 0, 2));
			$this->_setSiteMeta();
			Zend_Registry::set('metaOn', true);
		}
	}

	/**
	 * @todo informacje meta powinny być pobierane z seo.ini
	 */
	protected function _setSiteMeta() {
		if (isset($this->_getSeoConfig()->{$_SERVER['REQUEST_URI']}->title)) {
			$this->view->headTitle()->append($this->_getSeoConfig()->{$_SERVER['REQUEST_URI']}->title);
		}

		if (isset($this->_getSeoConfig()->{$_SERVER['REQUEST_URI']}->keywords)) {
			$this->view->headMeta()->appendName('keywords', $this->_getSeoConfig()->{$_SERVER['REQUEST_URI']}->keywords);
		}

		if (isset($this->_getSeoConfig()->{$_SERVER['REQUEST_URI']}->description)) {
			$this->view->headMeta()->appendName('description', $this->_getSeoConfig()->{$_SERVER['REQUEST_URI']}->description);
		}
	}

	protected function _getSeoConfig() {
		if ($this->_seoConfig == null) {
			$this->_seoConfig = new Zend_Config_Ini(APPLICATION_PATH . 'configs/seo.ini');
		}
		return $this->_seoConfig;
	}

	/**
	 * Ładuje główne menu strony
	 */
	protected function _initMainMenu() {
		$nav = new Zend_Navigation(Core_Module_SiteMenu::getNavigation());
		$this->view->navigation($nav);
	}

	protected function _initPaginationPartial() {
		$this->view->addScriptPath(APPLICATION_PATH . 'layouts/scripts');
		Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination-control.phtml');
	}

	/**
	 * @todo dodać obsługę ładowania bibliotek z pliku ini
	 */
	protected function _initHeadStyles() {
		Head_Libraries::getFrontendCssLibs();
	}

	/**
	 * @todo dodać obsługę ładowania bibliotek z pliku ini
	 */
	protected function _initHeadScripts() {
		Head_Libraries::getFrontendJsLibs();
	}

	protected function _initGlobalViewHelpersPath() {
		$this->view->addHelperPath(APPLICATION_PATH . '/modules/Core/views/helpers/');
	}

}

?>
