1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelEventsCalendar\Models; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\Facades\Cache; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
|
9
|
|
|
class Event extends Model |
10
|
|
|
{ |
11
|
|
|
/***************************************************************************/ |
12
|
|
|
/** |
13
|
|
|
* The table associated with the model. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $table = 'events'; |
18
|
|
|
|
19
|
|
|
/***************************************************************************/ |
20
|
|
|
|
21
|
|
|
protected $fillable = [ |
22
|
|
|
'title', 'description', 'organized_by', 'category_id', 'venue_id', 'image', 'facebook_event_link', 'website_event_link', 'status', 'repeat_type', 'repeat_until', 'repeat_weekly_on', 'repeat_monthly_on', 'on_monthly_kind', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/***************************************************************************/ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get the teachers for the event. |
29
|
|
|
*/ |
30
|
|
|
public function teachers() |
31
|
|
|
{ |
32
|
|
|
return $this->belongsToMany('App\Teacher', 'event_has_teachers', 'event_id', 'teacher_id'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/***************************************************************************/ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the organizers for the event. |
39
|
|
|
*/ |
40
|
|
|
public function organizers() |
41
|
|
|
{ |
42
|
|
|
return $this->belongsToMany('App\Organizer', 'event_has_organizers', 'event_id', 'organizer_id'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/***************************************************************************/ |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the organizers for the event. |
49
|
|
|
*/ |
50
|
|
|
public function eventRepetitions() |
51
|
|
|
{ |
52
|
|
|
return $this->hasMany('App\EventRepetition', 'event_id'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/***************************************************************************/ |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Delete all the previous repetitions from the event_repetitions table. |
59
|
|
|
* |
60
|
|
|
* @param int $eventId |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public static function deletePreviousRepetitions($eventId) |
64
|
|
|
{ |
65
|
|
|
EventRepetition::where('event_id', $eventId)->delete(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/***************************************************************************/ |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Return Start and End dates of the first repetition of an event - By Event ID. |
72
|
|
|
* |
73
|
|
|
* @param int $eventId |
74
|
|
|
* @return \DavideCasiraghi\LaravelEventsCalendar\Models\EventRepetition |
75
|
|
|
*/ |
76
|
|
|
public static function getFirstEventRpDatesByEventId($eventId) |
77
|
|
|
{ |
78
|
|
|
$ret = EventRepetition:: |
79
|
|
|
select('start_repeat', 'end_repeat') |
80
|
|
|
->where('event_id', $eventId) |
81
|
|
|
->first(); |
82
|
|
|
|
83
|
|
|
return $ret; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/***************************************************************************/ |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return Start and End dates of the first repetition of an event - By Repetition ID. |
90
|
|
|
* |
91
|
|
|
* @param int $repetitionId |
92
|
|
|
* @return \DavideCasiraghi\LaravelEventsCalendar\Models\EventRepetition |
93
|
|
|
*/ |
94
|
|
|
public static function getFirstEventRpDatesByRepetitionId($repetitionId) |
95
|
|
|
{ |
96
|
|
|
$ret = EventRepetition:: |
97
|
|
|
select('start_repeat', 'end_repeat') |
98
|
|
|
->where('id', $repetitionId) |
99
|
|
|
->first(); |
100
|
|
|
|
101
|
|
|
return $ret; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/***************************************************************************/ |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return the all the active events. |
108
|
|
|
* |
109
|
|
|
* @return \DavideCasiraghi\LaravelEventsCalendar\Models\Event |
110
|
|
|
*/ |
111
|
|
|
public static function getActiveEvents() |
112
|
|
|
{ |
113
|
|
|
$cacheExpireMinutes = 15; // Set the duration time of the cache |
114
|
|
|
|
115
|
|
|
$ret = Cache::remember('active_events', $cacheExpireMinutes, function () { |
116
|
|
|
date_default_timezone_set('Europe/Rome'); |
117
|
|
|
$searchStartDate = date('Y-m-d', time()); |
118
|
|
|
$lastestEventsRepetitionsQuery = EventRepetition::getLastestEventsRepetitionsQuery($searchStartDate, null); |
119
|
|
|
|
120
|
|
|
return Event:: |
121
|
|
|
select('title', 'countries.name AS country_name', 'countries.id AS country_id', 'countries.continent_id AS continent_id', 'event_venues.city AS city') |
122
|
|
|
->join('event_venues', 'event_venues.id', '=', 'events.venue_id') |
123
|
|
|
->join('countries', 'countries.id', '=', 'event_venues.country_id') |
124
|
|
|
->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
125
|
|
|
$join->on('events.id', '=', 'event_repetitions.event_id'); |
126
|
|
|
}) |
127
|
|
|
->get(); |
128
|
|
|
}); |
129
|
|
|
|
130
|
|
|
return $ret; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/***************************************************************************/ |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Return the active events based on the search keys provided. |
137
|
|
|
* |
138
|
|
|
* @param array $filters |
139
|
|
|
* @param int $itemPerPage |
140
|
|
|
* @return \DavideCasiraghi\LaravelEventsCalendar\Models\Event |
141
|
|
|
*/ |
142
|
|
|
//$keywords, $category, $city, $country, $continent, $teacher, $venue, $startDate, $endDate, |
143
|
|
|
public static function getEvents($filters, $itemPerPage) |
144
|
|
|
{ |
145
|
|
|
if (! $filters['startDate']) { |
146
|
|
|
$filters['startDate'] = Carbon::now()->format('Y-m-d'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// Sub-Query Joins - https://laravel.com/docs/5.7/queries |
150
|
|
|
$lastestEventsRepetitionsQuery = EventRepetition::getLastestEventsRepetitionsQuery($filters['startDate'], $filters['endDate']); |
151
|
|
|
|
152
|
|
|
// Retrieve the events that correspond to the selected filters |
153
|
|
|
if ($filters['keywords'] || $filters['category'] || $filters['city'] || $filters['country'] || $filters['continent'] || $filters['teacher'] || $filters['venue'] || $filters['endDate']) { |
154
|
|
|
|
155
|
|
|
//DB::enableQueryLog(); |
156
|
|
|
$ret = self:: |
157
|
|
|
when($filters['keywords'], function ($query, $keywords) { |
158
|
|
|
return $query->where('title', 'like', '%'.$keywords.'%'); |
159
|
|
|
}) |
160
|
|
|
->when($filters['category'], function ($query, $category) { |
161
|
|
|
return $query->where('category_id', '=', $category); |
162
|
|
|
}) |
163
|
|
|
->when($filters['teacher'], function ($query, $teacher) { |
164
|
|
|
return $query->whereRaw('json_contains(sc_teachers_id, \'["'.$teacher.'"]\')'); |
165
|
|
|
}) |
166
|
|
|
->when($filters['country'], function ($query, $country) { |
167
|
|
|
return $query->where('sc_country_id', '=', $country); |
168
|
|
|
}) |
169
|
|
|
->when($filters['continent'], function ($query, $continent) { |
170
|
|
|
return $query->where('sc_continent_id', '=', $continent); |
171
|
|
|
}) |
172
|
|
|
->when($filters['city'], function ($query, $city) { |
173
|
|
|
return $query->where('sc_city_name', 'like', '%'.$city.'%'); |
174
|
|
|
}) |
175
|
|
|
->when($filters['venue'], function ($query, $venue) { |
176
|
|
|
return $query->where('sc_venue_name', 'like', '%'.$venue.'%'); |
177
|
|
|
}) |
178
|
|
|
->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
179
|
|
|
$join->on('events.id', '=', 'event_repetitions.event_id'); |
180
|
|
|
}) |
181
|
|
|
->orderBy('event_repetitions.start_repeat', 'asc') |
182
|
|
|
->paginate($itemPerPage); |
183
|
|
|
//dd(DB::getQueryLog()); |
184
|
|
|
} |
185
|
|
|
// If no filter selected retrieve all the events |
186
|
|
|
else { |
187
|
|
|
$ret = self:: |
188
|
|
|
joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
189
|
|
|
$join->on('events.id', '=', 'event_repetitions.event_id'); |
190
|
|
|
}) |
191
|
|
|
->orderBy('event_repetitions.start_repeat', 'asc') |
192
|
|
|
->paginate($itemPerPage); |
193
|
|
|
|
194
|
|
|
// It works, but I don't use it now to develop |
195
|
|
|
/*$cacheExpireMinutes = 15; |
196
|
|
|
$events = Cache::remember('all_events', $cacheExpireTime, function () { |
197
|
|
|
return DB::table('events')->latest()->paginate(20); |
198
|
|
|
});*/ |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $ret; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/***************************************************************************/ |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Format the start date to be used in the search query. |
208
|
|
|
* If the start date is null return today's date. |
209
|
|
|
* |
210
|
|
|
* @param string $DatePickerStartDate |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
|
|
public static function prepareStartDate($DatePickerStartDate) |
214
|
|
|
{ |
215
|
|
|
if ($DatePickerStartDate) { |
216
|
|
|
list($tid, $tim, $tiy) = explode('/', $DatePickerStartDate); |
217
|
|
|
$ret = "$tiy-$tim-$tid"; |
218
|
|
|
} else { |
219
|
|
|
// If no start date selected the search start from today's date |
220
|
|
|
date_default_timezone_set('Europe/Rome'); |
221
|
|
|
$ret = date('Y-m-d', time()); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $ret; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/***************************************************************************/ |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Format the end date to be used in the search query. |
231
|
|
|
* |
232
|
|
|
* @param string $DatePickerEndDate |
233
|
|
|
* @return string |
234
|
|
|
*/ |
235
|
|
|
public static function prepareEndDate($DatePickerEndDate) |
236
|
|
|
{ |
237
|
|
|
if ($DatePickerEndDate) { |
238
|
|
|
list($tid, $tim, $tiy) = explode('/', $DatePickerEndDate); |
239
|
|
|
$ret = "$tiy-$tim-$tid"; |
240
|
|
|
} else { |
241
|
|
|
$ret = null; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $ret; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|