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
|
|
|
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', |
23
|
|
|
'description', |
24
|
|
|
'organized_by', |
25
|
|
|
'category_id', |
26
|
|
|
'venue_id', |
27
|
|
|
'image', |
28
|
|
|
'facebook_event_link', |
29
|
|
|
'website_event_link', |
30
|
|
|
'status', |
31
|
|
|
'repeat_type', |
32
|
|
|
'repeat_until', |
33
|
|
|
'repeat_weekly_on', |
34
|
|
|
'repeat_monthly_on', |
35
|
|
|
'on_monthly_kind', |
36
|
|
|
'multiple_dates', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/***************************************************************************/ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the user that owns the event. eg. $event->user. |
43
|
|
|
*/ |
44
|
|
|
public function user() |
45
|
|
|
{ |
46
|
|
|
return $this->belongsTo('\Illuminate\Foundation\Auth\User', 'created_by'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/***************************************************************************/ |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the teachers for the event. |
53
|
|
|
*/ |
54
|
29 |
|
public function teachers() |
55
|
|
|
{ |
56
|
29 |
|
return $this->belongsToMany('DavideCasiraghi\LaravelEventsCalendar\Models\Teacher', 'event_has_teachers', 'event_id', 'teacher_id'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/***************************************************************************/ |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the organizers for the event. |
63
|
|
|
*/ |
64
|
29 |
|
public function organizers() |
65
|
|
|
{ |
66
|
29 |
|
return $this->belongsToMany('DavideCasiraghi\LaravelEventsCalendar\Models\Organizer', 'event_has_organizers', 'event_id', 'organizer_id'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/***************************************************************************/ |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the organizers for the event. |
73
|
|
|
*/ |
74
|
1 |
|
public function eventRepetitions() |
75
|
|
|
{ |
76
|
1 |
|
return $this->hasMany('DavideCasiraghi\LaravelEventsCalendar\Models\EventRepetition', 'event_id'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/***************************************************************************/ |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return an array with active events datas. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
1 |
|
public static function getActiveEvents() |
87
|
|
|
{ |
88
|
1 |
|
$cacheExpireMinutes = 1440; // Set the duration time of the cache (1 day - 1440 minutes) (this cache tag get invalidates also on event save) |
89
|
|
|
|
90
|
|
|
$ret = Cache::remember('active_events', $cacheExpireMinutes, function () { |
91
|
1 |
|
date_default_timezone_set('Europe/Rome'); |
92
|
1 |
|
$searchStartDate = date('Y-m-d', time()); |
93
|
1 |
|
$lastestEventsRepetitionsQuery = EventRepetition::getLastestEventsRepetitionsQuery($searchStartDate, null); |
94
|
|
|
|
95
|
|
|
return self:: |
96
|
1 |
|
select('title', 'countries.name AS country_name', 'countries.id AS country_id', 'countries.continent_id AS continent_id', 'event_venues.city AS city', 'events.repeat_until', 'events.category_id', 'events.created_by', 'events.repeat_type') |
97
|
1 |
|
->join('event_venues', 'event_venues.id', '=', 'events.venue_id') |
98
|
1 |
|
->join('countries', 'countries.id', '=', 'event_venues.country_id') |
99
|
|
|
->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
100
|
1 |
|
$join->on('events.id', '=', 'event_repetitions.event_id'); |
101
|
1 |
|
}) |
102
|
1 |
|
->get(); |
103
|
1 |
|
}); |
104
|
|
|
|
105
|
1 |
|
return $ret; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/***************************************************************************/ |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return the active events based on the search keys provided. |
112
|
|
|
* |
113
|
|
|
* @param array $filters |
114
|
|
|
* @param int $itemPerPage |
115
|
|
|
* @return \DavideCasiraghi\LaravelEventsCalendar\Models\Event |
116
|
|
|
*/ |
117
|
|
|
//$keywords, $category, $city, $country, $continent, $teacher, $venue, $startDate, $endDate, |
118
|
2 |
|
public static function getEvents(array $filters, $itemPerPage) |
119
|
|
|
{ |
120
|
2 |
|
if (! array_key_exists('startDate', $filters) || ! $filters['startDate']) { |
121
|
2 |
|
$filters['startDate'] = Carbon::now()->format('Y-m-d'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Sub-Query Joins - https://laravel.com/docs/5.7/queries |
125
|
2 |
|
$lastestEventsRepetitionsQuery = EventRepetition::getLastestEventsRepetitionsQuery($filters['startDate'], $filters['endDate']); |
126
|
|
|
|
127
|
|
|
// Retrieve the events that correspond to the selected filters |
128
|
2 |
|
if ($filters['keywords'] || $filters['category'] || $filters['city'] || $filters['country'] || $filters['region'] || $filters['continent'] || $filters['teacher'] || $filters['venue'] || $filters['endDate']) { |
129
|
|
|
|
130
|
|
|
//$start = microtime(true); |
131
|
|
|
//DB::enableQueryLog(); |
132
|
|
|
$ret = self:: |
133
|
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') |
134
|
|
|
->when($filters['keywords'], function ($query, $keywords) { |
135
|
1 |
|
return $query->where('title', 'like', '%'.$keywords.'%'); |
136
|
1 |
|
}) |
137
|
|
|
->when($filters['category'], function ($query, $category) { |
138
|
1 |
|
return $query->where('category_id', '=', $category); |
139
|
1 |
|
}) |
140
|
|
|
->when($filters['teacher'], function ($query, $teacher) { |
141
|
|
|
return $query->whereRaw('json_contains(sc_teachers_id, \'["'.$teacher.'"]\')'); |
142
|
1 |
|
}) |
143
|
|
|
->when($filters['country'], function ($query, $country) { |
144
|
1 |
|
return $query->where('event_venues.country_id', '=', $country); |
145
|
1 |
|
}) |
146
|
|
|
->when($filters['region'], function ($query, $region) { |
147
|
1 |
|
return $query->where('event_venues.region_id', '=', $region); |
148
|
1 |
|
}) |
149
|
|
|
->when($filters['continent'], function ($query, $continent) { |
150
|
1 |
|
return $query->where('event_venues.continent_id', '=', $continent); //sc_continent_id |
151
|
1 |
|
}) |
152
|
|
|
->when($filters['city'], function ($query, $city) { |
153
|
1 |
|
return $query->where('event_venues.city', 'like', '%'.$city.'%'); |
154
|
1 |
|
}) |
155
|
|
|
->when($filters['venue'], function ($query, $venue) { |
156
|
1 |
|
return $query->where('event_venues.name', 'like', '%'.$venue.'%'); |
157
|
1 |
|
}) |
158
|
|
|
->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
159
|
1 |
|
$join->on('events.id', '=', 'event_repetitions.event_id'); |
160
|
1 |
|
}) |
161
|
|
|
|
162
|
1 |
|
->leftJoin('event_venues', 'events.venue_id', '=', 'event_venues.id') |
163
|
1 |
|
->leftJoin('continents', 'event_venues.continent_id', '=', 'continents.id') |
164
|
1 |
|
->leftJoin('countries', 'event_venues.country_id', '=', 'countries.id') |
165
|
1 |
|
->leftJoin('regions', 'event_venues.region_id', '=', 'regions.id') |
166
|
|
|
|
167
|
1 |
|
->orderBy('event_repetitions.start_repeat', 'asc') |
168
|
1 |
|
->paginate($itemPerPage); |
169
|
|
|
//dd(DB::getQueryLog()); |
170
|
|
|
|
171
|
|
|
//$time = microtime(true) - $start; |
172
|
|
|
//dd($time); |
173
|
|
|
} |
174
|
|
|
// If no filter selected retrieve all the events |
175
|
|
|
else { |
176
|
|
|
$ret = self:: |
177
|
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') |
178
|
|
|
->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
179
|
1 |
|
$join->on('events.id', '=', 'event_repetitions.event_id'); |
180
|
1 |
|
}) |
181
|
1 |
|
->leftJoin('event_venues', 'events.venue_id', '=', 'event_venues.id') |
182
|
1 |
|
->leftJoin('continents', 'event_venues.continent_id', '=', 'continents.id') |
183
|
1 |
|
->leftJoin('countries', 'event_venues.country_id', '=', 'countries.id') |
184
|
1 |
|
->leftJoin('regions', 'event_venues.region_id', '=', 'regions.id') |
185
|
|
|
|
186
|
1 |
|
->orderBy('event_repetitions.start_repeat', 'asc') |
187
|
1 |
|
->paginate($itemPerPage); |
188
|
|
|
|
189
|
|
|
// It works, but I don't use it now to develop |
190
|
|
|
/*$cacheExpireMinutes = 15; |
191
|
|
|
$events = Cache::remember('all_events', $cacheExpireTime, function () { |
192
|
|
|
return DB::table('events')->latest()->paginate(20); |
193
|
|
|
});*/ |
194
|
|
|
} |
195
|
|
|
|
196
|
2 |
|
return $ret; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/***************************************************************************/ |
200
|
|
|
/** |
201
|
|
|
* Return an array with active events map markers. |
202
|
|
|
* |
203
|
|
|
* @return array |
204
|
|
|
*/ |
205
|
1 |
|
public static function getActiveEventsMapGeoJSON(){ |
206
|
1 |
|
$cacheExpireMinutes = 1440; // Set the duration time of the cache (1 day - 1440 minutes) - this cache tag get invalidates also on event save |
207
|
|
|
|
208
|
|
|
$eventsMapGeoJSONArrayCached = Cache::remember('active_events_map_markers', $cacheExpireMinutes, function () { |
209
|
1 |
|
$eventsData = Event::getActiveEventsMapMarkersDataFromDb(); |
210
|
1 |
|
$eventsMapGeoJSONArray = []; |
211
|
1 |
|
foreach ($eventsData as $key => $eventData) { |
212
|
|
|
//dd($eventData); |
213
|
1 |
|
$eventsMapGeoJSONArray[] = [ |
214
|
1 |
|
"type" => "Feature", |
215
|
1 |
|
"id" => $eventData->id, |
216
|
|
|
"properties" => [ |
217
|
1 |
|
"Title" => $eventData->title, |
218
|
1 |
|
"City" => $eventData->city, |
219
|
1 |
|
"Category" => $eventData->category_id, |
220
|
1 |
|
"Location" => $eventData->city.", ".$eventData->address, |
221
|
|
|
], |
222
|
|
|
"geometry" => [ |
223
|
1 |
|
"type" => "Point", |
224
|
1 |
|
"coordinates" => [ $eventData->lat, $eventData->lng ], |
225
|
|
|
], |
226
|
|
|
]; |
227
|
|
|
} |
228
|
1 |
|
return $eventsMapGeoJSONArray; |
229
|
1 |
|
}); |
230
|
|
|
|
231
|
1 |
|
$ret = json_encode($eventsMapGeoJSONArrayCached); |
232
|
|
|
|
233
|
1 |
|
return $ret; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/***************************************************************************/ |
237
|
|
|
/** |
238
|
|
|
* Return an array with active events map markers. |
239
|
|
|
* |
240
|
|
|
* @return array |
241
|
|
|
*/ |
242
|
2 |
|
public static function getActiveEventsMapMarkersDataFromDb(){ |
243
|
2 |
|
date_default_timezone_set('Europe/Rome'); |
244
|
2 |
|
$searchStartDate = Carbon::now()->format('Y-m-d'); |
245
|
2 |
|
$lastestEventsRepetitionsQuery = EventRepetition::getLastestEventsRepetitionsQuery($searchStartDate, null); |
246
|
|
|
|
247
|
|
|
$ret = self:: |
248
|
2 |
|
select( 'events.id AS id', |
249
|
2 |
|
'events.title AS title', |
250
|
2 |
|
'event_venues.city AS city', |
251
|
2 |
|
'event_venues.address AS address', |
252
|
2 |
|
'event_venues.lat AS lat', |
253
|
2 |
|
'event_venues.lng AS lng', |
254
|
2 |
|
'events.repeat_until', |
255
|
2 |
|
'events.category_id', |
256
|
2 |
|
'events.created_by', |
257
|
2 |
|
'events.repeat_type' |
258
|
|
|
) |
259
|
2 |
|
->join('event_venues', 'event_venues.id', '=', 'events.venue_id') |
260
|
2 |
|
->join('countries', 'countries.id', '=', 'event_venues.country_id') |
261
|
|
|
->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
262
|
2 |
|
$join->on('events.id', '=', 'event_repetitions.event_id'); |
263
|
2 |
|
}) |
264
|
2 |
|
->get(); |
265
|
|
|
|
266
|
2 |
|
return $ret; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|