1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Association; |
6
|
|
|
use App\Division; |
7
|
|
|
use App\Match; |
8
|
|
|
use App\ResultSubmission; |
9
|
|
|
use App\Round; |
10
|
|
|
use App\Series; |
11
|
|
|
use App\Schedule; |
12
|
|
|
use App\User; |
13
|
|
|
use App\Venue; |
14
|
|
|
use Bouncer; |
15
|
|
|
use Illuminate\Http\Request; |
16
|
|
|
|
17
|
|
|
class AssociationsController extends Controller |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
public function __construct(Request $request) { |
21
|
|
|
$subdomain = array_first(explode('.', \Request::getHost())); |
|
|
|
|
22
|
|
|
|
23
|
|
|
$this->association = Association::where('subdomain', $subdomain)->first(); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function view(Association $association) { |
27
|
|
|
return view('association.view', ['association' => $association]); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function edit(Association $association) { |
31
|
|
|
if (Bouncer::can('edit', $association)) { |
32
|
|
|
return view('association.edit', [ |
33
|
|
|
'association' => $association, |
34
|
|
|
'series' => Series::where('association_id', $association->id)->get(), |
35
|
|
|
'divisions' => Division::orderBy('sequence', 'ASC')->where('association_id', $association->id)->get(), |
36
|
|
|
'venues' => Venue::orderBy('name', 'ASC')->where('association_id', $association->id)->get(), |
37
|
|
|
'current_user' => \Auth::user() |
38
|
|
|
]); |
39
|
|
|
} |
40
|
|
|
else { |
41
|
|
|
return view('denied'); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function home() { |
46
|
|
|
if (!empty($this->association)) { |
47
|
|
|
return view('association.home', ['association' => $this->association]); |
48
|
|
|
} |
49
|
|
|
else { |
50
|
|
|
abort(404); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function divisions(Association $association) { |
55
|
|
|
return view('association.divisions', ['association' => $association]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function teams(Association $association) { |
59
|
|
|
return view('association.teams', ['association' => $association]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function venues(Association $association) { |
63
|
|
|
return view('association.venues', ['association' => $association]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function series(Association $association) { |
67
|
|
|
return view('association.series', ['association' => $association]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function users(Association $association) { |
71
|
|
|
return view('association.users', ['association' => $association]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function editUser(Association $association, User $user) { |
75
|
|
|
return view('association.user.edit', ['association' => $association, 'user' => $user]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function updateUser(Request $request, Association $association, User $user) { |
79
|
|
|
|
80
|
|
|
if (isset($request->assoc_admin)) { |
81
|
|
|
Bouncer::assign('assocadmin')->to($user); |
82
|
|
|
Bouncer::allow($user)->toManage($association); |
83
|
|
|
} |
84
|
|
|
else { |
85
|
|
|
Bouncer::disallow($user)->toManage($association); |
86
|
|
|
Bouncer::retract('assocadmin')->from($user); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$url = $request->url; |
90
|
|
|
|
91
|
|
|
if (!empty($url)) { |
92
|
|
|
return redirect($url)->with('success', 'Data saved successfully!'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return redirect()->route('user', ['id' => \Auth::user()->id]); |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function addUser(Association $association) { |
100
|
|
|
return view('association.user.add', ['association' => $association]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function submitScoreBegin(Request $request) { |
104
|
|
|
if (!empty($this->association)) { |
105
|
|
|
// get schedules with start_date < today, end_date > today |
106
|
|
|
$schedules = $this->association->schedules |
107
|
|
|
->where('start_date', '<', date('Y-m-d', strtotime('today'))) |
108
|
|
|
->where('end_date', '>', date('Y-m-d', strtotime('today'))); |
109
|
|
|
|
110
|
|
|
// get rounds with start_date < today, but greater than today - 1 week |
111
|
|
|
$rounds = Round::whereIn('schedule_id', $schedules->pluck('id')) |
112
|
|
|
->where('start_date','>=', date('Y-m-d', strtotime('-1 week'))) |
113
|
|
|
->where('start_date', '<=', date('Y-m-d', strtotime("today")))->get(); |
114
|
|
|
|
115
|
|
|
$divisions = Division::whereIn('id', $rounds->pluck('division_id'))->get(); |
116
|
|
|
|
117
|
|
|
if (count($divisions) === 1) { |
118
|
|
|
$request->division_id = $divisions[0]->id; |
|
|
|
|
119
|
|
|
|
120
|
|
|
return $this->submitScoreStep2($request); |
121
|
|
|
} |
122
|
|
|
else { |
123
|
|
|
return view('forms.results.choose-division', [ |
124
|
|
|
'association' => $this->association, |
125
|
|
|
'divisions' => $divisions, |
126
|
|
|
]); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
else { |
130
|
|
|
abort(404); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function submitScoreStep2(Request $request) { |
135
|
|
|
if (!empty($this->association)) { |
136
|
|
|
$division = Division::find($request->division_id); |
137
|
|
|
|
138
|
|
|
// get schedules with start_date < today, end_date > today, matching division |
139
|
|
|
$schedules = $this->association->schedules |
140
|
|
|
->where('start_date', '<=', date('Y-m-d', strtotime('today'))) |
141
|
|
|
->where('end_date', '>=', date('Y-m-d', strtotime('today'))) |
142
|
|
|
->where('division_id', $division->id); |
143
|
|
|
|
144
|
|
|
// get rounds with start_date < today, but greater than today - 1 week, not closed |
145
|
|
|
$rounds = Round::whereIn('schedule_id', $schedules->pluck('id')) |
146
|
|
|
->where('start_date', '>=', date('Y-m-d', strtotime('-1 week'))) |
147
|
|
|
->where('start_date', '<=', date('Y-m-d', strtotime("today"))) |
148
|
|
|
->where(function ($query) { |
149
|
|
|
$query->where('scores_closed', 0); |
150
|
|
|
$query->orWhereNull('scores_closed'); |
151
|
|
|
}) |
152
|
|
|
->orderBy('start_date', 'DESC') |
153
|
|
|
->get(); |
154
|
|
|
|
155
|
|
|
return view('forms.results.choose-match', [ |
156
|
|
|
'association' => $this->association, |
157
|
|
|
'rounds' => $rounds, |
158
|
|
|
]); |
159
|
|
|
} |
160
|
|
|
else { |
161
|
|
|
abort(404); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function submitScoreStep3(Request $request) { |
166
|
|
|
if (!empty($this->association)) { |
167
|
|
|
$match = Match::find($request->match_id); |
168
|
|
|
|
169
|
|
|
return view('forms.results.input-scores', [ |
170
|
|
|
'association' => $this->association, |
171
|
|
|
'match' => $match, |
172
|
|
|
]); |
173
|
|
|
} |
174
|
|
|
else { |
175
|
|
|
abort(404); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function submitScoreStep4(Request $request) { |
180
|
|
|
if (!empty($this->association)) { |
181
|
|
|
$match_id = $request->match_id; |
182
|
|
|
|
183
|
|
|
if (!empty($match_id)) { |
184
|
|
|
$home_team_id = $request->home_team_id; |
185
|
|
|
$away_team_id = $request->away_team_id; |
186
|
|
|
$home_team_score = $request->home_team_score; |
187
|
|
|
$away_team_score = $request->away_team_score; |
188
|
|
|
|
189
|
|
|
$submission = new ResultSubmission(); |
190
|
|
|
$submission->association_id = $this->association->id; |
191
|
|
|
$submission->schedule_id = Match::find($match_id)->schedule_id; |
192
|
|
|
$submission->match_id = $match_id; |
193
|
|
|
$submission->home_team_score = $home_team_score; |
194
|
|
|
$submission->away_team_score = $away_team_score; |
195
|
|
|
$submission->save(); |
196
|
|
|
|
197
|
|
|
if ($home_team_score != $away_team_score) { |
198
|
|
|
$submission->win_team_id = $home_team_score > $away_team_score ? $home_team_id : $away_team_id; |
199
|
|
|
$submission->save(); |
200
|
|
|
|
201
|
|
|
return view('forms.results.thanks', [ |
202
|
|
|
'association' => $this->association, |
203
|
|
|
]); |
204
|
|
|
} |
205
|
|
|
else { |
206
|
|
|
return view('forms.results.choose-winner', [ |
207
|
|
|
'association' => $this->association, |
208
|
|
|
'match' => Match::find($submission->match_id), |
209
|
|
|
'submission' => $submission, |
210
|
|
|
]); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
else { |
214
|
|
|
abort(404); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
else { |
218
|
|
|
abort(404); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function submitScoreStep5(Request $request) { |
223
|
|
|
if (!empty($this->association)) { |
224
|
|
|
$submission_id = $request->submission_id; |
225
|
|
|
|
226
|
|
|
if (!empty($submission_id)) { |
227
|
|
|
$submission = ResultSubmission::find($submission_id); |
228
|
|
|
|
229
|
|
|
$submission->win_team_id = $request->win_team_id; |
230
|
|
|
|
231
|
|
|
$submission->save(); |
232
|
|
|
|
233
|
|
|
return view('forms.results.thanks', [ |
234
|
|
|
'association' => $this->association, |
235
|
|
|
]); |
236
|
|
|
} |
237
|
|
|
else { |
238
|
|
|
abort(404); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
else { |
242
|
|
|
abort(404); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function standings() { |
247
|
|
|
return view('association.standings', ['association' => $this->association]); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function schedule() { |
251
|
|
|
return view('association.schedule', ['association' => $this->association]); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function css() { |
255
|
|
|
$response = \Response::make('body { background-color: red; }'); |
256
|
|
|
$response->header('Content-Type', 'text/css'); |
257
|
|
|
return $response; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Store a new association. |
262
|
|
|
* |
263
|
|
|
* @param Request $request |
264
|
|
|
* @return Response |
|
|
|
|
265
|
|
|
*/ |
266
|
|
|
public function store(Request $request) { |
267
|
|
|
if (Bouncer::can('create', Association::class)) { |
268
|
|
|
$association = new Association; |
269
|
|
|
|
270
|
|
|
$association->name = $request->name; |
271
|
|
|
$association->user_id = $request->user_id; |
272
|
|
|
|
273
|
|
|
$association->save(); |
274
|
|
|
|
275
|
|
|
// TODO: Do not necessarily "onboard" for certain roles? |
276
|
|
|
return redirect()->route('onboard.association', ['association' => $association]); |
|
|
|
|
277
|
|
|
} |
278
|
|
|
else { |
279
|
|
|
return view('denied'); |
|
|
|
|
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function update(Request $request) { |
284
|
|
|
|
285
|
|
|
$association = Association::find($request->id); |
286
|
|
|
|
287
|
|
|
$association->name = $request->name; |
288
|
|
|
$association->user_id = $request->user_id; |
289
|
|
|
|
290
|
|
|
if (isset($request->subdomain)) { |
291
|
|
|
$association->subdomain = $request->subdomain; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
if (isset($request->home_image_file)) { |
295
|
|
|
$path = $request->home_image_file->storeAs( |
296
|
|
|
'home_image_file/' . $association->subdomain, $request->home_image_file->getClientOriginalName(), 'public' |
297
|
|
|
); |
298
|
|
|
|
299
|
|
|
$association->home_image_path = $path; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$association->save(); |
303
|
|
|
|
304
|
|
|
//Session::flash('message', 'Successfully updated nerd!'); |
305
|
|
|
|
306
|
|
|
$url = $request->url; |
307
|
|
|
|
308
|
|
|
if (!empty($url)) { |
309
|
|
|
return redirect($url)->with('success', 'Data saved successfully!'); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return redirect()->route('user', ['id' => \Auth::user()->id]); |
313
|
|
|
|
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public function create() { |
317
|
|
|
if (Bouncer::can('create', Association::class)) { |
318
|
|
|
return view('association.create', ['current_user' => \Auth::user()]); |
319
|
|
|
} |
320
|
|
|
else { |
321
|
|
|
return view('denied'); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public function deleteConfirm(Association $association) { |
326
|
|
|
return view('association.delete', ['association' => $association]); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function delete(Association $association) { |
330
|
|
|
$association->delete(); |
331
|
|
|
|
332
|
|
|
return redirect()->route('admin')->with('success', 'Association deleted successfully.'); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function undeleteConfirm(Association $association) { |
336
|
|
|
return view('association.undelete', ['association' => $association]); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
public function undelete(Association $association) { |
340
|
|
|
$association->restore(); |
341
|
|
|
|
342
|
|
|
return redirect()->route('user', ['user' => \Auth::user()])->with('success', 'Association restored successfully.'); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
|
346
|
|
|
|
347
|
|
|
} |
348
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.