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

/**
 * DashboardController
 * 
 * Handles the actions for dashboard related tasks
 * 
 * @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 DashboardController extends Controller
{
    public function init()
    {
        $pageScripts = array(
            'dashboard' => AssetsUrl::js('dashboard.js'),
        );

        foreach ($pageScripts as $handler => $url) {
            Yii::app()->clientScript->registerScriptFile($url);
        }

        parent::init();
    }
    
    /**
     * Define the filters for various controller actions
     * Merge the filters with the ones from parent implementation
     */
    public function filters()
    {
        return CMap::mergeArray(array(
            'postOnly + delete_log, delete_logs',
        ), parent::filters());
    }
    
    /**
     * Display dashboard informations
     */
    public function actionIndex()
    {
        if (file_exists(Yii::getPathOfAlias('root.install')) && is_dir($dir = Yii::getPathOfAlias('root.install'))) {
            Yii::app()->notify->addWarning(Yii::t('app', 'Please remove the install directory({dir}) from your application!', array(
                '{dir}' => $dir,
            )));
        }
        
        if (is_writable($dir = Yii::getPathOfAlias('common.config'))) {
            Yii::app()->notify->addWarning(Yii::t('app', 'Main configuration directory({dir}) is writable, please chmod it to 0755, recursive!', array(
                '{dir}' => $dir,
            )));
        }
        
        if (is_writable($file = Yii::getPathOfAlias('common.config.main-custom').'.php')) {
            Yii::app()->notify->addWarning(Yii::t('app', 'Main configuration file({file}) is writable, please chmod it to 0555!', array(
                '{file}' => $file,
            )));
        }
        
        if (!Yii::app()->options->get('system.customer.action_logging_enabled', true)) {
            $this->redirect(array('customers/index'));
        }
        
        $request = Yii::app()->request;
        $logs = new CustomerActionLog();
        $logs->attributes = (array)$request->getQuery($logs->modelName, array());
        
        $this->setData(array(
            'pageMetaTitle'     => $this->data->pageMetaTitle . ' | ' . Yii::t('dashboard', 'Dashboard'), 
            'pageHeading'       => Yii::t('dashboard', 'Dashboard'),
            'pageBreadcrumbs'   => array(
                Yii::t('dashboard', 'Recent activity'),
            ),
        ));
        
        $this->render('index', compact('logs'));
    }
    
    /**
     * Delete a single action log
     */
    public function actionDelete_log($id)
    {
        $model = CustomerActionLog::model()->findByAttributes(array(
            'log_id'    => $id,
        ));
        
        if ($model === null) {
            throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.'));
        }
        
        $model->delete();
                    
        $request = Yii::app()->request;
        $notify = Yii::app()->notify;
        
        if (!$request->isAjaxRequest) {
            $notify->addSuccess(Yii::t('app', 'Your item has been successfully deleted!'));
            $this->redirect($request->getPost('returnUrl', array('dashboard/index')));
        }
    }
    
    /**
     * Delete all action logs
     */
    public function actionDelete_logs()
    {
        CustomerActionLog::model()->deleteAll();
            
        $request = Yii::app()->request;
        $notify = Yii::app()->notify;
        
        if (!$request->isAjaxRequest) {
            $notify->addSuccess(Yii::t('app', 'Your items have been successfully deleted!'));
            $this->redirect($request->getPost('returnUrl', array('dashboard/index')));
        }
    }

}