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