<?php

/**
 * The AuditTrail module.
 * @package application.modules
 */
class AuditTrail extends TidansActiveRecord {
    /**
     * The followings are the available columns in table 'tbl_audit_trail':
     * @var integer $id
     * @var string $new_value
     * @var string $old_value
     * @var string $action
     * @var string $model
     * @var string $field
     * @var string $stamp
     * @var integer $user_id
     * @var string $model_id
     */

    /**
     * Returns the static model of the specified AR class.
     * @return AuditTrail the static model class
     */
    public static function model($className=__CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	if (isset(Yii::app()->params['AuditTrail']) && isset(Yii::app()->params['AuditTrail']['table']))
	    return Yii::app()->params['AuditTrail']['table'];
	else
	    return 'audit_trail';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
	// NOTE: you should only define rules for those attributes that
	// will receive user inputs.
	return array(
	    array('action, model, stamp, model_id', 'required'),
	    array('action', 'length', 'max' => 255),
	    array('model', 'length', 'max' => 255),
	    array('field', 'length', 'max' => 255),
	    array('model_id', 'length', 'max' => 255),
	    array('user_id', 'length', 'max' => 255),
	    // The following rule is used by search().
	    // Please remove those attributes that should not be searched.
	    array('id, new_value, old_value, action, model, field, stamp, user_id, model_id', 'safe', 'on' => 'search'),
	);
    }

    protected function beforeValidate() {
	return true;
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
	// NOTE: you may need to adjust the relation name and the related
	// class name for the relations automatically generated below.
	return array(
	    'user' => array(self::BELONGS_TO, 'User', 'user_id'),
	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'old_value' => Yii::t('default', 'Old Value'),
	    'new_value' => Yii::t('default', 'New Value'),
	    'action' => Yii::t('default', 'Action'),
	    'model' => Yii::t('default', 'Type'),
	    'field' => Yii::t('default', 'Field'),
	    'stamp' => Yii::t('default', 'Date'),
	    'user_id' => Yii::t('default', 'User'),
	    'model_id' => Yii::t('default', 'ID'),
	);
    }

    function getParent() {
	$model_name = $this->model;
	return $model_name::model();
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search($options = array()) {
	// @todo Please modify the following code to remove attributes that should not be searched.

	$criteria = $this->ModelCriteria($options);

	$sort = new CSort();
	$sort->defaultOrder = 'stamp DESC';
	$pageSize = $this->yiiparam('pageSize');
	return new CActiveDataProvider(
		get_class($this),
		array_merge(
			array(
			    'criteria' => $criteria,
			    'pagination' => array('pageSize' => $pageSize,),
			    'sort'=>$sort
			),
			$options
		)
	);
    }

    /**
     * Get the query used to get the current model information to be used in export to excel action
     * @return CDbCriteria the criteria used to get the current model information
     */
    public function ModelCriteria($options = array()) { // @todo Please modify the following code to remove attributes that should not be searched.
	$criteria = new CDbCriteria(
//		array(
//		    'order' => 'stamp DESC',
//		)
		);
	$pageSize = $this->yiiparam('pageSize');
	$criteria->compare('t.id', $this->id);
	$criteria->compare('old_value', $this->old_value, true);
	$criteria->compare('new_value', $this->new_value, true);
	$criteria->compare('action', $this->action, true);
	$criteria->compare('model', $this->model,true);
	$criteria->compare('field', $this->field, true);
	$criteria->compare('stamp', $this->stamp, true);
	$criteria->compare('user.username', $this->user_id,true);
	$criteria->compare('model_id', $this->model_id,true);
	$criteria->join = 'left join user on user.id = t.user_id';
	$criteria->mergeWith($this->getDbCriteria());


	return $criteria;
    }

    public function scopes() {
	return array(
	    'recently' => array(
		'order' => ' t.stamp DESC ',
	    ),
	);
    }

    /**
     * Create an obkect of log , for custom Log
     *
     * @param string $old_value old value of the filed
     * @param string $new_value new value of the filed
     * @param string $field field that we want to log its value
     * @param string $action name of the action , update, create or anything else
     * @param string $model the model of the logged record
     * @param string $user_id user id of the session
     * @param string $model_id the id  of the record
     */
    public static function newCustomLog($old_value, $new_value, $field, $action, $model, $user_id, $model_id) {
	$log = new AuditTrail();
	$log->action = $action;
	$log->model = $model;
	$log->user_id = $user_id;
	$log->stamp = date("Y-m-d H:i:s");
	$log->model_id = $model_id;
	$log->old_value = $old_value;
	$log->new_value = $new_value;
	$log->field = $field;
	$log->save(false);
    }

}
