<?php
/**
 * IMPORTANT NOTE
 * The code of this system is closed and a private proprietary of Flex Solutions.
 * A written authorization/license from Flex Solutions is required before using or modifying this code.
 * Email: sales@flexsolution.biz, Phone: +962795342585, Website: http://www.flexsolutions.biz
 */
 
/**
 * Log controller
 *
 * Yii controller to control Log actions
 *
 * @version 1.0
 *
 * @author Amaney
 * @package application.controllers
 */
class AuditTrailController extends Controller {

    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = '//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
//			'postOnly + delete', // we only allow deletion via POST request
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('index', 'exportToExcel'),
                'roles' => array(User::SYSTEM_ADMIN_ROLE),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    /**
     * Manages all models.
     */
    public function actionIndex() {
        $model = new AuditTrail('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['AuditTrail']))
            $model->attributes = $_GET['AuditTrail'];  
        $this->render('index', array(
            'model' => $model,
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id) {
        $model = AuditTrail::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'log-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

    /**
     * export log info to excel
     */
    public function actionExportToExcel() {
        $model = new AuditTrail('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['AuditTrail']))
            $model->attributes = $_GET['AuditTrail'];

        //Export Data to Excel File
        $header = array(
            'user_id' => Yii::t('default', 'User'),
            'model' => Yii::t('default', 'Type'),
            'model_id' => Yii::t('default', 'ID'),
            'action' => Yii::t('default', 'Action'),
            'field' => Yii::t('default', 'Field'),
            'old_value' => Yii::t('default', 'Old Value'),
            'new_value' => Yii::t('default', 'New Value'),
            'stamp' => Yii::t('default', 'Date'),
        );
        $column = "user.username,
		model,
                model_id,
                action,
		field,
		old_value,
                new_value,
		stamp
               ";

        $excel_file_name = Yii::t('default', 'Audit Trail')."-" . date('Y.m.d-H.i.s');
        $model->exportToExcel($excel_file_name, $model, $header, $column,$model->ModelCriteria());
    }

}
