1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use OwenIt\Auditing\Contracts\Auditable; |
9
|
|
|
use stdClass; |
10
|
|
|
|
11
|
|
|
class Association extends Model implements Auditable |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
public $timestamps = true; |
15
|
|
|
protected $table = 'association'; |
16
|
|
|
protected $guarded = ['id']; |
17
|
|
|
use SoftDeletes, \OwenIt\Auditing\Auditable; |
|
|
|
|
18
|
|
|
|
19
|
|
|
protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Same than fillSelect, but with VueJS Format |
23
|
|
|
* @param $user |
24
|
|
|
* @param $federationId |
25
|
|
|
* @return Collection |
26
|
|
|
*/ |
27
|
|
|
public static function fillSelectForVueJs($user, $federationId) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$associations = Association::where('federation_id', $federationId) |
30
|
|
|
->get(['id as value', 'name as text']); |
31
|
|
|
if (sizeof($associations) == 0) { |
32
|
|
|
$object = new stdClass; |
33
|
|
|
$object->value = 0; |
34
|
|
|
$object->text = trans('structures.no_association_yet'); |
35
|
|
|
$associations->push($object); |
36
|
|
|
}; |
37
|
|
|
return $associations; |
38
|
|
|
} |
39
|
|
|
|
40
|
15 |
|
protected static function boot() |
41
|
|
|
{ |
42
|
15 |
|
parent::boot(); |
43
|
15 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* An association has only 1 President |
47
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
48
|
|
|
*/ |
49
|
|
|
public function president() |
50
|
|
|
{ |
51
|
|
|
return $this->hasOne(User::class, 'id', 'president_id'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* An association has Many Clubs |
56
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
57
|
|
|
*/ |
58
|
|
|
public function clubs() |
59
|
|
|
{ |
60
|
|
|
return $this->hasMany(Club::class); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* An association belongs To a Federations |
65
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
66
|
|
|
*/ |
67
|
7 |
|
public function federation() |
68
|
|
|
{ |
69
|
7 |
|
return $this->belongsTo(Federation::class); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Filter the assocation List depending on the user type |
74
|
|
|
* Ex : SuperAdmin See all, Federation President only see his Associations, etc |
75
|
|
|
* @param $query |
76
|
|
|
* @param User $user |
77
|
|
|
*/ |
78
|
6 |
|
public function scopeForUser($query, User $user) |
79
|
|
|
{ |
80
|
|
|
// Limit association to the same country |
81
|
6 |
|
if (!$user->isSuperAdmin()) { |
82
|
4 |
|
$query->whereHas('federation', function ($query) use ($user) { |
83
|
4 |
|
$query->where('country_id', $user->country_id); |
84
|
4 |
|
}); |
85
|
|
|
} |
86
|
6 |
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Check if an Association is in the same Federation than a Federation President |
92
|
|
|
* @param User $user |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
1 |
|
public function belongsToFederationPresident(User $user) |
96
|
|
|
{ |
97
|
1 |
|
if ($user->isFederationPresident() && |
98
|
1 |
|
$user->federationOwned != null && |
99
|
1 |
|
$this->federation->id == $user->federationOwned->id |
100
|
|
|
) |
101
|
|
|
return true; |
102
|
1 |
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Check if an Association is in the same Association than a Association President |
107
|
|
|
* @param User $user |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
2 |
|
public function belongsToAssociationPresident(User $user) |
111
|
|
|
{ |
112
|
2 |
|
if ($user->isAssociationPresident() && |
113
|
2 |
|
$user->associationOwned != null && |
114
|
2 |
|
$this->id == $user->associationOwned->id |
115
|
|
|
) { |
116
|
1 |
|
return true; |
117
|
|
|
} |
118
|
1 |
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |