1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mpociot\Teamwork\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
7
|
|
|
use Illuminate\Support\Facades\Config; |
8
|
|
|
use Mpociot\Teamwork\Events\UserJoinedTeam; |
9
|
|
|
use Mpociot\Teamwork\Events\UserLeftTeam; |
10
|
|
|
use Mpociot\Teamwork\Exceptions\UserNotInTeamException; |
11
|
|
|
|
12
|
|
|
trait UserHasTeams |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Many-to-Many relations with the user model. |
16
|
|
|
* |
17
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
18
|
|
|
*/ |
19
|
|
|
public function teams() |
20
|
|
|
{ |
21
|
|
|
return $this->belongsToMany(Config::get('teamwork.team_model'), Config::get('teamwork.team_user_table'), 'user_id', 'team_id')->withTimestamps(); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* has-one relation with the current selected team model. |
26
|
|
|
* |
27
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
28
|
|
|
*/ |
29
|
|
|
public function currentTeam() |
30
|
|
|
{ |
31
|
|
|
return $this->hasOne(Config::get('teamwork.team_model'), 'id', 'current_team_id'); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
public function ownedTeams() |
38
|
|
|
{ |
39
|
|
|
return $this->teams()->where('owner_id', '=', $this->getKey()); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* One-to-Many relation with the invite model. |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
public function invites() |
47
|
|
|
{ |
48
|
|
|
return $this->hasMany(Config::get('teamwork.invite_model'), 'email', 'email'); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Boot the user model |
53
|
|
|
* Attach event listener to remove the many-to-many records when trying to delete |
54
|
|
|
* Will NOT delete any records if the user model uses soft deletes. |
55
|
|
|
* |
56
|
|
|
* @return void|bool |
57
|
|
|
*/ |
58
|
|
|
public static function bootUserHasTeams() |
59
|
|
|
{ |
60
|
|
|
static::deleting(function (Model $user) { |
61
|
|
|
if (! method_exists(Config::get('teamwork.user_model'), 'bootSoftDeletes')) { |
62
|
|
|
$user->teams()->sync([]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return true; |
66
|
|
|
}); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns if the user owns a team. |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function isOwner() |
75
|
|
|
{ |
76
|
|
|
return ($this->teams()->where('owner_id', '=', $this->getKey())->first()) ? true : false; |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Wrapper method for "isOwner". |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function isTeamOwner() |
85
|
|
|
{ |
86
|
|
|
return $this->isOwner(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $team |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
protected function retrieveTeamId($team) |
94
|
|
|
{ |
95
|
|
|
if (is_object($team)) { |
96
|
|
|
$team = $team->getKey(); |
97
|
|
|
} |
98
|
|
|
if (is_array($team) && isset($team['id'])) { |
99
|
|
|
$team = $team['id']; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $team; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns if the user owns the given team. |
107
|
|
|
* |
108
|
|
|
* @param mixed $team |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public function isOwnerOfTeam($team) |
112
|
|
|
{ |
113
|
|
|
$team_id = $this->retrieveTeamId($team); |
114
|
|
|
|
115
|
|
|
return ($this->teams() |
116
|
|
|
->where('owner_id', $this->getKey()) |
|
|
|
|
117
|
|
|
->where('team_id', $team_id)->first() |
118
|
|
|
) ? true : false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Alias to eloquent many-to-many relation's attach() method. |
123
|
|
|
* |
124
|
|
|
* @param mixed $team |
125
|
|
|
* @param array $pivotData |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function attachTeam($team, $pivotData = []) |
129
|
|
|
{ |
130
|
|
|
$team = $this->retrieveTeamId($team); |
131
|
|
|
/* |
132
|
|
|
* If the user has no current team, |
133
|
|
|
* use the attached one |
134
|
|
|
*/ |
135
|
|
|
if (is_null($this->current_team_id)) { |
136
|
|
|
$this->current_team_id = $team; |
|
|
|
|
137
|
|
|
$this->save(); |
|
|
|
|
138
|
|
|
|
139
|
|
|
if ($this->relationLoaded('currentTeam')) { |
|
|
|
|
140
|
|
|
$this->load('currentTeam'); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Reload relation |
145
|
|
|
$this->load('teams'); |
|
|
|
|
146
|
|
|
|
147
|
|
|
if (! $this->teams->contains($team)) { |
|
|
|
|
148
|
|
|
$this->teams()->attach($team, $pivotData); |
149
|
|
|
|
150
|
|
|
event(new UserJoinedTeam($this, $team)); |
151
|
|
|
|
152
|
|
|
if ($this->relationLoaded('teams')) { |
|
|
|
|
153
|
|
|
$this->load('teams'); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Alias to eloquent many-to-many relation's detach() method. |
162
|
|
|
* |
163
|
|
|
* @param mixed $team |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
|
|
public function detachTeam($team) |
167
|
|
|
{ |
168
|
|
|
$team = $this->retrieveTeamId($team); |
169
|
|
|
$this->teams()->detach($team); |
170
|
|
|
|
171
|
|
|
event(new UserLeftTeam($this, $team)); |
172
|
|
|
|
173
|
|
|
if ($this->relationLoaded('teams')) { |
|
|
|
|
174
|
|
|
$this->load('teams'); |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/* |
178
|
|
|
* If the user has no more teams, |
179
|
|
|
* unset the current_team_id |
180
|
|
|
*/ |
181
|
|
|
if ($this->teams()->count() === 0 || $this->current_team_id === $team) { |
182
|
|
|
$this->current_team_id = null; |
183
|
|
|
$this->save(); |
|
|
|
|
184
|
|
|
|
185
|
|
|
if ($this->relationLoaded('currentTeam')) { |
|
|
|
|
186
|
|
|
$this->load('currentTeam'); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Attach multiple teams to a user. |
195
|
|
|
* |
196
|
|
|
* @param mixed $teams |
197
|
|
|
* @return $this |
198
|
|
|
*/ |
199
|
|
|
public function attachTeams($teams) |
200
|
|
|
{ |
201
|
|
|
foreach ($teams as $team) { |
202
|
|
|
$this->attachTeam($team); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Detach multiple teams from a user. |
210
|
|
|
* |
211
|
|
|
* @param mixed $teams |
212
|
|
|
* @return $this |
213
|
|
|
*/ |
214
|
|
|
public function detachTeams($teams) |
215
|
|
|
{ |
216
|
|
|
foreach ($teams as $team) { |
217
|
|
|
$this->detachTeam($team); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Switch the current team of the user. |
225
|
|
|
* |
226
|
|
|
* @param object|array|int $team |
227
|
|
|
* @return $this |
228
|
|
|
* @throws ModelNotFoundException |
229
|
|
|
* @throws UserNotInTeamException |
230
|
|
|
*/ |
231
|
|
|
public function switchTeam($team) |
232
|
|
|
{ |
233
|
|
|
if ($team !== 0 && $team !== null) { |
234
|
|
|
$team = $this->retrieveTeamId($team); |
235
|
|
|
$teamModel = Config::get('teamwork.team_model'); |
236
|
|
|
$teamObject = ( new $teamModel() )->find($team); |
237
|
|
|
if (! $teamObject) { |
238
|
|
|
$exception = new ModelNotFoundException(); |
239
|
|
|
$exception->setModel($teamModel); |
240
|
|
|
throw $exception; |
241
|
|
|
} |
242
|
|
|
if (! $teamObject->users->contains($this->getKey())) { |
|
|
|
|
243
|
|
|
$exception = new UserNotInTeamException(); |
244
|
|
|
$exception->setTeam($teamObject->name); |
245
|
|
|
throw $exception; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
$this->current_team_id = $team; |
249
|
|
|
$this->save(); |
|
|
|
|
250
|
|
|
|
251
|
|
|
if ($this->relationLoaded('currentTeam')) { |
|
|
|
|
252
|
|
|
$this->load('currentTeam'); |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $this; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
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.