1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel Platfourm package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\Platfourm\Database\Eloquent\Traits; |
12
|
|
|
|
13
|
|
|
use Longman\Platfourm\Contracts\Auth\AuthUserService; |
14
|
|
|
|
15
|
|
|
trait SoftDeletes |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Indicates if the model is currently force deleting. |
19
|
|
|
* |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected $forceDeleting = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Boot the soft deleting trait for a model. |
26
|
|
|
* |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public static function bootSoftDeletes() |
30
|
|
|
{ |
31
|
|
|
static::addGlobalScope(new SoftDeletingScope); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Force a hard delete on a soft deleted model. |
36
|
|
|
* |
37
|
|
|
* @return bool|null |
38
|
|
|
*/ |
39
|
|
|
public function forceDelete() |
40
|
|
|
{ |
41
|
|
|
$this->forceDeleting = true; |
42
|
|
|
|
43
|
|
|
$deleted = $this->delete(); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$this->forceDeleting = false; |
46
|
|
|
|
47
|
|
|
return $deleted; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Perform the actual delete query on this model instance. |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
protected function performDeleteOnModel() |
56
|
|
|
{ |
57
|
|
|
if ($this->forceDeleting) { |
58
|
|
|
return $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey())->forceDelete(); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this->runSoftDelete(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Perform the actual delete query on this model instance. |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
protected function runSoftDelete() |
70
|
|
|
{ |
71
|
|
|
$query = $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey()); |
|
|
|
|
72
|
|
|
|
73
|
|
|
$this->{$this->getDeletedAtColumn()} = $time = $this->freshTimestamp(); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$update = [ |
76
|
|
|
$this->getDeletedAtColumn() => $this->fromDateTime($time), |
|
|
|
|
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
$userService = app()->make(AuthUserService::class); |
80
|
|
|
if ($userService->check()) { |
81
|
|
|
$this->{$this->getDeletedByColumn()} = $userService->user()->id; |
82
|
|
|
$update[$this->getDeletedByColumn()] = $userService->user()->id; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$query->update($update); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Restore a soft-deleted model instance. |
90
|
|
|
* |
91
|
|
|
* @return bool|null |
92
|
|
|
*/ |
93
|
|
|
public function restore() |
94
|
|
|
{ |
95
|
|
|
// If the restoring event does not return false, we will proceed with this |
96
|
|
|
// restore operation. Otherwise, we bail out so the developer will stop |
97
|
|
|
// the restore totally. We will clear the deleted timestamp and save. |
98
|
|
|
if ($this->fireModelEvent('restoring') === false) { |
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->{$this->getDeletedAtColumn()} = null; |
103
|
|
|
$this->{$this->getDeletedByColumn()} = null; |
104
|
|
|
|
105
|
|
|
// Once we have saved the model, we will fire the "restored" event so this |
106
|
|
|
// developer will do anything they need to after a restore operation is |
107
|
|
|
// totally finished. Then we will return the result of the save call. |
108
|
|
|
$this->exists = true; |
|
|
|
|
109
|
|
|
|
110
|
|
|
$result = $this->save(); |
|
|
|
|
111
|
|
|
|
112
|
|
|
$this->fireModelEvent('restored', false); |
|
|
|
|
113
|
|
|
|
114
|
|
|
return $result; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Determine if the model instance has been soft-deleted. |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function trashed() |
123
|
|
|
{ |
124
|
|
|
return !is_null($this->{$this->getDeletedAtColumn()}); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get a new query builder that includes soft deletes. |
129
|
|
|
* |
130
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
131
|
|
|
*/ |
132
|
|
|
public static function withTrashed() |
133
|
|
|
{ |
134
|
|
|
return (new static)->newQueryWithoutScope(new SoftDeletingScope); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get a new query builder that only includes soft deletes. |
139
|
|
|
* |
140
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
141
|
|
|
*/ |
142
|
|
|
public static function onlyTrashed() |
143
|
|
|
{ |
144
|
|
|
$instance = new static; |
145
|
|
|
|
146
|
|
|
$column = $instance->getQualifiedDeletedAtColumn(); |
147
|
|
|
|
148
|
|
|
return $instance->newQueryWithoutScope(new SoftDeletingScope)->whereNotNull($column); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Register a restoring model event with the dispatcher. |
153
|
|
|
* |
154
|
|
|
* @param \Closure|string $callback |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
public static function restoring($callback) |
158
|
|
|
{ |
159
|
|
|
static::registerModelEvent('restoring', $callback); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Register a restored model event with the dispatcher. |
164
|
|
|
* |
165
|
|
|
* @param \Closure|string $callback |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
|
|
public static function restored($callback) |
169
|
|
|
{ |
170
|
|
|
static::registerModelEvent('restored', $callback); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the name of the "deleted at" column. |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function getDeletedAtColumn() |
179
|
|
|
{ |
180
|
|
|
return defined('static::DELETED_AT') ? static::DELETED_AT : 'deleted_at'; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get the name of the "deleted by" column. |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
public function getDeletedByColumn() |
189
|
|
|
{ |
190
|
|
|
return defined('static::DELETED_BY') ? static::DELETED_BY : 'deleted_by'; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get the fully qualified "deleted at" column. |
195
|
|
|
* |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public function getQualifiedDeletedAtColumn() |
199
|
|
|
{ |
200
|
|
|
return $this->getTable() . '.' . $this->getDeletedAtColumn(); |
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get the fully qualified "deleted by" column. |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
public function getQualifiedDeletedByColumn() |
209
|
|
|
{ |
210
|
|
|
return $this->getTable() . '.' . $this->getDeletedByColumn(); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.