1
|
|
|
<?php |
2
|
|
|
use Cviebrock\EloquentSluggable\SluggableInterface; |
3
|
|
|
use Cviebrock\EloquentSluggable\SluggableTrait; |
4
|
|
|
use Illuminate\Database\Eloquent\Collection; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* League |
8
|
|
|
* |
9
|
|
|
* @property integer $id |
10
|
|
|
* @property string $name |
11
|
|
|
* @property string $slug |
12
|
|
|
* @property string $description |
13
|
|
|
* @property string $url |
14
|
|
|
* @property string $mode |
15
|
|
|
* @property integer $money |
16
|
|
|
* @property string $units |
17
|
|
|
* @property integer $extra_weeks |
18
|
|
|
* @property \Carbon\Carbon $start_date |
19
|
|
|
* @property \Carbon\Carbon $end_date |
20
|
|
|
* @property boolean $private |
21
|
|
|
* @property boolean $featured |
22
|
|
|
* @property boolean $active |
23
|
|
|
* @property \Carbon\Carbon $created_at |
24
|
|
|
* @property \Carbon\Carbon $updated_at |
25
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League whereId($value) |
26
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League whereName($value) |
27
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League whereSlug($value) |
28
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League whereDescription($value) |
29
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League whereUrl($value) |
30
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League whereMode($value) |
31
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League whereMoney($value) |
32
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League whereUnits($value) |
33
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League whereExtraWeeks($value) |
34
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League whereStartDate($value) |
35
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League whereEndDate($value) |
36
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League wherePrivate($value) |
37
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League whereFeatured($value) |
38
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League whereActive($value) |
39
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League whereCreatedAt($value) |
40
|
|
|
* @method static \Illuminate\Database\Query\Builder|\League whereUpdatedAt($value) |
41
|
|
|
* @method static \League season($year, $season) |
42
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\User[] $admins |
43
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\LeagueMovie[] $movies |
44
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\LeagueTeam[] $teams |
45
|
|
|
*/ |
46
|
|
|
class League extends Eloquent implements SluggableInterface { |
|
|
|
|
47
|
|
|
|
48
|
|
|
use SluggableTrait; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Fields to format as dates |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $dates = ['start_date', 'end_date']; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Allow filling of these fields |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
protected $fillable = ['name', 'description', 'url', 'private', 'mode', 'money', 'units']; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Sluggable configuration |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
protected $sluggable = [ |
67
|
|
|
'build_from' => 'name', |
68
|
|
|
'save_to' => 'slug', |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* League's admins relationship (n:m) |
73
|
|
|
* |
74
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
75
|
|
|
*/ |
76
|
9 |
|
public function admins() { |
77
|
9 |
|
return $this->belongsToMany('User', 'league_admins') |
78
|
9 |
|
->withTimestamps(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* League's movies relationship (1:n) |
83
|
|
|
* |
84
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
85
|
|
|
*/ |
86
|
9 |
|
public function movies() { |
87
|
9 |
|
return $this->hasMany('LeagueMovie'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* League's teams relationship (1:n) |
92
|
|
|
* |
93
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
94
|
|
|
*/ |
95
|
1 |
|
public function teams() { |
96
|
1 |
|
return $this->hasMany('LeagueTeam'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Search by season scope |
101
|
|
|
* |
102
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
103
|
|
|
* @param int $year Season year |
104
|
|
|
* @param int $season Season id as defined by configuration |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
1 |
|
public function scopeSeason(Illuminate\Database\Eloquent\Builder $query, $year, $season) { |
109
|
1 |
|
$seasons = Config::get('draft.seasons'); |
110
|
1 |
|
$search_start = $seasons[$season]['start']; |
111
|
1 |
|
$search_end = $seasons[$season]['end']; |
112
|
1 |
|
$search_start[0] = $search_end[0] = $year; |
113
|
|
|
|
114
|
1 |
|
$search_start = call_user_func_array(['Carbon', 'create'], $search_start); |
115
|
1 |
|
$search_end = call_user_func_array(['Carbon', 'create'], $search_end); |
116
|
|
|
|
117
|
1 |
|
return $query->whereBetween('start_date', [$search_start, $search_end]); |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
public function getEndedAttribute() { |
121
|
1 |
|
return $this->end_date->isPast(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Check if user is an admin of the league |
126
|
|
|
* |
127
|
|
|
* @param User $user |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function userIsAdmin(User $user) { |
132
|
|
|
if (isset($this->relations['admins'])) { |
133
|
|
|
return $this->admins->contains($user); |
134
|
|
|
} else { |
135
|
|
|
return $this->admins()->where('user_id', $user->id)->count(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Update league's dates |
141
|
|
|
*/ |
142
|
|
|
public function updateLeagueDates() { |
143
|
|
|
/** @var Collection|LeagueMovie[] $movies */ |
144
|
|
|
$movies = $this->movies()->with('movie')->ordered()->get(); // Get up to date data |
145
|
|
|
|
146
|
|
|
if ($movies->count()) { |
147
|
|
|
/** @var Movie $earliest */ |
148
|
|
|
$earliest = $movies->first()->movie; |
149
|
|
|
/** @var Movie $latest */ |
150
|
|
|
$latest = $movies->last()->movie; |
151
|
|
|
|
152
|
|
|
// Start date = Earliest release, End date = Last release + extra weeks |
153
|
|
|
$this->start_date = $earliest->release; |
154
|
|
|
$this->end_date = $latest->release->copy()->addWeeks($this->extra_weeks); |
155
|
|
|
|
156
|
|
|
// The draft length must not be longer than the hard-coded limit |
157
|
|
|
$max_date = $this->start_date->copy()->addWeeks(Config::get('draft.maximum_weeks')); |
158
|
|
|
if ($this->end_date > $max_date) { |
159
|
|
|
$this->end_date = $max_date; |
160
|
|
|
} |
161
|
|
|
} else { |
162
|
|
|
// No movies = Start and end date well in the future |
163
|
|
|
$this->start_date = $this->end_date = Carbon::now()->addWeeks(Config::get('draft.maximum_weeks')); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Calculate the last |
169
|
|
|
* |
170
|
|
|
* @return Carbon |
171
|
|
|
*/ |
172
|
|
|
public function maxLastMovieDate() { |
173
|
|
|
return $this->start_date->copy()->addWeeks(Config::get('draft.maximum_weeks'))->subWeeks($this->extra_weeks); |
174
|
|
|
} |
175
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.