Ejemplo n.º 1
0
 public static function track($msg, $type = "other", $info = "")
 {
     if (!Yii::app()->user->isGuest) {
         ## load info
         $pathInfo = AuditTrail::loadPageInfo($info);
         $uid = Yii::app()->user->id;
         if ($pathInfo['module'] == "sys") {
             return;
         }
         ## get last audit trail
         $lastTrail = AuditTrail::model()->find(['order' => '"id" desc']);
         if (!is_null($lastTrail)) {
             $lastTrail = $lastTrail->attributes;
         } else {
             return;
         }
         ## detect duplicate
         $isDuplicate = $lastTrail['pathinfo'] == $pathInfo['pathinfo'] && $lastTrail['params'] == $pathInfo['params'];
         $lastInsertHour = round(abs(strtotime($lastTrail['stamp']) - time()) / 3600);
         $isDifferentType = $lastTrail['type'] != $type;
         if ($isDuplicate) {
             if (isset($pathInfo['data']) && $lastTrail['data'] != $pathInfo['data']) {
                 $isDuplicate = false;
             }
         }
         ## if not duplicate OR is different type OR last tracked time is more than 1 hour ago
         if (!$isDuplicate || $isDuplicate && $isDifferentType || $isDuplicate && $lastInsertHour > 1) {
             ## create new track
             if ($isDuplicate) {
                 ## skip tracking view for same page after CRUD
                 $isCrud = in_array($lastTrail['type'], ['create', 'update', 'delete']);
                 if ($isCrud && $type == "view") {
                     return;
                 }
             }
             $at = $pathInfo;
             ## remove data from view tracker...
             if ($type == "view") {
                 $at['data'] = "{}";
             }
             if (is_string($msg) && $msg != "") {
                 $at['description'] = $msg;
             }
             $at['type'] = $type;
             $at['stamp'] = date("Y-m-d H:i:s");
             $at['user_id'] = Yii::app()->user->id;
             if (@$at['model_id'] != '') {
                 ActiveRecord::batch('AuditTrail', [$at]);
             }
         }
     }
 }