Passed
Push — master ( 7a0d99...4b4f75 )
by Davide
28:39
created

EventController   C

Complexity

Total Complexity 54

Size/Duplication

Total Lines 752
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

Changes 57
Bugs 14 Features 18
Metric Value
wmc 54
eloc 322
c 57
b 14
f 18
dl 0
loc 752
ccs 285
cts 330
cp 0.8636
rs 6.4799

20 Methods

Rating   Name   Duplication   Size   Complexity  
A reportMisuse() 0 39 1
A setEventRepeatFields() 0 30 5
A getCreatorEmail() 0 10 1
A eventsValidator() 0 38 3
A eventBySlugAndRepetition() 0 11 2
A mailToOrganizer() 0 22 1
A mailToOrganizerSent() 0 3 1
B edit() 0 43 8
A update() 0 12 2
A eventBySlug() 0 6 1
A calculateMonthlySelectOptions() 0 68 3
A destroy() 0 10 1
A store() 0 14 2
A saveEventRepetitions() 0 58 5
A show() 0 32 2
A create() 0 24 1
A __construct() 0 3 1
A reportMisuseThankyou() 0 3 1
B saveOnDb() 0 79 8
A index() 0 48 5

How to fix   Complexity   

Complex Class

Complex classes like EventController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use EventController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Http\Controllers;
4
5
use DavideCasiraghi\LaravelEventsCalendar\Facades\LaravelEventsCalendar;
6
use DavideCasiraghi\LaravelEventsCalendar\Mail\ContactOrganizer;
7
use DavideCasiraghi\LaravelEventsCalendar\Mail\ReportMisuse;
8
use DavideCasiraghi\LaravelEventsCalendar\Models\Continent;
9
use DavideCasiraghi\LaravelEventsCalendar\Models\Country;
10
use DavideCasiraghi\LaravelEventsCalendar\Models\Event;
11
use DavideCasiraghi\LaravelEventsCalendar\Models\EventCategory;
12
use DavideCasiraghi\LaravelEventsCalendar\Models\EventRepetition;
13
use DavideCasiraghi\LaravelEventsCalendar\Models\EventVenue;
14
use DavideCasiraghi\LaravelEventsCalendar\Models\Organizer;
15
use DavideCasiraghi\LaravelEventsCalendar\Models\Region;
16
use DavideCasiraghi\LaravelEventsCalendar\Models\Teacher;
17
use Illuminate\Foundation\Auth\User;
18
use Illuminate\Http\Request;
19
use Illuminate\Support\Facades\Auth;
20
use Illuminate\Support\Facades\DB;
21
use Illuminate\Support\Facades\Mail;
22
use Illuminate\Support\Str;
23
use Illuminate\Validation\Rule;
24
use Validator;
25
26
/**
27
 * @method static string getReportMisuseReasonDescription(integer $request->reason)  Return a string that describe the report misuse reason
28
 * @method static string getRepetitionTextString(collection $event, collection $firstRpDates)  Return a string that describe repetition kind in the event show view.
29
 * @method static string getCollectionIdsSeparatedByComma(iterable $event->teachers)  Return a string with the list of the collection id separated by comma.
30
 * @method static integer weekdayNumberOfMonth(string $date, string $dayOfWeekValue)  GET number of the specified weekday in this month (1 for the first).
31
 * @method static integer dayOfMonthFromTheEnd(integer $unixTimestamp) GET number of day from the end of the month.
32
 * @method static string getStringFromArraySeparatedByComma(iterable $items)  It returns a string that is composed by the array values separated by a comma.
33
 */
34
class EventController extends Controller
35
{
36
    /***************************************************************************/
37
    /* Restrict the access to this resource just to logged in users except show view */
38 32
    public function __construct()
39
    {
40 32
        $this->middleware('auth', ['except' => ['show', 'reportMisuse', 'reportMisuseThankyou', 'mailToOrganizer', 'mailToOrganizerSent', 'eventBySlug', 'eventBySlugAndRepetition', 'EventsListByCountry', 'calculateMonthlySelectOptions']]);
41 32
    }
42
43
    /***************************************************************************/
44
45
    /**
46
     * Display a listing of the resource.
47
     * @param  \Illuminate\Http\Request  $request
48
     * @return \Illuminate\View\View
49
     */
50 2
    public function index(Request $request)
51
    {
52
        // To show just the events created by the the user - If admin or super admin is set to null show all the events
53 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
54
55 2
        $eventCategories = EventCategory::listsTranslations('name')->orderBy('name')->pluck('name', 'id');
56 2
        $countries = Country::orderBy('name')->pluck('name', 'id');
57 2
        $venues = EventVenue::pluck('country_id', 'id');
58
59 2
        $searchKeywords = $request->input('keywords');
60 2
        $searchCategory = $request->input('category_id');
61 2
        $searchCountry = $request->input('country_id');
62
63 2
        if ($searchKeywords || $searchCategory || $searchCountry) {
64
            $events = Event::
65
                // Show only the events owned by the user, if the user is an admin or super admin show all the events
66
                when(isset($authorUserId), function ($query, $authorUserId) {
67
                    return $query->where('created_by', $authorUserId);
68 1
                })
69
                ->when($searchKeywords, function ($query, $searchKeywords) {
70 1
                    return $query->where('title', $searchKeywords)->orWhere('title', 'like', '%'.$searchKeywords.'%');
71 1
                })
72
                ->when($searchCategory, function ($query, $searchCategory) {
73 1
                    return $query->where('category_id', '=', $searchCategory);
74 1
                })
75
                ->when($searchCountry, function ($query, $searchCountry) {
76 1
                    return $query->join('event_venues', 'events.venue_id', '=', 'event_venues.id')->where('event_venues.country_id', '=', $searchCountry);
77 1
                })
78 1
                ->select('*', 'events.id as id', 'events.slug as slug', 'events.image as image') // To keep in the join the id of the Events table - https://stackoverflow.com/questions/28062308/laravel-eloquent-getting-id-field-of-joined-tables-in-eloquent
79 1
                ->paginate(20);
80
81
        //dd($events);
82
        } else {
83 1
            $events = Event::latest()
84
                ->when($authorUserId, function ($query, $authorUserId) {
85
                    return $query->where('created_by', $authorUserId);
86 1
                })
87 1
                ->paginate(20);
88
        }
89
90 2
        return view('laravel-events-calendar::events.index', compact('events'))
91 2
            ->with('i', (request()->input('page', 1) - 1) * 20)
92 2
            ->with('eventCategories', $eventCategories)
93 2
            ->with('countries', $countries)
94 2
            ->with('venues', $venues)
95 2
            ->with('searchKeywords', $searchKeywords)
96 2
            ->with('searchCategory', $searchCategory)
97 2
            ->with('searchCountry', $searchCountry);
98
    }
99
100
    /***************************************************************************/
101
102
    /**
103
     * Show the form for creating a new resource.
104
     *
105
     * @return \Illuminate\View\View
106
     */
107 1
    public function create()
108
    {
109 1
        $authorUserId = $this->getLoggedAuthorId();
110
111 1
        $eventCategories = EventCategory::listsTranslations('name')->orderBy('name')->pluck('name', 'id');
112 1
        $users = User::orderBy('name')->pluck('name', 'id');
113 1
        $teachers = Teacher::orderBy('name')->pluck('name', 'id');
114 1
        $organizers = Organizer::orderBy('name')->pluck('name', 'id');
115
        //$venues = EventVenue::pluck('name', 'id');
116 1
        $venues = DB::table('event_venues')
117 1
                ->select('id', 'name', 'city')->orderBy('name')->get();
118
119 1
        $dateTime = [];
120 1
        $dateTime['repeatUntil'] = null;
121 1
        $dateTime['multipleDates'] = null;
122
123 1
        return view('laravel-events-calendar::events.create')
124 1
            ->with('eventCategories', $eventCategories)
125 1
            ->with('users', $users)
126 1
            ->with('teachers', $teachers)
127 1
            ->with('organizers', $organizers)
128 1
            ->with('venues', $venues)
129 1
            ->with('dateTime', $dateTime)
130 1
            ->with('authorUserId', $authorUserId);
131
    }
132
133
    /***************************************************************************/
134
135
    /**
136
     * Store a newly created resource in storage.
137
     *
138
     * @param  \Illuminate\Http\Request  $request
139
     * @return \Illuminate\Http\RedirectResponse
140
     */
141 26
    public function store(Request $request)
142
    {
143
        // Validate form datas
144 26
        $validator = $this->eventsValidator($request);
145 26
        if ($validator->fails()) {
146
            //dd($validator->failed());
147 1
            return back()->withErrors($validator)->withInput();
148
        }
149
150 25
        $event = new Event();
151 25
        $this->saveOnDb($request, $event);
152
153 25
        return redirect()->route('events.index')
154 25
                        ->with('success', __('laravel-events-calendar::messages.event_added_successfully'));
155
    }
156
157
    /***************************************************************************/
158
159
    /**
160
     * Display the specified resource.
161
     *
162
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Event  $event
163
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\EventRepetition $firstRpDates
164
     * @return \Illuminate\View\View
165
     */
166 5
    public function show(Event $event, EventRepetition $firstRpDates)
167
    {
168
        //dd($firstRpDates);
169 5
        $category = EventCategory::find($event->category_id);
170 5
        $teachers = $event->teachers()->get();
171 5
        $organizers = $event->organizers()->get();
172
173 5
        $venue = DB::table('event_venues')
174 5
                ->select('id', 'name', 'city', 'address', 'zip_code', 'country_id', 'region_id', 'description', 'website', 'extra_info')
175 5
                ->where('id', $event->venue_id)
176 5
                ->first();
177
178 5
        $country = Country::find($venue->country_id);
179 5
        $region = Region::listsTranslations('name')->find($venue->region_id);
180 5
        $continent = Continent::find($country->continent_id);
181
182 5
        $repetition_text = LaravelEventsCalendar::getRepetitionTextString($event, $firstRpDates);
0 ignored issues
show
Bug introduced by
The method getRepetitionTextString() does not exist on DavideCasiraghi\LaravelE...s\LaravelEventsCalendar. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
        /** @scrutinizer ignore-call */ 
183
        $repetition_text = LaravelEventsCalendar::getRepetitionTextString($event, $firstRpDates);
Loading history...
183
184
        // True if the repetition start and end on the same day
185 5
        $sameDateStartEnd = ((date('Y-m-d', strtotime($firstRpDates->start_repeat))) == (date('Y-m-d', strtotime($firstRpDates->end_repeat)))) ? 1 : 0;
186
187 5
        return view('laravel-events-calendar::events.show', compact('event'))
188 5
                ->with('category', $category)
189 5
                ->with('teachers', $teachers)
190 5
                ->with('organizers', $organizers)
191 5
                ->with('venue', $venue)
192 5
                ->with('country', $country)
193 5
                ->with('region', $region)
194 5
                ->with('continent', $continent)
195 5
                ->with('datesTimes', $firstRpDates)
196 5
                ->with('repetition_text', $repetition_text)
197 5
                ->with('sameDateStartEnd', $sameDateStartEnd);
198
    }
199
200
    /***************************************************************************/
201
202
    /**
203
     * Show the form for editing the specified resource.
204
     *
205
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Event  $event
206
     * @return \Illuminate\Http\RedirectResponse | \Illuminate\View\View
207
     */
208 1
    public function edit(Event $event)
209
    {
210
        //if (Auth::user()->id == $event->created_by || Auth::user()->isSuperAdmin() || Auth::user()->isAdmin()) {
211 1
        if (Auth::user()->id == $event->created_by || Auth::user()->group == 1 || Auth::user()->group == 2) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
Accessing group on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
212 1
            $authorUserId = $this->getLoggedAuthorId();
213
214
            //$eventCategories = EventCategory::pluck('name', 'id');  // removed because was braking the tests
215 1
            $eventCategories = EventCategory::listsTranslations('name')->orderBy('name')->pluck('name', 'id');
216
217 1
            $users = User::orderBy('name')->pluck('name', 'id');
218 1
            $teachers = Teacher::orderBy('name')->pluck('name', 'id');
219 1
            $organizers = Organizer::orderBy('name')->pluck('name', 'id');
220 1
            $venues = DB::table('event_venues')
221 1
                    ->select('id', 'name', 'address', 'city')->orderBy('name')->get();
222
223 1
            $eventFirstRepetition = DB::table('event_repetitions')
224 1
                    ->select('id', 'start_repeat', 'end_repeat')
225 1
                    ->where('event_id', '=', $event->id)
226 1
                    ->first();
227
228 1
            $dateTime = [];
229 1
            $dateTime['dateStart'] = (isset($eventFirstRepetition->start_repeat)) ? date('d/m/Y', strtotime($eventFirstRepetition->start_repeat)) : '';
230 1
            $dateTime['dateEnd'] = (isset($eventFirstRepetition->end_repeat)) ? date('d/m/Y', strtotime($eventFirstRepetition->end_repeat)) : '';
231 1
            $dateTime['timeStart'] = (isset($eventFirstRepetition->start_repeat)) ? date('g:i A', strtotime($eventFirstRepetition->start_repeat)) : '';
232 1
            $dateTime['timeEnd'] = (isset($eventFirstRepetition->end_repeat)) ? date('g:i A', strtotime($eventFirstRepetition->end_repeat)) : '';
233 1
            $dateTime['repeatUntil'] = date('d/m/Y', strtotime($event->repeat_until));
234 1
            $dateTime['multipleDates'] = $event->multiple_dates;
235
236 1
            $multiple_teachers = LaravelEventsCalendar::getCollectionIdsSeparatedByComma($event->teachers);
0 ignored issues
show
Bug introduced by
The method getCollectionIdsSeparatedByComma() does not exist on DavideCasiraghi\LaravelE...s\LaravelEventsCalendar. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

236
            /** @scrutinizer ignore-call */ 
237
            $multiple_teachers = LaravelEventsCalendar::getCollectionIdsSeparatedByComma($event->teachers);
Loading history...
237 1
            $multiple_organizers = LaravelEventsCalendar::getCollectionIdsSeparatedByComma($event->organizers);
238
239 1
            return view('laravel-events-calendar::events.edit', compact('event'))
240 1
                        ->with('eventCategories', $eventCategories)
241 1
                        ->with('users', $users)
242 1
                        ->with('teachers', $teachers)
243 1
                        ->with('multiple_teachers', $multiple_teachers)
244 1
                        ->with('organizers', $organizers)
245 1
                        ->with('multiple_organizers', $multiple_organizers)
246 1
                        ->with('venues', $venues)
247 1
                        ->with('dateTime', $dateTime)
248 1
                        ->with('authorUserId', $authorUserId);
249
        } else {
250
            return redirect()->route('home')->with('message', __('auth.not_allowed_to_access'));
251
        }
252
    }
253
254
    /***************************************************************************/
255
256
    /**
257
     * Update the specified resource in storage.
258
     *
259
     * @param  \Illuminate\Http\Request  $request
260
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Event  $event
261
     * @return \Illuminate\Http\RedirectResponse
262
     */
263 2
    public function update(Request $request, Event $event)
264
    {
265
        // Validate form datas
266 2
        $validator = $this->eventsValidator($request);
267 2
        if ($validator->fails()) {
268 1
            return back()->withErrors($validator)->withInput();
269
        }
270
271 1
        $this->saveOnDb($request, $event);
272
273 1
        return redirect()->route('events.index')
274 1
                        ->with('success', __('laravel-events-calendar::messages.event_updated_successfully'));
275
    }
276
277
    /***************************************************************************/
278
279
    /**
280
     * Remove the specified resource from storage.
281
     *
282
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Event  $event
283
     * @return \Illuminate\Http\RedirectResponse
284
     */
285 1
    public function destroy(Event $event)
286
    {
287 1
        DB::table('event_repetitions')
288 1
                ->where('event_id', $event->id)
289 1
                ->delete();
290
291 1
        $event->delete();
292
293 1
        return redirect()->route('events.index')
294 1
                        ->with('success', __('laravel-events-calendar::messages.event_deleted_successfully'));
295
    }
296
297
    /***************************************************************************/
298
299
    /**
300
     * To save event repetitions for create and update methods.
301
     *
302
     * @param  \Illuminate\Http\Request  $request
303
     * @param  int  $eventId
304
     * @return void
305
     */
306 25
    public function saveEventRepetitions(Request $request, int $eventId)
307
    {
308 25
        EventRepetition::deletePreviousRepetitions($eventId);
309
310
        // Saving repetitions - If it's a single event will be stored with just one repetition
311
        //$timeStart = date('H:i:s', strtotime($request->get('time_start')));
312
        //$timeEnd = date('H:i:s', strtotime($request->get('time_end')));
313
        //$timeStart = $request->get('time_start');
314
        //$timeEnd = $request->get('time_end');
315 25
        $timeStart = date('H:i', strtotime($request->get('time_start')));
316 25
        $timeEnd = date('H:i', strtotime($request->get('time_end')));
317
318 25
        switch ($request->get('repeat_type')) {
319 25
                case '1':  // noRepeat
320 17
                    $eventRepetition = new EventRepetition();
321 17
                    $eventRepetition->event_id = $eventId;
322
323 17
                    $dateStart = implode('-', array_reverse(explode('/', $request->get('startDate'))));
324 17
                    $dateEnd = implode('-', array_reverse(explode('/', $request->get('endDate'))));
325
326 17
                    $eventRepetition->start_repeat = $dateStart.' '.$timeStart;
327 17
                    $eventRepetition->end_repeat = $dateEnd.' '.$timeEnd;
328 17
                    $eventRepetition->save();
329
330 17
                    break;
331
332 8
                case '2':   // repeatWeekly
333
                    // Convert the start date in a format that can be used for strtotime
334 2
                        $startDate = implode('-', array_reverse(explode('/', $request->get('startDate'))));
335
336
                    // Calculate repeat until day
337 2
                        $repeatUntilDate = implode('-', array_reverse(explode('/', $request->get('repeat_until'))));
338 2
                        EventRepetition::saveWeeklyRepeatDates($eventId, $request->get('repeat_weekly_on_day'), $startDate, $repeatUntilDate, $timeStart, $timeEnd);
0 ignored issues
show
Bug introduced by
It seems like $request->get('repeat_weekly_on_day') can also be of type null; however, parameter $weekDays of DavideCasiraghi\LaravelE...saveWeeklyRepeatDates() does only seem to accept array, 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 ignore-type  annotation

338
                        EventRepetition::saveWeeklyRepeatDates($eventId, /** @scrutinizer ignore-type */ $request->get('repeat_weekly_on_day'), $startDate, $repeatUntilDate, $timeStart, $timeEnd);
Loading history...
339
340 2
                    break;
341
342 6
                case '3':  //repeatMonthly
343
                    // Same of repeatWeekly
344 5
                        $startDate = implode('-', array_reverse(explode('/', $request->get('startDate'))));
345 5
                        $repeatUntilDate = implode('-', array_reverse(explode('/', $request->get('repeat_until'))));
346
347
                    // Get the array with month repeat details
348 5
                        $monthRepeatDatas = explode('|', $request->get('on_monthly_kind'));
349
                        //dump("pp_1");
350 5
                        EventRepetition::saveMonthlyRepeatDates($eventId, $monthRepeatDatas, $startDate, $repeatUntilDate, $timeStart, $timeEnd);
351
352 5
                    break;
353
354 1
                case '4':  //repeatMultipleDays
355
                    // Same of repeatWeekly
356 1
                        $startDate = implode('-', array_reverse(explode('/', $request->get('startDate'))));
357
358
                    // Get the array with single day repeat details
359 1
                        $singleDaysRepeatDatas = explode(',', $request->get('multiple_dates'));
360
361 1
                        EventRepetition::saveMultipleRepeatDates($eventId, $singleDaysRepeatDatas, $startDate, $timeStart, $timeEnd);
362
363 1
                    break;
364
            }
365 25
    }
366
367
    /***************************************************************************/
368
369
    /**
370
     * Send the Misuse mail.
371
     *
372
     * @param  \Illuminate\Http\Request  $request
373
     * @return \Illuminate\Http\RedirectResponse
374
     */
375
    public function reportMisuse(Request $request)
376
    {
377
        $report = [];
378
379
        //$report['senderEmail'] = '[email protected]';
380
        $report['senderEmail'] = $request->user_email;
381
        $report['senderName'] = 'Anonymus User';
382
        $report['subject'] = 'Report misuse form';
383
        //$report['adminEmail'] = env('ADMIN_MAIL');
384
        $report['creatorEmail'] = $this->getCreatorEmail($request->created_by);
385
386
        $report['message_misuse'] = $request->message_misuse;
387
        $report['event_title'] = $request->event_title;
388
        $report['event_id'] = $request->event_id;
389
        $report['event_slug'] = $request->slug;
390
391
        $report['reason'] = LaravelEventsCalendar::getReportMisuseReasonDescription($request->reason);
0 ignored issues
show
Bug introduced by
The method getReportMisuseReasonDescription() does not exist on DavideCasiraghi\LaravelE...s\LaravelEventsCalendar. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

391
        /** @scrutinizer ignore-call */ 
392
        $report['reason'] = LaravelEventsCalendar::getReportMisuseReasonDescription($request->reason);
Loading history...
392
393
        /*
394
                switch ($request->reason) {
395
                    case '1':
396
                        $report['reason'] = 'Not about Contact Improvisation';
397
                        break;
398
                    case '2':
399
                        $report['reason'] = 'Contains wrong informations';
400
                        break;
401
                    case '3':
402
                        $report['reason'] = 'It is not translated in english';
403
                        break;
404
                    case '4':
405
                        $report['reason'] = 'Other (specify in the message)';
406
                        break;
407
                }
408
        */
409
410
        //Mail::to($request->user())->send(new ReportMisuse($report));
411
        Mail::to(env('ADMIN_MAIL'))->send(new ReportMisuse($report));
412
413
        return redirect()->route('events.misuse-thankyou');
414
    }
415
416
    /***************************************************************************/
417
418
    /**
419
     * Send the mail to the Organizer (from the event modal in the event show view).
420
     *
421
     * @param  \Illuminate\Http\Request  $request
422
     * @return \Illuminate\Http\RedirectResponse
423
     */
424
    public function mailToOrganizer(Request $request)
425
    {
426
        $message = [];
427
        $message['senderEmail'] = $request->user_email;
428
        $message['senderName'] = $request->user_name;
429
        $message['subject'] = 'Request from the Global CI Calendar';
430
        //$message['emailTo'] = $organizersEmails;
431
432
        $message['message'] = $request->message;
433
        $message['event_title'] = $request->event_title;
434
        $message['event_id'] = $request->event_id;
435
        $message['event_slug'] = $request->slug;
436
437
        /*
438
        $eventOrganizers = Event::find($request->event_id)->organizers;
439
        foreach ($eventOrganizers as $eventOrganizer) {
440
            Mail::to($eventOrganizer->email)->send(new ContactOrganizer($message));
441
        }*/
442
443
        Mail::to($request->contact_email)->send(new ContactOrganizer($message));
444
445
        return redirect()->route('events.organizer-sent');
446
    }
447
448
    /***************************************************************************/
449
450
    /**
451
     * Display the thank you view after the mail to the organizer is sent (called by /mailToOrganizer/sent route).
452
     *
453
     * @return \Illuminate\View\View
454
     */
455 1
    public function mailToOrganizerSent()
456
    {
457 1
        return view('laravel-events-calendar::emails.contact.organizer-sent');
458
    }
459
460
    /***************************************************************************/
461
462
    /**
463
     * Display the thank you view after the misuse report mail is sent (called by /misuse/thankyou route).
464
     *
465
     * @return \Illuminate\View\View
466
     */
467 1
    public function reportMisuseThankyou()
468
    {
469 1
        return view('laravel-events-calendar::emails.report-thankyou');
470
    }
471
472
    /***************************************************************************/
473
474
    /**
475
     * Set the Event attributes about repeating before store or update (repeat until field and multiple days).
476
     *
477
     * @param  \Illuminate\Http\Request  $request
478
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Event  $event
479
     * @return \DavideCasiraghi\LaravelEventsCalendar\Models\Event  $event
480
     */
481 25
    public function setEventRepeatFields(Request $request, Event $event)
482
    {
483
        // Set Repeat Until
484 25
        $event->repeat_type = $request->get('repeat_type');
485 25
        if ($request->get('repeat_until')) {
486 7
            $dateRepeatUntil = implode('-', array_reverse(explode('/', $request->get('repeat_until'))));
487 7
            $event->repeat_until = $dateRepeatUntil.' 00:00:00';
488
        }
489
490
        // Weekely - Set multiple week days
491 25
        if ($request->get('repeat_weekly_on_day')) {
492 2
            $repeat_weekly_on_day = $request->get('repeat_weekly_on_day');
493
            //dd($repeat_weekly_on_day);
494 2
            $i = 0;
495 2
            $len = count($repeat_weekly_on_day); // to put "," to all items except the last
496 2
            $event->repeat_weekly_on = '';
497 2
            foreach ($repeat_weekly_on_day as $key => $weeek_day) {
498 2
                $event->repeat_weekly_on .= $weeek_day;
499 2
                if ($i != $len - 1) {  // not last
500 2
                    $event->repeat_weekly_on .= ',';
501
                }
502 2
                $i++;
503
            }
504
        }
505
506
        // Monthly
507
508
        /* $event->repeat_type = $request->get('repeat_monthly_on');*/
509
510 25
        return $event;
511
    }
512
513
    /***************************************************************************/
514
515
    /**
516
     * Return the HTML of the monthly select dropdown - inspired by - https://www.theindychannel.com/calendar
517
     * - Used by the AJAX in the event repeat view -
518
     * - The HTML contain a <select></select> with four <options></options>.
519
     *
520
     * @param  \Illuminate\Http\Request  $request  - Just the day
521
     * @return string
522
     */
523 1
    public function calculateMonthlySelectOptions(Request $request)
524
    {
525 1
        $monthlySelectOptions = [];
526 1
        $date = implode('-', array_reverse(explode('/', $request->day)));  // Our YYYY-MM-DD date string
527 1
        $unixTimestamp = strtotime($date);  // Convert the date string into a unix timestamp.
528 1
        $dayOfWeekString = date('l', $unixTimestamp); // Monday | Tuesday | Wednesday | ..
529
530
        // Same day number - eg. "the 28th day of the month"
531 1
        $dateArray = explode('/', $request->day);
532 1
        $dayNumber = ltrim($dateArray[0], '0'); // remove the 0 in front of a day number eg. 02/10/2018
533
534 1
        $format = __('laravel-events-calendar::ordinalDays.the_'.($dayNumber).'_x_of_the_month');
535 1
        $repeatText = sprintf($format, 'day');
0 ignored issues
show
Bug introduced by
It seems like $format can also be of type array and array; however, parameter $format of sprintf() does only seem to accept string, 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 ignore-type  annotation

535
        $repeatText = sprintf(/** @scrutinizer ignore-type */ $format, 'day');
Loading history...
536
537 1
        array_push($monthlySelectOptions, [
538 1
            'value' => '0|'.$dayNumber,
539 1
            'text' => $repeatText,
540
        ]);
541
542
        // Same weekday/week of the month - eg. the "1st Monday" 1|1|1 (first week, monday)
543 1
            $dayOfWeekValue = date('N', $unixTimestamp); // 1 (for Monday) through 7 (for Sunday)
544 1
            $weekOfTheMonth = LaravelEventsCalendar::weekdayNumberOfMonth($date, $dayOfWeekValue); // 1 | 2 | 3 | 4 | 5
0 ignored issues
show
Bug introduced by
The method weekdayNumberOfMonth() does not exist on DavideCasiraghi\LaravelE...s\LaravelEventsCalendar. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

544
            /** @scrutinizer ignore-call */ 
545
            $weekOfTheMonth = LaravelEventsCalendar::weekdayNumberOfMonth($date, $dayOfWeekValue); // 1 | 2 | 3 | 4 | 5
Loading history...
545
546 1
            $format = __('laravel-events-calendar::ordinalDays.the_'.($weekOfTheMonth).'_x_of_the_month');
547 1
        $repeatText = sprintf($format, $dayOfWeekString);
548
549 1
        array_push($monthlySelectOptions, [
550 1
            'value' => '1|'.$weekOfTheMonth.'|'.$dayOfWeekValue,
551 1
            'text' => $repeatText,
552
        ]);
553
554
        // Same day of the month (from the end) - the 3rd to last day (0 if last day, 1 if 2nd to last day, , 2 if 3rd to last day)
555 1
            $dayOfMonthFromTheEnd = LaravelEventsCalendar::dayOfMonthFromTheEnd($unixTimestamp); // 1 | 2 | 3 | 4 | 5
0 ignored issues
show
Bug introduced by
The method dayOfMonthFromTheEnd() does not exist on DavideCasiraghi\LaravelE...s\LaravelEventsCalendar. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

555
            /** @scrutinizer ignore-call */ 
556
            $dayOfMonthFromTheEnd = LaravelEventsCalendar::dayOfMonthFromTheEnd($unixTimestamp); // 1 | 2 | 3 | 4 | 5
Loading history...
556
557 1
            $format = __('laravel-events-calendar::ordinalDays.the_'.($dayOfMonthFromTheEnd + 1).'_to_last_x_of_the_month');
558 1
        $repeatText = sprintf($format, 'day');
559
560 1
        array_push($monthlySelectOptions, [
561 1
            'value' => '2|'.$dayOfMonthFromTheEnd,
562 1
            'text' => $repeatText,
563
        ]);
564
565
        // Same weekday/week of the month (from the end) - the last Friday - (0 if last Friday, 1 if the 2nd to last Friday, 2 if the 3nd to last Friday)
566 1
            $weekOfMonthFromTheEnd = LaravelEventsCalendar::weekOfMonthFromTheEnd($unixTimestamp); // 1 | 2 | 3 | 4 | 5
0 ignored issues
show
Bug introduced by
The method weekOfMonthFromTheEnd() does not exist on DavideCasiraghi\LaravelE...s\LaravelEventsCalendar. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

566
            /** @scrutinizer ignore-call */ 
567
            $weekOfMonthFromTheEnd = LaravelEventsCalendar::weekOfMonthFromTheEnd($unixTimestamp); // 1 | 2 | 3 | 4 | 5
Loading history...
567
568 1
            if ($weekOfMonthFromTheEnd == 1) {
569
                $weekValue = 0;
570
            } else {
571 1
                $weekValue = $weekOfMonthFromTheEnd - 1;
572
            }
573
574 1
        $format = __('laravel-events-calendar::ordinalDays.the_'.($weekOfMonthFromTheEnd).'_to_last_x_of_the_month');
575 1
        $repeatText = sprintf($format, $dayOfWeekString);
576
577 1
        array_push($monthlySelectOptions, [
578 1
            'value' => '3|'.$weekValue.'|'.$dayOfWeekValue,
579 1
            'text' => $repeatText,
580
        ]);
581
582
        // GENERATE the HTML to return
583 1
        $selectTitle = __('laravel-events-calendar::general.select_repeat_monthly_kind');
584 1
        $onMonthlyKindSelect = "<select name='on_monthly_kind' id='on_monthly_kind' class='selectpicker' title='".$selectTitle."'>";
0 ignored issues
show
Bug introduced by
Are you sure $selectTitle of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

584
        $onMonthlyKindSelect = "<select name='on_monthly_kind' id='on_monthly_kind' class='selectpicker' title='"./** @scrutinizer ignore-type */ $selectTitle."'>";
Loading history...
585 1
        foreach ($monthlySelectOptions as $key => $monthlySelectOption) {
586 1
            $onMonthlyKindSelect .= "<option value='".$monthlySelectOption['value']."'>".$monthlySelectOption['text'].'</option>';
587
        }
588 1
        $onMonthlyKindSelect .= '</select>';
589
590 1
        return $onMonthlyKindSelect;
591
    }
592
593
    // **********************************************************************
594
595
    /**
596
     * Save/Update the record on DB.
597
     *
598
     * @param  \Illuminate\Http\Request $request
599
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Event $event
600
     * @return void
601
     */
602 25
    public function saveOnDb(Request $request, Event $event)
603
    {
604
        //$countries = Country::getCountries();
605 25
        $teachers = Teacher::pluck('name', 'id');
606
607
        /*$venue = DB::table('event_venues')
608
                ->select('event_venues.id AS venue_id', 'event_venues.name AS venue_name', 'event_venues.country_id AS country_id', 'event_venues.continent_id', 'event_venues.city')
609
                ->where('event_venues.id', '=', $request->get('venue_id'))
610
                ->first();*/
611
612 25
        $event->title = $request->get('title');
613 25
        $event->description = clean($request->get('description'));
614
615 25
        if ($request->get('created_by')) {
616 25
            $event->created_by = $request->get('created_by');
617
        }
618
619 25
        if (! $event->slug) {
620 25
            $event->slug = Str::slug($event->title, '-').'-'.rand(100000, 1000000);
621
        }
622 25
        $event->category_id = $request->get('category_id');
623 25
        $event->venue_id = $request->get('venue_id');
624 25
        $event->image = $request->get('image');
625 25
        $event->contact_email = $request->get('contact_email');
626 25
        $event->website_event_link = $request->get('website_event_link');
627 25
        $event->facebook_event_link = $request->get('facebook_event_link');
628 25
        $event->status = $request->get('status');
629 25
        $event->on_monthly_kind = $request->get('on_monthly_kind');
630 25
        $event->multiple_dates = $request->get('multiple_dates');
631
632
        // Event teaser image upload
633 25
        if ($request->file('image')) {
634
            $imageFile = $request->file('image');
635
            $imageName = time().'.'.'jpg';  //$imageName = $teaserImageFile->hashName();
636
            $imageSubdir = 'events_teaser';
637
            $imageWidth = 968;
638
            $thumbWidth = 310;
639
640
            $this->uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth);
641
            $event->image = $imageName;
642
        } else {
643 25
            $event->image = $request->get('image');
644
        }
645
646
        // Support columns for homepage search (we need this to show events in HP with less use of resources)
647 25
        $event->sc_teachers_id = json_encode(explode(',', $request->get('multiple_teachers'))); // keep just this SC
648
649
        // Multiple teachers - populate support column field
650 25
        $event->sc_teachers_names = '';
651 25
        if ($request->get('multiple_teachers')) {
652 2
            $multiple_teachers = explode(',', $request->get('multiple_teachers'));
653
654 2
            $multiple_teachers_names = [];
655 2
            foreach ($multiple_teachers as $key => $teacher_id) {
656 2
                $multiple_teachers_names[] = $teachers[$teacher_id];
657
            }
658
659 2
            $event->sc_teachers_names .= LaravelEventsCalendar::getStringFromArraySeparatedByComma($multiple_teachers_names);
0 ignored issues
show
Bug introduced by
The method getStringFromArraySeparatedByComma() does not exist on DavideCasiraghi\LaravelE...s\LaravelEventsCalendar. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

659
            $event->sc_teachers_names .= LaravelEventsCalendar::/** @scrutinizer ignore-call */ getStringFromArraySeparatedByComma($multiple_teachers_names);
Loading history...
660
        }
661
662
        // Set the Event attributes about repeating (repeat until field and multiple days)
663 25
        $event = $this->setEventRepeatFields($request, $event);
664
665
        // Save event and repetitions
666 25
        $event->save();
667 25
        $this->saveEventRepetitions($request, $event->id);
668
669
        // Update multi relationships with teachers and organizers tables.
670 25
        if ($request->get('multiple_teachers')) {
671 2
            $multiple_teachers = explode(',', $request->get('multiple_teachers'));
672 2
            $event->teachers()->sync($multiple_teachers);
673
        } else {
674 23
            $event->teachers()->sync([]);
675
        }
676 25
        if ($request->get('multiple_organizers')) {
677
            $multiple_organizers = explode(',', $request->get('multiple_organizers'));
678
            $event->organizers()->sync($multiple_organizers);
679
        } else {
680 25
            $event->organizers()->sync([]);
681
        }
682 25
    }
683
684
    /***********************************************************************/
685
686
    /**
687
     * Get creator email.
688
     *
689
     * @param  int $created_by
690
     * @return \Illuminate\Foundation\Auth\User
691
     */
692
    public function getCreatorEmail(int $created_by)
693
    {
694
        $creatorEmail = DB::table('users')  // Used to send the Report misuse (not in english)
695
                ->select('email')
696
                ->where('id', $created_by)
697
                ->first();
698
699
        $ret = $creatorEmail->email;
700
701
        return $ret;
702
    }
703
704
    /***************************************************************************/
705
706
    /**
707
     * Return the event by SLUG. (eg. http://websitename.com/event/xxxx).
708
     *
709
     * @param  string  $slug
710
     * @return \Illuminate\View\View
711
     */
712 1
    public function eventBySlug(string $slug)
713
    {
714 1
        $event = Event::where('slug', $slug)->first();
715 1
        $firstRpDates = EventRepetition::getFirstEventRpDatesByEventId($event->id);
716
717 1
        return $this->show($event, $firstRpDates);
718
    }
719
720
    /***************************************************************************/
721
722
    /**
723
     * Return the event by SLUG. (eg. http://websitename.com/event/xxxx/300).
724
     * @param  string $slug
725
     * @param  int $repetitionId
726
     * @return \Illuminate\View\View
727
     */
728 4
    public function eventBySlugAndRepetition(string $slug, int $repetitionId)
729
    {
730 4
        $event = Event::where('slug', $slug)->first();
731 4
        $firstRpDates = EventRepetition::getFirstEventRpDatesByRepetitionId($repetitionId);
732
733
        // If not found get the first repetion of the event in the future.
734 4
        if (empty($firstRpDates)) {
735 1
            $firstRpDates = EventRepetition::getFirstEventRpDatesByEventId($event->id);
736
        }
737
738 4
        return $this->show($event, $firstRpDates);
739
    }
740
741
    /***************************************************************************/
742
743
    /**
744
     * Return the Event validator with all the defined constraint.
745
     * @param  \Illuminate\Http\Request  $request
746
     * @return \Illuminate\Http\Response
747
     */
748 26
    public function eventsValidator(Request $request)
749
    {
750
        $rules = [
751 26
            'title' => 'required',
752 26
            'description' => 'required',
753 26
            'category_id' => 'required',
754 26
            'venue_id' => 'required',
755 26
            'startDate' => 'required',
756 26
            'endDate' => 'required',
757 26
            'repeat_until' => Rule::requiredIf($request->repeat_type == 2 || $request->repeat_type == 3),
758 26
            'repeat_weekly_on_day' => Rule::requiredIf($request->repeat_type == 2),
759 26
            'on_monthly_kind' => Rule::requiredIf($request->repeat_type == 3),
760 26
            'contact_email' => 'nullable|email',
761 26
            'facebook_event_link' => 'nullable|url',
762 26
            'website_event_link' => 'nullable|url',
763
            // 'image' => 'nullable|image|mimes:jpeg,jpg,png|max:3000', // BUG create problems to validate on edit. Fix this after the rollout
764
        ];
765 26
        if ($request->hasFile('image')) {
766
            $rules['image'] = 'nullable|image|mimes:jpeg,jpg,png|max:5000';
767
        }
768
769
        $messages = [
770 26
            'repeat_weekly_on_day[].required' => 'Please specify which day of the week is repeting the event.',
771
            'on_monthly_kind.required' => 'Please specify the kind of monthly repetion',
772
            'endDate.same' => 'If the event is repetitive the start date and end date must match',
773
            'facebook_event_link.url' => 'The facebook link is invalid. It should start with https://',
774
            'website_event_link.url' => 'The website link is invalid. It should start with https://',
775
            'image.max' => 'The maximum image size is 5MB. If you need to resize it you can use: www.simpleimageresizer.com',
776
        ];
777
778 26
        $validator = Validator::make($request->all(), $rules, $messages);
779
780
        // End date and start date must match if the event is repetitive
781
        $validator->sometimes('endDate', 'same:startDate', function ($input) {
782 26
            return $input->repeat_type > 1;
783 26
        });
784
785 26
        return $validator;
786
    }
787
}
788