1 | <?php |
||
2 | |||
3 | namespace Xoco70\LaravelTournaments\Models; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Model; |
||
6 | use Illuminate\Database\Eloquent\SoftDeletes; |
||
7 | |||
8 | /** |
||
9 | * @property mixed type |
||
10 | * @property float latitude |
||
11 | * @property float longitude |
||
12 | * @property mixed created_at |
||
13 | * @property mixed updated_at |
||
14 | * @property mixed deleted_at |
||
15 | */ |
||
16 | class Tournament extends Model |
||
17 | { |
||
18 | use SoftDeletes; |
||
19 | |||
20 | /** |
||
21 | * Return the sluggable configuration array for this model. |
||
22 | * |
||
23 | * @return array |
||
24 | */ |
||
25 | public function sluggable() |
||
26 | { |
||
27 | return [ |
||
28 | 'slug' => [ |
||
29 | 'source' => 'name', |
||
30 | ], |
||
31 | ]; |
||
32 | } |
||
33 | |||
34 | protected $table = 'tournament'; |
||
35 | public $timestamps = true; |
||
36 | |||
37 | protected $fillable = [ |
||
38 | 'name', |
||
39 | 'slug', |
||
40 | 'dateIni', |
||
41 | 'dateFin', |
||
42 | 'registerDateLimit', |
||
43 | 'sport', |
||
44 | 'promoter', |
||
45 | 'host_organization', |
||
46 | 'technical_assistance', |
||
47 | 'category', |
||
48 | 'rule_id', |
||
49 | 'type', |
||
50 | 'venue_id', |
||
51 | 'level_id', |
||
52 | ]; |
||
53 | |||
54 | protected $dates = ['dateIni', 'dateFin', 'registerDateLimit', 'created_at', 'updated_at', 'deleted_at']; |
||
55 | |||
56 | protected static function boot() |
||
57 | { |
||
58 | parent::boot(); |
||
59 | static::deleting(function ($tournament) { |
||
60 | foreach ($tournament->championships as $ct) { |
||
61 | $ct->delete(); |
||
62 | } |
||
63 | // $tournament->invites()->delete(); |
||
0 ignored issues
–
show
|
|||
64 | }); |
||
65 | static::restoring(function ($tournament) { |
||
66 | foreach ($tournament->championships()->withTrashed()->get() as $ct) { |
||
67 | $ct->restore(); |
||
68 | } |
||
69 | }); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * A tournament is owned by a user. |
||
74 | * |
||
75 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
76 | */ |
||
77 | public function owner() |
||
78 | { |
||
79 | return $this->belongsTo(config('laravel-tournaments.user.model'), 'user_id', 'id'); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Get Full venue object. |
||
84 | * |
||
85 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
86 | */ |
||
87 | public function venue() |
||
88 | { |
||
89 | return $this->belongsTo(Venue::class); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * We can use $tournament->categories()->attach(id); |
||
94 | * Or $tournament->categories()->sync([1, 2, 3]);. |
||
95 | * |
||
96 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
97 | */ |
||
98 | public function categories() |
||
99 | { |
||
100 | return $this->belongsToMany(Category::class, 'championship') |
||
101 | ->withPivot('id') |
||
102 | ->withTimestamps(); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Get All categoriesTournament that belongs to a tournament. |
||
107 | * |
||
108 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
109 | */ |
||
110 | public function championships() |
||
111 | { |
||
112 | return $this->hasMany(Championship::class); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Get All categoriesSettings that belongs to a tournament. |
||
117 | * |
||
118 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
119 | */ |
||
120 | public function championshipSettings() |
||
121 | { |
||
122 | return $this->hasManyThrough(ChampionshipSettings::class, Championship::class); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Get All teams that belongs to a tournament. |
||
127 | * |
||
128 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
129 | */ |
||
130 | public function teams() |
||
131 | { |
||
132 | return $this->hasManyThrough(Team::class, Championship::class); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Get All competitors that belongs to a tournament. |
||
137 | * |
||
138 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
139 | */ |
||
140 | public function competitors() |
||
141 | { |
||
142 | return $this->hasManyThrough(Competitor::class, Championship::class); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Get All trees that belongs to a tournament. |
||
147 | * |
||
148 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
149 | */ |
||
150 | public function trees() |
||
151 | { |
||
152 | return $this->hasManyThrough(FightersGroup::class, Championship::class); |
||
153 | } |
||
154 | |||
155 | public function getDateAttribute($date) |
||
156 | { |
||
157 | return $date; |
||
158 | } |
||
159 | |||
160 | public function getRegisterDateLimitAttribute($date) |
||
161 | { |
||
162 | return $date; |
||
163 | } |
||
164 | |||
165 | public function getDateIniAttribute($date) |
||
166 | { |
||
167 | return $date; |
||
168 | } |
||
169 | |||
170 | public function getDateFinAttribute($date) |
||
171 | { |
||
172 | return $date; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Check if the tournament is Open. |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | public function isOpen() |
||
181 | { |
||
182 | return $this->type == 1; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * * Check if the tournament needs Invitation. |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | public function needsInvitation() |
||
191 | { |
||
192 | return $this->type == 0; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @return bool |
||
197 | */ |
||
198 | public function isInternational() |
||
199 | { |
||
200 | return $this->level_id == 8; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return bool |
||
205 | */ |
||
206 | public function isNational() |
||
207 | { |
||
208 | return $this->level_id == 7; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @return bool |
||
213 | */ |
||
214 | public function isRegional() |
||
215 | { |
||
216 | return $this->level_id == 6; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @return bool |
||
221 | */ |
||
222 | public function isEstate() |
||
223 | { |
||
224 | return $this->level_id == 5; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function isMunicipal() |
||
231 | { |
||
232 | return $this->level_id == 4; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * @return bool |
||
237 | */ |
||
238 | public function isDistrictal() |
||
239 | { |
||
240 | return $this->level_id == 3; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @return bool |
||
245 | */ |
||
246 | public function isLocal() |
||
247 | { |
||
248 | return $this->level_id == 2; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @return bool |
||
253 | */ |
||
254 | public function hasNoLevel() |
||
255 | { |
||
256 | return $this->level_id == 1; |
||
257 | } |
||
258 | |||
259 | public function getRouteKeyName() |
||
260 | { |
||
261 | return 'slug'; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @return bool |
||
266 | */ |
||
267 | public function isDeleted() |
||
268 | { |
||
269 | return $this->deleted_at != null; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Check if the tournament has a Team Championship. |
||
274 | * |
||
275 | * @return int |
||
276 | */ |
||
277 | public function hasTeamCategory() |
||
278 | { |
||
279 | return $this |
||
280 | ->categories() |
||
281 | ->where('isTeam', '1') |
||
282 | ->count(); |
||
283 | } |
||
284 | } |
||
285 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.