samtny /
league-frontend
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Http\Controllers; |
||
| 4 | |||
| 5 | use App\Association; |
||
| 6 | use App\Division; |
||
| 7 | use App\PLMatch; |
||
| 8 | use App\Round; |
||
| 9 | use App\Schedule; |
||
| 10 | use App\Series; |
||
| 11 | use Illuminate\Http\Request; |
||
| 12 | |||
| 13 | class ScheduleController extends Controller |
||
| 14 | { |
||
| 15 | |||
| 16 | public function view(Schedule $schedule) { |
||
| 17 | return view('schedule.view', ['schedule' => $schedule]); |
||
| 18 | } |
||
| 19 | |||
| 20 | public function create(Series $series) { |
||
| 21 | $association_id = $series->association_id; |
||
| 22 | $available_series = \App\Series::where(['association_id' => $series->association_id])->get()->all(); |
||
| 23 | $available_divisions = \App\Division::orderBy('sequence' , 'ASC')->where(['association_id' => $series->association_id])->get()->all(); |
||
| 24 | |||
| 25 | return view('schedule.create', [ |
||
| 26 | 'association_id' => $association_id, |
||
| 27 | 'series' => $series, |
||
| 28 | 'available_series' => $available_series, |
||
| 29 | 'available_divisions' => $available_divisions, |
||
| 30 | ]); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function edit(Association $association, Schedule $schedule) { |
||
| 34 | return view('schedule.edit', [ |
||
| 35 | 'association' => $schedule->association, |
||
| 36 | 'schedule' => $schedule, |
||
| 37 | ]); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function rounds(Schedule $schedule) { |
||
| 41 | return view('schedule.rounds', [ |
||
| 42 | 'schedule' => $schedule, |
||
| 43 | ]); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function store(Series $series, Request $request) { |
||
| 47 | $validatedData = $request->validate([ |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 48 | 'name' => 'required|max:255', |
||
| 49 | ]); |
||
| 50 | |||
| 51 | $name = $request->name; |
||
| 52 | $association_id = $series->association->id; |
||
| 53 | $division_id = $request->division_id; |
||
| 54 | $start_date = $request->start_date; |
||
| 55 | $end_date = $request->end_date; |
||
| 56 | $weekday = $request->weekday; |
||
| 57 | |||
| 58 | $schedule = new Schedule; |
||
| 59 | |||
| 60 | $schedule->name = $name; |
||
| 61 | $schedule->association_id = $association_id; |
||
| 62 | $schedule->series_id = $series->id; |
||
| 63 | $schedule->division_id = $division_id; |
||
| 64 | $schedule->start_date = $start_date; |
||
| 65 | $schedule->end_date = $end_date; |
||
| 66 | |||
| 67 | $division = Division::where(['id' => $division_id])->first(); |
||
| 68 | |||
| 69 | if (!empty($division)) { |
||
| 70 | $schedule->name = $division->name; |
||
| 71 | $schedule->sequence = $division->sequence; |
||
| 72 | } |
||
| 73 | else { |
||
| 74 | $schedule->name = $schedule->start_date; |
||
| 75 | $schedule->sequence = null; |
||
| 76 | } |
||
| 77 | |||
| 78 | $schedule->save(); |
||
| 79 | |||
| 80 | if ($request->generate) { |
||
| 81 | $start_datetime = new \DateTime($start_date); |
||
| 82 | $end_datetime = new \DateTime($end_date); |
||
| 83 | |||
| 84 | $days_interval = $start_datetime->diff($end_datetime); |
||
| 85 | |||
| 86 | $days = $days_interval->format('%a'); |
||
| 87 | |||
| 88 | $association = Association::where(['id' => $association_id])->first(); |
||
| 89 | $venues = $association->venues; |
||
| 90 | |||
| 91 | $round_number = 1; |
||
| 92 | |||
| 93 | for ($i = 0; $i <= $days; $i += 1) { |
||
| 94 | if (strtolower($start_datetime->format('D')) == strtolower($weekday)) { |
||
| 95 | $round = new Round; |
||
| 96 | |||
| 97 | $round->schedule_id = $schedule->id; |
||
| 98 | $round->division_id = $division_id; |
||
| 99 | $round->series_id = $series->id; |
||
| 100 | |||
| 101 | $round->start_date = $start_datetime; |
||
| 102 | $round->end_date = $start_datetime; |
||
| 103 | $round->name = 'Round ' . $round_number; |
||
| 104 | |||
| 105 | $round->save(); |
||
| 106 | |||
| 107 | $round_number += 1; |
||
| 108 | |||
| 109 | foreach ($venues as $venue) { |
||
| 110 | $match = new PLMatch; |
||
| 111 | |||
| 112 | $match->name = $venue->name . ' – ' . $round->start_date->format('m-d-Y'); |
||
|
0 ignored issues
–
show
|
|||
| 113 | $match->association_id = $association->id; |
||
|
0 ignored issues
–
show
|
|||
| 114 | $match->series_id = $series->id; |
||
|
0 ignored issues
–
show
|
|||
| 115 | $match->division_id = $division->id; |
||
|
0 ignored issues
–
show
|
|||
| 116 | |||
| 117 | // Unique key fields: |
||
| 118 | $match->schedule_id = $schedule->id; |
||
|
0 ignored issues
–
show
|
|||
| 119 | $match->round_id = $round->id; |
||
|
0 ignored issues
–
show
|
|||
| 120 | $match->venue_id = $venue->id; |
||
|
0 ignored issues
–
show
|
|||
| 121 | $match->sequence = 1; |
||
|
0 ignored issues
–
show
|
|||
| 122 | |||
| 123 | $match->start_date = $round->start_date; |
||
|
0 ignored issues
–
show
|
|||
| 124 | $match->end_date = $round->end_date; |
||
|
0 ignored issues
–
show
|
|||
| 125 | |||
| 126 | $match->save(); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | $start_datetime->add(new \DateInterval('P1D')); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | return redirect()->route('series.schedules', ['series' => $series]); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function update(Request $request) { |
||
| 138 | $validatedData = $request->validate([ |
||
|
0 ignored issues
–
show
|
|||
| 139 | 'name' => 'required|max:255', |
||
| 140 | ]); |
||
| 141 | |||
| 142 | $schedule = schedule::find($request->id); |
||
| 143 | |||
| 144 | $schedule->name = $request->name; |
||
| 145 | $schedule->division_id = !empty($request->division_id) ? $request->division_id : null; |
||
| 146 | $schedule->start_date = $request->start_date; |
||
| 147 | $schedule->end_date = $request->end_date; |
||
| 148 | $schedule->archived = $request->archived; |
||
| 149 | |||
| 150 | $schedule->save(); |
||
| 151 | |||
| 152 | if ($request->generate) { |
||
| 153 | foreach ($schedule->matches as $match) { |
||
| 154 | $home_team_id = $request->{'match_' . $match->id . '__home_team_id'}; |
||
| 155 | $away_team_id = $request->{'match_' . $match->id . '__away_team_id'}; |
||
| 156 | |||
| 157 | $match->home_team_id = $home_team_id; |
||
| 158 | $match->away_team_id = $away_team_id; |
||
| 159 | |||
| 160 | $match->save(); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | $request->session()->flash('message', __('Successfully updated schedule')); |
||
| 165 | |||
| 166 | return redirect()->route('schedule.view', ['schedule' => $schedule]); |
||
| 167 | |||
| 168 | } |
||
| 169 | |||
| 170 | } |
||
| 171 |