-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathLogsActivity.php
More file actions
102 lines (86 loc) · 3.58 KB
/
LogsActivity.php
File metadata and controls
102 lines (86 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace App\Core;
use Illuminate\Support\HtmlString;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity as BaseLogsActivity;
use App\Models\User;
trait LogsActivity
{
use BaseLogsActivity;
/**
* Activity log options definition
*
* @return LogOptions
*/
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly($this->getFillable())
->setDescriptionForEvent(function(string $eventName) {
// Define attributes to exclude from the change log
$excludedAttributes = ['updated_at', 'created_at', 'deleted_at'];
// Get changes and original values
$changes = array_diff_key($this->getDirty(), array_flip($excludedAttributes));
$originals = array_diff_key($this->getOriginal(), array_flip($excludedAttributes));
// Generate changes description
$changesDescription = '';
foreach ($changes as $key => $value) {
$originalValue = $originals[$key] ?? 'null';
if($key=="responsible_id")
{
// Fetch the responsible user based on the current responsible_id
$responsibleUserOld = User::withTrashed()->find($originalValue);
// Get the responsible user's name if available
$responsibleUserNameOld = $responsibleUserOld ? $responsibleUserOld->name : 'None';
// Fetch the responsible user based on the current responsible_id
$responsibleUserNew = User::withTrashed()->find($value);
// Get the responsible user's name if available
$responsibleUserNameNew = $responsibleUserNew ? $responsibleUserNew->name : 'Unknown User';
$changesDescription .= "Responsible: {$responsibleUserNameOld} to {$responsibleUserNameNew} ";
continue;
}
$changesDescription .= "{$key}: '{$originalValue}' => '{$value}' ";
}
if($eventName == "created"){
$changesDescription = "" ;
}
// Generate the description
return new HtmlString(
'<div class="flex flex-col gap-1">'
. (auth()->user()->name ?? '')
. " "
. $eventName
. " "
. $this->fromCamelCase((new \ReflectionClass($this))->getShortName())
. " "
. $this
. "<br>"
. $changesDescription
. ' <a class="text-primary-500 hover:underline hover:cursor-pointer"
target="_blank"
href="' . $this->activityLogLink() . '">
' . __('See details')
. '</a>'
. '</div>'
);
});
}
/**
* Transform a camel case to normal case
*
* @param $input
* @return string
*/
private function fromCamelCase($input)
{
$pattern = '!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!';
preg_match_all($pattern, $input, $matches);
$ret = $matches[0];
foreach ($ret as &$match) {
$match = $match == strtoupper($match) ?
strtolower($match) :
lcfirst($match);
}
return implode(' ', $ret);
}
}