public function init()
 {
     parent::init();
     if (is_null($this->model)) {
         throw new Exception("model darf nicht null sein");
     }
     $this->logs = $this->loadLogs();
     $this->type = is_null($this->type) ? self::VIEW_DEFAULT : $this->type;
     switch ($this->type) {
         case self::VIEW_NEW:
             $this->haveSeen = LogEntry::findOne(['model_id' => $this->model->id, 'model_type' => $this->model->className(), 'created_by' => User::getCurrentUser()->id, 'action' => 'view']);
             break;
         case self::VIEW_COUNT_SEEN:
             $model = $this->model;
             $modelClass = $model->className();
             if (!class_exists($modelClass)) {
                 throw new Exception("Klasse konnte nicht gefunden werden!");
             }
             $this->countSeen = $modelClass::find(['id' => $model->id])->count() - LogEntry::find(['model_id' => $model->id, 'model_type' => $model->className(), 'created_by' => User::getCurrentUser()->id, 'action' => 'view'])->count();
             break;
         case self::VIEW_DEFAULT:
             $this->behavior = $this->model->behaviors['LoggableBehavior'];
             break;
     }
 }
 /**
  * Creates and saves a new log entry. Only properties that have been changed will be saved.
  *
  * @param int $model_id
  *      the id of this model
  * @param $model_type
  *      the class name of the model
  * @param $action
  *      the controller action
  * @param null $old_attr
  *      array of properties
  * @param null $new_attr
  *      array of properties
  * @return bool
  *      whether the log entry has been saved
  */
 protected function addLogEntry($model_id, $model_type, $action, $old_attr = null, $new_attr = null, $eventData)
 {
     if ($action == self::ACTION_VIEW || $action == self::ACTION_CREATE) {
         $entry = LogEntry::findOne(['model_id' => $model_id, 'model_type' => $model_type, 'action' => $action, 'created_by' => \Yii::$app->user->id]);
         if (!is_null($entry)) {
             return false;
         }
     }
     $logEntry = new LogEntry();
     $logEntry->model_id = $model_id;
     $logEntry->model_type = $model_type;
     $logEntry->action = $action;
     if (!is_null($old_attr) && !is_null($new_attr)) {
         if ($eventData != null && $eventData->hasProperty('attributes')) {
             /**
              * @var LogEvent $eventData
              */
             foreach ($eventData->attributes as $key => $value) {
                 $new_attr[$key] = $value;
                 $old_attr[$key] = '';
             }
         }
         if (count(array_diff_assoc($old_attr, $new_attr))) {
             $logEntry->old_attr = array_diff_assoc($old_attr, $new_attr);
             $logEntry->new_attr = array_diff_assoc($new_attr, $old_attr);
             $logEntry->old_attr = json_encode($logEntry->old_attr);
             $logEntry->new_attr = json_encode($logEntry->new_attr);
         } else {
             if ($action == self::ACTION_UPDATE) {
                 return false;
             }
         }
     }
     return $logEntry->save(true);
 }
 /**
  * @param ActiveRecord $model
  * @return bool
  */
 public static function hasEntry(ActiveRecord $model)
 {
     if (!is_null($model)) {
         $logEntry = LogEntry::findOne(['model_id' => $model->id]);
         if (!is_null($logEntry)) {
             return true;
         }
     }
     return false;
 }