forked from octobercms/library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoftDelete.php
More file actions
325 lines (278 loc) · 9.41 KB
/
SoftDelete.php
File metadata and controls
325 lines (278 loc) · 9.41 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php namespace October\Rain\Database\Traits;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Database\Eloquent\Collection as CollectionBase;
use Illuminate\Support\Arr;
use October\Rain\Database\Scopes\SoftDeleteScope;
/**
* SoftDelete trait for flagging models as deleted instead of actually deleting them.
*
* @package october\database
* @author Alexey Bobkov, Samuel Georges
*/
trait SoftDelete
{
/**
* @var bool forceDeleting indicates if the model is currently force deleting.
*/
protected $forceDeleting = false;
/**
* bootSoftDelete trait for a model.
*/
public static function bootSoftDelete()
{
static::addGlobalScope(new SoftDeleteScope);
static::softDeleted(function($model) {
/**
* @event model.afterTrash
* Called after the model is soft deleted (trashed)
*
* Example usage:
*
* $model->bindEvent('model.afterTrash', function() use (\October\Rain\Database\Model $model) {
* \Log::info("{$model->name} has been trashed!");
* });
*
*/
$model->fireEvent('model.afterTrash');
if ($model->methodExists('afterTrash')) {
$model->afterTrash();
}
});
static::restoring(function($model) {
/**
* @event model.beforeRestore
* Called before the model is restored from a soft delete
*
* Example usage:
*
* $model->bindEvent('model.beforeRestore', function() use (\October\Rain\Database\Model $model) {
* \Log::info("{$model->name} is going to be restored!");
* });
*
*/
$model->fireEvent('model.beforeRestore');
if ($model->methodExists('beforeRestore')) {
$model->beforeRestore();
}
});
static::restored(function($model) {
/**
* @event model.afterRestore
* Called after the model is restored from a soft delete
*
* Example usage:
*
* $model->bindEvent('model.afterRestore', function() use (\October\Rain\Database\Model $model) {
* \Log::info("{$model->name} has been brought back to life!");
* });
*
*/
$model->fireEvent('model.afterRestore');
if ($model->methodExists('afterRestore')) {
$model->afterRestore();
}
});
}
/**
* isSoftDelete helper method to check if the model is currently
* being hard or soft deleted, useful in events.
* @return bool
*/
public function isSoftDelete()
{
return !$this->forceDeleting;
}
/**
* forceDelete on a soft deleted model.
*/
public function forceDelete()
{
$this->forceDeleting = true;
$this->delete();
$this->forceDeleting = false;
}
/**
* performDeleteOnModel performs the actual delete query on this model instance.
*/
protected function performDeleteOnModel()
{
if ($this->forceDeleting || !$this->isSoftDeleteEnabled()) {
$this->performDeleteOnRelations();
$this->setKeysForSaveQuery($this->newQuery()->withTrashed())->forceDelete();
$this->exists = false;
}
else {
$this->performSoftDeleteOnRelations();
$this->runSoftDelete();
}
}
/**
* performSoftDeleteOnRelations locates relations with softDelete flag and
* cascades the delete event.
*/
protected function performSoftDeleteOnRelations()
{
$definitions = $this->getRelationDefinitions();
foreach ($definitions as $type => $relations) {
foreach ($relations as $name => $options) {
if (!Arr::get($options, 'softDelete', false)) {
continue;
}
if (!$relation = $this->{$name}) {
continue;
}
if ($relation instanceof EloquentModel) {
$relation->delete();
}
elseif ($relation instanceof CollectionBase) {
$relation->each(function ($model) {
$model->delete();
});
}
}
}
}
/**
* runSoftDelete performs the actual delete query on this model instance.
*/
protected function runSoftDelete()
{
$query = $this->setKeysForSaveQuery($this->newQuery());
$time = $this->freshTimestamp();
$columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)];
$this->{$this->getDeletedAtColumn()} = $time;
if ($this->timestamps && ! is_null($this->getUpdatedAtColumn())) {
$this->{$this->getUpdatedAtColumn()} = $time;
$columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
}
$query->update($columns);
$this->syncOriginalAttributes(array_keys($columns));
$this->fireModelEvent('trashed', false);
}
/**
* restore a soft-deleted model instance.
* @return bool|null
*/
public function restore()
{
// If the restoring event does not return false, we will proceed with this
// restore operation. Otherwise, we bail out so the developer will stop
// the restore totally. We will clear the deleted timestamp and save.
if ($this->fireModelEvent('restoring') === false) {
return false;
}
$this->performRestoreOnRelations();
$this->{$this->getDeletedAtColumn()} = null;
// Once we have saved the model, we will fire the "restored" event so this
// developer will do anything they need to after a restore operation is
// totally finished. Then we will return the result of the save call.
$this->exists = true;
$result = $this->save();
$this->fireModelEvent('restored', false);
return $result;
}
/**
* performRestoreOnRelations locates relations with softDelete flag and cascades
* the restore event.
*/
protected function performRestoreOnRelations()
{
$definitions = $this->getRelationDefinitions();
foreach ($definitions as $type => $relations) {
foreach ($relations as $name => $options) {
if (!Arr::get($options, 'softDelete', false)) {
continue;
}
$relation = $this->{$name}()->onlyTrashed()->getResults();
if (!$relation) {
continue;
}
if ($relation instanceof EloquentModel) {
$relation->restore();
}
elseif ($relation instanceof CollectionBase) {
$relation->each(function ($model) {
$model->restore();
});
}
}
}
}
/**
* trashed determines if the model instance has been soft-deleted.
* @return bool
*/
public function trashed()
{
return !is_null($this->{$this->getDeletedAtColumn()});
}
/**
* withTrashed gets a new query builder that includes soft deletes.
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public static function withTrashed()
{
return with(new static)->newQueryWithoutScope(new SoftDeleteScope);
}
/**
* onlyTrashed gets a new query builder that only includes soft deletes.
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public static function onlyTrashed()
{
$instance = new static;
$column = $instance->getQualifiedDeletedAtColumn();
return $instance->newQueryWithoutScope(new SoftDeleteScope)->whereNotNull($column);
}
/**
* softDeleted registers a "trashed" model event callback with the dispatcher.
* @param \Closure|string $callback
* @return void
*/
public static function softDeleted($callback)
{
static::registerModelEvent('trashed', $callback);
}
/**
* restoring registers a restoring model event with the dispatcher.
* @param \Closure|string $callback
* @return void
*/
public static function restoring($callback)
{
static::registerModelEvent('restoring', $callback);
}
/**
* restored registers a restored model event with the dispatcher.
* @param \Closure|string $callback
* @return void
*/
public static function restored($callback)
{
static::registerModelEvent('restored', $callback);
}
/**
* isSoftDeleteEnabled allows for programmatic toggling
* @return bool
*/
public function isSoftDeleteEnabled()
{
return true;
}
/**
* getDeletedAtColumn gets the name of the "deleted at" column.
* @return string
*/
public function getDeletedAtColumn()
{
return defined('static::DELETED_AT') ? static::DELETED_AT : 'deleted_at';
}
/**
* getQualifiedDeletedAtColumn gets the fully qualified "deleted at" column.
* @return string
*/
public function getQualifiedDeletedAtColumn()
{
return $this->qualifyColumn($this->getDeletedAtColumn());
}
}