1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Championship; |
6
|
|
|
use App\Http\Requests\TeamRequest; |
7
|
|
|
use App\Team; |
8
|
|
|
use App\Tournament; |
9
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
10
|
|
|
use Illuminate\Database\QueryException; |
11
|
|
|
use Illuminate\Http\JsonResponse; |
12
|
|
|
use Illuminate\Http\Request; |
13
|
|
|
use Illuminate\Support\Collection; |
14
|
|
|
use Illuminate\Support\Facades\Auth; |
15
|
|
|
use Illuminate\Support\Facades\Lang; |
16
|
|
|
use Illuminate\Support\Facades\Response; |
17
|
|
|
use Illuminate\Support\Facades\View; |
18
|
|
|
|
19
|
|
|
class TeamController extends Controller |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Display a listing of the teams for a tournament. |
23
|
|
|
* |
24
|
|
|
* @param Tournament $tournament |
25
|
|
|
* @return View |
26
|
|
|
*/ |
27
|
|
|
public function index(Tournament $tournament) |
28
|
|
|
{ |
29
|
|
|
$tournament = Tournament::with(['championships' => function ($query) { |
|
|
|
|
30
|
|
|
$query->with('teams') |
31
|
|
|
->whereHas('category', function ($subquery) { |
32
|
|
|
$subquery->where('isTeam', '=', 1); |
33
|
|
|
}); |
34
|
|
|
}])->withCount('competitors', 'teams') |
35
|
|
|
->find($tournament->id); |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
$arrChampionshipsWithTeamsAndCompetitors = $tournament->championships->map(function ($championship) { |
39
|
|
|
$competitors = $championship->competitors->map(function ($competitor) { |
40
|
|
|
return ["id" => $competitor->id, "name" => $competitor->user->name]; |
41
|
|
|
})->toArray(); |
42
|
|
|
$teams = $championship->teams->map(function ($team) { |
43
|
|
|
return ["id" => $team->id, "name" => $team->name, 'competitors' => $team->with('user')]; |
44
|
|
|
})->toArray(); |
45
|
|
|
|
46
|
|
|
$tempAssignCompatitors = new Collection(); |
47
|
|
|
$assignedCompetitors = $this->getAssignedCompetitors($championship, $tempAssignCompatitors); |
48
|
|
|
|
49
|
|
|
$freeCompetitors = $championship->competitors; |
50
|
|
|
if ($assignedCompetitors != null) { |
51
|
|
|
$freeCompetitors = $freeCompetitors->diff($assignedCompetitors); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return ['championship' => $championship->id, 'competitors' => $competitors, 'freeCompetitors' => $freeCompetitors, 'teams' => $teams]; |
55
|
|
|
})->toArray(); |
56
|
|
|
|
57
|
|
|
return view("teams.index", compact('tournament', 'arrChampionshipsWithTeamsAndCompetitors')); |
58
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $championship |
63
|
|
|
* @param $tempAssignCompatitors |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
private function getAssignedCompetitors($championship, $tempAssignCompatitors) |
67
|
|
|
{ |
68
|
|
|
return $championship->teams->reduce(function ($acc, $team) use ($tempAssignCompatitors) { |
69
|
|
|
return $tempAssignCompatitors->push($team->competitors()->with('user')->get())->collapse(); |
70
|
|
|
}); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Show the form for creating a new competitor. |
75
|
|
|
* |
76
|
|
|
* @param Tournament $tournament |
77
|
|
|
* @return View |
78
|
|
|
* @throws AuthorizationException |
79
|
|
|
*/ |
80
|
|
|
public function create(Tournament $tournament) |
81
|
|
|
{ |
82
|
|
|
$team = new Team; |
83
|
|
|
$this->authorize('create', [Team::class, $tournament, Auth::user()]); |
84
|
|
|
return view("teams.form", compact('tournament', 'team', 'cts')); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Store a newly created resource in storage. |
89
|
|
|
* |
90
|
|
|
* @param Request $request |
91
|
|
|
* @param Championship $championship |
92
|
|
|
* @return View |
93
|
|
|
* @throws AuthorizationException |
94
|
|
|
*/ |
95
|
|
|
public function store(Request $request, Championship $championship) |
96
|
|
|
{ |
97
|
|
|
$this->authorize('store', [Team::class, $championship->tournament, Auth::user()]); |
98
|
|
|
try { |
99
|
|
|
$team = Team::where('championship_id', $championship->id)->orderBy('id', 'desc')->first(); |
100
|
|
|
$short_id = 1; |
101
|
|
|
if ($team != null) { |
102
|
|
|
$short_id = $team->short_id + 1; |
103
|
|
|
} |
104
|
|
|
$request->request->add(['short_id' => $short_id]); |
105
|
|
|
$team = Team::create($request->all()); |
106
|
|
|
flash()->success(trans('msg.team_create_successful', ['name' => $team->name])); |
|
|
|
|
107
|
|
|
} catch (QueryException $e) { |
108
|
|
|
flash()->error(trans('msg.team_create_error_already_exists', ['name' => $request->name])); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return back()->with('activeTab', $request->activeTab); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Show the form for creating a new competitor. |
116
|
|
|
* |
117
|
|
|
* @param Tournament $tournament |
118
|
|
|
* @param $teamId |
119
|
|
|
* @return View |
120
|
|
|
* @throws AuthorizationException |
121
|
|
|
*/ |
122
|
|
|
public function edit(Tournament $tournament, $teamId) |
123
|
|
|
{ |
124
|
|
|
$team = Team::findOrFail($teamId); |
125
|
|
|
$this->authorize('edit', [Team::class, $tournament, Auth::user()]); |
126
|
|
|
|
127
|
|
|
$cts = $tournament->buildCategoryList(); |
128
|
|
|
return view("teams.form", compact('tournament', 'team', 'cts')); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Update a newly created resource in storage. |
133
|
|
|
* |
134
|
|
|
* @param TeamRequest $request |
135
|
|
|
* @param Tournament $tournament |
136
|
|
|
* @param $teamId |
137
|
|
|
* @return Response |
138
|
|
|
* @throws AuthorizationException |
139
|
|
|
*/ |
140
|
|
|
public function update(TeamRequest $request, Tournament $tournament, $teamId) |
141
|
|
|
{ |
142
|
|
|
$team = Team::findOrFail($teamId); |
143
|
|
|
$this->authorize('update', [Team::class, $tournament, Auth::user()]); |
144
|
|
|
|
145
|
|
|
$team->update($request->all()); |
146
|
|
|
flash()->success(trans('msg.team_edit_successful', ['name' => $team->name])); |
|
|
|
|
147
|
|
|
return redirect()->route('teams.index', $tournament->slug); |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Remove the Team from storage. |
153
|
|
|
* |
154
|
|
|
* @param Team $team |
155
|
|
|
* @return JsonResponse |
156
|
|
|
* @throws AuthorizationException |
157
|
|
|
*/ |
158
|
|
|
public function destroy(Team $team) |
159
|
|
|
{ |
160
|
|
|
$tournament = $team->championship->tournament; |
161
|
|
|
$this->authorize('delete', [Team::class, $tournament, Auth::user()]); |
162
|
|
|
|
163
|
|
|
if ($team->forceDelete()) { |
164
|
|
|
return Response::json(['msg' => Lang::get('msg.team_delete_successful', ['name' => $team->name]), 'status' => 'success']); |
165
|
|
|
} |
166
|
|
|
return Response::json(['msg' => Lang::get('msg.team_delete_error', ['name' => $team->name]), 'status' => 'error']); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: