1 | <?php |
||
10 | class Championship extends Model |
||
11 | { |
||
12 | use SoftDeletes; |
||
13 | protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
||
14 | protected $table = 'championship'; |
||
15 | |||
16 | public $timestamps = true; |
||
17 | protected $fillable = [ |
||
18 | "tournament_id", |
||
19 | "category_id", |
||
20 | ]; |
||
21 | |||
22 | protected static function boot() |
||
23 | { |
||
24 | parent::boot(); |
||
25 | |||
26 | static::deleting(function ($championship) { |
||
27 | $championship->competitors()->delete(); |
||
28 | $championship->settings()->delete(); |
||
29 | }); |
||
30 | static::restoring(function ($championship) { |
||
31 | $championship->competitors()->restore(); |
||
32 | $championship->settings()->restore(); |
||
33 | |||
34 | }); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * A championship has many Competitors |
||
39 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
40 | */ |
||
41 | public function competitors() |
||
42 | { |
||
43 | return $this->hasMany(Competitor::class, 'championship_id', 'id'); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * A championship belongs to a Category |
||
48 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
49 | */ |
||
50 | public function category() |
||
51 | { |
||
52 | return $this->belongsTo(Category::class); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * A championship belongs to a Tournament |
||
57 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
58 | */ |
||
59 | public function tournament() |
||
60 | { |
||
61 | return $this->belongsTo(Tournament::class); |
||
62 | } |
||
63 | |||
64 | |||
65 | /** |
||
66 | * Get All competitors from a Championships |
||
67 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
68 | */ |
||
69 | public function users() |
||
70 | { |
||
71 | return $this->belongsToMany(User::class, 'competitor', 'championship_id') |
||
72 | ->withPivot('confirmed') |
||
73 | ->withTimestamps(); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * A championship only has 1 Settings |
||
78 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
||
79 | */ |
||
80 | public function settings() |
||
81 | { |
||
82 | return $this->hasOne(ChampionshipSettings::class); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * A championship has Many Teams |
||
87 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
88 | */ |
||
89 | public function teams() |
||
90 | { |
||
91 | return $this->hasMany(Team::class); |
||
92 | } |
||
93 | |||
94 | |||
95 | /** |
||
96 | * Check if Championship has Preliminary Round Configured |
||
97 | * @return bool |
||
98 | */ |
||
99 | public function hasPreliminary() |
||
100 | { |
||
101 | return ($this->settings == null || $this->settings->hasPreliminary); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Check if 2nd Round of Championship is Round Robin |
||
106 | * @return bool |
||
107 | */ |
||
108 | public function isRoundRobinType() |
||
109 | { |
||
110 | return ($this->settings != null && $this->settings->treeType == Config::get('kendo-tournaments.ROUND_ROBIN')); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Check if 2nd Round of Championship is Direct Elimination |
||
115 | * @return bool |
||
116 | */ |
||
117 | public function isDirectEliminationType() |
||
118 | { |
||
119 | return ($this->settings == null || $this->settings->treeType == Config::get('kendo-tournaments.DIRECT_ELIMINATION')); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * A championship has Many Rounds |
||
124 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
125 | */ |
||
126 | public function rounds() |
||
127 | { |
||
128 | return $this->hasMany(Round::class, 'championship_id', 'round_id'); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * A championship has Many fights |
||
133 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
134 | */ |
||
135 | public function fights() |
||
139 | |||
140 | |||
141 | |||
142 | } |
||
143 |