1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare\Traits\ModelAdmin; |
4
|
|
|
|
5
|
|
|
use LaravelFlare\Flare\Events\ModelDelete; |
6
|
|
|
use LaravelFlare\Flare\Events\AfterDelete; |
7
|
|
|
use LaravelFlare\Flare\Events\BeforeDelete; |
8
|
|
|
use LaravelFlare\Flare\Events\ModelRestore; |
9
|
|
|
use LaravelFlare\Flare\Events\AfterRestore; |
10
|
|
|
use LaravelFlare\Flare\Events\BeforeRestore; |
11
|
|
|
use LaravelFlare\Flare\Events\ModelSoftDelete; |
12
|
|
|
use LaravelFlare\Flare\Exceptions\ModelAdminWriteableException as WriteableException; |
13
|
|
|
|
14
|
|
|
trait ModelSoftDeleting |
15
|
|
|
{ |
16
|
|
|
use ModelDeleting; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Overrides the ModelQuerying provided method |
20
|
|
|
* with one which searches withTrashed scope. |
21
|
|
|
* |
22
|
|
|
* @param int $modelitem_id |
23
|
|
|
* |
24
|
|
|
* @return |
25
|
|
|
*/ |
26
|
|
|
public function find($modelitem_id) |
27
|
|
|
{ |
28
|
|
|
return $this->findWithTrashed($modelitem_id); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Finds model and includes withTrashed() scope in query. |
33
|
|
|
* |
34
|
|
|
* @param int $modelitem_id |
35
|
|
|
* |
36
|
|
|
* @return |
37
|
|
|
*/ |
38
|
|
|
public function findWithTrashed($modelitem_id) |
39
|
|
|
{ |
40
|
|
|
$this->model = $this->model->withTrashed()->findOrFail($modelitem_id); |
41
|
|
|
|
42
|
|
|
return $this->model; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Finds model and includes onlyTrashed() scope in query. |
47
|
|
|
* |
48
|
|
|
* @param int $modelitem_id |
49
|
|
|
* |
50
|
|
|
* @return |
51
|
|
|
*/ |
52
|
|
|
public function findOnlyTrashed($modelitem_id) |
53
|
|
|
{ |
54
|
|
|
$this->model = $this->model->onlyTrashed()->findOrFail($modelitem_id); |
55
|
|
|
|
56
|
|
|
return $this->model; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Method fired before the Delete action is undertaken. |
61
|
|
|
* |
62
|
|
|
* @return |
63
|
|
|
*/ |
64
|
|
|
protected function beforeDelete() |
65
|
|
|
{ |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Delete Action. |
70
|
|
|
* |
71
|
|
|
* Fires off beforeDelete(), doDelete() and afterDelete() |
72
|
|
|
* |
73
|
|
|
* @param int $modelitem_id |
74
|
|
|
* |
75
|
|
|
* @return |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function delete($modelitem_id) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
event(new BeforeDelete($this)); |
80
|
|
|
|
81
|
|
|
$this->findWithTrashed($modelitem_id); |
82
|
|
|
|
83
|
|
|
$this->beforeDelete(); |
84
|
|
|
|
85
|
|
|
$this->doDelete(); |
86
|
|
|
|
87
|
|
|
$this->afterDelete(); |
88
|
|
|
|
89
|
|
|
event(new AfterDelete($this)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* The actual delete action. |
94
|
|
|
* |
95
|
|
|
* @return |
96
|
|
|
*/ |
97
|
|
|
private function doDelete() |
98
|
|
|
{ |
99
|
|
|
if (!$this->model->trashed()) { |
100
|
|
|
$this->model->delete(); |
101
|
|
|
event(new ModelSoftDelete($this)); |
102
|
|
|
|
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->model->forceDelete(); |
107
|
|
|
event(new ModelDelete($this)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Method fired after the Delete action is complete. |
112
|
|
|
* |
113
|
|
|
* @return |
114
|
|
|
*/ |
115
|
|
|
protected function afterDelete() |
116
|
|
|
{ |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Method fired before the Restore action is undertaken. |
121
|
|
|
* |
122
|
|
|
* @return |
123
|
|
|
*/ |
124
|
|
|
protected function beforeRestore() |
125
|
|
|
{ |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Restore Action. |
130
|
|
|
* |
131
|
|
|
* Fires off beforeRestore(), doRestore() and afterRestore() |
132
|
|
|
* |
133
|
|
|
* @param int $modelitem_id |
134
|
|
|
* |
135
|
|
|
* @return |
136
|
|
|
*/ |
137
|
|
|
public function restore($modelitem_id) |
138
|
|
|
{ |
139
|
|
|
event(new BeforeRestore($this)); |
140
|
|
|
|
141
|
|
|
$this->findOnlyTrashed($modelitem_id); |
142
|
|
|
|
143
|
|
|
$this->beforeRestore(); |
144
|
|
|
|
145
|
|
|
$this->doRestore(); |
146
|
|
|
|
147
|
|
|
$this->afterRestore(); |
148
|
|
|
|
149
|
|
|
event(new AfterRestore($this)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* The actual restore action. |
154
|
|
|
* |
155
|
|
|
* @return |
156
|
|
|
*/ |
157
|
|
|
private function doRestore() |
158
|
|
|
{ |
159
|
|
|
$this->model->restore(); |
160
|
|
|
|
161
|
|
|
event(new ModelRestore($this)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Method fired after the Restore action is complete. |
166
|
|
|
* |
167
|
|
|
* @return |
168
|
|
|
*/ |
169
|
|
|
protected function afterRestore() |
170
|
|
|
{ |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.