<?php

/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace common\behaviors;

use yii\base\Model;
use yii\base\Behavior;

/*
 * @author HuLimin http://blog.u-bo.com
 * @since 0.1
 */

class ActiveLogBehavior extends \yii\base\Behavior
{

    private $_oldattributes = array();

    /**
     * save操作
     * @param  [type] $event [description]
     * @return [type]        [description]
     */
    public function afterSave($event)
    {
        //非新记录，即非插入
        if(!$this->isNewRecord){

            $newattributes = $this->attributes;             //获得AR类中已修改的各字段值
            $oldattributes = $this->oldAttributes;          //之前的旧数据
            //比较新旧数据
            foreach ($newattributes as $name => $value) {
                if(!empty($oldattributes)){
                    $old = $oldattributes[$name];
                } else {
                    $old = '';
                }
                //如果该字段旧数据与新数据不一样，则进行记录
                if($value != $old){
                    $menu_list_for_log = $app->cache->get('menu_list_for_log');
                    $action_name = empty($action_name) ? $menu_list_for_log[$app->requestedRoute]['name'] : $action_name;
                    $actionLog = new ActionLog();
                    $actionLog->module = $app->controller->module->id;
                    $actionLog->controller = $app->controller->id;
                    $actionLog->action = $app->controller->action->id;
                    $actionLog->type = trim($type);
                    $actionLog->url = $app->getRequest()->getUrl();
                    $actionLog->ip = $app->getRequest()->getUserIP();
                    $actionLog->create_time = time();
                    $actionLog->user_id = $app->user->identity->user_id;
                    $actionLog->action_name = trim($action_name);
                    $actionLog->app_name = trim($app_name);
                    $actionLog->content = $content;
                    $actionLog->remark = $remark;
                    $actionLog->user_id = $app->user->identity->user_id;
                }
            }
        } else {//新纪录直接保存操作日志入库
            $log = new ActiveRecordLog;
            $log->description = 'User ' . Yii::app()->user->Name
                    . ' created ' . get_class($this)
                    . '[' . $this->getPrimaryKey() . '].';
            $log->action = 'CREATE';
            $log->model = get_class($this);
            $log->idModel = $this->getPrimaryKey();
            $log->field = '';
            $log->creationdate = new CDbExpression('NOW()');
            $log->userid = Yii::app()->user->id;
            $log->save();
        }
    }

    /**
     * 删除操作
     * @param  [type] $event [description]
     * @return [type]        [description]
     */
    public function afterDelete($event)
    {
        $log = new ActiveRecordLog;
        $log->description = 'User ' . Yii::app()->user->Name . ' deleted '
                . get_class($this)
                . '[' . $this->getPrimaryKey() . '].';
        $log->action = 'DELETE';
        $log->model = get_class($this);
        $log->idModel = $this->getPrimaryKey();
        $log->field = '';
        $log->creationdate = new CDbExpression('NOW()');
        $log->userid = Yii::app()->user->id;
        $log->save();
    }

    public function afterFind($event)
    {
        //保存查询出来的数据
        $this->setOldAttributes($this->getAttributes());
    }

    public function getOldAttributes()
    {
        return $this->_oldattributes;
    }

    public function setOldAttributes($value)
    {
        $this->_oldattributes = $value;
    }

}
