1 | <?php |
||
2 | |||
3 | namespace App; |
||
4 | |||
5 | use Illuminate\Auth\Access\AuthorizationException; |
||
6 | use Illuminate\Database\Eloquent\Builder; |
||
7 | use Illuminate\Database\Eloquent\Model; |
||
8 | use Illuminate\Database\Eloquent\SoftDeletes; |
||
9 | use Illuminate\Support\Collection; |
||
10 | use OwenIt\Auditing\AuditingTrait; |
||
11 | use OwenIt\Auditing\Contracts\Auditable; |
||
12 | |||
13 | class Club extends Model implements Auditable |
||
14 | { |
||
15 | public $timestamps = true; |
||
16 | protected $table = 'club'; |
||
17 | protected $guarded = ['id']; |
||
18 | use SoftDeletes, \OwenIt\Auditing\Auditable; |
||
19 | |||
20 | protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
||
21 | |||
22 | /** |
||
23 | * Fill Selects depending on the Logged user type |
||
24 | * @return Collection |
||
25 | */ |
||
26 | 1 | public static function fillSelect($federationId, $associationId) |
|
27 | { |
||
28 | 1 | if ($associationId == 0 || $associationId == null) { |
|
29 | // Get list of associations in the federation |
||
30 | 1 | $clubs = Club::where('federation_id', $federationId)->pluck('name', 'id'); |
|
31 | } else if ($federationId != 0) { |
||
32 | $clubs = Club::where('association_id', $associationId)->pluck('name', 'id'); |
||
33 | } else { |
||
34 | $clubs = new Collection; |
||
35 | } |
||
36 | 1 | return $clubs; |
|
37 | } |
||
38 | |||
39 | /** |
||
40 | * Same than fillSelect, but with VueJS Format |
||
41 | * @param User $user |
||
42 | * @param $federationId |
||
43 | * @param $associationId |
||
44 | * @return Collection |
||
45 | */ |
||
46 | public static function fillSelectForVueJs(User $user, $federationId, $associationId) |
||
0 ignored issues
–
show
|
|||
47 | { |
||
48 | if ($associationId == 0) { |
||
49 | // Get list of associations in the federation |
||
50 | $clubs = Club::where('federation_id', $federationId) |
||
51 | ->get(['id as value', 'name as text']); |
||
52 | } else if ($federationId != 0) { |
||
53 | $clubs = Club::where('association_id', $associationId) |
||
54 | ->get(['id as value', 'name as text']); |
||
55 | } else { |
||
56 | $clubs = new Collection; |
||
57 | } |
||
58 | return $clubs; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * A Club has only a President |
||
63 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
||
64 | */ |
||
65 | public function president() |
||
66 | { |
||
67 | return $this->hasOne(User::class, 'id', 'president_id'); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * A Club has many practicants |
||
72 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
73 | */ |
||
74 | public function practicants() |
||
75 | { |
||
76 | return $this->hasMany('User'); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Get the Federation that federate the Club |
||
81 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
82 | */ |
||
83 | 2 | public function federation() |
|
84 | { |
||
85 | 2 | if ($this->association != null) |
|
86 | return $this->association->federation(); |
||
87 | else |
||
88 | 2 | return $this->association(); //TODO THIS IS BAD :( |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * A Club belongs to an Association |
||
93 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
94 | */ |
||
95 | 2 | public function association() |
|
96 | { |
||
97 | 2 | return $this->belongsTo(Association::class); |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Filter the Club List depending on the user type |
||
102 | * Ex : SuperAdmin See all, Federation President only see his Clubs, etc |
||
103 | * @param $query - The builder |
||
104 | * @param User $user |
||
105 | * @return Builder |
||
106 | * @throws AuthorizationException |
||
107 | */ |
||
108 | 1 | public function scopeForUser($query, User $user) |
|
109 | { |
||
110 | switch (true) { |
||
111 | 1 | case $user->isSuperAdmin(): |
|
112 | 1 | return $query; |
|
113 | case $user->isFederationPresident() && $user->federationOwned != null: |
||
114 | return $query->where('federation_id', $user->federationOwned->id); |
||
115 | case |
||
116 | $user->isAssociationPresident() && $user->associationOwned: |
||
117 | return $query->whereHas('association', function ($query) use ($user) { |
||
118 | $query->where('id', $user->associationOwned->id); |
||
119 | }); |
||
120 | case $user->isClubPresident() && $user->clubOwned: |
||
121 | return $query->where('id', $user->clubOwned->id); |
||
122 | default: |
||
123 | throw new AuthorizationException(); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Check if the Club is in the same Federation than a Federation President |
||
129 | * @param User $user |
||
130 | * @return bool |
||
131 | */ |
||
132 | public function belongsToFederationPresident(User $user) // Should be FederationUser???? |
||
133 | { |
||
134 | return |
||
135 | $user->federationOwned != null && |
||
136 | $user->federationOwned->id == $this->federation_id; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Check if the Club is in the same Association than a Association President |
||
141 | * @param User $user |
||
142 | * @return bool |
||
143 | */ |
||
144 | 1 | public function belongsToAssociationPresident(User $user) |
|
145 | { |
||
146 | 1 | return $user->associationOwned != null && |
|
147 | 1 | $this->association != null && |
|
148 | 1 | $user->associationOwned->id == $this->association->id; |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * Check if the Club is in the same Association than a Club President |
||
153 | * @param User $user |
||
154 | * @return bool |
||
155 | */ |
||
156 | 1 | public function belongsToClubPresident(User $user) |
|
157 | { |
||
158 | return |
||
159 | 1 | $user->clubOwned != null && |
|
160 | 1 | $user->clubOwned->id == $this->id; |
|
161 | } |
||
162 | |||
163 | |||
164 | } |
||
165 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.