PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.11
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.11
2.1.0 2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 All 42 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Database / Orm / SoftDeletes.php

SoftDeletes.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.11, at vendor/wpfluent/framework/src/WPFluent/Database/Orm/SoftDeletes.php

217 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\Framework\Database\Orm;
4
5 use FluentBoards\Framework\Support\Helper;
6
7 /**
8 * @method static static|\FluentBoards\Framework\Database\Orm\Builder|\FluentBoards\Framework\Database\Query\Builder withTrashed(bool $withTrashed = true)
9 * @method static static|\FluentBoards\Framework\Database\Orm\Builder|\FluentBoards\Framework\Database\Query\Builder onlyTrashed()
10 * @method static static|\FluentBoards\Framework\Database\Orm\Builder|\FluentBoards\Framework\Database\Query\Builder withoutTrashed()
11 */
12 trait SoftDeletes
13 {
14 /**
15 * Indicates if the model is currently force deleting.
16 *
17 * @var bool
18 */
19 protected $forceDeleting = false;
20
21 /**
22 * Boot the soft deleting trait for a model.
23 *
24 * @return void
25 */
26 public static function bootSoftDeletes()
27 {
28 static::addGlobalScope(new SoftDeletingScope);
29 }
30
31 /**
32 * Initialize the soft deleting trait for an instance.
33 *
34 * @return void
35 */
36 public function initializeSoftDeletes()
37 {
38 if (! isset($this->casts[$this->getDeletedAtColumn()])) {
39 $this->casts[$this->getDeletedAtColumn()] = 'datetime';
40 }
41 }
42
43 /**
44 * Force a hard delete on a soft deleted model.
45 *
46 * @return bool|null
47 */
48 public function forceDelete()
49 {
50 $this->forceDeleting = true;
51
52 return Helper::tap($this->delete(), function ($deleted) {
53 $this->forceDeleting = false;
54
55 if ($deleted) {
56 $this->fireModelEvent('forceDeleted', false);
57 }
58 });
59 }
60
61 /**
62 * Perform the actual delete query on this model instance.
63 *
64 * @return mixed
65 */
66 protected function performDeleteOnModel()
67 {
68 if ($this->forceDeleting) {
69 return Helper::tap($this->setKeysForSaveQuery($this->newModelQuery())->forceDelete(), function () {
70 $this->exists = false;
71 });
72 }
73
74 return $this->runSoftDelete();
75 }
76
77 /**
78 * Perform the actual delete query on this model instance.
79 *
80 * @return void
81 */
82 protected function runSoftDelete()
83 {
84 $query = $this->setKeysForSaveQuery($this->newModelQuery());
85
86 $time = $this->freshTimestamp();
87
88 $columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)];
89
90 $this->{$this->getDeletedAtColumn()} = $time;
91
92 if ($this->timestamps && ! is_null($this->getUpdatedAtColumn())) {
93 $this->{$this->getUpdatedAtColumn()} = $time;
94
95 $columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
96 }
97
98 $query->update($columns);
99
100 $this->syncOriginalAttributes(array_keys($columns));
101
102 $this->fireModelEvent('trashed', false);
103 }
104
105 /**
106 * Restore a soft-deleted model instance.
107 *
108 * @return bool|null
109 */
110 public function restore()
111 {
112 // If the restoring event does not return false, we will proceed with this
113 // restore operation. Otherwise, we bail out so the developer will stop
114 // the restore totally. We will clear the deleted timestamp and save.
115 if ($this->fireModelEvent('restoring') === false) {
116 return false;
117 }
118
119 $this->{$this->getDeletedAtColumn()} = null;
120
121 // Once we have saved the model, we will fire the "restored" event so this
122 // developer will do anything they need to after a restore operation is
123 // totally finished. Then we will return the result of the save call.
124 $this->exists = true;
125
126 $result = $this->save();
127
128 $this->fireModelEvent('restored', false);
129
130 return $result;
131 }
132
133 /**
134 * Determine if the model instance has been soft-deleted.
135 *
136 * @return bool
137 */
138 public function trashed()
139 {
140 return ! is_null($this->{$this->getDeletedAtColumn()});
141 }
142
143 /**
144 * Register a "softDeleted" model event callback with the dispatcher.
145 *
146 * @param \Closure|string $callback
147 * @return void
148 */
149 public static function softDeleted($callback)
150 {
151 static::registerModelEvent('trashed', $callback);
152 }
153
154 /**
155 * Register a "restoring" model event callback with the dispatcher.
156 *
157 * @param \Closure|string $callback
158 * @return void
159 */
160 public static function restoring($callback)
161 {
162 static::registerModelEvent('restoring', $callback);
163 }
164
165 /**
166 * Register a "restored" model event callback with the dispatcher.
167 *
168 * @param \Closure|string $callback
169 * @return void
170 */
171 public static function restored($callback)
172 {
173 static::registerModelEvent('restored', $callback);
174 }
175
176 /**
177 * Register a "forceDeleted" model event callback with the dispatcher.
178 *
179 * @param \Closure|string $callback
180 * @return void
181 */
182 public static function forceDeleted($callback)
183 {
184 static::registerModelEvent('forceDeleted', $callback);
185 }
186
187 /**
188 * Determine if the model is currently force deleting.
189 *
190 * @return bool
191 */
192 public function isForceDeleting()
193 {
194 return $this->forceDeleting;
195 }
196
197 /**
198 * Get the name of the "deleted at" column.
199 *
200 * @return string
201 */
202 public function getDeletedAtColumn()
203 {
204 return defined('static::DELETED_AT') ? static::DELETED_AT : 'deleted_at';
205 }
206
207 /**
208 * Get the fully qualified "deleted at" column.
209 *
210 * @return string
211 */
212 public function getQualifiedDeletedAtColumn()
213 {
214 return $this->qualifyColumn($this->getDeletedAtColumn());
215 }
216 }
217