Changed event history to type
This commit is contained in:
parent
01cef75e0c
commit
50a5aa5604
5 changed files with 113 additions and 9 deletions
29
app/Enums/EventHistoryType.php
Normal file
29
app/Enums/EventHistoryType.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum EventHistoryType: int
|
||||
{
|
||||
case created = 0;
|
||||
case moved = 1;
|
||||
case changed = 2;
|
||||
case destroyed = 3;
|
||||
case labelled = 4;
|
||||
|
||||
/**
|
||||
* Get num label
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
static::created => trans('Création'),
|
||||
static::moved => trans('Déplacement'),
|
||||
static::changed => trans('Modification'),
|
||||
static::destroyed => trans('Suppression'),
|
||||
static::labelled => trans('Etiquette'),
|
||||
default => trans('MISSING LABEL')
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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'),
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
use App\Enums\EventHistoryType;
|
||||
use App\Models\Historique;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Historique::all()->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();
|
||||
});
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue