PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.8.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.8.0
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Database / Orm / SoftDeletes.php

SoftDeletes.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.8.0, at vendor/wpfluent/framework/src/WPFluent/Database/Orm/SoftDeletes.php

270 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Framework\Database\Orm;
4
5 use FluentCommunity\Framework\Support\Helper;
6 use FluentCommunity\Framework\Support\Collection as BaseCollection;
7 use FluentCommunity\Framework\Database\Orm\Collection as OrmCollection;
8
9 /**
10 * @method static static|\FluentCommunity\Framework\Database\Orm\Builder|\FluentCommunity\Framework\Database\Query\Builder withTrashed(bool $withTrashed = true)
11 * @method static static|\FluentCommunity\Framework\Database\Orm\Builder|\FluentCommunity\Framework\Database\Query\Builder onlyTrashed()
12 * @method static static|\FluentCommunity\Framework\Database\Orm\Builder|\FluentCommunity\Framework\Database\Query\Builder withoutTrashed()
13 */
14 trait SoftDeletes
15 {
16 /**
17 * Indicates if the model is currently force deleting.
18 *
19 * @var bool
20 */
21 protected $forceDeleting = false;
22
23 /**
24 * Boot the soft deleting trait for a model.
25 *
26 * @return void
27 */
28 public static function bootSoftDeletes()
29 {
30 static::addGlobalScope(new SoftDeletingScope);
31 }
32
33 /**
34 * Initialize the soft deleting trait for an instance.
35 *
36 * @return void
37 */
38 public function initializeSoftDeletes()
39 {
40 if (! isset($this->casts[$this->getDeletedAtColumn()])) {
41 $this->casts[$this->getDeletedAtColumn()] = 'datetime';
42 }
43 }
44
45 /**
46 * Force a hard delete on a soft deleted model.
47 *
48 * @return bool|null
49 */
50 public function forceDelete()
51 {
52 $this->forceDeleting = true;
53
54 return Helper::tap($this->delete(), function ($deleted) {
55 $this->forceDeleting = false;
56
57 if ($deleted) {
58 $this->fireModelEvent('forceDeleted', false);
59 }
60 });
61 }
62
63 /**
64 * Force a hard delete on a soft deleted model without raising any events.
65 *
66 * @return bool|null
67 */
68 public function forceDeleteQuietly()
69 {
70 return static::withoutEvents(function() {
71 $this->forceDelete();
72 });
73 }
74
75 /**
76 * Destroy the models for the given IDs.
77 *
78 * @param \FluentCommunity\Framework\Support\Collection|array|int|string $ids
79 * @return int
80 */
81 public static function forceDestroy($ids)
82 {
83 if ($ids instanceof OrmCollection) {
84 $ids = $ids->modelKeys();
85 }
86
87 if ($ids instanceof BaseCollection) {
88 $ids = $ids->all();
89 }
90
91 $ids = is_array($ids) ? $ids : func_get_args();
92
93 if (count($ids) === 0) {
94 return 0;
95 }
96
97 // We will actually pull the models from the database table and call
98 // delete on each of them individually so that their events get fired
99 // properly with a correct set of attributes in case the developers
100 // wants to check these.
101 $key = ($instance = new static)->getKeyName();
102
103 $count = 0;
104
105 foreach ($instance->withTrashed()->whereIn($key, $ids)->get() as $model) {
106 if ($model->forceDelete()) {
107 $count++;
108 }
109 }
110
111 return $count;
112 }
113
114 /**
115 * Perform the actual delete query on this model instance.
116 *
117 * @return mixed
118 */
119 protected function performDeleteOnModel()
120 {
121 if ($this->forceDeleting) {
122 return Helper::tap($this->setKeysForSaveQuery($this->newModelQuery())->forceDelete(), function () {
123 $this->exists = false;
124 });
125 }
126
127 return $this->runSoftDelete();
128 }
129
130 /**
131 * Perform the actual delete query on this model instance.
132 *
133 * @return void
134 */
135 protected function runSoftDelete()
136 {
137 $query = $this->setKeysForSaveQuery($this->newModelQuery());
138
139 $time = $this->freshTimestamp();
140
141 $columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)];
142
143 $this->{$this->getDeletedAtColumn()} = $time;
144
145 if ($this->timestamps && ! is_null($this->getUpdatedAtColumn())) {
146 $this->{$this->getUpdatedAtColumn()} = $time;
147
148 $columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
149 }
150
151 $query->update($columns);
152
153 $this->syncOriginalAttributes(array_keys($columns));
154
155 $this->fireModelEvent('trashed', false);
156 }
157
158 /**
159 * Restore a soft-deleted model instance.
160 *
161 * @return bool|null
162 */
163 public function restore()
164 {
165 // If the restoring event does not return false, we will proceed with this
166 // restore operation. Otherwise, we bail out so the developer will stop
167 // the restore totally. We will clear the deleted timestamp and save.
168 if ($this->fireModelEvent('restoring') === false) {
169 return false;
170 }
171
172 $this->{$this->getDeletedAtColumn()} = null;
173
174 // Once we have saved the model, we will fire the "restored" event so this
175 // developer will do anything they need to after a restore operation is
176 // totally finished. Then we will return the result of the save call.
177 $this->exists = true;
178
179 $result = $this->save();
180
181 $this->fireModelEvent('restored', false);
182
183 return $result;
184 }
185
186 /**
187 * Determine if the model instance has been soft-deleted.
188 *
189 * @return bool
190 */
191 public function trashed()
192 {
193 return ! is_null($this->{$this->getDeletedAtColumn()});
194 }
195
196 /**
197 * Register a "softDeleted" model event callback with the dispatcher.
198 *
199 * @param \Closure|string $callback
200 * @return void
201 */
202 public static function softDeleted($callback)
203 {
204 static::registerModelEvent('trashed', $callback);
205 }
206
207 /**
208 * Register a "restoring" model event callback with the dispatcher.
209 *
210 * @param \Closure|string $callback
211 * @return void
212 */
213 public static function restoring($callback)
214 {
215 static::registerModelEvent('restoring', $callback);
216 }
217
218 /**
219 * Register a "restored" model event callback with the dispatcher.
220 *
221 * @param \Closure|string $callback
222 * @return void
223 */
224 public static function restored($callback)
225 {
226 static::registerModelEvent('restored', $callback);
227 }
228
229 /**
230 * Register a "forceDeleted" model event callback with the dispatcher.
231 *
232 * @param \Closure|string $callback
233 * @return void
234 */
235 public static function forceDeleted($callback)
236 {
237 static::registerModelEvent('forceDeleted', $callback);
238 }
239
240 /**
241 * Determine if the model is currently force deleting.
242 *
243 * @return bool
244 */
245 public function isForceDeleting()
246 {
247 return $this->forceDeleting;
248 }
249
250 /**
251 * Get the name of the "deleted at" column.
252 *
253 * @return string
254 */
255 public function getDeletedAtColumn()
256 {
257 return defined('static::DELETED_AT') ? static::DELETED_AT : 'deleted_at';
258 }
259
260 /**
261 * Get the fully qualified "deleted at" column.
262 *
263 * @return string
264 */
265 public function getQualifiedDeletedAtColumn()
266 {
267 return $this->qualifyColumn($this->getDeletedAtColumn());
268 }
269 }
270