1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\DataTables; |
4
|
|
|
|
5
|
|
|
use App\User; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Yajra\DataTables\Services\DataTable; |
8
|
|
|
use Spatie\Activitylog\Models\Activity; |
9
|
|
|
|
10
|
|
|
class ActivityLogDataTable extends DataTable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Build DataTable class. |
14
|
|
|
* |
15
|
|
|
* @return \Yajra\DataTables\DataTableAbstract |
16
|
|
|
*/ |
17
|
|
|
public function dataTable($query) |
18
|
|
|
{ |
19
|
|
|
return datatables($query) |
20
|
|
View Code Duplication |
->editColumn('causer_id', function ($activity) { |
|
|
|
|
21
|
|
|
if ($activity->causer_id && is_object($activity->causer)) { |
22
|
|
|
return '<a href="' . route($this->getRouteFromType($activity->causer_type), $activity->causer_id) . '">' . $activity->causer->name ?? '' . '</a>'; |
23
|
|
|
} else { |
24
|
|
|
return ''; |
25
|
|
|
} |
26
|
|
|
}) |
27
|
|
View Code Duplication |
->editColumn('subject_id', function ($activity) { |
|
|
|
|
28
|
|
|
if ($activity->subject_id && is_object($activity->subject)) { |
29
|
|
|
return '<a href="' . route($this->getRouteFromType($activity->subject_type), $activity->subject_id) . '">' . $activity->subject->name ?? '' . '</a>'; |
30
|
|
|
} else { |
31
|
|
|
return ''; |
32
|
|
|
} |
33
|
|
|
}) |
34
|
|
|
->editColumn('properties', function ($activity) { |
35
|
|
|
return $activity->properties; |
36
|
|
|
}) |
37
|
|
|
->editColumn('created_at', function ($activity) { |
38
|
|
|
if ($activity->created_at->diffInDays() > 0) { |
39
|
|
|
return $activity->created_at->setTimezone(Auth::user()->timezone)->format('M d, Y h:i a'); |
40
|
|
|
} else { |
41
|
|
|
return $activity->created_at->diffForHumans(); |
42
|
|
|
} |
43
|
|
|
}) |
44
|
|
|
->rawColumns(['causer_id', 'subject_id', 'properties']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getRouteFromType($type) { |
48
|
|
|
switch ($type) { |
49
|
|
|
case "App\Device": |
50
|
|
|
return 'device.show'; |
51
|
|
|
case "App\Deviceimage": |
52
|
|
|
return 'image.device'; |
53
|
|
|
case "App\Location": |
54
|
|
|
return 'location.show'; |
55
|
|
|
case "App\Sensor": |
56
|
|
|
return 'sensor.show'; |
57
|
|
|
case "App\SensorData": |
58
|
|
|
return 'sensordata.show'; |
59
|
|
|
case "App\Site": |
60
|
|
|
return 'site.show'; |
61
|
|
|
case "App\User": |
62
|
|
|
return 'user.show'; |
63
|
|
|
default: |
64
|
|
|
return 'logs'; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get query source of dataTable. |
70
|
|
|
* |
71
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
72
|
|
|
*/ |
73
|
|
|
public function query() |
74
|
|
|
{ |
75
|
|
|
$query = Activity::query(); |
76
|
|
|
return $this->applyScopes($query); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Optional method if you want to use html builder. |
81
|
|
|
* |
82
|
|
|
* @return \Yajra\DataTables\Html\Builder |
83
|
|
|
*/ |
84
|
|
|
public function html() |
85
|
|
|
{ |
86
|
|
|
return $this->builder() |
87
|
|
|
->columns($this->getColumns()) |
88
|
|
|
->minifiedAjax() |
89
|
|
|
->parameters($this->getBuilderParameters()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get columns. |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
protected function getColumns() |
98
|
|
|
{ |
99
|
|
|
return [ |
100
|
|
|
'created_at', |
101
|
|
|
[ 'name' => 'causer_id', 'data' => 'causer_id', 'title' => 'Actor', 'render' => null, 'searchable' => true, 'orderable' => true, 'exportable' => true, 'printable' => true, 'footer' => '' ], |
102
|
|
|
[ 'name' => 'description', 'data' => 'description', 'title' => 'Action', 'render' => null, 'searchable' => true, 'orderable' => true, 'exportable' => true, 'printable' => true, 'footer' => '' ], |
103
|
|
|
[ 'name' => 'subject_id', 'data' => 'subject_id', 'title' => 'Subject', 'render' => null, 'searchable' => true, 'orderable' => true, 'exportable' => true, 'printable' => true, 'footer' => '' ], |
104
|
|
|
[ 'name' => 'properties', 'data' => 'properties', 'title' => 'Changes', 'render' => null, 'searchable' => true, 'orderable' => false, 'exportable' => true, 'printable' => true, 'footer' => '' ] |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get builder parameters. |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
protected function getBuilderParameters() |
114
|
|
|
{ |
115
|
|
|
return [ |
116
|
|
|
'dom' => 'Bfrtip', |
117
|
|
|
'order' => [ [ 0, 'desc' ] ], |
118
|
|
|
'buttons' => [ |
119
|
|
|
[ 'extend' => 'collection', 'text' => '<i class="fa fa-file-excel-o"></i> Export', 'buttons' => [ |
120
|
|
|
[ 'extend' => 'csv', 'exportOptions' => [ 'modifier' => [ 'search' => true ] ] ], |
121
|
|
|
[ 'extend' => 'excel', 'exportOptions' => [ 'modifier' => [ 'search' => true ] ] ], |
122
|
|
|
] ], |
123
|
|
|
[ 'extend' => 'print', 'exportOptions' => [ 'modifier' => [ 'search' => true ] ] ], |
124
|
|
|
'reset', |
125
|
|
|
'reload', |
126
|
|
|
], |
127
|
|
|
'paging' => true, |
128
|
|
|
'searching' => true, |
129
|
|
|
'info' => true, |
130
|
|
|
'searchDelay' => 500, |
131
|
|
|
]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get filename for export. |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
protected function filename() |
140
|
|
|
{ |
141
|
|
|
return 'activitylog_'.time(); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.