|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Association; |
|
6
|
|
|
use App\Club; |
|
7
|
|
|
use App\Federation; |
|
8
|
|
|
use App\Http\Requests\ClubRequest; |
|
9
|
|
|
use App\User; |
|
10
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
|
11
|
|
|
use Illuminate\Database\QueryException; |
|
12
|
|
|
use Illuminate\Http\JsonResponse; |
|
13
|
|
|
use Illuminate\Http\RedirectResponse; |
|
14
|
|
|
use Illuminate\Support\Collection; |
|
15
|
|
|
use Illuminate\Support\Facades\Auth; |
|
16
|
|
|
use Illuminate\Support\Facades\Request; |
|
17
|
|
|
use Illuminate\Support\Facades\View; |
|
18
|
|
|
use Response; |
|
19
|
|
|
|
|
20
|
|
|
class ClubController extends Controller |
|
21
|
|
|
{ |
|
22
|
|
|
// Only Super Admin and Club President can manage Clubs |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Display a listing of the resource. |
|
26
|
|
|
* |
|
27
|
|
|
* @return Collection|View |
|
28
|
|
|
*/ |
|
29
|
|
|
public function index() |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
if (Request::ajax()) { |
|
33
|
|
|
return Club::fillSelect(Auth::user()->federation_id, Auth::user()->association_id); |
|
|
|
|
|
|
34
|
|
|
} else { |
|
35
|
|
|
$clubs = Club::with('president', 'association.federation') |
|
36
|
|
|
->forUser(Auth::user()) |
|
37
|
|
|
->where('id', '>', 1) |
|
38
|
|
|
->get(); |
|
39
|
|
|
|
|
40
|
|
|
return view('clubs.index', compact('clubs')); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Show the form for creating a new resource. |
|
48
|
|
|
* @return View |
|
49
|
|
|
* @throws AuthorizationException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function create() |
|
52
|
|
|
{ |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
$club = new Club; |
|
56
|
|
|
// Authorization |
|
57
|
|
|
|
|
58
|
|
|
$federations = Federation::fillSelect(); |
|
59
|
|
|
$associations = Association::forUser(auth()->user())->pluck('name', 'id')->prepend('-', 0); |
|
|
|
|
|
|
60
|
|
|
$users = Auth::user()->fillSelect(); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
$defaultLng = Auth::user()->latitude ?? geoip()->lat; |
|
|
|
|
|
|
64
|
|
|
$defaultLat = Auth::user()->longitude ?? geoip()->lon; |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
return view('clubs.form', compact('club', 'users', 'federations', 'associations', 'defaultLng', 'defaultLat')); // |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Store a newly created resource in storage. |
|
72
|
|
|
* |
|
73
|
|
|
* @param ClubRequest $request |
|
74
|
|
|
* @return JsonResponse|RedirectResponse |
|
75
|
|
|
*/ |
|
76
|
|
|
public function store(ClubRequest $request) |
|
77
|
|
|
{ |
|
78
|
|
|
|
|
79
|
|
|
try { |
|
80
|
|
|
if ($request->president_id == 0) $request->merge(['president_id' => null]); |
|
|
|
|
|
|
81
|
|
|
if ($request->federation_id == 0) $request->merge(['federation_id' => null]); |
|
|
|
|
|
|
82
|
|
|
if ($request->association_id == 0) $request->merge(['association_id' => null]); |
|
|
|
|
|
|
83
|
|
|
$club = Club::create($request->all()); |
|
84
|
|
|
|
|
85
|
|
View Code Duplication |
if (Request::ajax()) { |
|
|
|
|
|
|
86
|
|
|
return Response::json(['msg' => trans('msg.club_create_successful', ['name' => $club->name]), 'status' => 'success', 'data' => $club], 200); |
|
87
|
|
|
} else { |
|
88
|
|
|
$msg = trans('msg.club_edit_successful', ['name' => $club->name]); |
|
89
|
|
|
flash()->success($msg); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
return redirect("clubs"); |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
} catch (QueryException $e) { |
|
95
|
|
|
$user = User::find($request->president_id); |
|
|
|
|
|
|
96
|
|
|
$msg = trans('msg.club_president_already_exists', ['user' => $user->name]); |
|
97
|
|
|
flash()->error($msg); |
|
|
|
|
|
|
98
|
|
|
return redirect()->back(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Display the specified resource. |
|
106
|
|
|
* |
|
107
|
|
|
* @param int $id |
|
108
|
|
|
* @return View |
|
109
|
|
|
*/ |
|
110
|
|
|
public function show($id) |
|
111
|
|
|
{ |
|
112
|
|
|
$club = Club::findOrFail($id); |
|
113
|
|
|
return view('clubs.show', compact('club')); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Show the form for editing the specified resource. |
|
118
|
|
|
* |
|
119
|
|
|
* @param int $id |
|
120
|
|
|
* @return View |
|
121
|
|
|
* @throws AuthorizationException |
|
122
|
|
|
*/ |
|
123
|
|
|
public function edit($id) |
|
124
|
|
|
{ |
|
125
|
|
|
|
|
126
|
|
|
$defaultLng = Auth::user()->latitude ?? geoip()->lat; |
|
|
|
|
|
|
127
|
|
|
$defaultLat = Auth::user()->longitude ?? geoip()->lon; |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
$club = Club::findOrFail($id); |
|
130
|
|
|
$this->authorize('edit', [$club, Auth::user()]); |
|
131
|
|
|
|
|
132
|
|
|
$federations = Federation::fillSelect(); |
|
133
|
|
|
$associations = Association::forUser(auth()->user())->pluck('name', 'id')->prepend('-', 0); |
|
|
|
|
|
|
134
|
|
|
$users = Auth::user()->fillSelect(); |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
return view('clubs.form', compact('club', 'users', 'associations', 'federations', 'defaultLng', 'defaultLat')); // |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Update the specified resource in storage. |
|
141
|
|
|
* |
|
142
|
|
|
* @param ClubRequest|Request $request |
|
143
|
|
|
* @param int $id |
|
144
|
|
|
* @return \Illuminate\Http\Response |
|
145
|
|
|
* @throws AuthorizationException |
|
146
|
|
|
*/ |
|
147
|
|
|
public function update(ClubRequest $request, $id) |
|
148
|
|
|
{ |
|
149
|
|
|
$club = Club::findOrFail($id); |
|
150
|
|
|
|
|
151
|
|
|
$this->authorize('update', [$club, Auth::user()]); |
|
152
|
|
|
|
|
153
|
|
|
try { |
|
154
|
|
|
if ($request->president_id == 0) $request->merge(['president_id' => null]); |
|
|
|
|
|
|
155
|
|
|
if ($request->association_id == 0) $request->merge(['association_id' => null]); |
|
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
$club->update($request->all()); |
|
158
|
|
|
$msg = trans('msg.club_edit_successful', ['name' => $club->name]); |
|
159
|
|
|
flash()->success($msg); |
|
|
|
|
|
|
160
|
|
|
return redirect("clubs"); |
|
161
|
|
|
} catch (QueryException $e) { |
|
162
|
|
|
// Already |
|
163
|
|
|
$msg = "User" . $request->id . "is already president of another club"; |
|
|
|
|
|
|
164
|
|
|
flash()->error($msg); |
|
|
|
|
|
|
165
|
|
|
return redirect()->back(); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Remove the specified resource from storage. |
|
171
|
|
|
* |
|
172
|
|
|
* @param $clubId |
|
173
|
|
|
* @return JsonResponse |
|
174
|
|
|
* @internal param Club $club |
|
175
|
|
|
*/ |
|
176
|
|
View Code Duplication |
public function destroy($clubId) |
|
|
|
|
|
|
177
|
|
|
{ |
|
178
|
|
|
$club = Club::find($clubId); |
|
179
|
|
|
if ($club->delete()) { |
|
180
|
|
|
return Response::json(['msg' => trans('msg.club_delete_successful', ['name' => $club->name]), 'status' => 'success']); |
|
181
|
|
|
} else { |
|
182
|
|
|
return Response::json(['msg' => trans('msg.club_delete_error', ['name' => $club->name]), 'status' => 'error']); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param $id |
|
188
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
189
|
|
|
*/ |
|
190
|
|
View Code Duplication |
public function restore($id) |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
{ |
|
193
|
|
|
$club = Club::withTrashed()->find($id); |
|
194
|
|
|
if ($club->restore()) { |
|
195
|
|
|
return Response::json(['msg' => trans('msg.club_restored_successful', ['name' => $club->name]), 'status' => 'success']); |
|
196
|
|
|
} else { |
|
197
|
|
|
return Response::json(['msg' => trans('msg.club_restored_error', ['name' => $club->name]), 'status' => 'error']); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: