forked from octobercms/library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRevisionable.php
More file actions
202 lines (172 loc) · 5.29 KB
/
Revisionable.php
File metadata and controls
202 lines (172 loc) · 5.29 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php namespace October\Rain\Database\Traits;
use Db;
use Exception;
use DateTime;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Support\Arr;
/**
* Revisionable trait tracks changes to specific attributes
*
* @package october\database
* @author Alexey Bobkov, Samuel Georges
*/
trait Revisionable
{
/**
* @var array revisionable list of attributes to monitor for changes and store revisions for.
*
* protected $revisionable = [];
*/
/**
* @var int revisionableLimit is the maximum number of revision records to keep.
*
* public $revisionableLimit = 500;
*/
/**
* @var const REVISION_HISTORY changes the relation name used to store revisions.
*
* const REVISION_HISTORY = 'revision_history';
*/
/**
* @var bool revisionsEnabled flag for arbitrarily disabling revision history.
*/
public $revisionsEnabled = true;
/**
* initializeRevisionable trait for a model.
*/
public function initializeRevisionable()
{
if (!is_array($this->revisionable)) {
throw new Exception(sprintf(
'The $revisionable property in %s must be an array to use the Revisionable trait.',
static::class
));
}
$this->bindEvent('model.afterUpdate', function () {
$this->revisionableAfterUpdate();
});
$this->bindEvent('model.afterDelete', function () {
$this->revisionableAfterDelete();
});
}
/**
* revisionableAfterUpdate event
*/
public function revisionableAfterUpdate()
{
if (!$this->revisionsEnabled) {
return;
}
$relation = $this->getRevisionHistoryName();
$relationObject = $this->{$relation}();
$revisionModel = $relationObject->getRelated();
$toSave = [];
$dirty = $this->getDirty();
foreach ($dirty as $attribute => $value) {
if (!in_array($attribute, $this->revisionable)) {
continue;
}
$toSave[] = [
'field' => $attribute,
'old_value' => Arr::get($this->original, $attribute),
'new_value' => $value,
'revisionable_type' => $relationObject->getMorphClass(),
'revisionable_id' => $this->getKey(),
'user_id' => $this->revisionableGetUser(),
'cast' => $this->revisionableGetCastType($attribute),
'created_at' => new DateTime,
'updated_at' => new DateTime
];
}
// Nothing to do
if (!count($toSave)) {
return;
}
Db::table($revisionModel->getTable())->insert($toSave);
$this->revisionableCleanUp();
}
/**
* revisionableAfterDelete event
*/
public function revisionableAfterDelete()
{
if (!$this->revisionsEnabled) {
return;
}
$softDeletes = in_array(
\October\Rain\Database\Traits\SoftDelete::class,
class_uses_recursive(static::class)
);
if (!$softDeletes) {
return;
}
if (!in_array('deleted_at', $this->revisionable)) {
return;
}
$relation = $this->getRevisionHistoryName();
$relationObject = $this->{$relation}();
$revisionModel = $relationObject->getRelated();
$toSave = [
'field' => 'deleted_at',
'old_value' => null,
'new_value' => $this->deleted_at,
'revisionable_type' => $relationObject->getMorphClass(),
'revisionable_id' => $this->getKey(),
'user_id' => $this->revisionableGetUser(),
'created_at' => new DateTime,
'updated_at' => new DateTime
];
Db::table($revisionModel->getTable())->insert($toSave);
$this->revisionableCleanUp();
}
/*
* revisionableCleanUp deletes revision records exceeding the limit.
*/
protected function revisionableCleanUp()
{
$relation = $this->getRevisionHistoryName();
$relationObject = $this->{$relation}();
$revisionLimit = property_exists($this, 'revisionableLimit')
? (int) $this->revisionableLimit
: 500;
$toDelete = $relationObject
->orderBy('id', 'desc')
->skip($revisionLimit)
->limit(64)
->get();
foreach ($toDelete as $record) {
$record->delete();
}
}
/**
* revisionableGetCastType
*/
protected function revisionableGetCastType($attribute)
{
if (in_array($attribute, $this->getDates())) {
return 'date';
}
return null;
}
/**
* revisionableGetUser
*/
protected function revisionableGetUser()
{
if (method_exists($this, 'getRevisionableUser')) {
$user = $this->getRevisionableUser();
return $user instanceof EloquentModel
? $user->getKey()
: $user;
}
return null;
}
/**
* getRevisionHistoryName
* @return string
*/
public function getRevisionHistoryName()
{
return defined('static::REVISION_HISTORY') ? static::REVISION_HISTORY : 'revision_history';
}
}