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\Casts\NewlineCast; |
12
|
|
|
use Sfneal\Currency\FormatDollars; |
13
|
|
|
use Sfneal\Models\AbstractAuthenticatable; |
14
|
|
|
use Sfneal\Scopes\OrderScope; |
15
|
|
|
use Sfneal\Users\Builders\UserBuilder; |
16
|
|
|
use Sfneal\Users\Scopes\UserActiveScope; |
17
|
|
|
use Sfneal\Users\Services\OrganizationService; |
18
|
|
|
use Vkovic\LaravelCustomCasts\HasCustomCasts; |
19
|
|
|
|
20
|
|
|
class User extends AbstractAuthenticatable |
21
|
|
|
{ |
22
|
|
|
// todo: refactor status to use Status model? |
23
|
|
|
use HasCustomCasts; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The "booting" method of the model. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public static function boot() |
31
|
|
|
{ |
32
|
|
|
parent::boot(); |
33
|
|
|
|
34
|
|
|
// Global scopes |
35
|
|
|
static::addGlobalScope(new UserActiveScope()); |
36
|
|
|
static::addGlobalScope(new OrderScope('last_name', 'asc')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected $dates = ['deleted_at']; |
40
|
|
|
protected $table = 'user'; |
41
|
|
|
protected $primaryKey = 'id'; |
42
|
|
|
protected $connection = 'mysql'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The attributes that are mass assignable. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $fillable = [ |
50
|
|
|
'role_id', |
51
|
|
|
'first_name', |
52
|
|
|
'middle_name', |
53
|
|
|
'last_name', |
54
|
|
|
'nickname', |
55
|
|
|
'nickname_preferred', |
56
|
|
|
'title', |
57
|
|
|
'suffix', |
58
|
|
|
'email', |
59
|
|
|
'phone_work', |
60
|
|
|
'phone_mobile', |
61
|
|
|
'fax', |
62
|
|
|
'website', |
63
|
|
|
'bio', |
64
|
|
|
'username', |
65
|
|
|
'password', |
66
|
|
|
'status', |
67
|
|
|
'rate', |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The attributes that should be hidden from arrays. |
72
|
|
|
* |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
protected $hidden = [ |
76
|
|
|
'password', |
77
|
|
|
'remember_token', |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The attributes that should type cast. |
82
|
|
|
* |
83
|
|
|
* @var array |
84
|
|
|
*/ |
85
|
|
|
protected $casts = [ |
86
|
|
|
'bio' => NewlineCast::class, |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Query Builder. |
91
|
|
|
* @param $query |
92
|
|
|
* @return UserBuilder |
93
|
|
|
*/ |
94
|
|
|
public function newEloquentBuilder($query) |
95
|
|
|
{ |
96
|
|
|
return new UserBuilder($query); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Custom User query Builder. |
101
|
|
|
* |
102
|
|
|
* @return UserBuilder|Builder |
103
|
|
|
*/ |
104
|
|
|
public static function query(): UserBuilder |
105
|
|
|
{ |
106
|
|
|
return parent::query(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* User's 'role' relationship. |
111
|
|
|
* |
112
|
|
|
* @return BelongsTo |
113
|
|
|
*/ |
114
|
|
|
public function role() |
115
|
|
|
{ |
116
|
|
|
return $this->belongsTo(Role::class, 'role_id', 'role_id'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* User's Notification Subscriptions. |
121
|
|
|
* |
122
|
|
|
* @return HasMany |
123
|
|
|
*/ |
124
|
|
|
public function notificationSubscriptions() |
125
|
|
|
{ |
126
|
|
|
return $this->hasMany(UserNotification::class, 'user_id', 'id'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* User's 'team' relationship - indicates user is a member of the public team. |
131
|
|
|
* |
132
|
|
|
* @return HasOne |
133
|
|
|
*/ |
134
|
|
|
public function team() |
135
|
|
|
{ |
136
|
|
|
return $this->hasOne(Team::class, 'user_id', 'id'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* User's address. |
141
|
|
|
* |
142
|
|
|
* @return MorphOne|Address |
143
|
|
|
*/ |
144
|
|
|
public function address() |
145
|
|
|
{ |
146
|
|
|
return $this->morphOne(Address::class, 'addressable'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Determine if a User has a particular 'role_id'. |
151
|
|
|
* |
152
|
|
|
* @param int $role_id |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
public function isRoleId(int $role_id): bool |
156
|
|
|
{ |
157
|
|
|
return $this->role_id == $role_id; |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Determine if a User has a particular 'role name'. |
162
|
|
|
* |
163
|
|
|
* @param string $role |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
|
|
public function isRole(string $role): bool |
167
|
|
|
{ |
168
|
|
|
return strtolower($this->role->name) == strtolower($role); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Determine if a User has an 'admin' role. |
173
|
|
|
* |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
public function isAdmin(): bool |
177
|
|
|
{ |
178
|
|
|
return $this->isRoleId(3); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Determine if a User has an 'employee' role. |
183
|
|
|
* |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
public function isEmployee(): bool |
187
|
|
|
{ |
188
|
|
|
return $this->isRoleId(1); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Determine if a User has an 'contractor' role. |
193
|
|
|
* |
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
|
|
public function isContractor(): bool |
197
|
|
|
{ |
198
|
|
|
return $this->isRoleId(2); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Determine if a User is 'active'. |
203
|
|
|
* |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
|
|
public function isActive(): bool |
207
|
|
|
{ |
208
|
|
|
return $this->status == 1; |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Determine if a User's $nickname is preferred over their $first_name. |
213
|
|
|
* |
214
|
|
|
* @return bool |
215
|
|
|
*/ |
216
|
|
|
public function isNicknamePreferred(): bool |
217
|
|
|
{ |
218
|
|
|
return $this->nickname_preferred == 1; |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Retrieve a User's initials. |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
public function getInitialsAttribute() |
227
|
|
|
{ |
228
|
|
|
return implodeFiltered('', collect([ |
229
|
|
|
$this->first_name, |
|
|
|
|
230
|
|
|
$this->middle_name, |
|
|
|
|
231
|
|
|
$this->last_name, |
|
|
|
|
232
|
|
|
])->map(function ($name) { |
233
|
|
|
return substr($name, 0, 1); |
234
|
|
|
})->toArray()); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Get the AWS S3 file upload directory for an Inquiry model by retrieving the table name and primary key. |
239
|
|
|
* |
240
|
|
|
* @param string $base_dir |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
|
|
public function getUploadDirectory($base_dir = 'images'): string |
244
|
|
|
{ |
245
|
|
|
return $base_dir.'/'.str_replace('_', '-', $this->getTable()).'/'.$this->getKey(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Get the 'city_state' attribute. |
250
|
|
|
* |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
|
|
public function getCityStateAttribute() |
254
|
|
|
{ |
255
|
|
|
return $this->address->city_state ?? null; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Set the 'password' attribute. |
260
|
|
|
* |
261
|
|
|
* @param $value |
262
|
|
|
*/ |
263
|
|
|
public function setPasswordAttribute($value) |
264
|
|
|
{ |
265
|
|
|
if (! empty($value)) { |
266
|
|
|
$this->attributes['password'] = bcrypt($value); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Mutate the 'middle_name' attribute. |
272
|
|
|
* |
273
|
|
|
* @param string|null $value |
274
|
|
|
*/ |
275
|
|
|
public function setMiddleNameAttribute(string $value = null) |
276
|
|
|
{ |
277
|
|
|
if (! is_null($value)) { |
278
|
|
|
// Remove leading & trailing whitespace |
279
|
|
|
$middle_name = trim($value); |
280
|
|
|
|
281
|
|
|
// Append '.' to the middle name if's a single letter |
282
|
|
|
$this->attributes['middle_name'] = strlen($middle_name) == 1 ? "{$middle_name}." : $middle_name; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Retrieve the User's name (first & last). |
288
|
|
|
* |
289
|
|
|
* @return string |
290
|
|
|
*/ |
291
|
|
|
public function getNameAttribute(): string |
292
|
|
|
{ |
293
|
|
|
// Use nickname instead of first if it is set & preferred |
294
|
|
|
$first = (isset($this->nickname) && $this->isNicknamePreferred()) ? $this->nickname : $this->first_name; |
|
|
|
|
295
|
|
|
|
296
|
|
|
// Return concatenated name |
297
|
|
|
return "{$first} {$this->last_name}"; |
|
|
|
|
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Retrieve the User's full name with middle initial. |
302
|
|
|
* |
303
|
|
|
* @return string |
304
|
|
|
*/ |
305
|
|
|
public function getNameFullAttribute(): string |
306
|
|
|
{ |
307
|
|
|
$name = $this->first_name; |
|
|
|
|
308
|
|
|
if ($this->middle_name) { |
|
|
|
|
309
|
|
|
$name .= ' '.$this->middle_name; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return "{$name} {$this->last_name}"; |
|
|
|
|
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Retrieve the User's name with their suffix. |
317
|
|
|
* |
318
|
|
|
* @return string |
319
|
|
|
*/ |
320
|
|
|
public function getNameSuffixAttribute(): string |
321
|
|
|
{ |
322
|
|
|
return $this->name_full.($this->suffix ? ", {$this->suffix}" : ''); |
|
|
|
|
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Get the 'list_name' attribute. |
327
|
|
|
* |
328
|
|
|
* @return string |
329
|
|
|
*/ |
330
|
|
|
public function getListNameAttribute() |
331
|
|
|
{ |
332
|
|
|
return implode(', ', array_reverse(explode(' ', $this->name))); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Get the 'name_link' attribute that returns a url to the User's team.show page. |
337
|
|
|
* |
338
|
|
|
* @return string |
339
|
|
|
*/ |
340
|
|
|
public function getNameLinkAttribute() |
341
|
|
|
{ |
342
|
|
|
return '<a href="'.route('user.show', ['user'=>$this->id]).'">'.$this->name.'</a>'; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
// todo: add address accessor method to a sfneal/address trait |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Retrieve the User's 'address1' attribute. |
349
|
|
|
* |
350
|
|
|
* @return mixed |
351
|
|
|
*/ |
352
|
|
|
public function getAddress1Attribute() |
353
|
|
|
{ |
354
|
|
|
return $this->address->address_1 ?? null; |
|
|
|
|
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Retrieve the User's 'address2' attribute. |
359
|
|
|
* |
360
|
|
|
* @return mixed |
361
|
|
|
*/ |
362
|
|
|
public function getAddress2Attribute() |
363
|
|
|
{ |
364
|
|
|
return $this->address->address_2 ?? null; |
|
|
|
|
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Retrieve the User's 'city' attribute. |
369
|
|
|
* |
370
|
|
|
* @return mixed |
371
|
|
|
*/ |
372
|
|
|
public function getCityAttribute() |
373
|
|
|
{ |
374
|
|
|
return $this->address->city ?? null; |
|
|
|
|
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Retrieve the User's 'state' attribute. |
379
|
|
|
* |
380
|
|
|
* @return mixed |
381
|
|
|
*/ |
382
|
|
|
public function getStateAttribute() |
383
|
|
|
{ |
384
|
|
|
return $this->address->state ?? null; |
|
|
|
|
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Retrieve the User's 'zip' attribute. |
389
|
|
|
* |
390
|
|
|
* @return mixed |
391
|
|
|
*/ |
392
|
|
|
public function getZipAttribute() |
393
|
|
|
{ |
394
|
|
|
return $this->address->zip ?? null; |
|
|
|
|
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Retrieve an email link. |
399
|
|
|
* |
400
|
|
|
* @return string |
401
|
|
|
*/ |
402
|
|
|
public function getEmailLinkAttribute(): string |
403
|
|
|
{ |
404
|
|
|
return $this->email ? ('mailto:'.$this->email) : '#!'; |
|
|
|
|
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Retrieve a work phone link. |
409
|
|
|
* |
410
|
|
|
* @return string |
411
|
|
|
*/ |
412
|
|
|
public function getPhoneWorkLinkAttribute(): string |
413
|
|
|
{ |
414
|
|
|
return $this->phone_work ? ('tel:'.$this->phone_work) : '#!'; |
|
|
|
|
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Retrieve a mobile phone link. |
419
|
|
|
* |
420
|
|
|
* @return string |
421
|
|
|
*/ |
422
|
|
|
public function getPhoneMobileLinkAttribute(): string |
423
|
|
|
{ |
424
|
|
|
return $this->phone_mobile ? ('tel:'.$this->phone_mobile) : '#!'; |
|
|
|
|
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Retrieve the User's rate formatted as dollars. |
429
|
|
|
* |
430
|
|
|
* @return string |
431
|
|
|
*/ |
432
|
|
|
public function getRateFormattedAttribute(): string |
433
|
|
|
{ |
434
|
|
|
return (! empty($this->rate)) ? '$'.FormatDollars::execute($this->rate) : '-'; |
|
|
|
|
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Retrieve the raw 'text' attribute with newline chars. |
439
|
|
|
* |
440
|
|
|
* @return mixed |
441
|
|
|
*/ |
442
|
|
|
public function getTextareaAttribute() |
443
|
|
|
{ |
444
|
|
|
return $this->attributes['bio']; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Retrieve a User's custom email footer. |
449
|
|
|
* |
450
|
|
|
* @return string |
451
|
|
|
*/ |
452
|
|
|
public function getEmailFooterAttribute(): string |
453
|
|
|
{ |
454
|
|
|
$footer = "{$this->name}"; |
455
|
|
|
$footer .= $this->title ? "\n{$this->title}" : ''; |
|
|
|
|
456
|
|
|
$footer .= "\n" . OrganizationService::name() ?? ''; |
457
|
|
|
$footer .= "\n" . ($this->phone_work ?? OrganizationService::phone()) ?? ''; |
|
|
|
|
458
|
|
|
$footer .= $this->email ? "\n{$this->email}" : ''; |
|
|
|
|
459
|
|
|
|
460
|
|
|
return $footer; |
461
|
|
|
} |
462
|
|
|
} |
463
|
|
|
|