Skip to content

Commit 70bee5a

Browse files
committed
Basic start of the LogsChanges trait (for administrative changes)
1 parent 27d7449 commit 70bee5a

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

app/Models/Category.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Watson\Validating\ValidatingTrait;
1212
use App\Helpers\Helper;
1313
use Illuminate\Support\Str;
14+
use App\Models\Traits\LogsChanges;
1415

1516
/**
1617
* Model for Categories. Categories are a higher-level group
@@ -27,6 +28,7 @@ class Category extends SnipeModel
2728
protected $presenter = \App\Presenters\CategoryPresenter::class;
2829
use Presentable;
2930
use SoftDeletes;
31+
use LogsChanges;
3032

3133
protected $table = 'categories';
3234
protected $hidden = ['created_by', 'deleted_at'];

app/Models/Traits/LogsChanges.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace App\Models\Traits;
4+
5+
use App\Enums\ActionType;
6+
use App\Models\Actionlog;
7+
trait LogsChanges {
8+
9+
private ?ActionType $action_type = null;
10+
private array $meta = [];
11+
static function bootLogsChanges() {
12+
static::creating(function ($model) {
13+
\Log::error("CREATING!");
14+
$model->action_type = ActionType::Create;
15+
});
16+
17+
static::updating(function ($model) {
18+
\Log::error("UPDATING!");
19+
if(!$model->action_type) {
20+
\Log::error("No action type - so definitely doing 'update'!");
21+
$model->action_type = ActionType::Update;
22+
}
23+
});
24+
25+
static::restoring(function ($model) {
26+
$model->action_type = ActionType::Restore;
27+
});
28+
29+
/* The main functionality is here: */
30+
static::saving(function ($model) {
31+
\Log::error("recording changes.......");
32+
$model->record_changes();
33+
});
34+
35+
static::saved(function ($model) {
36+
\Log::error("SAVED!!!!");
37+
$model->add_action_log();
38+
});
39+
40+
static::deleted(function ($model) {
41+
$model->action_type = ActionType::Delete;
42+
$model->add_action_log();
43+
\Log::error("deleted!!!!!!!!!!!");
44+
});
45+
}
46+
47+
function record_changes()
48+
{
49+
$changed = [];
50+
51+
// something here with custom fields is needed? or will getRawOriginal et al just do that for us?
52+
foreach ($this->getRawOriginal() as $key => $value) { //on 'create' this doesn't write down the new attributes
53+
if ($this->getRawOriginal()[$key] != $this->getAttributes()[$key]) {
54+
$changed[$key]['old'] = $this->getRawOriginal()[$key];
55+
$changed[$key]['new'] = $this->getAttributes()[$key];
56+
57+
if (property_exists($this, 'hidden') && in_array($key, $this->hidden)) {
58+
$changed[$key]['old'] = '*************'; //FIXME deleted_at is hidden?!
59+
$changed[$key]['new'] = '*************';
60+
}
61+
}
62+
}
63+
$this->meta = $changed;
64+
}
65+
66+
function add_action_log()
67+
{
68+
if(!$this->action_type && !$this->meta) {
69+
\Log::warning("No action type set, and no changes to record. Not logging.");
70+
return;
71+
}
72+
if($this->action_type == ActionType::Update && !$this->meta) {
73+
\Log::warning("An update with no actual changes to record. Not logging");
74+
return;
75+
}
76+
$logAction = new Actionlog();
77+
$logAction->action_type = $this->action_type->value;
78+
$logAction->item()->associate($this);
79+
$logAction->created_at = date('Y-m-d H:i:s');
80+
$logAction->action_date = date('Y-m-d H:i:s');
81+
// target_id and target_type?
82+
// need IP and user-agent!!!!!
83+
$logAction->created_by = auth()->id();
84+
$logAction->log_meta = json_encode($this->meta); // this gets weird on 'create'
85+
if($logAction->save()) {
86+
//success! Reset for more actions later...
87+
$this->action_type = null;
88+
$this->meta = [];
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)