Completed
Push — master ( ce343e...8c2661 )
by Sam
12:36
created

AssociationsController::deleteConfirm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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
use Illuminate\Support\Str;
17
18
class AssociationsController extends Controller
19
{
20
21
    public function __construct(Request $request) {
22
        $subdomain = array_first(explode('.', \Request::getHost()));
0 ignored issues
show
Deprecated Code introduced by
The function array_first() has been deprecated: Arr::first() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

22
        $subdomain = /** @scrutinizer ignore-deprecated */ array_first(explode('.', \Request::getHost()));

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.

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