From 10b7c6179ae15550f1c3a81fd11920f781c5ed9d Mon Sep 17 00:00:00 2001 From: Adbhuta Gauranga das Date: Wed, 22 Sep 2021 11:58:08 +0300 Subject: [PATCH] add custom operation meta support (#19) * add custom operation meta support to `HasHistories` trait --- README.md | 26 ++++++++++++++++++++++++++ src/HasHistories.php | 39 ++++++++++++++++++++++++++++++++++++++- src/HistoryObserver.php | 30 +++++++++--------------------- 3 files changed, 73 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index b84fcf9..379f957 100644 --- a/README.md +++ b/README.md @@ -313,6 +313,32 @@ To fix this, you'll need to enable custom auth guards scanning in config file: 'auth_guards' => null ``` +### Custom meta + +You can define your own method for meta data. By default for `updating` event meta consists of modified keys and for other events meta is `null`. + +Just redefine the method `getModelMeta` for the trait. + +Example: + +```php +class Article extends Model +{ + use HasHistories; + + ... + + public function getModelMeta($event) + { + // using defaults for updating + if($event == 'updating') return parent::getModelMeta($event); + // passing full model to meta + // ['key1' => 'value1', 'key2' => 'value2', ...] + else return $this; + } +} +``` + ### Known issues 1. When updating a model, if its model label(attributes returned from `getModelLabel`) has been modified, the history message will use its new attributes, which might not be what you expect. diff --git a/src/HasHistories.php b/src/HasHistories.php index e69dc79..8edb6b8 100644 --- a/src/HasHistories.php +++ b/src/HasHistories.php @@ -38,10 +38,47 @@ public static function bootHasHistories() static::observe(HistoryObserver::class); } + /** + * Get the model's meta in history. + * + * @return array + */ + public function getModelMeta($event) + { + switch($event) + { + case 'updating': + /* + * Gets the model's altered values and tracks what had changed + */ + $changes = $this->getDirty(); + + $changed = []; + foreach ($changes as $key => $value) { + if(static::isIgnored($this, $key)) continue; + + array_push($changed, ['key' => $key, 'old' => $this->getOriginal($key), 'new' => $this->$key]); + } + return $changed; + case 'created': + case 'deleting': + case 'restored': + return null; + } + } + + public static function isIgnored($model, $key) + { + $blacklist = config('history.attributes_blacklist'); + $name = get_class($model); + $array = isset($blacklist[$name])? $blacklist[$name]: null; + return !empty($array) && in_array($key, $array); + } + /** * Get the model's label in history. * * @return string */ public abstract function getModelLabel(); -} \ No newline at end of file +} diff --git a/src/HistoryObserver.php b/src/HistoryObserver.php index 58b28f0..dded9d8 100644 --- a/src/HistoryObserver.php +++ b/src/HistoryObserver.php @@ -14,11 +14,12 @@ class HistoryObserver * @return void */ public function created($model) - { + { if(!static::filter('created')) return; $model->morphMany(History::class, 'model')->create([ 'message' => trans('panoscape::history.created', ['model' => static::getModelName($model), 'label' => $model->getModelLabel()]), + 'meta' => $model->getModelMeta('created'), 'user_id' => static::getUserID(), 'user_type' => static::getUserType(), 'performed_at' => time(), @@ -32,7 +33,7 @@ public function created($model) * @return void */ public function updating($model) - { + { if(!static::filter('updating')) return; /* @@ -43,17 +44,10 @@ public function updating($model) * Bypass restoring event */ if(array_key_exists('deleted_at', $changes)) return; - - $changed = []; - foreach ($changes as $key => $value) { - if(static::isIgnored($model, $key)) continue; - - array_push($changed, ['key' => $key, 'old' => $model->getOriginal($key), 'new' => $model->$key]); - } $model->morphMany(History::class, 'model')->create([ 'message' => trans('panoscape::history.updating', ['model' => static::getModelName($model), 'label' => $model->getModelLabel()]), - 'meta' => $changed, + 'meta' => $model->getModelMeta('updating'), 'user_id' => static::getUserID(), 'user_type' => static::getUserType(), 'performed_at' => time(), @@ -67,11 +61,12 @@ public function updating($model) * @return void */ public function deleting($model) - { + { if(!static::filter('deleting')) return; $model->morphMany(History::class, 'model')->create([ 'message' => trans('panoscape::history.deleting', ['model' => static::getModelName($model), 'label' => $model->getModelLabel()]), + 'meta' => $model->getModelMeta('deleting'), 'user_id' => static::getUserID(), 'user_type' => static::getUserType(), 'performed_at' => time(), @@ -85,11 +80,12 @@ public function deleting($model) * @return void */ public function restored($model) - { + { if(!static::filter('restored')) return; $model->morphMany(History::class, 'model')->create([ 'message' => trans('panoscape::history.restored', ['model' => static::getModelName($model), 'label' => $model->getModelLabel()]), + 'meta' => $model->getModelMeta('restored'), 'user_id' => static::getUserID(), 'user_type' => static::getUserType(), 'performed_at' => time(), @@ -115,14 +111,6 @@ public static function getUserType() return static::getAuth()->check() ? get_class(static::getAuth()->user()) : null; } - public static function isIgnored($model, $key) - { - $blacklist = config('history.attributes_blacklist'); - $name = get_class($model); - $array = isset($blacklist[$name])? $blacklist[$name]: null; - return !empty($array) && in_array($key, $array); - } - public static function filter($action) { if(!static::getAuth()->check()) { @@ -158,5 +146,5 @@ private static function activeGuard() } return null; } - + }