<?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());
    }
    
    /**
     * List dashboard informations
     */
    public function actionIndex()
    {             
        if (!Yii::app()->options->get('system.customer.action_logging_enabled', true)) {
            $this->redirect(array('lists/index'));
        }
        
        $request = Yii::app()->request;
        $logs = new CustomerActionLog();
        $logs->attributes = (array)$request->getQuery($logs->modelName, array());
        $logs->customer_id = (int)Yii::app()->customer->getId();
        
        $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 activity log
     */
    public function actionDelete_log($id)
    {
        $model = CustomerActionLog::model()->findByAttributes(array(
            'log_id'        => $id,
            'customer_id'    => (int)Yii::app()->customer->getId(),
        ));
        
        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 activity logs
     * This can be time consuming, so we need to make sure we have enough time for it.
     */
    public function actionDelete_logs()
    {
        set_time_limit(0);
        ignore_user_abort(true);
            
        CustomerActionLog::model()->deleteAllByAttributes(array(
            'customer_id'    => (int)Yii::app()->customer->getId(),
        ));
            
        $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')));
        }
    }

}