<?php defined('MW_PATH') || exit('No direct script access allowed');

/**
 * CustomerSystemInit
 * 
 * @package MailWizz EMA
 * @author Serban George Cristian <cristian.serban@mailwizz.com> 
 * @link http://www.mailwizz.com/
 * @copyright 2013-2014 MailWizz EMA (http://www.mailwizz.com)
 * @license http://www.mailwizz.com/license/
 * @since 1.0
 */
 
class CustomerSystemInit extends CApplicationComponent 
{
    protected $_hasRanOnBeginRequest = false;
    protected $_hasRanOnEndRequest = false;
    
    public function init()
    {
        parent::init();
        Yii::app()->attachEventHandler('onBeginRequest', array($this, 'runOnBeginRequest'));
        Yii::app()->attachEventHandler('onEndRequest', array($this, 'runOnEndRequest'));
    }
    
    public function runOnBeginRequest(CEvent $event)
    {
        if ($this->_hasRanOnBeginRequest) {
            return;
        }

        // a safety hook for logged in vs not logged in users.
        Yii::app()->hooks->addAction('customer_controller_init', array($this, 'checkControllerAccess'));

        // register core assets if not cli mode and no theme active
        if (!MW_IS_CLI && (!Yii::app()->hasComponent('themeManager') || !Yii::app()->getTheme())) {
            $this->registerAssets();
        }
        
        // and mark the event as completed.
        $this->_hasRanOnBeginRequest = true;
    }
    
    public function runOnEndRequest(CEvent $event)
    {
        if ($this->_hasRanOnEndRequest) {
            return;
        }

        // and mark the event as completed.
        $this->_hasRanOnEndRequest = true;
    }

    // callback for customer_controller_init and customer_before_controller_action action.
    public function checkControllerAccess() 
    {
        static $_unprotectedControllersHookDone = false;
        static $_hookCalled = false;
        
        if ($_hookCalled) {
            return;
        }
        
        $controller = Yii::app()->getController();
        $_hookCalled = true;
        $unprotectedControllers = (array)Yii::app()->params->itemAt('unprotectedControllers');

        if (!$_unprotectedControllersHookDone) {
            Yii::app()->params->add('unprotectedControllers', $unprotectedControllers);
            $_unprotectedControllersHookDone = true;
        }

        if (!in_array($controller->id, $unprotectedControllers) && !Yii::app()->customer->getId()) {
            // make sure we set a return url to the previous page that required the customer to be logged in.
            Yii::app()->customer->setReturnUrl(Yii::app()->request->requestUri);
            // and redirect to the login url.
            $controller->redirect(Yii::app()->customer->loginUrl);
        }
        
        if (Yii::app()->options->get('system.customer.action_logging_enabled', true)) {
            if (Yii::app()->customer->getModel()) {
                // and attach the actionLog behavior to log various actions for this customer.
                Yii::app()->customer->getModel()->attachBehavior('logAction', array(
                    'class' => 'customer.components.behaviors.CustomerActionLogBehavior',
                ));            
            }
        }
    }
    
    public function registerAssets()
    {
        $cs = Yii::app()->clientScript;
        
        $scripts = array(
            'bootstrap'     => AssetsUrl::js('bootstrap.min.js'),
            'notify'        => AssetsUrl::js('notify.js'),
            'application'   => AssetsUrl::js('app.js') 
        );    
        
        $styles = array(
            'font-open-sans'    => 'http://fonts.googleapis.com/css?family=Open+Sans',
            'font-noto-sans'    => 'http://fonts.googleapis.com/css?family=Noto+Sans:700italic',
            'bootstrap'         => AssetsUrl::css('bootstrap.min.css'),
            'application'       => AssetsUrl::css('style.css'),
        );
        
        $cs->registerCoreScript('jquery');
        
        foreach ($scripts as $handler => $src) {
            $cs->registerScriptFile($src, null, array('id' => 'script-' . $handler));
        }
        
        foreach ($styles as $handler => $src) {
            $cs->registerCssFile($src);
        }    
    }
}