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; } }
/** * @param ActiveRecord $model * @param int $pageSize * @return array|ActiveDataProvider */ public static function getLogs(ActiveRecord $model, $pageSize = 5) { if (!is_null($model)) { $id = $model->id; $type = $model->className(); return new ActiveDataProvider(['query' => LogEntry::find()->where(['model_id' => $id, 'model_type' => $type])->orderBy('created_at DESC'), 'pagination' => ['pageSize' => $pageSize, 'pageParam' => 'log']]); } return []; }