Skip to content

Commit 49497b7

Browse files
committed
Activity logs
1 parent db93a80 commit 49497b7

29 files changed

Lines changed: 595 additions & 43 deletions

app/Core/HasLogsActivity.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Core;
4+
5+
interface HasLogsActivity
6+
{
7+
8+
public function __toString(): string;
9+
10+
public function activityLogLink(): string;
11+
12+
}

app/Core/LogsActivity.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Core;
4+
5+
use Illuminate\Support\HtmlString;
6+
use Spatie\Activitylog\LogOptions;
7+
use Spatie\Activitylog\Traits\LogsActivity as BaseLogsActivity;
8+
9+
trait LogsActivity
10+
{
11+
12+
use BaseLogsActivity;
13+
14+
/**
15+
* Activity log options definition
16+
*
17+
* @return LogOptions
18+
*/
19+
public function getActivitylogOptions(): LogOptions
20+
{
21+
return LogOptions::defaults()
22+
->logOnly($this->getFillable())
23+
->setDescriptionForEvent(fn(string $eventName) => new HtmlString(
24+
'<div class="flex flex-col gap-1">'
25+
. auth()->user()->name . " " . $eventName . " " . $this->fromCamelCase((new \ReflectionClass($this))->getShortName()) . " " . $this
26+
. ' <a class="text-primary-500 hover:underline hover:cursor-pointer" target="_blank" href="' . $this->activityLogLink() . '">' . __('See details') . '</a>'
27+
. '</div>'
28+
));
29+
}
30+
31+
/**
32+
* Transform a camel case to normal case
33+
*
34+
* @param $input
35+
* @return string
36+
*/
37+
private function fromCamelCase($input)
38+
{
39+
$pattern = '!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!';
40+
preg_match_all($pattern, $input, $matches);
41+
$ret = $matches[0];
42+
foreach ($ret as &$match) {
43+
$match = $match == strtoupper($match) ?
44+
strtolower($match) :
45+
lcfirst($match);
46+
}
47+
return implode(' ', $ret);
48+
}
49+
50+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace App\Http\Livewire\Administration;
4+
5+
use App\Models\TicketPriority;
6+
use Filament\Forms\Components\Grid;
7+
use Filament\Forms\Components\TextInput;
8+
use Filament\Forms\Concerns\InteractsWithForms;
9+
use Filament\Forms\Contracts\HasForms;
10+
use Livewire\Component;
11+
use Spatie\Activitylog\Models\Activity;
12+
13+
class ActivityLogs extends Component implements HasForms
14+
{
15+
use InteractsWithForms;
16+
17+
public $search;
18+
19+
public function mount(): void
20+
{
21+
$this->form->fill();
22+
}
23+
24+
public function render()
25+
{
26+
$query = Activity::query();
27+
if ($this->search) {
28+
$query->where('description', 'like', '%' . $this->search . '%');
29+
}
30+
$logs = $query->paginate();
31+
return view('livewire.administration.activity-logs', compact('logs'));
32+
}
33+
34+
/**
35+
* Form schema definition
36+
*
37+
* @return array
38+
*/
39+
protected function getFormSchema(): array
40+
{
41+
return [
42+
Grid::make(1)
43+
->schema([
44+
TextInput::make('search')
45+
->label(__('Search for activity logs'))
46+
->disableLabel()
47+
->type('search')
48+
->placeholder(__('Search for activity logs')),
49+
]),
50+
];
51+
}
52+
53+
/**
54+
* Search for activity logs
55+
*
56+
* @return void
57+
*/
58+
public function search(): void
59+
{
60+
$data = $this->form->getState();
61+
$this->search = $data['search'] ?? null;
62+
}
63+
64+
}

app/Http/Livewire/Administration/TicketPriorities.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected function getFormSchema(): array
5656
}
5757

5858
/**
59-
* Search for tickets statutses
59+
* Search for tickets priorities
6060
*
6161
* @return void
6262
*/

app/Http/Livewire/Administration/TicketStatuses.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected function getFormSchema(): array
5555
}
5656

5757
/**
58-
* Search for tickets statutses
58+
* Search for tickets statuses
5959
*
6060
* @return void
6161
*/

app/Http/Livewire/Administration/TicketTypes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected function getFormSchema(): array
5656
}
5757

5858
/**
59-
* Search for tickets statutses
59+
* Search for tickets types
6060
*
6161
* @return void
6262
*/

app/Models/Comment.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
namespace App\Models;
44

5+
use App\Core\HasLogsActivity;
6+
use App\Core\LogsActivity;
57
use Illuminate\Database\Eloquent\Builder;
68
use Illuminate\Database\Eloquent\Factories\HasFactory;
79
use Illuminate\Database\Eloquent\Model;
810
use Illuminate\Database\Eloquent\Relations\BelongsTo;
911
use Illuminate\Database\Eloquent\SoftDeletes;
1012

11-
class Comment extends Model
13+
class Comment extends Model implements HasLogsActivity
1214
{
13-
use HasFactory, SoftDeletes;
15+
use HasFactory, SoftDeletes, LogsActivity;
1416

1517
protected $fillable = [
1618
'owner_id',
@@ -35,4 +37,14 @@ public function ticket(): BelongsTo
3537
{
3638
return $this->belongsTo(Ticket::class);
3739
}
40+
41+
public function __toString(): string
42+
{
43+
return $this->content;
44+
}
45+
46+
public function activityLogLink(): string
47+
{
48+
return route('tickets.number', $this->ticket->ticket_number);
49+
}
3850
}

app/Models/Project.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Models;
44

5+
use App\Core\HasLogsActivity;
6+
use App\Core\LogsActivity;
57
use Illuminate\Database\Eloquent\Builder;
68
use Illuminate\Database\Eloquent\Factories\HasFactory;
79
use Illuminate\Database\Eloquent\Model;
@@ -10,9 +12,9 @@
1012
use Illuminate\Database\Eloquent\Relations\HasMany;
1113
use Illuminate\Database\Eloquent\SoftDeletes;
1214

13-
class Project extends Model
15+
class Project extends Model implements HasLogsActivity
1416
{
15-
use HasFactory, SoftDeletes;
17+
use HasFactory, SoftDeletes, LogsActivity;
1618

1719
protected $fillable = [
1820
'name',
@@ -34,11 +36,6 @@ public function owner(): BelongsTo
3436
return $this->belongsTo(User::class, 'owner_id');
3537
}
3638

37-
public function users(): BelongsToMany
38-
{
39-
return $this->belongsToMany(User::class, 'user_projects', 'project_id', 'user_id')->withPivot('role');
40-
}
41-
4239
public function tickets(): HasMany
4340
{
4441
return $this->hasMany(Ticket::class);
@@ -52,4 +49,14 @@ public function favoriteUsers(): BelongsToMany
5249
}
5350
return $query;
5451
}
52+
53+
public function __toString(): string
54+
{
55+
return $this->name;
56+
}
57+
58+
public function activityLogLink(): string
59+
{
60+
return route('home');
61+
}
5562
}

app/Models/Ticket.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Models;
44

5+
use App\Core\HasLogsActivity;
6+
use App\Core\LogsActivity;
57
use Illuminate\Database\Eloquent\Builder;
68
use Illuminate\Database\Eloquent\Casts\Attribute;
79
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -10,9 +12,9 @@
1012
use Illuminate\Database\Eloquent\Relations\HasMany;
1113
use Illuminate\Database\Eloquent\SoftDeletes;
1214

13-
class Ticket extends Model
15+
class Ticket extends Model implements HasLogsActivity
1416
{
15-
use HasFactory, SoftDeletes;
17+
use HasFactory, SoftDeletes, LogsActivity;
1618

1719
protected $fillable = [
1820
'title',
@@ -67,4 +69,14 @@ public function ticketNumber(): Attribute
6769
get: fn() => $this->project?->ticket_prefix . '' . $this->number
6870
);
6971
}
72+
73+
public function __toString(): string
74+
{
75+
return $this->title;
76+
}
77+
78+
public function activityLogLink(): string
79+
{
80+
return route('tickets.number', $this->ticket_number);
81+
}
7082
}

app/Models/TicketPriority.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace App\Models;
44

5+
use App\Core\HasLogsActivity;
6+
use App\Core\LogsActivity;
57
use Illuminate\Database\Eloquent\Factories\HasFactory;
68
use Illuminate\Database\Eloquent\Model;
79
use Illuminate\Database\Eloquent\SoftDeletes;
810

9-
class TicketPriority extends Model
11+
class TicketPriority extends Model implements HasLogsActivity
1012
{
11-
use HasFactory, SoftDeletes;
13+
use HasFactory, SoftDeletes, LogsActivity;
1214

1315
protected $fillable = [
1416
'title',
@@ -17,4 +19,14 @@ class TicketPriority extends Model
1719
'icon',
1820
'slug'
1921
];
22+
23+
public function __toString(): string
24+
{
25+
return $this->title;
26+
}
27+
28+
public function activityLogLink(): string
29+
{
30+
return route('administration.ticket-priorities');
31+
}
2032
}

0 commit comments

Comments
 (0)