|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelEventsCalendar\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Support\Facades\Cache; |
|
8
|
|
|
|
|
9
|
|
|
use DavideCasiraghi\LaravelEventsCalendar\Models\EventRepetition; |
|
10
|
|
|
|
|
11
|
|
|
class Event extends Model |
|
12
|
|
|
{ |
|
13
|
|
|
/***************************************************************************/ |
|
14
|
|
|
/** |
|
15
|
|
|
* The table associated with the model. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $table = 'events'; |
|
20
|
|
|
|
|
21
|
|
|
/***************************************************************************/ |
|
22
|
|
|
|
|
23
|
|
|
protected $fillable = [ |
|
24
|
|
|
'title', |
|
25
|
|
|
'description', |
|
26
|
|
|
'organized_by', |
|
27
|
|
|
'category_id', |
|
28
|
|
|
'venue_id', |
|
29
|
|
|
'image', |
|
30
|
|
|
'facebook_event_link', |
|
31
|
|
|
'website_event_link', |
|
32
|
|
|
'status', |
|
33
|
|
|
'repeat_type', |
|
34
|
|
|
'repeat_until', |
|
35
|
|
|
'repeat_weekly_on', |
|
36
|
|
|
'repeat_monthly_on', |
|
37
|
|
|
'on_monthly_kind', |
|
38
|
|
|
'multiple_dates', |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
/***************************************************************************/ |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the teachers for the event. |
|
45
|
|
|
*/ |
|
46
|
21 |
|
public function teachers() |
|
47
|
|
|
{ |
|
48
|
21 |
|
return $this->belongsToMany('DavideCasiraghi\LaravelEventsCalendar\Models\Teacher', 'event_has_teachers', 'event_id', 'teacher_id'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/***************************************************************************/ |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get the organizers for the event. |
|
55
|
|
|
*/ |
|
56
|
21 |
|
public function organizers() |
|
57
|
|
|
{ |
|
58
|
21 |
|
return $this->belongsToMany('DavideCasiraghi\LaravelEventsCalendar\Models\Organizer', 'event_has_organizers', 'event_id', 'organizer_id'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/***************************************************************************/ |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the organizers for the event. |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function eventRepetitions() |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->hasMany('DavideCasiraghi\LaravelEventsCalendar\Models\EventRepetition', 'event_id'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/***************************************************************************/ |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Delete all the previous repetitions from the event_repetitions table. |
|
75
|
|
|
* |
|
76
|
|
|
* @param int $eventId |
|
77
|
|
|
* @return void |
|
78
|
|
|
*/ |
|
79
|
21 |
|
public static function deletePreviousRepetitions($eventId) |
|
80
|
|
|
{ |
|
81
|
21 |
|
EventRepetition::where('event_id', $eventId)->delete(); |
|
82
|
21 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/***************************************************************************/ |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Return Start and End dates of the first repetition of an event - By Event ID. |
|
88
|
|
|
* |
|
89
|
|
|
* @param int $eventId |
|
90
|
|
|
* @return \DavideCasiraghi\LaravelEventsCalendar\Models\EventRepetition |
|
91
|
|
|
*/ |
|
92
|
2 |
|
public static function getFirstEventRpDatesByEventId($eventId) |
|
93
|
|
|
{ |
|
94
|
|
|
$ret = EventRepetition:: |
|
95
|
2 |
|
select('start_repeat', 'end_repeat') |
|
96
|
2 |
|
->where('event_id', $eventId) |
|
97
|
2 |
|
->first(); |
|
98
|
|
|
|
|
99
|
2 |
|
return $ret; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/***************************************************************************/ |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Return Start and End dates of the first repetition of an event - By Repetition ID. |
|
106
|
|
|
* |
|
107
|
|
|
* @param int $repetitionId |
|
108
|
|
|
* @return \DavideCasiraghi\LaravelEventsCalendar\Models\EventRepetition |
|
109
|
|
|
*/ |
|
110
|
3 |
|
public static function getFirstEventRpDatesByRepetitionId($repetitionId) |
|
111
|
|
|
{ |
|
112
|
|
|
$ret = EventRepetition:: |
|
113
|
3 |
|
select('start_repeat', 'end_repeat') |
|
114
|
3 |
|
->where('id', $repetitionId) |
|
115
|
3 |
|
->first(); |
|
116
|
|
|
|
|
117
|
3 |
|
return $ret; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/***************************************************************************/ |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Return the all the active events. |
|
124
|
|
|
* |
|
125
|
|
|
* @return \DavideCasiraghi\LaravelEventsCalendar\Models\Event |
|
126
|
|
|
*/ |
|
127
|
1 |
|
public static function getActiveEvents() |
|
128
|
|
|
{ |
|
129
|
1 |
|
$cacheExpireMinutes = 15; // Set the duration time of the cache |
|
130
|
|
|
|
|
131
|
|
|
$ret = Cache::remember('active_events', $cacheExpireMinutes, function () { |
|
132
|
1 |
|
date_default_timezone_set('Europe/Rome'); |
|
133
|
1 |
|
$searchStartDate = date('Y-m-d', time()); |
|
134
|
1 |
|
$lastestEventsRepetitionsQuery = EventRepetition::getLastestEventsRepetitionsQuery($searchStartDate, null); |
|
135
|
|
|
|
|
136
|
|
|
return self:: |
|
137
|
1 |
|
select('title', 'countries.name AS country_name', 'countries.id AS country_id', 'countries.continent_id AS continent_id', 'event_venues.city AS city') |
|
138
|
1 |
|
->join('event_venues', 'event_venues.id', '=', 'events.venue_id') |
|
139
|
1 |
|
->join('countries', 'countries.id', '=', 'event_venues.country_id') |
|
140
|
|
|
->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
|
141
|
1 |
|
$join->on('events.id', '=', 'event_repetitions.event_id'); |
|
142
|
1 |
|
}) |
|
143
|
1 |
|
->get(); |
|
144
|
1 |
|
}); |
|
145
|
|
|
|
|
146
|
1 |
|
return $ret; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/***************************************************************************/ |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Return the active events based on the search keys provided. |
|
153
|
|
|
* |
|
154
|
|
|
* @param array $filters |
|
155
|
|
|
* @param int $itemPerPage |
|
156
|
|
|
* @return \DavideCasiraghi\LaravelEventsCalendar\Models\Event |
|
157
|
|
|
*/ |
|
158
|
|
|
//$keywords, $category, $city, $country, $continent, $teacher, $venue, $startDate, $endDate, |
|
159
|
1 |
|
public static function getEvents(array $filters, $itemPerPage) |
|
160
|
|
|
{ |
|
161
|
1 |
|
if (! array_key_exists('startDate', $filters) || ! $filters['startDate']) { |
|
162
|
1 |
|
$filters['startDate'] = Carbon::now()->format('Y-m-d'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
// Sub-Query Joins - https://laravel.com/docs/5.7/queries |
|
166
|
1 |
|
$lastestEventsRepetitionsQuery = EventRepetition::getLastestEventsRepetitionsQuery($filters['startDate'], $filters['endDate']); |
|
167
|
|
|
|
|
168
|
|
|
// Retrieve the events that correspond to the selected filters |
|
169
|
1 |
|
if ($filters['keywords'] || $filters['category'] || $filters['city'] || $filters['country'] || $filters['region'] || $filters['continent'] || $filters['teacher'] || $filters['venue'] || $filters['endDate']) { |
|
170
|
|
|
|
|
171
|
|
|
//$start = microtime(true); |
|
172
|
|
|
//DB::enableQueryLog(); |
|
173
|
|
|
$ret = self:: |
|
174
|
1 |
|
select('events.title', 'events.category_id', 'events.slug', 'event_venues.name as venue_name', 'event_venues.city as city_name', 'countries.name as country_name', 'events.sc_teachers_names', 'event_repetitions.start_repeat', 'event_repetitions.end_repeat') |
|
175
|
|
|
->when($filters['keywords'], function ($query, $keywords) { |
|
176
|
1 |
|
return $query->where('title', 'like', '%'.$keywords.'%'); |
|
177
|
1 |
|
}) |
|
178
|
|
|
->when($filters['category'], function ($query, $category) { |
|
179
|
1 |
|
return $query->where('category_id', '=', $category); |
|
180
|
1 |
|
}) |
|
181
|
|
|
->when($filters['teacher'], function ($query, $teacher) { |
|
182
|
|
|
return $query->whereRaw('json_contains(sc_teachers_id, \'["'.$teacher.'"]\')'); |
|
183
|
1 |
|
}) |
|
184
|
|
|
->when($filters['country'], function ($query, $country) { |
|
185
|
1 |
|
return $query->where('event_venues.country_id', '=', $country); |
|
186
|
1 |
|
}) |
|
187
|
|
|
->when($filters['region'], function ($query, $region) { |
|
188
|
|
|
return $query->where('event_venues.region_id', '=', $region); |
|
189
|
1 |
|
}) |
|
190
|
|
|
->when($filters['continent'], function ($query, $continent) { |
|
191
|
1 |
|
return $query->where('event_venues.continent_id', '=', $continent); //sc_continent_id |
|
192
|
1 |
|
}) |
|
193
|
|
|
->when($filters['city'], function ($query, $city) { |
|
194
|
|
|
return $query->where('event_venues.city', 'like', '%'.$city.'%'); |
|
195
|
1 |
|
}) |
|
196
|
|
|
->when($filters['venue'], function ($query, $venue) { |
|
197
|
|
|
return $query->where('event_venues.name', 'like', '%'.$venue.'%'); |
|
198
|
1 |
|
}) |
|
199
|
|
|
->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
|
200
|
1 |
|
$join->on('events.id', '=', 'event_repetitions.event_id'); |
|
201
|
1 |
|
}) |
|
202
|
|
|
|
|
203
|
1 |
|
->leftJoin('event_venues', 'events.venue_id', '=', 'event_venues.id') |
|
204
|
1 |
|
->leftJoin('continents', 'event_venues.continent_id', '=', 'continents.id') |
|
205
|
1 |
|
->leftJoin('countries', 'event_venues.country_id', '=', 'countries.id') |
|
206
|
1 |
|
->leftJoin('regions', 'event_venues.region_id', '=', 'regions.id') |
|
207
|
|
|
//->leftJoin('region_translations', 'regions.id', '=', 'region_translations.region_id') |
|
208
|
|
|
|
|
209
|
1 |
|
->orderBy('event_repetitions.start_repeat', 'asc') |
|
210
|
1 |
|
->paginate($itemPerPage); |
|
211
|
|
|
//dd(DB::getQueryLog()); |
|
212
|
|
|
|
|
213
|
|
|
//$time = microtime(true) - $start; |
|
214
|
|
|
//dd($time); |
|
215
|
|
|
} |
|
216
|
|
|
// If no filter selected retrieve all the events |
|
217
|
|
|
else { |
|
218
|
|
|
$ret = self:: |
|
219
|
|
|
select('events.title', 'events.category_id', 'events.slug', 'event_venues.name as venue_name', 'event_venues.city as city_name', 'countries.name as country_name', 'events.sc_teachers_names', 'event_repetitions.start_repeat', 'event_repetitions.end_repeat') |
|
220
|
|
|
->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
|
221
|
|
|
$join->on('events.id', '=', 'event_repetitions.event_id'); |
|
222
|
|
|
}) |
|
223
|
|
|
->leftJoin('event_venues', 'events.venue_id', '=', 'event_venues.id') |
|
224
|
|
|
->leftJoin('continents', 'event_venues.continent_id', '=', 'continents.id') |
|
225
|
|
|
->leftJoin('countries', 'event_venues.country_id', '=', 'countries.id') |
|
226
|
|
|
->leftJoin('regions', 'event_venues.region_id', '=', 'regions.id') |
|
227
|
|
|
->leftJoin('region_translations', 'regions.id', '=', 'region_translations.region_id') |
|
228
|
|
|
|
|
229
|
|
|
->orderBy('event_repetitions.start_repeat', 'asc') |
|
230
|
|
|
->paginate($itemPerPage); |
|
231
|
|
|
|
|
232
|
|
|
// It works, but I don't use it now to develop |
|
233
|
|
|
/*$cacheExpireMinutes = 15; |
|
234
|
|
|
$events = Cache::remember('all_events', $cacheExpireTime, function () { |
|
235
|
|
|
return DB::table('events')->latest()->paginate(20); |
|
236
|
|
|
});*/ |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
1 |
|
return $ret; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/***************************************************************************/ |
|
243
|
|
|
|
|
244
|
|
|
/* |
|
245
|
|
|
* Format the start date to be used in the search query. |
|
246
|
|
|
* If the start date is null return today's date. |
|
247
|
|
|
* |
|
248
|
|
|
* @param string $DatePickerStartDate |
|
249
|
|
|
* @return string |
|
250
|
|
|
*/ |
|
251
|
|
|
/*public static function prepareStartDate($DatePickerStartDate) |
|
252
|
|
|
{ |
|
253
|
|
|
if ($DatePickerStartDate) { |
|
254
|
|
|
list($tid, $tim, $tiy) = explode('/', $DatePickerStartDate); |
|
255
|
|
|
$ret = "$tiy-$tim-$tid"; |
|
256
|
|
|
} else { |
|
257
|
|
|
// If no start date selected the search start from today's date |
|
258
|
|
|
date_default_timezone_set('Europe/Rome'); |
|
259
|
|
|
$ret = date('Y-m-d', time()); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
return $ret; |
|
263
|
|
|
}*/ |
|
264
|
|
|
|
|
265
|
|
|
/***************************************************************************/ |
|
266
|
|
|
|
|
267
|
|
|
/* |
|
268
|
|
|
* Format the end date to be used in the search query. |
|
269
|
|
|
* |
|
270
|
|
|
* @param string $DatePickerEndDate |
|
271
|
|
|
* @return string |
|
272
|
|
|
*/ |
|
273
|
|
|
/*public static function prepareEndDate($DatePickerEndDate) |
|
274
|
|
|
{ |
|
275
|
|
|
if ($DatePickerEndDate) { |
|
276
|
|
|
list($tid, $tim, $tiy) = explode('/', $DatePickerEndDate); |
|
277
|
|
|
$ret = "$tiy-$tim-$tid"; |
|
278
|
|
|
} else { |
|
279
|
|
|
$ret = null; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
return $ret; |
|
283
|
|
|
}*/ |
|
284
|
|
|
|
|
285
|
|
|
/***************************************************************************/ |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Save all the weekly repetitions in the event_repetitions table |
|
289
|
|
|
* useful: http://thisinterestsme.com/php-get-first-monday-of-month/. |
|
290
|
|
|
* |
|
291
|
|
|
* @param \DavideCasiraghi\LaravelEventsCalendar\Models\Event $event |
|
292
|
|
|
* @param array $monthRepeatDatas - explode of $request->get('on_monthly_kind') |
|
293
|
|
|
* 0|28 the 28th day of the month |
|
294
|
|
|
* 1|2|2 the 2nd Tuesday of the month |
|
295
|
|
|
* 2|17 the 18th to last day of the month |
|
296
|
|
|
* 3|1|3 the 2nd to last Wednesday of the month |
|
297
|
|
|
* @param string $startDate (Y-m-d) |
|
298
|
|
|
* @param string $repeatUntilDate (Y-m-d) |
|
299
|
|
|
* @param string $timeStart (H:i:s) |
|
300
|
|
|
* @param string $timeEnd (H:i:s) |
|
301
|
|
|
* @return void |
|
302
|
|
|
*/ |
|
303
|
6 |
|
public static function saveMonthlyRepeatDates(int $eventId, array $monthRepeatDatas, string $startDate, string $repeatUntilDate, string $timeStart, string $timeEnd) |
|
304
|
|
|
{ |
|
305
|
6 |
|
$start = $month = Carbon::createFromFormat('Y-m-d', $startDate); |
|
|
|
|
|
|
306
|
6 |
|
$end = Carbon::createFromFormat('Y-m-d', $repeatUntilDate); |
|
307
|
6 |
|
$numberOfTheWeekArray = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']; |
|
|
|
|
|
|
308
|
6 |
|
$weekdayArray = [Carbon::MONDAY, Carbon::TUESDAY, Carbon::WEDNESDAY, Carbon::THURSDAY, Carbon::FRIDAY, Carbon::SATURDAY, Carbon::SUNDAY]; |
|
309
|
|
|
|
|
310
|
|
|
//$timeStart = $timeStart.":00"; |
|
311
|
|
|
//$timeEnd = $timeEnd.":00"; |
|
312
|
|
|
|
|
313
|
6 |
|
switch ($monthRepeatDatas[0]) { |
|
314
|
6 |
|
case '0': // Same day number - eg. "the 28th day of the month" |
|
315
|
2 |
|
while ($month < $end) { |
|
316
|
2 |
|
$day = $month; |
|
317
|
|
|
//dump("ee_3"); |
|
318
|
|
|
//dump($timeStart); |
|
319
|
2 |
|
EventRepetition::saveEventRepetitionOnDB($eventId, $day->format('Y-m-d'), $day->format('Y-m-d'), $timeStart, $timeEnd); |
|
320
|
2 |
|
$month = $month->addMonth(); |
|
321
|
|
|
} |
|
322
|
2 |
|
break; |
|
323
|
4 |
|
case '1': // Same weekday/week of the month - eg. the "1st Monday" |
|
324
|
2 |
|
$numberOfTheWeek = $monthRepeatDatas[1]; // eg. 1(first) | 2(second) | 3(third) | 4(fourth) | 5(fifth) |
|
325
|
2 |
|
$weekday = $weekdayArray[$monthRepeatDatas[2] - 1]; // eg. monday | tuesday | wednesday |
|
326
|
|
|
|
|
327
|
2 |
|
while ($month < $end) { |
|
328
|
2 |
|
$month_number = Carbon::parse($month)->isoFormat('M'); |
|
329
|
2 |
|
$year_number = Carbon::parse($month)->isoFormat('YYYY'); |
|
330
|
|
|
|
|
331
|
2 |
|
$day = Carbon::create($year_number, $month_number, 30, 0, 0, 0)->nthOfMonth($numberOfTheWeek, $weekday); // eg. Carbon::create(2014, 5, 30, 0, 0, 0)->nthOfQuarter(2, Carbon::SATURDAY); |
|
|
|
|
|
|
332
|
|
|
//dump("ee_4"); |
|
333
|
2 |
|
EventRepetition::saveEventRepetitionOnDB($eventId, $day->format('Y-m-d'), $day->format('Y-m-d'), $timeStart, $timeEnd); |
|
334
|
|
|
|
|
335
|
2 |
|
$month = $month->addMonth(); |
|
336
|
|
|
} |
|
337
|
2 |
|
break; |
|
338
|
2 |
|
case '2': // 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) |
|
339
|
1 |
|
$dayFromTheEnd = $monthRepeatDatas[1]; |
|
340
|
1 |
|
while ($month < $end) { |
|
341
|
1 |
|
$month_number = Carbon::parse($month)->isoFormat('M'); |
|
342
|
1 |
|
$year_number = Carbon::parse($month)->isoFormat('YYYY'); |
|
343
|
|
|
|
|
344
|
1 |
|
$day = Carbon::create($year_number, $month_number, 30, 0, 0, 0)->lastOfMonth()->subDays($dayFromTheEnd); |
|
345
|
|
|
//dump("ee_1"); |
|
346
|
1 |
|
EventRepetition::saveEventRepetitionOnDB($eventId, $day->format('Y-m-d'), $day->format('Y-m-d'), $timeStart, $timeEnd); |
|
347
|
1 |
|
$month = $month->addMonth(); |
|
348
|
|
|
} |
|
349
|
1 |
|
break; |
|
350
|
1 |
|
case '3': // 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) |
|
351
|
1 |
|
$weekday = $weekdayArray[$monthRepeatDatas[2] - 1]; // eg. monday | tuesday | wednesday |
|
352
|
1 |
|
$weeksFromTheEnd = $monthRepeatDatas[1]; |
|
353
|
|
|
|
|
354
|
1 |
|
while ($month < $end) { |
|
355
|
1 |
|
$month_number = Carbon::parse($month)->isoFormat('M'); |
|
356
|
1 |
|
$year_number = Carbon::parse($month)->isoFormat('YYYY'); |
|
357
|
|
|
|
|
358
|
1 |
|
$day = Carbon::create($year_number, $month_number, 30, 0, 0, 0)->lastOfMonth($weekday)->subWeeks($weeksFromTheEnd); |
|
359
|
|
|
//dump("ee_2"); |
|
360
|
1 |
|
EventRepetition::saveEventRepetitionOnDB($eventId, $day->format('Y-m-d'), $day->format('Y-m-d'), $timeStart, $timeEnd); |
|
361
|
1 |
|
$month = $month->addMonth(); |
|
362
|
|
|
} |
|
363
|
1 |
|
break; |
|
364
|
|
|
} |
|
365
|
6 |
|
} |
|
366
|
|
|
|
|
367
|
|
|
} |
|
368
|
|
|
|