Skip to content

Commit

Permalink
feat: add custom history translation support
Browse files Browse the repository at this point in the history
  • Loading branch information
seancheung committed Mar 22, 2024
1 parent d65b263 commit 3e319da
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ Example meta

### Custom history

Besides the built in `created/updating/deleting/restoring` events, you may store custom history record with `ModelChanged` event.
Besides the built in `created/updating/deleting/restoring` events, you may track custom history record by firing an `ModelChanged` event.

```php
use Panoscape\History\Events\ModelChanged;
Expand All @@ -190,7 +190,7 @@ use Panoscape\History\Events\ModelChanged;
event(new ModelChanged($user, 'User roles updated', $user->roles()->pluck('id')->toArray()));
```

The `ModelChanged` constructor accepts two/three arguments. The first is the associated model instance; the second is the message; the third is optional, which is the meta(array);
The `ModelChanged` constructor accepts two/three/four arguments. The first is the associated model instance; the second is the message; the third is optional, which is the meta(array); the fourth is also optional, being the translation key of the event(see [Localization](#localization)).

### Localization

Expand Down Expand Up @@ -234,6 +234,22 @@ This will translate your model history into
创建项目project_001
```

You can also translate custom history messages from `ModelChanged` events

```php
/*
|--------------------------------------------------------------------------
| Tracker Language Lines
|--------------------------------------------------------------------------
*/
'switched_role' => ':model switched role',
```

```php
// if you specified the translation key, the message argument will be ignored, simply just pass `null`
event(new ModelChanged($user, null, $user->roles()->pluck('id')->toArray()), 'switched_role');
```

### Filters

You may set whitelist and blacklist in config file. Please follow the description guide in the published config file.
Expand Down
6 changes: 5 additions & 1 deletion src/Events/ModelChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ class ModelChanged

public $meta;

public $trans;

/**
* Create a new event instance.
*
* @param Model $model
* @param string $message
* @param array $meta
* @param string $trans
*
* @return void
*/
public function __construct($model, $message, $meta = null)
public function __construct($model, $message, $meta = null, $trans = null)
{
$this->model = $model;
$this->message = $message;
$this->meta = $meta;
$this->trans = $trans;
}
}
3 changes: 2 additions & 1 deletion src/Listeners/HistoryEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public function onModelChanged($event)
{
if(!HistoryObserver::filter(null)) return;

$message = $event->trans == null? $event->message : trans('panoscape::history.'.$event->trans, ['model' => static::getModelName($model), 'label' => $model->getModelLabel()]),
$event->model->morphMany(History::class, 'model')->create([
'message' => $event->message,
'message' => $message,
'meta' => $event->meta,
'user_id' => HistoryObserver::getUserID(),
'user_type' => HistoryObserver::getUserType(),
Expand Down

0 comments on commit 3e319da

Please sign in to comment.