Issues (84)

app/Http/Controllers/AssociationsController.php (8 issues)

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Association;
6
use App\ContactSubmission;
7
use App\Division;
8
use App\PLMatch;
9
use App\ResultSubmission;
10
use App\Round;
11
use App\Series;
12
use App\Schedule;
13
use App\User;
14
use App\Venue;
15
use Bouncer;
0 ignored issues
show
The type Bouncer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Illuminate\Http\Request;
17
use Illuminate\Support\Arr;
18
use Illuminate\Support\Str;
19
20
class AssociationsController extends Controller
21
{
22
23
    public function __construct(Request $request) {
24
        $subdomain = Arr::first(explode('.', \Request::getHost()));
25
26
        $this->association = Association::where('subdomain', $subdomain)->first();
0 ignored issues
show
Bug Best Practice introduced by
The property association does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27
    }
28
29
    public function view(Association $association) {
30
        return view('association.view', ['association' => $association]);
31
    }
32
33
    public function edit(Association $association) {
34
        if (Bouncer::can('edit', $association)) {
35
            return view('association.edit', [
36
                'association' => $association,
37
                'series' => Series::where('association_id', $association->id)->get(),
38
                'divisions' => Division::orderBy('sequence', 'ASC')->where('association_id', $association->id)->get(),
39
                'venues' => Venue::orderBy('name', 'ASC')->where('association_id', $association->id)->get(),
40
                'current_user' => \Auth::user()
41
            ]);
42
        }
43
        else {
44
            return view('denied');
45
        }
46
    }
47
48
    public function home() {
49
        if (!empty($this->association)) {
50
            return view('association.home', ['association' => $this->association]);
51
        }
52
        else {
53
            abort(404);
54
        }
55
    }
56
57
    public function divisions(Association $association) {
58
        return view('association.divisions', ['association' => $association]);
59
    }
60
61
    public function teams(Association $association) {
62
        return view('association.teams', ['association' => $association]);
63
    }
64
65
    public function venues(Association $association) {
66
        return view('association.venues', ['association' => $association]);
67
    }
68
69
    public function series(Association $association) {
70
        return view('association.series', ['association' => $association]);
71
    }
72
73
    public function users(Association $association) {
74
        return view('association.users', ['association' => $association]);
75
    }
76
77
    public function viewUser(Association $association, User $user) {
78
        return view('association.user.view', ['association' => $association, 'user' => $user]);
79
    }
80
81
    public function editUser(Association $association, User $user) {
82
        return view('association.user.edit', ['association' => $association, 'user' => $user]);
83
    }
84
85
    public function userToken(Association $association, User $user) {
86
        return view('association.user.token', ['association' => $association, 'user' => $user]);
87
    }
88
89
    public function userTokenRefresh(Association $association, User $user) {
90
        $token = Str::random(60);
91
92
        $user->forceFill([
93
            'api_token' => hash('sha256', $token),
94
        ])->save();
95
96
        return view('association.user.token-refresh', ['association' => $association, 'user' => $user, 'token' => $token]);
97
    }
98
99
    public function updateUser(Request $request, Association $association, User $user) {
100
101
        if (isset($request->assoc_admin)) {
102
            Bouncer::assign('assocadmin')->to($user);
103
            Bouncer::allow($user)->toManage($association);
104
        }
105
        else {
106
            Bouncer::disallow($user)->toManage($association);
107
            Bouncer::retract('assocadmin')->from($user);
108
        }
109
110
        $url = $request->url;
111
112
        if (!empty($url)) {
113
            return redirect($url)->with('success', 'Data saved successfully!');
114
        }
115
116
        return redirect()->route('user', ['id' => \Auth::user()->id]);
117
118
    }
119
120
    public function addUser(Association $association) {
121
        return view('association.user.add', ['association' => $association]);
122
    }
123
124
    public function submitScoreBegin(Request $request) {
125
        if (!empty($this->association)) {
126
            // get schedules with start_date < today, end_date > today
127
            $schedules = $this->association->schedules
128
            ->where('start_date', '<=', date('Y-m-d', strtotime('today')))
129
            ->where('end_date', '>=', date('Y-m-d', strtotime('today')));
130
131
            // get rounds with start_date < today, but greater than today - 1 week
132
            $rounds = Round::whereIn('schedule_id', $schedules->pluck('id'))
133
                ->where('start_date','>=', date('Y-m-d', strtotime('-1 week')))
134
                ->where('start_date', '<=', date('Y-m-d', strtotime("today")))->get();
135
136
            $divisions = Division::whereIn('id', $rounds->pluck('division_id'))->get();
137
138
            if (count($divisions) === 1) {
139
                $request->division_id = $divisions[0]->id;
0 ignored issues
show
The property division_id does not seem to exist on Illuminate\Http\Request.
Loading history...
140
141
                return $this->submitScoreStep2($request);
142
            }
143
            else {
144
                return view('forms.results.choose-division', [
145
                    'association' => $this->association,
146
                    'divisions' => $divisions,
147
                    ]);
148
            }
149
        }
150
        else {
151
            abort(404);
152
        }
153
    }
154
155
    public function submitScoreStep2(Request $request) {
156
        if (!empty($this->association)) {
157
            $division = Division::find($request->division_id);
158
159
            // get schedules with start_date < today, end_date > today, matching division
160
            $schedules = $this->association->schedules
161
                ->where('start_date', '<=', date('Y-m-d', strtotime('today')))
162
                ->where('end_date', '>=', date('Y-m-d', strtotime('today')))
163
                ->where('division_id', $division->id);
164
165
            // get rounds with start_date < today, but greater than today - 1 week, not closed
166
            $rounds = Round::whereIn('schedule_id', $schedules->pluck('id'))
167
                ->where('start_date', '>=', date('Y-m-d', strtotime('-1 week')))
168
                ->where('start_date', '<=', date('Y-m-d', strtotime("today")))
169
                ->where(function ($query) {
170
                    $query->where('scores_closed', 0);
171
                    $query->orWhereNull('scores_closed');
172
                })
173
                ->orderBy('start_date', 'DESC')
174
                ->get();
175
176
            return view('forms.results.choose-match', [
177
                'association' => $this->association,
178
                'rounds' => $rounds,
179
                ]);
180
        }
181
        else {
182
            abort(404);
183
        }
184
    }
185
186
    public function submitScoreStep3(Request $request) {
187
        if (!empty($this->association)) {
188
            $match = PLMatch::find($request->match_id);
189
190
            return view('forms.results.input-scores', [
191
                'association' => $this->association,
192
                'match' => $match,
193
                ]);
194
        }
195
        else {
196
            abort(404);
197
        }
198
    }
199
200
    public function submitScoreStep4(Request $request) {
201
        if (!empty($this->association)) {
202
            $match_id = $request->match_id;
203
204
            if (!empty($match_id)) {
205
                $home_team_id = $request->home_team_id;
206
                $away_team_id = $request->away_team_id;
207
                $home_team_score = $request->home_team_score;
208
                $away_team_score = $request->away_team_score;
209
210
                $submission = new ResultSubmission();
211
                $submission->association_id = $this->association->id;
212
                $submission->schedule_id = PLMatch::find($match_id)->schedule_id;
213
                $submission->match_id = $match_id;
214
                $submission->home_team_score = $home_team_score;
215
                $submission->away_team_score = $away_team_score;
216
                $submission->save();
217
218
                if ($home_team_score != $away_team_score) {
219
                    $submission->win_team_id = $home_team_score > $away_team_score ? $home_team_id : $away_team_id;
220
                    $submission->save();
221
222
                    return view('forms.results.thanks', [
223
                        'association' => $this->association,
224
                        ]);
225
                }
226
                else {
227
                    return view('forms.results.choose-winner', [
228
                        'association' => $this->association,
229
                        'match' => PLMatch::find($submission->match_id),
230
                        'submission' => $submission,
231
                        ]);
232
                }
233
            }
234
            else {
235
                abort(404);
236
            }
237
        }
238
        else {
239
            abort(404);
240
        }
241
    }
242
243
    public function submitScoreStep5(Request $request) {
244
        if (!empty($this->association)) {
245
            $submission_id = $request->submission_id;
246
247
            if (!empty($submission_id)) {
248
                $submission = ResultSubmission::find($submission_id);
249
250
                $submission->win_team_id = $request->win_team_id;
251
252
                $submission->save();
253
254
                return view('forms.results.thanks', [
255
                    'association' => $this->association,
256
                    ]);
257
            }
258
            else {
259
                abort(404);
260
            }
261
        }
262
        else {
263
            abort(404);
264
        }
265
    }
266
267
    public function standings() {
268
        return view('association.standings', ['association' => $this->association]);
269
    }
270
271
    public function schedule() {
272
        return view('association.schedule', ['association' => $this->association]);
273
    }
274
275
    public function css() {
276
        $response = \Response::make('body { background-color: red; }');
277
        $response->header('Content-Type', 'text/css');
278
        return $response;
279
    }
280
281
    /**
282
     * Store a new association.
283
     *
284
     * @param  Request  $request
285
     * @return Response
0 ignored issues
show
The type App\Http\Controllers\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
286
     */
287
    public function store(Request $request) {
288
        if (Bouncer::can('create', Association::class)) {
289
            $association = new Association;
290
291
            $association->name = $request->name;
292
            $association->user_id = $request->user_id;
293
294
            $association->save();
295
296
            // TODO: Do not necessarily "onboard" for certain roles?
297
            return redirect()->route('onboard.association', ['association' => $association]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...tion' => $association)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
298
        }
299
        else {
300
            return view('denied');
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('denied') returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
301
        }
302
    }
303
304
    public function update(Request $request) {
305
306
        $association = Association::find($request->id);
307
308
        $association->name = $request->name;
309
        $association->user_id = $request->user_id;
310
311
        if (isset($request->subdomain)) {
312
            $association->subdomain = $request->subdomain;
313
        }
314
315
        if (isset($request->home_image_file)) {
316
            $path = $request->home_image_file->storeAs(
317
                'home_image_file/' . $association->subdomain, $request->home_image_file->getClientOriginalName(), 'public'
318
            );
319
320
            $association->home_image_path = $path;
321
        }
322
323
        if (isset($request->rules_file)) {
324
            $path = $request->rules_file->storeAs(
325
                'rules_file/' . $association->subdomain, $request->rules_file->getClientOriginalName(), 'public'
326
            );
327
328
            $association->rules_file_path = $path;
329
        }
330
331
        $association->about = $request->about;
332
333
        $association->save();
334
335
        //Session::flash('message', 'Successfully updated nerd!');
336
337
        $url = $request->url;
338
339
        if (!empty($url)) {
340
            return redirect($url)->with('success', 'Data saved successfully!');
341
        }
342
343
        return redirect()->route('user', ['id' => \Auth::user()->id]);
344
345
    }
346
347
    public function create() {
348
        if (Bouncer::can('create', Association::class)) {
349
            return view('association.create', ['current_user' => \Auth::user()]);
350
        }
351
        else {
352
            return view('denied');
353
        }
354
    }
355
356
    public function deleteConfirm(Association $association) {
357
        return view('association.delete', ['association' => $association]);
358
    }
359
360
    public function delete(Association $association) {
361
        $association->delete();
362
363
        return redirect()->route('admin')->with('success', 'Association deleted successfully.');
364
    }
365
366
    public function undeleteConfirm(Association $association) {
367
        return view('association.undelete', ['association' => $association]);
368
    }
369
370
    public function undelete(Association $association) {
371
        $association->restore();
372
373
        return redirect()->route('user', ['user' => \Auth::user()])->with('success', 'Association restored successfully.');
374
    }
375
376
    public function about() {
377
        return view('association.about' , ['association' => $this->association]);
378
    }
379
380
    public function rules() {
381
        return view('association.rules' , ['association' => $this->association]);
382
    }
383
384
    public function rulesDelete(Association $association) {
385
        $association->rules_file_path = NULL;
386
387
        $association->save();
388
389
        return view('association.edit', [
390
            'association' => $association,
391
            'current_user' => \Auth::user(),
392
        ]);
393
    }
394
395
    public function contact() {
396
        return view('forms.contact', ['association' => $this->association]);
397
    }
398
399
    public function contactSubmit(Request $request) {
400
        $validatedData = $request->validate([
0 ignored issues
show
The assignment to $validatedData is dead and can be removed.
Loading history...
401
            'email' => 'required|email|max:255',
402
        ]);
403
404
        $contact = new ContactSubmission();
405
406
        $contact->email = $request->email;
407
        $contact->reason = $request->reason;
408
        $contact->comment = $request->comment;
409
        $contact->association_id = $request->association_id;
410
411
        $contact->save();
412
413
        return redirect()->route('contact.thanks');
414
    }
415
416
    public function contactThanks() {
417
        return view('contact-thanks');
418
    }
419
420
    /**
421
     * Display a listing of the resource.
422
     *
423
     * @return \Illuminate\Http\Response
424
     */
425
    public function contactSubmissions(Association $association)
426
    {
427
        return view('association.contact_submissions', ['association' => $association]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('association...tion' => $association)) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
428
    }
429
430
}
431