1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\Users\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne; |
10
|
|
|
use Sfneal\Address\Models\Address; |
11
|
|
|
use Sfneal\Models\AbstractAuthenticatable; |
12
|
|
|
use Sfneal\Scopes\OrderScope; |
13
|
|
|
use Sfneal\Users\Builders\UserBuilder; |
14
|
|
|
use Sfneal\Users\Scopes\UserActiveScope; |
15
|
|
|
use Sfneal\Currency\FormatDollars; |
16
|
|
|
|
17
|
|
|
class User extends AbstractAuthenticatable |
18
|
|
|
{ |
19
|
|
|
// todo: refactor status to use Status model? |
20
|
|
|
// use HasCustomCasts; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The "booting" method of the model. |
24
|
|
|
* |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
|
public static function boot() |
28
|
|
|
{ |
29
|
|
|
parent::boot(); |
30
|
|
|
|
31
|
|
|
// Global scopes |
32
|
|
|
static::addGlobalScope(new UserActiveScope()); |
33
|
|
|
static::addGlobalScope(new OrderScope('last_name', 'asc')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected $dates = ['deleted_at']; |
37
|
|
|
protected $table = 'user'; |
38
|
|
|
protected $primaryKey = 'id'; |
39
|
|
|
protected $connection = 'mysql'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The attributes that are mass assignable. |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $fillable = [ |
47
|
|
|
'role_id', |
48
|
|
|
'first_name', |
49
|
|
|
'middle_name', |
50
|
|
|
'last_name', |
51
|
|
|
'nickname', |
52
|
|
|
'nickname_preferred', |
53
|
|
|
'title', |
54
|
|
|
'suffix', |
55
|
|
|
'email', |
56
|
|
|
'phone_work', |
57
|
|
|
'phone_mobile', |
58
|
|
|
'fax', |
59
|
|
|
'website', |
60
|
|
|
'bio', |
61
|
|
|
'username', |
62
|
|
|
'password', |
63
|
|
|
'status', |
64
|
|
|
'rate', |
65
|
|
|
'plan_management_buckets', |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The attributes that should be hidden from arrays. |
70
|
|
|
* |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
protected $hidden = [ |
74
|
|
|
'password', |
75
|
|
|
'remember_token', |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
// /** |
79
|
|
|
// * The attributes that should type cast. |
80
|
|
|
// * |
81
|
|
|
// * @var array |
82
|
|
|
// */ |
83
|
|
|
// protected $casts = [ |
84
|
|
|
// 'bio' => NewlineCast::class, |
85
|
|
|
// 'plan_management_buckets' => 'array', |
86
|
|
|
// ]; |
87
|
|
|
|
88
|
|
|
// /** |
89
|
|
|
// * @var array Events to be dispatched after certain events |
90
|
|
|
// */ |
91
|
|
|
// protected $dispatchesEvents = [ |
92
|
|
|
// 'updated' => UserUpdatedEvent::class, |
93
|
|
|
// 'deleted' => DeletedFileModelsParentEvent::class, |
94
|
|
|
// ]; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Query Builder. |
98
|
|
|
* @param $query |
99
|
|
|
* @return UserBuilder |
100
|
|
|
*/ |
101
|
|
|
public function newEloquentBuilder($query) |
102
|
|
|
{ |
103
|
|
|
return new UserBuilder($query); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Custom User query Builder. |
108
|
|
|
* |
109
|
|
|
* @return UserBuilder|Builder |
110
|
|
|
*/ |
111
|
|
|
public static function query(): UserBuilder |
112
|
|
|
{ |
113
|
|
|
return parent::query(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* User's 'role' relationship. |
118
|
|
|
* |
119
|
|
|
* @return BelongsTo |
120
|
|
|
*/ |
121
|
|
|
public function role() |
122
|
|
|
{ |
123
|
|
|
return $this->belongsTo(Role::class, 'role_id', 'role_id'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// /** |
127
|
|
|
// * Tasks belonging to the User. |
128
|
|
|
// * |
129
|
|
|
// * @return HasMany |
130
|
|
|
// */ |
131
|
|
|
// public function tasks() |
132
|
|
|
// { |
133
|
|
|
// return $this->hasMany(Task::class, 'user_id', 'id')->orderBy('updated_at', 'desc'); |
134
|
|
|
// } |
135
|
|
|
// |
136
|
|
|
// /** |
137
|
|
|
// * Projects that the User is a team member of. |
138
|
|
|
// * |
139
|
|
|
// * @return BelongsToMany |
140
|
|
|
// */ |
141
|
|
|
// public function projects() |
142
|
|
|
// { |
143
|
|
|
// return $this->belongsToMany(Project::class, 'project_user', 'user_id', 'project_id')->with(['status']); |
144
|
|
|
// } |
145
|
|
|
// |
146
|
|
|
// /** |
147
|
|
|
// * Comment created by the user. |
148
|
|
|
// * |
149
|
|
|
// * @return HasMany |
150
|
|
|
// */ |
151
|
|
|
// public function comments() |
152
|
|
|
// { |
153
|
|
|
// return $this->hasMany(ProjectComment::class, 'user_id', 'id')->orderBy('created_at', 'desc'); |
154
|
|
|
// } |
155
|
|
|
// |
156
|
|
|
// /** |
157
|
|
|
// * Documents uploaded by the User. |
158
|
|
|
// * |
159
|
|
|
// * @return HasMany |
160
|
|
|
// */ |
161
|
|
|
// public function docs() |
162
|
|
|
// { |
163
|
|
|
// return $this->hasMany(ProjectDocument::class, 'user_id', 'id')->orderBy('created_at', 'desc'); |
164
|
|
|
// } |
165
|
|
|
// |
166
|
|
|
// /** |
167
|
|
|
// * Traffic tracking for a User. |
168
|
|
|
// * |
169
|
|
|
// * @return HasMany |
170
|
|
|
// */ |
171
|
|
|
// public function traffic() |
172
|
|
|
// { |
173
|
|
|
// return $this->hasMany(TrackTraffic::class, 'user_id', 'id'); |
174
|
|
|
// } |
175
|
|
|
// |
176
|
|
|
// /** |
177
|
|
|
// * Activity tracking for a User. |
178
|
|
|
// * |
179
|
|
|
// * @return HasMany |
180
|
|
|
// */ |
181
|
|
|
// public function activity() |
182
|
|
|
// { |
183
|
|
|
// return $this->hasMany(TrackActivity::class, 'user_id', 'id'); |
184
|
|
|
// } |
185
|
|
|
// |
186
|
|
|
// /** |
187
|
|
|
// * Task Rate History for a User. |
188
|
|
|
// * |
189
|
|
|
// * @return HasMany |
190
|
|
|
// */ |
191
|
|
|
// public function rateHistory() |
192
|
|
|
// { |
193
|
|
|
// return $this->hasMany(RateHistory::class, 'user_id', 'id'); |
194
|
|
|
// } |
195
|
|
|
// |
196
|
|
|
// /** |
197
|
|
|
// * The image file. |
198
|
|
|
// * |
199
|
|
|
// * @return MorphOne|File |
200
|
|
|
// */ |
201
|
|
|
// public function file() |
202
|
|
|
// { |
203
|
|
|
// return $this->morphOne(File::class, 'fileable'); |
204
|
|
|
// } |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* User's Notification Subscriptions. |
208
|
|
|
* |
209
|
|
|
* @return HasMany |
210
|
|
|
*/ |
211
|
|
|
public function notificationSubscriptions() |
212
|
|
|
{ |
213
|
|
|
return $this->hasMany(UserNotification::class, 'user_id', 'id'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* User's 'team' relationship - indicates user is a member of the public team. |
218
|
|
|
* |
219
|
|
|
* @return HasOne |
220
|
|
|
*/ |
221
|
|
|
public function team() |
222
|
|
|
{ |
223
|
|
|
return $this->hasOne(Team::class, 'user_id', 'id'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* User's address. |
228
|
|
|
* |
229
|
|
|
* @return MorphOne|Address |
230
|
|
|
*/ |
231
|
|
|
public function address() |
232
|
|
|
{ |
233
|
|
|
return $this->morphOne(Address::class, 'addressable'); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Determine if a User has a particular 'role_id'. |
238
|
|
|
* |
239
|
|
|
* @param int $role_id |
240
|
|
|
* @return bool |
241
|
|
|
*/ |
242
|
|
|
public function isRoleId(int $role_id): bool |
243
|
|
|
{ |
244
|
|
|
return $this->role_id == $role_id; |
|
|
|
|
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Determine if a User has a particular 'role name'. |
249
|
|
|
* |
250
|
|
|
* @param string $role |
251
|
|
|
* @return bool |
252
|
|
|
*/ |
253
|
|
|
public function isRole(string $role): bool |
254
|
|
|
{ |
255
|
|
|
return strtolower($this->role->name) == strtolower($role); |
|
|
|
|
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Determine if a User has an 'admin' role. |
260
|
|
|
* |
261
|
|
|
* @return bool |
262
|
|
|
*/ |
263
|
|
|
public function isAdmin(): bool |
264
|
|
|
{ |
265
|
|
|
return $this->isRoleId(3); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Determine if a User has an 'employee' role. |
270
|
|
|
* |
271
|
|
|
* @return bool |
272
|
|
|
*/ |
273
|
|
|
public function isEmployee(): bool |
274
|
|
|
{ |
275
|
|
|
return $this->isRoleId(1); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Determine if a User has an 'contractor' role. |
280
|
|
|
* |
281
|
|
|
* @return bool |
282
|
|
|
*/ |
283
|
|
|
public function isContractor(): bool |
284
|
|
|
{ |
285
|
|
|
return $this->isRoleId(2); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Determine if a User is 'active'. |
290
|
|
|
* |
291
|
|
|
* @return bool |
292
|
|
|
*/ |
293
|
|
|
public function isActive(): bool |
294
|
|
|
{ |
295
|
|
|
return $this->status == 1; |
|
|
|
|
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Determine if a User's $nickname is preferred over their $first_name. |
300
|
|
|
* |
301
|
|
|
* @return bool |
302
|
|
|
*/ |
303
|
|
|
public function isNicknamePreferred(): bool |
304
|
|
|
{ |
305
|
|
|
return $this->nickname_preferred == 1; |
|
|
|
|
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Determine if the current user is permitted to take actions on plans in the this bucket. |
310
|
|
|
* |
311
|
|
|
* @param string $bucket |
312
|
|
|
* @return bool |
313
|
|
|
*/ |
314
|
|
|
public function isBucketPermitted(string $bucket) |
315
|
|
|
{ |
316
|
|
|
return in_array($bucket, $this->permitted_buckets); |
|
|
|
|
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Retrieve a User's initials. |
321
|
|
|
* |
322
|
|
|
* @return string |
323
|
|
|
*/ |
324
|
|
|
public function getInitialsAttribute() |
325
|
|
|
{ |
326
|
|
|
return implodeFiltered('', collect([ |
327
|
|
|
$this->first_name, |
|
|
|
|
328
|
|
|
$this->middle_name, |
|
|
|
|
329
|
|
|
$this->last_name, |
|
|
|
|
330
|
|
|
])->map(function ($name) { |
331
|
|
|
return substr($name, 0, 1); |
332
|
|
|
})->toArray()); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Retrieve a Plan Review ID for displaying a plans.plan.show modal. |
337
|
|
|
* |
338
|
|
|
* @param int $review_id |
339
|
|
|
* @return int |
340
|
|
|
*/ |
341
|
|
|
public function validatePlanReviewRequest(int $review_id) |
342
|
|
|
{ |
343
|
|
|
// Determine what bucket permissions are being checked |
344
|
|
|
if ($review_id == 1) { |
345
|
|
|
$bucket = 'review'; |
346
|
|
|
} elseif ($review_id > 1) { |
347
|
|
|
$bucket = 'complete'; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
// Confirm bucket is requested and permitted |
351
|
|
|
if (isset($bucket) && $this->isBucketPermitted($bucket)) { |
352
|
|
|
return $review_id; |
353
|
|
|
} else { |
354
|
|
|
return 0; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Get the AWS S3 file upload directory for an Inquiry model by retrieving the table name and primary key. |
360
|
|
|
* |
361
|
|
|
* @param string $base_dir |
362
|
|
|
* @return string |
363
|
|
|
*/ |
364
|
|
|
public function getUploadDirectory($base_dir = 'images'): string |
365
|
|
|
{ |
366
|
|
|
return $base_dir.'/'.str_replace('_', '-', $this->getTable()).'/'.$this->getKey(); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Get the 'city_state' attribute. |
371
|
|
|
* |
372
|
|
|
* @return string |
373
|
|
|
*/ |
374
|
|
|
public function getCityStateAttribute() |
375
|
|
|
{ |
376
|
|
|
return $this->address->city_state ?? null; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Set the 'password' attribute. |
381
|
|
|
* |
382
|
|
|
* @param $value |
383
|
|
|
*/ |
384
|
|
|
public function setPasswordAttribute($value) |
385
|
|
|
{ |
386
|
|
|
if (! empty($value)) { |
387
|
|
|
$this->attributes['password'] = bcrypt($value); |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
// /** |
392
|
|
|
// * Get a User's permitted plan management buckets. |
393
|
|
|
// * |
394
|
|
|
// * @return array|mixed |
395
|
|
|
// */ |
396
|
|
|
// public function getPermittedBucketsAttribute() |
397
|
|
|
// { |
398
|
|
|
// if (isset($this->plan_management_buckets)) { |
399
|
|
|
// return $this->plan_management_buckets; |
400
|
|
|
// } else { |
401
|
|
|
// return array_keys(PlanManagement::buckets()); |
402
|
|
|
// } |
403
|
|
|
// } |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Mutate the 'middle_name' attribute. |
407
|
|
|
* |
408
|
|
|
* @param string|null $value |
409
|
|
|
*/ |
410
|
|
|
public function setMiddleNameAttribute(string $value = null) |
411
|
|
|
{ |
412
|
|
|
if (! is_null($value)) { |
413
|
|
|
// Remove leading & trailing whitespace |
414
|
|
|
$middle_name = trim($value); |
415
|
|
|
|
416
|
|
|
// Append '.' to the middle name if's a single letter |
417
|
|
|
$this->attributes['middle_name'] = strlen($middle_name) == 1 ? "{$middle_name}." : $middle_name; |
418
|
|
|
} |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Retrieve the User's name (first & last). |
423
|
|
|
* |
424
|
|
|
* @return string |
425
|
|
|
*/ |
426
|
|
|
public function getNameAttribute(): string |
427
|
|
|
{ |
428
|
|
|
// Use nickname instead of first if it is set & preferred |
429
|
|
|
$first = (isset($this->nickname) && $this->isNicknamePreferred()) ? $this->nickname : $this->first_name; |
|
|
|
|
430
|
|
|
|
431
|
|
|
// Return concatenated name |
432
|
|
|
return "{$first} {$this->last_name}"; |
|
|
|
|
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Retrieve the User's full name with middle initial. |
437
|
|
|
* |
438
|
|
|
* @return string |
439
|
|
|
*/ |
440
|
|
|
public function getNameFullAttribute(): string |
441
|
|
|
{ |
442
|
|
|
$name = $this->first_name; |
|
|
|
|
443
|
|
|
if ($this->middle_name) { |
|
|
|
|
444
|
|
|
$name .= ' '.$this->middle_name; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
return "{$name} {$this->last_name}"; |
|
|
|
|
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Retrieve the User's name with their suffix. |
452
|
|
|
* |
453
|
|
|
* @return string |
454
|
|
|
*/ |
455
|
|
|
public function getNameSuffixAttribute(): string |
456
|
|
|
{ |
457
|
|
|
return $this->name_full.($this->suffix ? ", {$this->suffix}" : ''); |
|
|
|
|
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* Get the 'list_name' attribute. |
462
|
|
|
* |
463
|
|
|
* @return string |
464
|
|
|
*/ |
465
|
|
|
public function getListNameAttribute() |
466
|
|
|
{ |
467
|
|
|
return implode(', ', array_reverse(explode(' ', $this->name))); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Get the 'name_link' attribute that returns a url to the User's team.show page. |
472
|
|
|
* |
473
|
|
|
* @return string |
474
|
|
|
*/ |
475
|
|
|
public function getNameLinkAttribute() |
476
|
|
|
{ |
477
|
|
|
return '<a href="'.route('user.show', ['user'=>$this->id]).'">'.$this->name.'</a>'; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Retrieve the User's 'address1' attribute. |
482
|
|
|
* |
483
|
|
|
* @return mixed |
484
|
|
|
*/ |
485
|
|
|
public function getAddress1Attribute() |
486
|
|
|
{ |
487
|
|
|
return $this->address->address_1 ?? null; |
|
|
|
|
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* Retrieve the User's 'address2' attribute. |
492
|
|
|
* |
493
|
|
|
* @return mixed |
494
|
|
|
*/ |
495
|
|
|
public function getAddress2Attribute() |
496
|
|
|
{ |
497
|
|
|
return $this->address->address_2 ?? null; |
|
|
|
|
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* Retrieve the User's 'city' attribute. |
502
|
|
|
* |
503
|
|
|
* @return mixed |
504
|
|
|
*/ |
505
|
|
|
public function getCityAttribute() |
506
|
|
|
{ |
507
|
|
|
return $this->address->city ?? null; |
|
|
|
|
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* Retrieve the User's 'state' attribute. |
512
|
|
|
* |
513
|
|
|
* @return mixed |
514
|
|
|
*/ |
515
|
|
|
public function getStateAttribute() |
516
|
|
|
{ |
517
|
|
|
return $this->address->state ?? null; |
|
|
|
|
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* Retrieve the User's 'zip' attribute. |
522
|
|
|
* |
523
|
|
|
* @return mixed |
524
|
|
|
*/ |
525
|
|
|
public function getZipAttribute() |
526
|
|
|
{ |
527
|
|
|
return $this->address->zip ?? null; |
|
|
|
|
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
/** |
531
|
|
|
* Retrieve the User's image path if an image exists. |
532
|
|
|
* |
533
|
|
|
* @return string|null |
534
|
|
|
*/ |
535
|
|
|
public function getImageAttribute() |
536
|
|
|
{ |
537
|
|
|
return $this->file->path ?? null; |
|
|
|
|
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* Retrieve an email link. |
542
|
|
|
* |
543
|
|
|
* @return string |
544
|
|
|
*/ |
545
|
|
|
public function getEmailLinkAttribute(): string |
546
|
|
|
{ |
547
|
|
|
return $this->email ? ('mailto:'.$this->email) : '#!'; |
|
|
|
|
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* Retrieve a work phone link. |
552
|
|
|
* |
553
|
|
|
* @return string |
554
|
|
|
*/ |
555
|
|
|
public function getPhoneWorkLinkAttribute(): string |
556
|
|
|
{ |
557
|
|
|
return $this->phone_work ? ('tel:'.$this->phone_work) : '#!'; |
|
|
|
|
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
/** |
561
|
|
|
* Retrieve a mobile phone link. |
562
|
|
|
* |
563
|
|
|
* @return string |
564
|
|
|
*/ |
565
|
|
|
public function getPhoneMobileLinkAttribute(): string |
566
|
|
|
{ |
567
|
|
|
return $this->phone_mobile ? ('tel:'.$this->phone_mobile) : '#!'; |
|
|
|
|
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
/** |
571
|
|
|
* Retrieve the User's rate formatted as dollars. |
572
|
|
|
* |
573
|
|
|
* @return string |
574
|
|
|
*/ |
575
|
|
|
public function getRateFormattedAttribute(): string |
576
|
|
|
{ |
577
|
|
|
return (! empty($this->rate)) ? '$'.FormatDollars::execute($this->rate) : '-'; |
|
|
|
|
578
|
|
|
} |
579
|
|
|
} |
580
|
|
|
|