1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victorlap\Approvable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
|
10
|
|
|
trait Approvable |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** @var array */ |
14
|
|
|
public $approveOf = array(); |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
public $dontApproveOf = array(); |
18
|
|
|
|
19
|
|
|
/** @var bool */ |
20
|
|
|
protected $withoutApproval = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Create the event listeners for the saving event |
24
|
|
|
* This lets us save approvals whenever a save is made, no matter the |
25
|
|
|
* http method |
26
|
|
|
*/ |
27
|
|
|
public static function bootApprovable(): void |
28
|
|
|
{ |
29
|
15 |
|
static::saving(function ($model) { |
30
|
15 |
|
return $model->preSave(); |
31
|
15 |
|
}); |
32
|
15 |
|
} |
33
|
|
|
|
34
|
5 |
|
public function approvals(): MorphMany |
35
|
|
|
{ |
36
|
5 |
|
return $this->morphMany(Approval::class, 'approvable'); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Check if this model has pending changes, |
41
|
|
|
* If an attribute is provided, check if the attribute has pending changes. |
42
|
|
|
* |
43
|
|
|
* @param null $attribute |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
2 |
|
public function isPendingApproval($attribute = null): bool |
47
|
|
|
{ |
48
|
2 |
|
return $this->approvals() |
49
|
2 |
|
->when($attribute !== null, function ($query) use ($attribute) { |
50
|
1 |
|
$query->where('key', $attribute); |
51
|
2 |
|
}) |
52
|
2 |
|
->where('approved', null) |
53
|
2 |
|
->exists(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* List all the attributes, that currently have pending changes. |
58
|
|
|
* |
59
|
|
|
* @return \Illuminate\Support\Collection |
60
|
|
|
*/ |
61
|
1 |
|
public function getPendingApprovalAttributes(): Collection |
62
|
|
|
{ |
63
|
1 |
|
return $this->approvals() |
64
|
1 |
|
->where('approved', null) |
65
|
1 |
|
->groupBy('key') |
66
|
1 |
|
->pluck('key'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Disable the approval process for this model instance. |
71
|
|
|
* |
72
|
|
|
* @param bool $withoutApproval |
73
|
|
|
*/ |
74
|
1 |
|
public function withoutApproval(bool $withoutApproval = true): void |
75
|
|
|
{ |
76
|
1 |
|
$this->withoutApproval = $withoutApproval; |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Invoked before a model is saved. Return false to abort the operation. |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
15 |
|
protected function preSave(): bool |
85
|
|
|
{ |
86
|
15 |
|
if ($this->withoutApproval) { |
87
|
1 |
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
15 |
|
if ($this->currentUserCanApprove()) { |
91
|
|
|
// If the user is able to approve edits, do nothing. |
92
|
3 |
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
13 |
|
if (!$this->exists) { |
|
|
|
|
96
|
|
|
// There is currently no way (implemented) to enable this for new models. |
97
|
13 |
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
11 |
|
$changes_to_record = $this->changedApprovableFields(); |
101
|
|
|
|
102
|
11 |
|
$approvals = array(); |
103
|
11 |
|
foreach ($changes_to_record as $key => $change) { |
104
|
11 |
|
$approvals[] = array( |
105
|
11 |
|
'approvable_type' => $this->getMorphClass(), |
|
|
|
|
106
|
11 |
|
'approvable_id' => $this->getKey(), |
|
|
|
|
107
|
11 |
|
'key' => $key, |
108
|
11 |
|
'value' => $change, |
109
|
11 |
|
'user_id' => $this->getSystemUserId(), |
110
|
11 |
|
'created_at' => new \DateTime(), |
111
|
11 |
|
'updated_at' => new \DateTime(), |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
11 |
|
if (count($approvals) > 0) { |
116
|
11 |
|
$approval = new Approval(); |
117
|
11 |
|
DB::table($approval->getTable())->insert($approvals); |
118
|
|
|
} |
119
|
|
|
|
120
|
11 |
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get all of the changes that have been made, that are also supposed |
125
|
|
|
* to be approved. |
126
|
|
|
* |
127
|
|
|
* @return array fields with new data, that should be recorded |
128
|
|
|
*/ |
129
|
11 |
|
private function changedApprovableFields(): array |
130
|
|
|
{ |
131
|
11 |
|
$dirty = $this->getDirty(); |
|
|
|
|
132
|
11 |
|
$changes_to_record = array(); |
133
|
|
|
|
134
|
11 |
|
foreach ($dirty as $key => $value) { |
135
|
11 |
|
if ($this->isApprovable($key)) { |
136
|
11 |
|
if (!isset($this->original[$key]) || $this->original[$key] != $this->attributes[$key]) { |
137
|
11 |
|
$changes_to_record[$key] = $value; |
138
|
|
|
|
139
|
|
|
// Reset changes that we want to approve |
140
|
11 |
|
if (!isset($this->original[$key])) { |
141
|
2 |
|
unset($this->attributes[$key]); |
142
|
|
|
} else { |
143
|
11 |
|
$this->attributes[$key] = $this->original[$key]; |
|
|
|
|
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
11 |
|
return $changes_to_record; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Return whether an attribute of this model should be approvable. |
154
|
|
|
* |
155
|
|
|
* @param string $key |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
11 |
|
private function isApprovable(string $key): bool |
159
|
|
|
{ |
160
|
11 |
|
if (isset($this->approveOf) && in_array($key, $this->approveOf)) { |
161
|
1 |
|
return true; |
162
|
|
|
} |
163
|
11 |
|
if (isset($this->dontApproveOf) && in_array($key, $this->dontApproveOf)) { |
164
|
1 |
|
return false; |
165
|
|
|
} |
166
|
|
|
|
167
|
11 |
|
return empty($this->approveOf); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get the user id that should be stored as the requester for the approval. |
172
|
|
|
* |
173
|
|
|
* @return int|null |
174
|
|
|
*/ |
175
|
11 |
|
protected function getSystemUserId(): ?int |
176
|
|
|
{ |
177
|
11 |
|
return Auth::id() ?? null; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Check if the approval process needs to happen for the currently logged in user. |
182
|
|
|
* |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
2 |
|
protected function currentUserCanApprove(): bool |
186
|
|
|
{ |
187
|
2 |
|
return Auth::check() && Auth::user()->can('approve', $this) ?? false; |
|
|
|
|
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.