1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\User; |
6
|
|
|
use Validator; |
7
|
|
|
use App\Organizer; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Support\Facades\DB; |
11
|
|
|
use Illuminate\Support\Facades\Auth; |
12
|
|
|
|
13
|
|
|
class OrganizerController extends Controller |
14
|
|
|
{ |
15
|
|
|
/* Restrict the access to this resource just to logged in users except show view */ |
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->middleware('auth', ['except' => ['show', 'organizerBySlug']]); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/***************************************************************************/ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Display a listing of the resource. |
25
|
|
|
* |
26
|
|
|
* @return \Illuminate\Http\Response |
27
|
|
|
*/ |
28
|
|
|
public function index(Request $request) |
29
|
|
|
{ |
30
|
|
|
// To show just the organizers created by the the user - If admin or super admin is set to null show all the organizers |
31
|
|
|
$authorUserId = ($this->getLoggedAuthorId()) ? $this->getLoggedAuthorId() : null; // if is 0 (super admin or admin) it's setted to null to avoid include it in the query |
32
|
|
|
|
33
|
|
|
$searchKeywords = $request->input('keywords'); |
34
|
|
|
|
35
|
|
|
if ($searchKeywords) { |
36
|
|
|
$organizers = DB::table('organizers') |
37
|
|
|
->when($authorUserId, function ($query, $authorUserId) { |
38
|
|
|
return $query->where('created_by', $authorUserId); |
39
|
|
|
}) |
40
|
|
|
->when($searchKeywords, function ($query, $searchKeywords) { |
41
|
|
|
return $query->where('name', $searchKeywords)->orWhere('name', 'like', '%'.$searchKeywords.'%'); |
42
|
|
|
}) |
43
|
|
|
->paginate(20); |
44
|
|
|
} else { |
45
|
|
|
$organizers = DB::table('organizers') |
46
|
|
|
->when($authorUserId, function ($query, $authorUserId) { |
47
|
|
|
return $query->where('created_by', $authorUserId); |
48
|
|
|
}) |
49
|
|
|
->paginate(20); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return view('organizers.index', compact('organizers')) |
|
|
|
|
53
|
|
|
->with('i', (request()->input('page', 1) - 1) * 20) |
54
|
|
|
->with('searchKeywords', $searchKeywords); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/***************************************************************************/ |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Show the form for creating a new resource. |
61
|
|
|
* |
62
|
|
|
* @return \Illuminate\Http\Response |
63
|
|
|
*/ |
64
|
|
|
public function create() |
65
|
|
|
{ |
66
|
|
|
$users = User::pluck('name', 'id'); |
67
|
|
|
$authorUserId = $this->getLoggedUser(); |
68
|
|
|
|
69
|
|
|
return view('organizers.create') |
|
|
|
|
70
|
|
|
->with('users', $users) |
71
|
|
|
->with('authorUserId', $authorUserId); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/***************************************************************************/ |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Store a newly created resource in storage. |
78
|
|
|
* |
79
|
|
|
* @param \Illuminate\Http\Request $request |
80
|
|
|
* @return \Illuminate\Http\Response |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function store(Request $request) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
// Validate form datas |
85
|
|
|
$validator = $this->organizersValidator($request); |
86
|
|
|
if ($validator->fails()) { |
87
|
|
|
return back()->withErrors($validator)->withInput(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$organizer = new Organizer(); |
91
|
|
|
$this->saveOnDb($request, $organizer); |
92
|
|
|
|
93
|
|
|
return redirect()->route('organizers.index') |
94
|
|
|
->with('success', __('messages.organizer_added_successfully')); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/***************************************************************************/ |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Display the specified resource. |
101
|
|
|
* |
102
|
|
|
* @param \App\Organizer $organizer |
103
|
|
|
* @return \Illuminate\Http\Response |
104
|
|
|
*/ |
105
|
|
|
public function show(Organizer $organizer) |
106
|
|
|
{ |
107
|
|
|
return view('organizers.show', compact('organizer')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/***************************************************************************/ |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Show the form for editing the specified resource. |
114
|
|
|
* |
115
|
|
|
* @param \App\Organizer $organizer |
116
|
|
|
* @return \Illuminate\Http\Response |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function edit(Organizer $organizer) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
if (Auth::user()->id == $organizer->created_by || Auth::user()->isSuperAdmin() || Auth::user()->isAdmin()) { |
|
|
|
|
121
|
|
|
$authorUserId = $this->getLoggedAuthorId(); |
122
|
|
|
$users = User::pluck('name', 'id'); |
123
|
|
|
|
124
|
|
|
return view('organizers.edit', compact('organizer')) |
|
|
|
|
125
|
|
|
->with('users', $users) |
126
|
|
|
->with('authorUserId', $authorUserId); |
127
|
|
|
} else { |
128
|
|
|
return redirect()->route('home')->with('message', __('auth.not_allowed_to_access')); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/***************************************************************************/ |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Update the specified resource in storage. |
136
|
|
|
* |
137
|
|
|
* @param \Illuminate\Http\Request $request |
138
|
|
|
* @param \App\Organizer $organizer |
139
|
|
|
* @return \Illuminate\Http\Response |
140
|
|
|
*/ |
141
|
|
|
public function update(Request $request, Organizer $organizer) |
142
|
|
|
{ |
143
|
|
|
// Validate form datas |
144
|
|
|
$validator = $this->organizersValidator($request); |
145
|
|
|
if ($validator->fails()) { |
146
|
|
|
return back()->withErrors($validator)->withInput(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$this->saveOnDb($request, $organizer); |
150
|
|
|
|
151
|
|
|
return redirect()->route('organizers.index') |
152
|
|
|
->with('success', __('messages.organizer_updated_successfully')); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/***************************************************************************/ |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Remove the specified resource from storage. |
159
|
|
|
* |
160
|
|
|
* @param \App\Organizer $organizer |
161
|
|
|
* @return \Illuminate\Http\Response |
162
|
|
|
*/ |
163
|
|
|
public function destroy(Organizer $organizer) |
164
|
|
|
{ |
165
|
|
|
$organizer->delete(); |
166
|
|
|
|
167
|
|
|
return redirect()->route('organizers.index') |
168
|
|
|
->with('success', __('messages.organizer_deleted_successfully')); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/***************************************************************************/ |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Save the record on DB. |
175
|
|
|
* @param \Illuminate\Http\Request $request |
176
|
|
|
* @param \App\Organizer $organizer |
177
|
|
|
* @return void |
178
|
|
|
*/ |
179
|
|
|
public function saveOnDb($request, $organizer) |
180
|
|
|
{ |
181
|
|
|
$organizer->name = $request->get('name'); |
182
|
|
|
$organizer->description = clean($request->get('description')); |
183
|
|
|
$organizer->website = $request->get('website'); |
184
|
|
|
$organizer->email = $request->get('email'); |
185
|
|
|
$organizer->phone = $request->get('phone'); |
186
|
|
|
|
187
|
|
|
$organizer->created_by = \Auth::user()->id; |
188
|
|
|
if (! $organizer->slug) { |
189
|
|
|
$organizer->slug = Str::slug($organizer->name, '-').'-'.rand(10000, 100000); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$organizer->save(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/***************************************************************************/ |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Open a modal in the event view when create organizer is clicked. |
199
|
|
|
* |
200
|
|
|
* @return view |
201
|
|
|
*/ |
202
|
|
|
public function modal() |
203
|
|
|
{ |
204
|
|
|
return view('organizers.modal'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/***************************************************************************/ |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Store a newly created organizer from the create event view modal in storage. |
211
|
|
|
* |
212
|
|
|
* @param \Illuminate\Http\Request $request |
213
|
|
|
* @return \Illuminate\Http\Response |
214
|
|
|
*/ |
215
|
|
View Code Duplication |
public function storeFromModal(Request $request) |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
$organizer = new Organizer(); |
218
|
|
|
request()->validate([ |
219
|
|
|
'name' => 'required', |
220
|
|
|
]); |
221
|
|
|
|
222
|
|
|
$this->saveOnDb($request, $organizer); |
223
|
|
|
|
224
|
|
|
return redirect()->back()->with('message', 'Organizer created'); |
225
|
|
|
//return redirect()->back()->with('message', __('auth.successfully_registered')); |
226
|
|
|
//return true; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/***************************************************************************/ |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Return the organizer by SLUG. (eg. http://websitename.com/organizer/xxxxx). |
233
|
|
|
* |
234
|
|
|
* @param string $slug |
235
|
|
|
* @return \Illuminate\Http\Response |
236
|
|
|
*/ |
237
|
|
|
public function organizerBySlug($slug) |
238
|
|
|
{ |
239
|
|
|
$organizer = Organizer:: |
240
|
|
|
where('slug', $slug) |
241
|
|
|
->first(); |
242
|
|
|
|
243
|
|
|
return $this->show($organizer); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/***************************************************************************/ |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Return the validator with all the defined constraint. |
250
|
|
|
* |
251
|
|
|
* @param \Illuminate\Http\Request $request |
252
|
|
|
* @return \Illuminate\Validation\Validator |
253
|
|
|
*/ |
254
|
|
View Code Duplication |
public function organizersValidator($request) |
|
|
|
|
255
|
|
|
{ |
256
|
|
|
$rules = [ |
257
|
|
|
'name' => 'required', |
258
|
|
|
'email' => 'required|email', |
259
|
|
|
'website' => 'nullable|url', |
260
|
|
|
]; |
261
|
|
|
$messages = [ |
262
|
|
|
'website.url' => 'The website link is invalid. It should start with https://', |
263
|
|
|
]; |
264
|
|
|
|
265
|
|
|
$validator = Validator::make($request->all(), $rules, $messages); |
266
|
|
|
|
267
|
|
|
return $validator; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
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: