mirror of
https://github.com/Kovah/LinkAce.git
synced 2025-03-20 06:39:38 +01:00
Refactor history entry components
This commit is contained in:
parent
6960ddf99e
commit
dbccdeb5b7
@ -49,7 +49,7 @@ class Setting extends Model implements Auditable
|
||||
'updated',
|
||||
];
|
||||
|
||||
public static array $auditModifiers = [
|
||||
public array $auditModifiers = [
|
||||
'archive_backups_enabled' => BooleanModifier::class,
|
||||
'archive_private_backups_enabled' => BooleanModifier::class,
|
||||
'darkmode_setting' => DarkmodeSettingModifier::class,
|
||||
|
@ -64,7 +64,7 @@ class User extends Authenticatable implements Auditable
|
||||
'email',
|
||||
];
|
||||
|
||||
public static array $auditModifiers = [];
|
||||
public array $auditModifiers = [];
|
||||
|
||||
/*
|
||||
* ========================================================================
|
||||
|
@ -30,7 +30,12 @@ use OwenIt\Auditing\Models\Audit;
|
||||
*/
|
||||
class LinkEntry extends Component
|
||||
{
|
||||
public function __construct(private Audit $entry, private array $changes = [])
|
||||
use ProcessesHistory;
|
||||
|
||||
private string $model = Link::class;
|
||||
private string $translateBase = 'link';
|
||||
|
||||
public function __construct(private Audit $entry)
|
||||
{
|
||||
}
|
||||
|
||||
@ -53,56 +58,4 @@ class LinkEntry extends Component
|
||||
'changes' => $this->changes,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function processChange(string $field, array $changeData): void
|
||||
{
|
||||
$fieldName = trans('link.' . $field);
|
||||
[$oldValue, $newValue] = $this->processValues($field, $changeData);
|
||||
|
||||
if ($oldValue === null) {
|
||||
$change = trans('linkace.history_added', [
|
||||
'fieldname' => $fieldName,
|
||||
'newvalue' => htmlspecialchars($newValue),
|
||||
]);
|
||||
} elseif ($newValue === null) {
|
||||
$change = trans('linkace.history_removed', [
|
||||
'fieldname' => $fieldName,
|
||||
'oldvalue' => htmlspecialchars($oldValue),
|
||||
]);
|
||||
} else {
|
||||
$change = trans('linkace.history_changed', [
|
||||
'fieldname' => $fieldName,
|
||||
'oldvalue' => htmlspecialchars($oldValue),
|
||||
'newvalue' => htmlspecialchars($newValue),
|
||||
]);
|
||||
}
|
||||
|
||||
$this->changes[] = $change;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply specialized methods for different fields to handle particular
|
||||
* formatting needs of these fields.
|
||||
*
|
||||
* @param string $field
|
||||
* @param array $changeData
|
||||
* @return array
|
||||
*/
|
||||
protected function processValues(string $field, array $changeData): array
|
||||
{
|
||||
$oldValue = $changeData['old'] ?? null;
|
||||
$newValue = $changeData['new'] ?? null;
|
||||
|
||||
/** @var Link $model */
|
||||
$model = app($this->entry->auditable_type);
|
||||
|
||||
if (isset($model->auditModifiers[$field])) {
|
||||
$modifier = app($model->auditModifiers[$field]);
|
||||
$oldValue = $modifier->modify($oldValue);
|
||||
$newValue = $modifier->modify($newValue);
|
||||
return [$oldValue, $newValue];
|
||||
}
|
||||
|
||||
return [$oldValue, $newValue];
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,12 @@ use OwenIt\Auditing\Models\Audit;
|
||||
|
||||
class ListEntry extends Component
|
||||
{
|
||||
public function __construct(private Audit $entry, private array $changes = [])
|
||||
use ProcessesHistory;
|
||||
|
||||
private string $model = LinkList::class;
|
||||
private string $translateBase = 'list';
|
||||
|
||||
public function __construct(private Audit $entry)
|
||||
{
|
||||
}
|
||||
|
||||
@ -31,56 +36,4 @@ class ListEntry extends Component
|
||||
'changes' => $this->changes,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function processChange(string $field, array $changeData): void
|
||||
{
|
||||
$fieldName = trans('list.' . $field);
|
||||
[$oldValue, $newValue] = $this->processValues($field, $changeData);
|
||||
|
||||
if ($oldValue === null) {
|
||||
$change = trans('linkace.history_added', [
|
||||
'fieldname' => $fieldName,
|
||||
'newvalue' => htmlspecialchars($newValue),
|
||||
]);
|
||||
} elseif ($newValue === null) {
|
||||
$change = trans('linkace.history_removed', [
|
||||
'fieldname' => $fieldName,
|
||||
'oldvalue' => htmlspecialchars($oldValue),
|
||||
]);
|
||||
} else {
|
||||
$change = trans('linkace.history_changed', [
|
||||
'fieldname' => $fieldName,
|
||||
'oldvalue' => htmlspecialchars($oldValue),
|
||||
'newvalue' => htmlspecialchars($newValue),
|
||||
]);
|
||||
}
|
||||
|
||||
$this->changes[] = $change;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply specialized methods for different fields to handle particular
|
||||
* formatting needs of these fields.
|
||||
*
|
||||
* @param string $field
|
||||
* @param array $changeData
|
||||
* @return array
|
||||
*/
|
||||
protected function processValues(string $field, array $changeData): array
|
||||
{
|
||||
$oldValue = $changeData['old'] ?? null;
|
||||
$newValue = $changeData['new'] ?? null;
|
||||
|
||||
/** @var LinkList $model */
|
||||
$model = app($this->entry->auditable_type);
|
||||
|
||||
if (isset($model->auditModifiers[$field])) {
|
||||
$modifier = app($model->auditModifiers[$field]);
|
||||
$oldValue = $modifier->modify($oldValue);
|
||||
$newValue = $modifier->modify($newValue);
|
||||
return [$oldValue, $newValue];
|
||||
}
|
||||
|
||||
return [$oldValue, $newValue];
|
||||
}
|
||||
}
|
||||
|
59
app/View/Components/History/ProcessesHistory.php
Normal file
59
app/View/Components/History/ProcessesHistory.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Components\History;
|
||||
|
||||
trait ProcessesHistory
|
||||
{
|
||||
protected array $changes = [];
|
||||
|
||||
protected function processChange(string $field, array $changeData): void
|
||||
{
|
||||
$fieldName = trans($this->translateBase . '.' . $field);
|
||||
[$oldValue, $newValue] = $this->processValues($field, $changeData);
|
||||
|
||||
if ($oldValue === null) {
|
||||
$change = trans('linkace.history_added', [
|
||||
'fieldname' => $fieldName,
|
||||
'newvalue' => htmlspecialchars($newValue),
|
||||
]);
|
||||
} elseif ($newValue === null) {
|
||||
$change = trans('linkace.history_removed', [
|
||||
'fieldname' => $fieldName,
|
||||
'oldvalue' => htmlspecialchars($oldValue),
|
||||
]);
|
||||
} else {
|
||||
$change = trans('linkace.history_changed', [
|
||||
'fieldname' => $fieldName,
|
||||
'oldvalue' => htmlspecialchars($oldValue),
|
||||
'newvalue' => htmlspecialchars($newValue),
|
||||
]);
|
||||
}
|
||||
|
||||
$this->changes[] = $change;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply specialized methods for different fields to handle particular
|
||||
* formatting needs of these fields.
|
||||
*
|
||||
* @param string $field
|
||||
* @param array $changeData
|
||||
* @return array
|
||||
*/
|
||||
protected function processValues(string $field, array $changeData): array
|
||||
{
|
||||
$oldValue = $changeData['old'] ?? null;
|
||||
$newValue = $changeData['new'] ?? null;
|
||||
|
||||
$model = app($this->model);
|
||||
|
||||
if (isset($model->auditModifiers[$field])) {
|
||||
$modifier = app($model->auditModifiers[$field]);
|
||||
$oldValue = $modifier->modify($oldValue);
|
||||
$newValue = $modifier->modify($newValue);
|
||||
return [$oldValue, $newValue];
|
||||
}
|
||||
|
||||
return [$oldValue, $newValue];
|
||||
}
|
||||
}
|
@ -8,7 +8,12 @@ use OwenIt\Auditing\Models\Audit;
|
||||
|
||||
class SettingsEntry extends Component
|
||||
{
|
||||
public function __construct(private Audit $entry, private array $changes = [])
|
||||
use ProcessesHistory;
|
||||
|
||||
private string $model = Setting::class;
|
||||
private string $translateBase = 'settings';
|
||||
|
||||
public function __construct(private Audit $entry)
|
||||
{
|
||||
}
|
||||
|
||||
@ -86,8 +91,10 @@ class SettingsEntry extends Component
|
||||
$field = 'share_service';
|
||||
}
|
||||
|
||||
if (isset(Setting::$auditModifiers[$field])) {
|
||||
$modifier = app(Setting::$auditModifiers[$field]);
|
||||
$model = app(Setting::class);
|
||||
|
||||
if (isset($model->auditModifiers[$field])) {
|
||||
$modifier = app($model->auditModifiers[$field]);
|
||||
$oldValue = $modifier->modify($oldValue);
|
||||
$newValue = $modifier->modify($newValue);
|
||||
return [$oldValue, $newValue];
|
||||
|
@ -8,7 +8,12 @@ use OwenIt\Auditing\Models\Audit;
|
||||
|
||||
class TagEntry extends Component
|
||||
{
|
||||
public function __construct(private Audit $entry, private array $changes = [])
|
||||
use ProcessesHistory;
|
||||
|
||||
private string $model = Tag::class;
|
||||
private string $translateBase = 'tag';
|
||||
|
||||
public function __construct(private Audit $entry)
|
||||
{
|
||||
}
|
||||
|
||||
@ -31,56 +36,4 @@ class TagEntry extends Component
|
||||
'changes' => $this->changes,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function processChange(string $field, array $changeData): void
|
||||
{
|
||||
$fieldName = trans('tag.' . $field);
|
||||
[$oldValue, $newValue] = $this->processValues($field, $changeData);
|
||||
|
||||
if ($oldValue === null) {
|
||||
$change = trans('linkace.history_added', [
|
||||
'fieldname' => $fieldName,
|
||||
'newvalue' => htmlspecialchars($newValue),
|
||||
]);
|
||||
} elseif ($newValue === null) {
|
||||
$change = trans('linkace.history_removed', [
|
||||
'fieldname' => $fieldName,
|
||||
'oldvalue' => htmlspecialchars($oldValue),
|
||||
]);
|
||||
} else {
|
||||
$change = trans('linkace.history_changed', [
|
||||
'fieldname' => $fieldName,
|
||||
'oldvalue' => htmlspecialchars($oldValue),
|
||||
'newvalue' => htmlspecialchars($newValue),
|
||||
]);
|
||||
}
|
||||
|
||||
$this->changes[] = $change;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply specialized methods for different fields to handle particular
|
||||
* formatting needs of these fields.
|
||||
*
|
||||
* @param string $field
|
||||
* @param array $changeData
|
||||
* @return array
|
||||
*/
|
||||
protected function processValues(string $field, array $changeData): array
|
||||
{
|
||||
$oldValue = $changeData['old'] ?? null;
|
||||
$newValue = $changeData['new'] ?? null;
|
||||
|
||||
/** @var Tag $model */
|
||||
$model = app($this->entry->auditable_type);
|
||||
|
||||
if (isset($model->auditModifiers[$field])) {
|
||||
$modifier = app($model->auditModifiers[$field]);
|
||||
$oldValue = $modifier->modify($oldValue);
|
||||
$newValue = $modifier->modify($newValue);
|
||||
return [$oldValue, $newValue];
|
||||
}
|
||||
|
||||
return [$oldValue, $newValue];
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,12 @@ use OwenIt\Auditing\Models\Audit;
|
||||
|
||||
class UserEntry extends Component
|
||||
{
|
||||
public function __construct(private Audit $entry, private array $changes = [])
|
||||
use ProcessesHistory;
|
||||
|
||||
private string $model = User::class;
|
||||
private string $translateBase = 'user';
|
||||
|
||||
public function __construct(private Audit $entry)
|
||||
{
|
||||
}
|
||||
|
||||
@ -25,6 +30,13 @@ class UserEntry extends Component
|
||||
} else {
|
||||
foreach ($this->entry->getModified() as $field => $change) {
|
||||
$this->processChange($field, $change);
|
||||
|
||||
$this->changes = array_map(function ($change) {
|
||||
return trans('audit.user_history_entry', [
|
||||
'id' => $this->entry->auditable->id,
|
||||
'change' => $change,
|
||||
]);
|
||||
}, $this->changes);
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,56 +45,4 @@ class UserEntry extends Component
|
||||
'changes' => $this->changes,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function processChange(string $field, array $changeData): void
|
||||
{
|
||||
$fieldName = trans('user.' . $field);
|
||||
[$oldValue, $newValue] = $this->processValues($field, $changeData);
|
||||
|
||||
if ($oldValue === null) {
|
||||
$change = trans('linkace.history_added', [
|
||||
'fieldname' => $fieldName,
|
||||
'newvalue' => htmlspecialchars($newValue),
|
||||
]);
|
||||
} elseif ($newValue === null) {
|
||||
$change = trans('linkace.history_removed', [
|
||||
'fieldname' => $fieldName,
|
||||
'oldvalue' => htmlspecialchars($oldValue),
|
||||
]);
|
||||
} else {
|
||||
$change = trans('linkace.history_changed', [
|
||||
'fieldname' => $fieldName,
|
||||
'oldvalue' => htmlspecialchars($oldValue),
|
||||
'newvalue' => htmlspecialchars($newValue),
|
||||
]);
|
||||
}
|
||||
|
||||
$this->changes[] = trans('audit.user_history_entry', [
|
||||
'id' => $this->entry->auditable->id,
|
||||
'change' => $change,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply specialized methods for different fields to handle particular
|
||||
* formatting needs of these fields.
|
||||
*
|
||||
* @param string $field
|
||||
* @param array $changeData
|
||||
* @return array
|
||||
*/
|
||||
protected function processValues(string $field, array $changeData): array
|
||||
{
|
||||
$oldValue = $changeData['old'] ?? null;
|
||||
$newValue = $changeData['new'] ?? null;
|
||||
|
||||
if (isset(User::$auditModifiers[$field])) {
|
||||
$modifier = app(User::$auditModifiers[$field]);
|
||||
$oldValue = $modifier->modify($oldValue);
|
||||
$newValue = $modifier->modify($newValue);
|
||||
return [$oldValue, $newValue];
|
||||
}
|
||||
|
||||
return [$oldValue, $newValue];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user