Issues (232)

app/Tournament.php (1 issue)

1
<?php
2
3
namespace App;
4
5
6
use Cocur\Slugify\Slugify;
7
//use Cviebrock\EloquentSluggable\Engines\IdeographicEngine;
8
//use Cviebrock\EloquentSluggable\Engines\KoreanEngine;
9
use Cviebrock\EloquentSluggable\Sluggable;
10
use Cviebrock\EloquentSluggable\SluggableScopeHelpers;
11
use Illuminate\Database\Eloquent\SoftDeletes;
12
use OwenIt\Auditing\Contracts\Auditable;
13
use Xoco70\LaravelTournaments\Models\ChampionshipSettings;
14
15
16
/**
17
 * @property mixed type
18
 * @property float latitude
19
 * @property float longitude
20
 * @property mixed created_at
21
 * @property mixed updated_at
22
 * @property mixed deleted_at
23
 */
24
class Tournament extends \Xoco70\LaravelTournaments\Models\Tournament implements Auditable
25
{
26
    use SoftDeletes, \OwenIt\Auditing\Auditable;
27
    use Sluggable;
28
    use SluggableScopeHelpers;
29
30
    public $timestamps = true;
31
    protected $table = 'tournament';
32
    protected $fillable = [
33
        'name',
34
        'dateIni',
35
        'dateFin',
36
        'registerDateLimit',
37
        'sport',
38
        'promoter',
39
        'host_organization',
40
        'technical_assistance',
41
        'category',
42
        'rule_id',
43
        'type',
44
        'venue_id',
45
        'level_id'
46
    ];
47
    protected $dates = ['dateIni', 'dateFin', 'registerDateLimit', 'created_at', 'updated_at', 'deleted_at'];
48
49 34
    protected static function boot()
50
    {
51 34
        parent::boot();
52 34
        static::deleting(function ($tournament) {
53 2
            $tournament->championships->each->delete();
54 2
            $tournament->invites->each->delete();
55
56 34
        });
57 34
        static::restoring(function ($tournament) {
58 1
            $tournament->championships()->withTrashed()->get()->each->restore();
59 34
        });
60
61 34
    }
62
63
    /**
64
     * Return the sluggable configuration array for this model.
65
     *
66
     * @return array
67
     */
68 20
    public function sluggable()
69
    {
70
        return [
71 20
            'slug' => [
72
                'source' => 'name'
73
            ]
74
        ];
75
    }
76
77
//    public function customizeSlugEngine(Slugify $engine, $attribute)
78
//    {
79
//        if (isJapanese($this->name)) { // Or Korean, or any unsupported language
80
//            return new IdeographicEngine();
81
//        }
82
//        if (isKorean($this->name)) { // Or Korean, or any unsupported language
83
//            return new KoreanEngine();
84
//        }
85
//        return $engine;
86
//    }
87
88
    /**
89
     * A tournament is owned by a user
90
     *
91
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
92
     */
93 13
    public function owner()
94
    {
95 13
        return $this->belongsTo(User::class, 'user_id', 'id');
96
    }
97
98
    /**
99
     * Get All Tournaments levels
100
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
101
     */
102 1
    public function level()
103
    {
104 1
        return $this->belongsTo(TournamentLevel::class, 'level_id', 'id');
105
    }
106
107
    /**
108
     * Get Full venue object
109
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
110
     */
111 8
    public function venue()
112
    {
113 8
        return $this->belongsTo(Venue::class);
114
    }
115
116
    /**
117
     * Get All categoriesTournament that belongs to a tournament
118
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
119
     */
120 15
    public function championships()
121
    {
122 15
        return $this->hasMany(Championship::class);
123
    }
124
125
    /**
126
     * Get All categoriesSettings that belongs to a tournament
127
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
128
     */
129 8
    public function championshipSettings()
130
    {
131 8
        return $this->hasManyThrough(ChampionshipSettings::class, Championship::class);
132
    }
133
134
    /**
135
     * çGet All teams that belongs to a tournament
136
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
137
     */
138 6
    public function teams()
139
    {
140 6
        return $this->hasManyThrough(Team::class, Championship::class);
141
    }
142
143
    /**
144
     * Get All competitors that belongs to a tournament
145
     * @param null $championshipId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $championshipId is correct as it would always require null to be passed?
Loading history...
146
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
147
     */
148 11
    public function competitors($championshipId = null)
149
    {
150 11
        return $this->hasManyThrough(Competitor::class, Championship::class);
151
    }
152
153
    /**
154
     * Get All trees that belongs to a tournament
155
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
156
     */
157 5
    public function trees()
158
    {
159 5
        return $this->hasManyThrough(FightersGroup::class, Championship::class);
160
    }
161
162
    /**
163
     * Get all Invitations that belongs to a tournament
164
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
165
     */
166 2
    public function invites()
167
    {
168 2
        return $this->morphMany(Invite::class, 'object');
169
    }
170
171
    /**
172
     * Get Category List with <Select> Format
173
     * @return mixed
174
     */
175 6
    public function getCategoryIdArray()
176
    {
177 6
        return $this->categories->pluck('id')->all();
178
    }
179
180
    public function getDateAttribute($date)
181
    {
182
        return $date;
183
    }
184
185 6
    public function getRegisterDateLimitAttribute($date)
186
    {
187 6
        return $date;
188
    }
189
190 8
    public function getDateIniAttribute($date)
191
    {
192 8
        return $date;
193
    }
194
195 6
    public function getDateFinAttribute($date)
196
    {
197 6
        return $date;
198
    }
199
200
    /**
201
     * Check if the tournament is Open
202
     * @return bool
203
     */
204 1
    public function isOpen()
205
    {
206 1
        return $this->type == 1;
207
    }
208
209
    /**
210
     * * Check if the tournament needs Invitation
211
     * @return bool
212
     */
213
    public function needsInvitation()
214
    {
215
        return $this->type == 0;
216
    }
217
218
    /**
219
     * @return bool
220
     */
221
    public function isInternational()
222
    {
223
        return $this->level_id == 8;
224
    }
225
226
    /**
227
     * @return bool
228
     */
229
    public function isNational()
230
    {
231
        return $this->level_id == 7;
232
    }
233
234
    /**
235
     * @return bool
236
     */
237
    public function isRegional()
238
    {
239
        return $this->level_id == 6;
240
    }
241
242
    /**
243
     * @return bool
244
     */
245
    public function isEstate()
246
    {
247
        return $this->level_id == 5;
248
    }
249
250
    /**
251
     * @return bool
252
     */
253
    public function isMunicipal()
254
    {
255
        return $this->level_id == 4;
256
    }
257
258
    /**
259
     * @return bool
260
     */
261
    public function isDistrictal()
262
    {
263
        return $this->level_id == 3;
264
    }
265
266
    /**
267
     * @return bool
268
     */
269
    public function isLocal()
270
    {
271
        return $this->level_id == 2;
272
    }
273
274
    /**
275
     * @return bool
276
     */
277
    public function hasNoLevel()
278
    {
279
        return $this->level_id == 1;
280
    }
281
282 15
    public function getRouteKeyName()
283
    {
284 15
        return 'slug';
285
    }
286
287
    /**
288
     * @return bool
289
     */
290 2
    public function isDeleted()
291
    {
292 2
        return $this->deleted_at != null;
293
    }
294
295
    /**
296
     * Create and Configure Championships depending the rule ( IKF, EKF, LAKF, etc )
297
     * @param $ruleId
298
     */
299 1
    public function setAndConfigureCategories($ruleId)
300
    {
301 1
        if ($ruleId == 0) return; // No Rules Selected
302
303 1
        $options = $this->loadRulesOptions($ruleId);
304
305
        // Create Tournament Categories
306 1
        $arrCategories = array_keys($options);
307 1
        $this->categories()->sync($arrCategories);
308
309
        // Configure each category creating categorySetting Object
310
311 1
        foreach ($this->championships as $championship) {
312 1
            $rules = $options[$championship->category->id];
313 1
            $rules['championship_id'] = $championship->id;
314 1
            ChampionshipSettings::create($rules);
315
        }
316 1
    }
317
318
    /**
319
     * return correct presets rues
320
     * @param $ruleId
321
     * @return mixed|null
322
     */
323 1
    private function loadRulesOptions($ruleId)
324
    {
325
        switch ($ruleId) {
326 1
            case 0: // No preset selected
327
                return null;
328 1
            case 1:
329 1
                return $options = config('options.ikf_settings');
330
                break;
331
            case 2:
332
                return $options = config('options.ekf_settings');
333
                break;
334
            case 3:
335
                return $options = config('options.lakc_settings');
336
                break;
337
            default:
338
                return null;
339
        }
340
    }
341
342
    /**
343
     * We can use $tournament->categories()->attach(id);
344
     * Or         $tournament->categories()->sync([1, 2, 3]);
345
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
346
     */
347 9
    public function categories()
348
    {
349 9
        return $this->belongsToMany(Category::class, 'championship')
350 9
            ->withPivot('id')
351 9
            ->withTimestamps();
352
    }
353
354
    /**
355
     * create a category List with Category name associated to championshipId
356
     *
357
     * @return array
358
     */
359
    public function buildCategoryList()
360
    {
361
        $championships = Championship::with('category', 'settings')
362
            ->whereHas('category', function ($query) {
363
                return $query->where('isTeam', 1);
364
            })
365
            ->where('tournament_id', $this->id)
366
            ->get();
367
368
        $array = [];
369
        foreach ($championships as $championship) {
370
            $array[$championship->id] = $championship->settings->alias != ''
371
                ? $championship->settings->alias
372
                : trim($championship->buildName());
373
        }
374
        return $array;
375
    }
376
377
}