1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Audit\Models; |
4
|
|
|
|
5
|
|
|
use DB; |
6
|
|
|
use Facilitador; |
7
|
|
|
use Config; |
8
|
|
|
use PedreiroURL; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
use Pedreiro\Template\Input\Search; |
11
|
|
|
use Muleta\Library\Utils\Text; |
12
|
|
|
use Illuminate\Database\Eloquent\Model; |
13
|
|
|
use Audit\Models\Base; |
14
|
|
|
use App\Models\User; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Reperesents a single model change event. Typically a single CRUD action on |
18
|
|
|
* a model. |
19
|
|
|
*/ |
20
|
|
|
class Change extends Base |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The query param key used when previewing |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
const QUERY_KEY = 'view-change'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The attributes that should be cast to native types. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $casts = [ |
35
|
|
|
'changed' => 'array', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
protected $fillable = [ |
39
|
|
|
'admin', |
40
|
|
|
'key', |
41
|
|
|
'changeable_id', |
42
|
|
|
'changed', |
43
|
|
|
'deleted', |
44
|
|
|
|
45
|
|
|
'action', |
46
|
|
|
'model', |
47
|
|
|
'model_title', |
48
|
|
|
'date', |
49
|
|
|
'title', |
50
|
|
|
'meta', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the admin associated with the change |
55
|
|
|
* |
56
|
|
|
* @return Illuminate\Database\Eloquent\Relations\Relation |
57
|
|
|
*/ |
58
|
|
|
public function admin() |
59
|
|
|
{ |
60
|
|
|
return $this->belongsTo(\Illuminate\Support\Facades\Config::get('sitec.core.models.user', \App\Models\User::class)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The polymorphic relation back to the parent model |
65
|
|
|
* |
66
|
|
|
* @var mixed |
67
|
|
|
*/ |
68
|
|
|
public function loggable() |
69
|
|
|
{ |
70
|
|
|
return $this->morphTo('loggable', 'model', 'key'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the related model, including trashed instances |
75
|
|
|
* |
76
|
|
|
* @return Model |
77
|
|
|
*/ |
78
|
|
|
public function changedModel() |
79
|
|
|
{ |
80
|
|
|
return $this->loggable()->withTrashed(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Default ordering by descending time, designed to be overridden |
85
|
|
|
* |
86
|
|
|
* @param Illuminate\Database\Query\Builder $query |
87
|
|
|
* @return Illuminate\Database\Query\Builder |
88
|
|
|
*/ |
89
|
|
|
public function scopeOrdered($query) |
90
|
|
|
{ |
91
|
|
|
return $query->orderBy('changes.id', 'desc')->with('admin'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Don't log changes |
96
|
|
|
* |
97
|
|
|
* @param string $action |
98
|
|
|
* @return boolean |
99
|
|
|
*/ |
100
|
|
|
public function shouldLogChange($action) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* A convenience method for saving a change instance |
107
|
|
|
* |
108
|
|
|
* @param Model $model The model being touched |
109
|
|
|
* @param string $action Generally a CRUD verb: "created", "updated", "deleted" |
110
|
|
|
* @param User $admin The admin acting on the record |
111
|
|
|
* @return static|void |
112
|
|
|
*/ |
113
|
|
|
public static function log(Model $model, $action, User $admin = null) |
114
|
|
|
{ |
115
|
|
|
// Create a new change instance |
116
|
|
|
if (static::shouldWriteChange($model, $action)) { |
|
|
|
|
117
|
|
|
$changed = static::getChanged($model, $action); |
|
|
|
|
118
|
|
|
$change = static::createLog($model, $action, $admin, $changed); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Log published / unpblished changes |
122
|
|
|
static::logPublishingChange($model, $action, $admin); |
123
|
|
|
|
124
|
|
|
// If the action was a deletion, mark all of the records for this model as |
125
|
|
|
// deleted |
126
|
|
|
if ($action == 'deleted') { |
127
|
|
|
DB::table('changes') |
128
|
|
|
->where('model', get_class($model)) |
129
|
|
|
->where('key', $model->getKey()) |
130
|
|
|
->update(['deleted' => 1]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Return the changed instance |
134
|
|
|
if (isset($change)) { |
135
|
|
|
return $change; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Don't log changes when the only thing that changed was the published |
141
|
|
|
* state or updated timestamp. We check if there are any attributes |
142
|
|
|
* besides these that changed. |
143
|
|
|
* |
144
|
|
|
* @param Model $model The model being touched |
145
|
|
|
* @param string $action |
146
|
|
|
* @return boolean |
147
|
|
|
*/ |
148
|
|
|
static private function shouldWriteChange(Model $model, $action) |
149
|
|
|
{ |
150
|
|
|
if (in_array($action, ['created', 'deleted'])) { return true; |
151
|
|
|
} |
152
|
|
|
$changed_attributes = array_keys($model->getDirty()); |
153
|
|
|
$ignored = ['updated_at', 'public']; |
154
|
|
|
$loggable = array_diff($changed_attributes, $ignored); |
155
|
|
|
return count($loggable) > 0; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the changes attributes |
160
|
|
|
* |
161
|
|
|
* @param Model $model The model being touched |
162
|
|
|
* @param string $action |
163
|
|
|
* @return array|null |
164
|
|
|
*/ |
165
|
|
|
static private function getChanged(Model $model, $action) |
166
|
|
|
{ |
167
|
|
|
$changed = $model->getDirty(); |
168
|
|
|
if ($action == 'deleted' || empty($changed)) { |
169
|
|
|
$changed = null; |
170
|
|
|
} |
171
|
|
|
return $changed; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Create a change entry |
176
|
|
|
* |
177
|
|
|
* @param Model $model Th |
178
|
|
|
* @param string $action |
179
|
|
|
* @param User $admin |
180
|
|
|
*/ |
181
|
|
|
static protected function createLog( |
182
|
|
|
Model $model, |
183
|
|
|
$action, |
184
|
|
|
User $admin = null, |
185
|
|
|
$changed = null |
186
|
|
|
) { |
187
|
|
|
return static::create( |
|
|
|
|
188
|
|
|
[ |
189
|
|
|
'model' => get_class($model), |
190
|
|
|
'key' => $model->getKey(), |
191
|
|
|
'action' => $action, |
192
|
|
|
'title' => static::getModelTitle($model), |
193
|
|
|
'changed' => $changed, |
194
|
|
|
'changeable_id' => static::getAdminId($admin), |
195
|
|
|
] |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get the title of the model |
201
|
|
|
* |
202
|
|
|
* @param Model $model |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
static protected function getModelTitle(Model $model) |
206
|
|
|
{ |
207
|
|
|
return method_exists($model, 'getAdminTitleAttribute') ? |
208
|
|
|
$model->getAdminTitleAttribute() : null; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get the admin id |
213
|
|
|
* |
214
|
|
|
* @param User $admin |
215
|
|
|
* @return integer |
216
|
|
|
*/ |
217
|
|
|
static protected function getAdminId(User $admin = null) |
218
|
|
|
{ |
219
|
|
|
if (!$admin) { |
220
|
|
|
$admin = app('facilitador.user'); |
221
|
|
|
} |
222
|
|
|
return $admin ? $admin->getKey() : null; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Log changes to publishing state. The initial publish should be logged |
227
|
|
|
* but not an initil unpublished state. |
228
|
|
|
* |
229
|
|
|
* @param Model $model |
230
|
|
|
* @param string $action |
231
|
|
|
* @param User $admin |
232
|
|
|
* @return void |
233
|
|
|
*/ |
234
|
|
|
static public function logPublishingChange( |
235
|
|
|
Model $model, |
236
|
|
|
$action, |
237
|
|
|
User $admin = null |
238
|
|
|
) { |
239
|
|
|
if ($model->isDirty('public')) { |
240
|
|
|
if ($model->public) { |
241
|
|
|
static::createLog($model, 'published', $admin); |
242
|
|
|
} else if (!$model->public && $action != 'created') { |
243
|
|
|
static::createLog($model, 'unpublished', $admin); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Return a list of all the actions currently being used as a hash for use |
250
|
|
|
* in a select menu |
251
|
|
|
* |
252
|
|
|
* @return array |
253
|
|
|
*/ |
254
|
|
|
public static function getActions() |
255
|
|
|
{ |
256
|
|
|
return static::groupBy('action')->pluck('action', 'action') |
257
|
|
|
->mapWithKeys( |
258
|
|
|
function ($item) { |
259
|
|
|
return [$item => __("facilitador::changes.actions.$item")]; |
260
|
|
|
} |
261
|
|
|
); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Return a list of all the admins as a hash for use in a select menu |
266
|
|
|
* |
267
|
|
|
* @return array |
268
|
|
|
*/ |
269
|
|
|
public static function getAdmins() |
270
|
|
|
{ |
271
|
|
|
return User::all(['id', 'email'])->pluck('email', 'id'); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Format the the activity like a sentence |
276
|
|
|
* |
277
|
|
|
* @return string HTML |
278
|
|
|
*/ |
279
|
|
|
public function getAdminTitleHtmlAttribute() |
280
|
|
|
{ |
281
|
|
|
return __( |
282
|
|
|
'facilitador::changes.admin_title', [ |
283
|
|
|
'admin' => $this->getAdminLinkAttribute(), |
284
|
|
|
'action' => $this->getActionLabelAttribute(), |
285
|
|
|
'model' => $this->getModelNameHtmlAttribute(), |
286
|
|
|
'model_title' => $this->getLinkedTitleAttribute(), |
287
|
|
|
'date' => $this->getDateAttribute() |
288
|
|
|
] |
289
|
|
|
); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Get the admin name and link |
294
|
|
|
* |
295
|
|
|
* @return string HTML |
296
|
|
|
*/ |
297
|
|
|
public function getAdminLinkAttribute() |
298
|
|
|
{ |
299
|
|
|
if ($this->changeable_id) { |
|
|
|
|
300
|
|
|
return sprintf( |
301
|
|
|
'<a href="%s">%s</a>', |
302
|
|
|
$this->filterUrl(['changeable_id' => $this->changeable_id]), |
|
|
|
|
303
|
|
|
$this->admin->getAdminTitleHtmlAttribute() |
|
|
|
|
304
|
|
|
); |
305
|
|
|
} else { |
306
|
|
|
return 'Someone'; |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Format the activity as a colored label |
312
|
|
|
* |
313
|
|
|
* @return string HTML |
314
|
|
|
*/ |
315
|
|
|
public function getActionLabelAttribute() |
316
|
|
|
{ |
317
|
|
|
$map = [ |
318
|
|
|
'created' => 'success', |
319
|
|
|
'updated' => 'warning', |
320
|
|
|
'deleted' => 'danger', |
321
|
|
|
'published' => 'info', |
322
|
|
|
'unpublished' => 'default', |
323
|
|
|
]; |
324
|
|
|
|
325
|
|
|
return sprintf( |
326
|
|
|
'<a href="%s" class="label label-%s">%s</a>', |
327
|
|
|
$this->filterUrl(['action' => $this->action]), |
|
|
|
|
328
|
|
|
isset($map[$this->action]) ? $map[$this->action] : 'info', |
|
|
|
|
329
|
|
|
__("facilitador::changes.actions.$this->action") |
|
|
|
|
330
|
|
|
); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Format the model name by translating it through the controllers's defined |
335
|
|
|
* title |
336
|
|
|
* |
337
|
|
|
* @return string HTML |
338
|
|
|
*/ |
339
|
|
|
public function getModelNameHtmlAttribute() |
340
|
|
|
{ |
341
|
|
|
$class = Facilitador::controllerForModel($this->model); |
|
|
|
|
342
|
|
|
|
343
|
|
|
// There is not a controller for the model |
344
|
|
|
if (!$class || !class_exists($class)) { |
345
|
|
|
return sprintf( |
346
|
|
|
'<b><a href="%s">%s</a></b>', |
347
|
|
|
$this->filterUrl(['model' => $this->model]), |
|
|
|
|
348
|
|
|
preg_replace('#(?<!\ )[A-Z]#', ' $0', $this->model) |
|
|
|
|
349
|
|
|
); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
// There is a corresponding controller class |
353
|
|
|
$controller = new $class; |
354
|
|
|
return sprintf( |
355
|
|
|
'<b class="js-tooltip" title="%s"><a href="%s">%s</a></b>', |
356
|
|
|
htmlentities($controller->description()), |
357
|
|
|
$this->filterUrl(['model' => $this->model]), |
|
|
|
|
358
|
|
|
Str::singular($controller->title()) |
359
|
|
|
); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Get the title of the model. Perhaps in the future there will be more smarts |
364
|
|
|
* here, like generating a link to the edit view |
365
|
|
|
* |
366
|
|
|
* @return string HTML |
367
|
|
|
*/ |
368
|
|
|
public function getLinkedTitleAttribute() |
369
|
|
|
{ |
370
|
|
|
if (!$this->title) { return; |
|
|
|
|
371
|
|
|
} |
372
|
|
|
return sprintf( |
373
|
|
|
'<a href="%s">"%s"</a>', |
374
|
|
|
$this->filterUrl(['model' => $this->model, 'key' => $this->key]), |
|
|
|
|
375
|
|
|
$this->title |
|
|
|
|
376
|
|
|
); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Get the date of the change |
381
|
|
|
* |
382
|
|
|
* @return string HTML |
383
|
|
|
*/ |
384
|
|
|
public function getDateAttribute() |
385
|
|
|
{ |
386
|
|
|
\Carbon\Carbon::setLocale(Facilitador::locale()); |
387
|
|
|
if (is_null($this->created_at)) { |
|
|
|
|
388
|
|
|
return '-'; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return sprintf( |
392
|
|
|
'<a href="%s" class="js-tooltip" title="%s">%s</a>', |
393
|
|
|
$this->filterUrl(['created_at' => $this->created_at->format('m/d/Y')]), |
|
|
|
|
394
|
|
|
$this->getHumanDateAttribute(), |
395
|
|
|
$this->created_at->diffForHumans() |
|
|
|
|
396
|
|
|
); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Get the human readable date |
401
|
|
|
* |
402
|
|
|
* @return string |
403
|
|
|
*/ |
404
|
|
|
public function getHumanDateAttribute() |
405
|
|
|
{ |
406
|
|
|
$format = __('pedreiro::changes.human_date'); |
407
|
|
|
if (is_null($format)) { |
408
|
|
|
$format = 'd \d\e F \d\e Y \à\s h\hi'; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
if (is_null($this->created_at)) { |
|
|
|
|
412
|
|
|
return '-'; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
return $this->created_at->format($format); |
|
|
|
|
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Customize the action links |
420
|
|
|
* |
421
|
|
|
* @param array $data The data passed to a listing view |
422
|
|
|
* @return array |
423
|
|
|
*/ |
424
|
|
|
public function makeAdminActions($data) |
|
|
|
|
425
|
|
|
{ |
426
|
|
|
return array_filter( |
427
|
|
|
[ |
428
|
|
|
$this->filter_action, |
|
|
|
|
429
|
|
|
$this->changes_action, |
|
|
|
|
430
|
|
|
$this->preview_action, |
|
|
|
|
431
|
|
|
] |
432
|
|
|
); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Make the preview filter icon |
437
|
|
|
* |
438
|
|
|
* @return string |
439
|
|
|
*/ |
440
|
|
|
public function getFilterActionAttribute() |
441
|
|
|
{ |
442
|
|
|
return sprintf( |
443
|
|
|
'<a href="%s" |
444
|
|
|
class="glyphicon glyphicon-filter js-tooltip" |
445
|
|
|
title="' . __('pedreiro::changes.standard_list.filter') . '" |
446
|
|
|
data-placement="left"></a>', |
447
|
|
|
$this->filterUrl(['model' => $this->model, 'key' => $this->key]), |
|
|
|
|
448
|
|
|
strip_tags($this->getModelNameHtmlAttribute()) |
449
|
|
|
); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Make a link to filter the result set |
454
|
|
|
* |
455
|
|
|
* @return string |
456
|
|
|
*/ |
457
|
|
|
public function filterUrl($query) |
458
|
|
|
{ |
459
|
|
|
return PedreiroURL::action('changes').'?'.Search::query($query); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Make the changes icon |
464
|
|
|
* |
465
|
|
|
* @return string |
466
|
|
|
*/ |
467
|
|
|
public function getChangesActionAttribute() |
468
|
|
|
{ |
469
|
|
|
// If there are changes, add the modal button |
470
|
|
|
if ($this->changed) { |
|
|
|
|
471
|
|
|
return sprintf( |
472
|
|
|
'<a href="%s" |
473
|
|
|
class="glyphicon glyphicon-export js-tooltip changes-modal-link" |
474
|
|
|
title="%s" data-placement="left"></a>', |
475
|
|
|
PedreiroURL::action('changes@edit', $this->id), |
|
|
|
|
476
|
|
|
__('pedreiro::changes.standard_list.view') |
477
|
|
|
); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
// Else, show a disabled button |
481
|
|
|
else { |
482
|
|
|
return sprintf( |
483
|
|
|
'<span |
484
|
|
|
class="glyphicon glyphicon-export js-tooltip" |
485
|
|
|
title="%s" data-placement="left"></span>', __('pedreiro::changes.standard_list.no_changed') |
486
|
|
|
); |
487
|
|
|
} |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* Make link to preview a version as long as the model has a URI and the |
492
|
|
|
* action wasn't a delete action. |
493
|
|
|
* |
494
|
|
|
* @return string |
495
|
|
|
*/ |
496
|
|
|
public function getPreviewActionAttribute() |
497
|
|
|
{ |
498
|
|
|
if ($this->changedModel |
|
|
|
|
499
|
|
|
&& $this->changedModel->uri |
|
|
|
|
500
|
|
|
&& $this->action != 'deleted' |
|
|
|
|
501
|
|
|
) { |
502
|
|
|
return sprintf( |
503
|
|
|
'<a href="%s" target="_blank" |
504
|
|
|
class="glyphicon glyphicon-bookmark js-tooltip" |
505
|
|
|
title="%s" data-placement="left"></a>', |
506
|
|
|
$this->preview_url, |
|
|
|
|
507
|
|
|
__('pedreiro::changes.standard_list.preview') |
508
|
|
|
); |
509
|
|
|
} else { |
510
|
|
|
return '<span class="glyphicon glyphicon-bookmark disabled"></span>'; |
511
|
|
|
} |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* Make the preview URL for a the model |
516
|
|
|
* |
517
|
|
|
* @return string |
518
|
|
|
*/ |
519
|
|
|
public function getPreviewUrlAttribute() |
520
|
|
|
{ |
521
|
|
|
return vsprintf( |
522
|
|
|
'%s?%s=%s', [ |
523
|
|
|
$this->changedModel->uri, |
|
|
|
|
524
|
|
|
static::QUERY_KEY, |
525
|
|
|
$this->id, |
|
|
|
|
526
|
|
|
] |
527
|
|
|
); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
/** |
531
|
|
|
* Get just the attributes that should be displayed in the admin modal. |
532
|
|
|
* |
533
|
|
|
* @return array |
534
|
|
|
*/ |
535
|
|
|
public function attributesForModal() |
536
|
|
|
{ |
537
|
|
|
// Remove some specific attributes. Leaving empties in there so the updating |
538
|
|
|
// of values to NULL is displayed. |
539
|
|
|
$attributes = array_except( |
540
|
|
|
$this->changed, [ |
|
|
|
|
541
|
|
|
'id', 'updated_at', 'created_at', 'password', 'remember_token', |
542
|
|
|
] |
543
|
|
|
); |
544
|
|
|
|
545
|
|
|
// Make more readable titles |
546
|
|
|
$out = []; |
547
|
|
|
foreach ($attributes as $key => $val) { |
548
|
|
|
$out[Text::titleFromKey($key)] = $val; |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
return $out; |
552
|
|
|
} |
553
|
|
|
} |
554
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.