|
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', null) |
|
60
|
2 |
|
->exists(); |
|
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', null) |
|
72
|
1 |
|
->groupBy('key') |
|
73
|
1 |
|
->pluck('key'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Invoked before a model is saved. Return false to abort the operation. |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public function preSave() |
|
82
|
|
|
{ |
|
83
|
1 |
|
if ($this->currentUserCanApprove()) { |
|
84
|
|
|
// If the user is able to approve edits, do nothing. |
|
85
|
1 |
|
return true; |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (!$this->exists) { |
|
|
|
|
|
|
89
|
|
|
// There is currently no way (implemented) to enable this for new models. |
|
90
|
|
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$changes_to_record = $this->changedApprovableFields(); |
|
94
|
8 |
|
|
|
95
|
|
|
$approvals = array(); |
|
96
|
8 |
|
foreach ($changes_to_record as $key => $change) { |
|
97
|
|
|
$approvals[] = array( |
|
98
|
2 |
|
'approvable_type' => $this->getMorphClass(), |
|
|
|
|
|
|
99
|
|
|
'approvable_id' => $this->getKey(), |
|
|
|
|
|
|
100
|
|
|
'key' => $key, |
|
101
|
6 |
|
'value' => $change, |
|
102
|
|
|
'user_id' => $this->getSystemUserId(), |
|
103
|
6 |
|
'created_at' => new \DateTime(), |
|
104
|
|
|
'updated_at' => new \DateTime(), |
|
105
|
|
|
); |
|
106
|
5 |
|
} |
|
107
|
|
|
|
|
108
|
5 |
|
if (count($approvals) > 0) { |
|
109
|
5 |
|
$approval = new Approval(); |
|
110
|
5 |
|
DB::table($approval->getTable())->insert($approvals); |
|
111
|
5 |
|
} |
|
112
|
5 |
|
|
|
113
|
5 |
|
return true; |
|
114
|
5 |
|
} |
|
115
|
5 |
|
|
|
116
|
5 |
|
/** |
|
117
|
5 |
|
* Get all of the changes that have been made, that are also supposed |
|
118
|
|
|
* to be approved. |
|
119
|
|
|
* |
|
120
|
|
|
* @return array fields with new data, that should be recorded |
|
121
|
5 |
|
*/ |
|
122
|
5 |
|
private function changedApprovableFields() |
|
123
|
5 |
|
{ |
|
124
|
|
|
$dirty = $this->getDirty(); |
|
|
|
|
|
|
125
|
|
|
$changes_to_record = array(); |
|
126
|
5 |
|
|
|
127
|
|
|
foreach ($dirty as $key => $value) { |
|
128
|
|
|
if ($this->isApprovable($key)) { |
|
129
|
|
|
if (!isset($this->original[$key]) || $this->original[$key] != $this->attributes[$key]) { |
|
130
|
|
|
$changes_to_record[$key] = $value; |
|
131
|
|
|
|
|
132
|
|
|
// Reset changes that we want to approve |
|
133
|
|
|
if (!isset($this->original[$key])) { |
|
134
|
|
|
unset($this->attributes[$key]); |
|
135
|
5 |
|
} else { |
|
136
|
|
|
$this->attributes[$key] = $this->original[$key]; |
|
|
|
|
|
|
137
|
5 |
|
} |
|
138
|
5 |
|
} |
|
139
|
|
|
} |
|
140
|
5 |
|
} |
|
141
|
5 |
|
|
|
142
|
5 |
|
return $changes_to_record; |
|
143
|
5 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
5 |
|
* @param $key |
|
147
|
|
|
* @return bool |
|
148
|
|
|
*/ |
|
149
|
5 |
|
private function isApprovable($key) |
|
150
|
|
|
{ |
|
151
|
|
|
if (isset($this->approveOf) && in_array($key, $this->approveOf)) { |
|
152
|
|
|
return true; |
|
153
|
|
|
} |
|
154
|
|
|
if (isset($this->dontApproveOf) && in_array($key, $this->dontApproveOf)) { |
|
155
|
5 |
|
return false; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return empty($this->approveOf); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
5 |
|
* @return mixed|null |
|
163
|
|
|
*/ |
|
164
|
5 |
|
protected function getSystemUserId() |
|
165
|
|
|
{ |
|
166
|
|
|
return Auth::id() ?? null; |
|
167
|
5 |
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return bool |
|
171
|
5 |
|
*/ |
|
172
|
|
|
protected function currentUserCanApprove() |
|
173
|
|
|
{ |
|
174
|
|
|
return Auth::user()->can('approve', $this) ?? false; |
|
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
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
Idableprovides a methodequalsIdthat 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.