-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUser.php
430 lines (387 loc) · 12.3 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
namespace App\Models;
use App\Utils\StringUtil;
use Database\Factories\UserFactory;
use Eloquent;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Notifications\DatabaseNotificationCollection;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
/**
* App\Models\User
*
* @property int $id
* @property string $name
* @property string $email
* @property Carbon|null $email_verified_at
* @property string $password
* @property string|null $two_factor_secret
* @property string|null $two_factor_recovery_codes
* @property string|null $two_factor_confirmed_at
* @property string|null $remember_token
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property-read Collection|Event[] $events
* @property-read int|null $events_count
* @property-read DatabaseNotificationCollection|DatabaseNotification[] $notifications
* @property-read int|null $notifications_count
* @property-read Collection|Role[] $roles
* @property-read int|null $roles_count
*
* @method static UserFactory factory(...$parameters)
* @method static Builder|User newModelQuery()
* @method static Builder|User newQuery()
* @method static Builder|User query()
* @method static Builder|User whereCreatedAt($value)
* @method static Builder|User whereEmail($value)
* @method static Builder|User whereEmailVerifiedAt($value)
* @method static Builder|User whereId($value)
* @method static Builder|User whereName($value)
* @method static Builder|User wherePassword($value)
* @method static Builder|User whereRememberToken($value)
* @method static Builder|User whereTwoFactorConfirmedAt($value)
* @method static Builder|User whereTwoFactorRecoveryCodes($value)
* @method static Builder|User whereTwoFactorSecret($value)
* @method static Builder|User whereUpdatedAt($value)
*
* @mixin Eloquent
*/
/**
* App\Models\User
*
* @uses \Illuminate\Auth\MustVerifyEmail
*
* @property int $id
* @property string $first_name
* @property string $family_name
* @property Carbon|null $birthday
* @property string|null $gender
* @property string|null $mobile_phone
* @property string|null $health_issues
* @property string $email
* @property Carbon|null $email_verified_at
* @property string $password
* @property string|null $two_factor_secret
* @property string|null $two_factor_recovery_codes
* @property string|null $remember_token
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property string $uuid
* @property-read Collection|Comment[] $authoredComments
* @property-read int|null $authored_comments_count
* @property-read BioFamily|null $bioFamily
* @property-read CounselorInfo|null $counselor
* @property-read Collection|Document[] $documents
* @property-read int|null $documents_count
* @property-read Collection|Event[] $events
* @property-read int|null $events_count
* @property-read Collection|HostFamily[] $hostFamilies
* @property-read int|null $host_families_count
* @property-read DatabaseNotificationCollection|DatabaseNotification[] $notifications
* @property-read int|null $notifications_count
* @property-read Passport|null $passport
* @property-read RegistrationComment|null $registrationComment
* @property-read Collection|Role[] $roles
* @property-read int|null $roles_count
* @property-read RotaryInfo|null $rotaryInfo
* @property-read YeoInfo|null $yeo
* @property-read string $full_name
*
* @method static UserFactory factory(...$parameters)
* @method static Builder|User newModelQuery()
* @method static Builder|User newQuery()
* @method static Builder|User query()
* @method static Builder|User whereBirthday($value)
* @method static Builder|User whereCreatedAt($value)
* @method static Builder|User whereEmail($value)
* @method static Builder|User whereEmailVerifiedAt($value)
* @method static Builder|User whereFamilyName($value)
* @method static Builder|User whereFirstName($value)
* @method static Builder|User whereGender($value)
* @method static Builder|User whereHealthIssues($value)
* @method static Builder|User whereId($value)
* @method static Builder|User whereMobilePhone($value)
* @method static Builder|User wherePassword($value)
* @method static Builder|User whereRememberToken($value)
* @method static Builder|User whereTwoFactorRecoveryCodes($value)
* @method static Builder|User whereTwoFactorSecret($value)
* @method static Builder|User whereUpdatedAt($value)
* @method static Builder|User whereUuid($value)
*
* @property-read AdditionalInfo|null $additionalInfo
* @property-read string $comment_display_name
* @property-read string $short_name
* @property-read Collection|Payment[] $payments
* @property-read int|null $payments_count
*
* @mixin Eloquent
*/
class User extends Authenticatable implements MustVerifyEmail
{
use HasFactory, Notifiable, HasCompletenessCheck, HasRoles, HasDocuments, \Illuminate\Auth\MustVerifyEmail;
/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'first_name',
'family_name',
'email',
'password',
'uuid',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
'birthday' => 'date:Y-m-d',
];
protected static function booted(): void
{
static::deleting(function (User $user) {
$user->authoredComments()->delete();
$user->bioFamily()->delete();
$user->hostFamilies()->delete();
$user->documents()->delete();
$user->yeo()->delete();
$user->counselor()->delete();
$user->passport()->delete();
$user->additionalInfo()->delete();
$user->payments()->delete();
});
}
/**
* @return HasMany<Comment>
*/
public function authoredComments(): HasMany
{
return $this->hasMany(Comment::class, 'author_id');
}
/**
* @retrun HasOne
*
* @return HasOne<BioFamily>
*/
public function bioFamily(): HasOne
{
return $this->hasOne(BioFamily::class, 'user_id');
}
/**
* @return HasMany<HostFamily>
*/
public function hostFamilies(): HasMany
{
return $this->hasMany(HostFamily::class, 'user_id');
}
/**
* @retrun HasOne
*
* @return HasOne<YeoInfo>
*/
public function yeo(): HasOne
{
return $this->hasOne(YeoInfo::class, 'user_id');
}
/**
* @retrun HasOne
*
* @return HasOne<CounselorInfo>
*/
public function counselor(): HasOne
{
return $this->hasOne(CounselorInfo::class, 'user_id');
}
/**
* Accessor combining first name and family name
*/
public function getFullNameAttribute(): string
{
return $this->first_name.' '.$this->family_name;
}
/**
* Convenience wrapper around the full name accessor, replacing every non-word character
* with an underscore.
* @return string
*/
public function getFilePathNameAttribute(): string
{
$onlyWordAndWhitespaceCharacters = preg_replace('/[^\s\w-]/', ' ', $this->full_name) ?? '';
return preg_replace('/\s+/', ' ',$onlyWordAndWhitespaceCharacters) ?? '';
}
/**
* Accessor for the display name used in comments
* Returns the full_name for normal users, and "Rotex 1800" for 'rotex'
* users
*/
public function getCommentDisplayNameAttribute(): string
{
if ($this->hasRole('rotex')) {
return 'Rotex 1800';
}
return $this->full_name;
}
/**
*; @return HasOne
*
* @return HasOne<Passport>
*/
public function passport(): HasOne
{
return $this->hasOne(Passport::class, 'user_id');
}
/**
* Checks whether the user has registered for the given event.
*/
public function hasRegisteredFor(Event $event): bool
{
return $this->events()->where('id', $event->id)->exists();
}
/**
* @return BelongsToMany<Event>
*/
public function events(): BelongsToMany
{
return $this->belongsToMany(Event::class);
}
public function registrationComment(): HasOne
{
return $this->hasOne(RegistrationComment::class);
}
/**
* @retrun HasOne
*
* @return HasOne<RotaryInfo>
*/
public function rotaryInfo(): HasOne
{
return $this->hasOne(RotaryInfo::class, 'user_id');
}
/**
* @return HasOne<AdditionalInfo>
*/
public function additionalInfo(): HasOne
{
return $this->hasOne(AdditionalInfo::class, 'user_id');
}
public function firstHostFamily(): HostFamily
{
return $this->hostFamily(1);
}
public function hostFamily(int $order): HostFamily
{
$familyOrNull = $this
->hostFamilies()
->order($order)
->first();
if ($familyOrNull != null) {
return $familyOrNull;
}
return HostFamily::factory()->empty()->nth($order)->make();
}
public function secondHostFamily(): HostFamily
{
return $this->hostFamily(2);
}
public function thirdHostFamily(): HostFamily
{
return $this->hostFamily(3);
}
/**
* @return Collection<Event>
*/
public function canRegisterFor(): Collection
{
return $this->possibleEvents()
->diff($this->participatesIn());
}
/**
* All events that the user shares at least one role with.
*
*
* @return Collection<Event>
*/
public function possibleEvents(): Collection
{
$roleIds = $this->roles()->allRelatedIds();
return Event::whereHas(
'roles',
function (Builder $q) use ($roleIds) {
$q->whereIn('id', $roleIds);
}
)->get();
}
/**
* @return Collection<Event>
*/
public function participatesIn(): Collection
{
return $this->events()->get();
}
public function payments(): HasMany
{
return $this->hasMany(Payment::class);
}
public function isComplete(): bool
{
return
$this->notBlankOrEmpty($this->first_name)
&& $this->notBlankOrEmpty($this->family_name)
&& $this->notBlankOrEmpty($this->birthday)
&& $this->notBlankOrEmpty($this->gender)
&& $this->notBlankOrEmpty($this->mobile_phone)
&& (
$this->notBlankOrEmpty($this->additionalInfo?->tshirt_size->value ?? '')
&& ($this->additionalInfo?->tshirt_size ?? ClothesSize::NA) != ClothesSize::NA
)
&& $this->notBlankOrEmpty($this->health_issues);
}
/** @noinspection PhpUnused */
private function notBlankOrEmpty(?string $value): bool
{
return $value != null && trim($value) != '';
}
public function getShortNameAttribute(): string
{
$birthdayComponent = $this->birthday?->translatedFormat('dm') ?? 'XXXX';
$nameComponent = StringUtil::firstCharacterOfEachWord($this->full_name);
return $nameComponent.'-'.$birthdayComponent;
}
public function overallDocumentState(): DocumentState
{
$docStates = [];
foreach (DocumentCategory::validCategories() as $category) {
$docStates[] = $this->documentBy($category)->state ?? DocumentState::Missing;
}
$docStates = DocumentState::sort($docStates);
return $docStates[0];
}
public function sumPaidFor(Event $event): float
{
/** @var float $sum */
$sum = Payment::whereEventId($event->id)
->whereUserId($this->id)
->sum('amount');
return $sum;
}
}