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