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