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