diff --git a/app/Enums/EventHistoryType.php b/app/Enums/EventHistoryType.php new file mode 100644 index 0000000..844082f --- /dev/null +++ b/app/Enums/EventHistoryType.php @@ -0,0 +1,29 @@ + trans('Création'), + static::moved => trans('Déplacement'), + static::changed => trans('Modification'), + static::destroyed => trans('Suppression'), + static::labelled => trans('Etiquette'), + default => trans('MISSING LABEL') + }; + } +} diff --git a/app/Listeners/CreateIntoArcadia.php b/app/Listeners/CreateIntoArcadia.php index 064c2d4..fb7a1ad 100644 --- a/app/Listeners/CreateIntoArcadia.php +++ b/app/Listeners/CreateIntoArcadia.php @@ -2,6 +2,7 @@ namespace App\Listeners; +use App\Enums\EventHistoryType; use App\Events\MediaLinkWithArcadia; use App\Lib\ArcadiaLinkUtilities; use App\Models\Historique; @@ -21,7 +22,7 @@ class CreateIntoArcadia $media = $event->getMedia(); $historique = new Historique(); - $historique->type = trans('Modification'); + $historique->type = EventHistoryType::changed; $historique->description = trans('Ajout de la liaison Arcadia'); $historique->media()->associate($media); $historique->user()->associate($user); diff --git a/app/Listeners/HistorizeMediaEvent.php b/app/Listeners/HistorizeMediaEvent.php index ed43abe..2fa79bd 100644 --- a/app/Listeners/HistorizeMediaEvent.php +++ b/app/Listeners/HistorizeMediaEvent.php @@ -2,6 +2,7 @@ namespace App\Listeners; +use App\Enums\EventHistoryType; use App\Events\MediaChangedLabel; use App\Events\MediaCreated; use App\Events\MediaDestroyed; @@ -20,29 +21,27 @@ class HistorizeMediaEvent */ public function handle(MediaCreated $event) { - $historique = new Historique(); - - // TODO: put enum in here. - $type = trans('Création'); + $historique = new Historique(); + $type = EventHistoryType::created; $description = trans('Média ajouté'); if ($event instanceof MediaMoved) { - $type = trans('Déplacement'); + $type = EventHistoryType::moved; $description = $event->getMedia()->dossier_id > 0 ? trans('Déplacement dans le dossier :libelle', [ 'libelle' => $event->getMedia()->dossier->libelle ]) : trans('Média déplacé à la racine'); } if ($event instanceof MediaUpdated) { - $type = trans('Modification'); + $type = EventHistoryType::changed; $description = trans('Le média a été mis à jour'); } if ($event instanceof MediaDestroyed) { - $type = trans('Suppression'); + $type = EventHistoryType::destroyed; $description = trans('Le média a été supprimé'); } if ($event instanceof MediaChangedLabel) { - $type = trans('Etiquette'); + $type = EventHistoryType::labelled; $description = sprintf( '%s -> %s', \optional($event->getOldLabel())->libelle ?? trans('aucune'), diff --git a/app/Models/Historique.php b/app/Models/Historique.php index a78e038..1cd005d 100644 --- a/app/Models/Historique.php +++ b/app/Models/Historique.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Enums\EventHistoryType; use Illuminate\Database\Eloquent\Model; class Historique extends Model @@ -13,6 +14,15 @@ class Historique extends Model */ public $timestamps = false; + /** + * The attributes that should be cast. + * + * @var array + */ + public $casts = [ + 'type' => EventHistoryType::class + ]; + // * RELATIONSHIPS /** diff --git a/database/migrations/2022_07_25_110851_change_history_type_on_historiques.php b/database/migrations/2022_07_25_110851_change_history_type_on_historiques.php new file mode 100644 index 0000000..496a092 --- /dev/null +++ b/database/migrations/2022_07_25_110851_change_history_type_on_historiques.php @@ -0,0 +1,65 @@ +map(function (Historique $historique) { + if (in_array(strtolower($historique->getRawOriginal('type')), ['retouche'])) { + $historique->forceDelete(); + return; + } + $historique->type = match (strtolower($historique->getRawOriginal('type'))) { + 'création' => EventHistoryType::created, + 'déplacement' => EventHistoryType::moved, + 'modification' => EventHistoryType::changed, + 'suppression' => EventHistoryType::destroyed, + 'etiquette' => EventHistoryType::labelled, + default => throw new \RuntimeException($historique->getRawOriginal('type') . ' not handled') + }; + $historique->setRawAttributes(array_merge($historique->toArray(), [ + 'type' => $historique->type->value + ])); + return $historique; + })->filter()->map(function (Historique $historique) { + $historique->saveOrFail(); + }); + Schema::table('historiques', function (Blueprint $table) { + $table->unsignedInteger('type')->default(EventHistoryType::created->value) + ->comment('Enum EventHistoryType')->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Historique::all()->map(function (Historique $historique) { + $historique->type = match ($historique->getRawOriginal('type')) { + EventHistoryType::created->value => 'Création', + EventHistoryType::changed->value => 'Modification', + EventHistoryType::destroyed->value => 'Suppression', + EventHistoryType::labelled->value => 'Etiquette', + EventHistoryType::moved->value => 'Déplacement', + default => throw new \RuntimeException($historique->getRawOriginal('type') . 'not handled') + }; + }); + Schema::table('historiques', function (Blueprint $table) { + $table->string('type')->default('')->change(); + }); + } +};