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 teachers for the event. |
43
|
|
|
*/ |
44
|
25 |
|
public function teachers() |
45
|
|
|
{ |
46
|
25 |
|
return $this->belongsToMany('DavideCasiraghi\LaravelEventsCalendar\Models\Teacher', 'event_has_teachers', 'event_id', 'teacher_id'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/***************************************************************************/ |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the organizers for the event. |
53
|
|
|
*/ |
54
|
25 |
|
public function organizers() |
55
|
|
|
{ |
56
|
25 |
|
return $this->belongsToMany('DavideCasiraghi\LaravelEventsCalendar\Models\Organizer', 'event_has_organizers', 'event_id', 'organizer_id'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/***************************************************************************/ |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the organizers for the event. |
63
|
|
|
*/ |
64
|
1 |
|
public function eventRepetitions() |
65
|
|
|
{ |
66
|
1 |
|
return $this->hasMany('DavideCasiraghi\LaravelEventsCalendar\Models\EventRepetition', 'event_id'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/***************************************************************************/ |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return the all the active events. |
73
|
|
|
* |
74
|
|
|
* @return \DavideCasiraghi\LaravelEventsCalendar\Models\Event |
75
|
|
|
*/ |
76
|
1 |
|
public static function getActiveEvents() |
77
|
|
|
{ |
78
|
1 |
|
$cacheExpireMinutes = 15; // Set the duration time of the cache |
79
|
|
|
|
80
|
|
|
$ret = Cache::remember('active_events', $cacheExpireMinutes, function () { |
81
|
1 |
|
date_default_timezone_set('Europe/Rome'); |
82
|
1 |
|
$searchStartDate = date('Y-m-d', time()); |
83
|
1 |
|
$lastestEventsRepetitionsQuery = EventRepetition::getLastestEventsRepetitionsQuery($searchStartDate, null); |
84
|
|
|
|
85
|
|
|
return self:: |
86
|
1 |
|
select('title', 'countries.name AS country_name', 'countries.id AS country_id', 'countries.continent_id AS continent_id', 'event_venues.city AS city') |
87
|
1 |
|
->join('event_venues', 'event_venues.id', '=', 'events.venue_id') |
88
|
1 |
|
->join('countries', 'countries.id', '=', 'event_venues.country_id') |
89
|
|
|
->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
90
|
1 |
|
$join->on('events.id', '=', 'event_repetitions.event_id'); |
91
|
1 |
|
}) |
92
|
1 |
|
->get(); |
93
|
1 |
|
}); |
94
|
|
|
|
95
|
1 |
|
return $ret; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/***************************************************************************/ |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return the active events based on the search keys provided. |
102
|
|
|
* |
103
|
|
|
* @param array $filters |
104
|
|
|
* @param int $itemPerPage |
105
|
|
|
* @return \DavideCasiraghi\LaravelEventsCalendar\Models\Event |
106
|
|
|
*/ |
107
|
|
|
//$keywords, $category, $city, $country, $continent, $teacher, $venue, $startDate, $endDate, |
108
|
2 |
|
public static function getEvents(array $filters, $itemPerPage) |
109
|
|
|
{ |
110
|
2 |
|
if (! array_key_exists('startDate', $filters) || ! $filters['startDate']) { |
111
|
2 |
|
$filters['startDate'] = Carbon::now()->format('Y-m-d'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Sub-Query Joins - https://laravel.com/docs/5.7/queries |
115
|
2 |
|
$lastestEventsRepetitionsQuery = EventRepetition::getLastestEventsRepetitionsQuery($filters['startDate'], $filters['endDate']); |
116
|
|
|
|
117
|
|
|
// Retrieve the events that correspond to the selected filters |
118
|
2 |
|
if ($filters['keywords'] || $filters['category'] || $filters['city'] || $filters['country'] || $filters['region'] || $filters['continent'] || $filters['teacher'] || $filters['venue'] || $filters['endDate']) { |
119
|
|
|
|
120
|
|
|
//$start = microtime(true); |
121
|
|
|
//DB::enableQueryLog(); |
122
|
|
|
$ret = self:: |
123
|
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') |
124
|
|
|
->when($filters['keywords'], function ($query, $keywords) { |
125
|
1 |
|
return $query->where('title', 'like', '%'.$keywords.'%'); |
126
|
1 |
|
}) |
127
|
|
|
->when($filters['category'], function ($query, $category) { |
128
|
1 |
|
return $query->where('category_id', '=', $category); |
129
|
1 |
|
}) |
130
|
|
|
->when($filters['teacher'], function ($query, $teacher) { |
131
|
|
|
return $query->whereRaw('json_contains(sc_teachers_id, \'["'.$teacher.'"]\')'); |
132
|
1 |
|
}) |
133
|
|
|
->when($filters['country'], function ($query, $country) { |
134
|
1 |
|
return $query->where('event_venues.country_id', '=', $country); |
135
|
1 |
|
}) |
136
|
|
|
->when($filters['region'], function ($query, $region) { |
137
|
1 |
|
return $query->where('event_venues.region_id', '=', $region); |
138
|
1 |
|
}) |
139
|
|
|
->when($filters['continent'], function ($query, $continent) { |
140
|
1 |
|
return $query->where('event_venues.continent_id', '=', $continent); //sc_continent_id |
141
|
1 |
|
}) |
142
|
|
|
->when($filters['city'], function ($query, $city) { |
143
|
1 |
|
return $query->where('event_venues.city', 'like', '%'.$city.'%'); |
144
|
1 |
|
}) |
145
|
|
|
->when($filters['venue'], function ($query, $venue) { |
146
|
1 |
|
return $query->where('event_venues.name', 'like', '%'.$venue.'%'); |
147
|
1 |
|
}) |
148
|
|
|
->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
149
|
1 |
|
$join->on('events.id', '=', 'event_repetitions.event_id'); |
150
|
1 |
|
}) |
151
|
|
|
|
152
|
1 |
|
->leftJoin('event_venues', 'events.venue_id', '=', 'event_venues.id') |
153
|
1 |
|
->leftJoin('continents', 'event_venues.continent_id', '=', 'continents.id') |
154
|
1 |
|
->leftJoin('countries', 'event_venues.country_id', '=', 'countries.id') |
155
|
1 |
|
->leftJoin('regions', 'event_venues.region_id', '=', 'regions.id') |
156
|
|
|
|
157
|
1 |
|
->orderBy('event_repetitions.start_repeat', 'asc') |
158
|
1 |
|
->paginate($itemPerPage); |
159
|
|
|
//dd(DB::getQueryLog()); |
160
|
|
|
|
161
|
|
|
//$time = microtime(true) - $start; |
162
|
|
|
//dd($time); |
163
|
|
|
} |
164
|
|
|
// If no filter selected retrieve all the events |
165
|
|
|
else { |
166
|
|
|
$ret = self:: |
167
|
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') |
168
|
|
|
->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
169
|
1 |
|
$join->on('events.id', '=', 'event_repetitions.event_id'); |
170
|
1 |
|
}) |
171
|
1 |
|
->leftJoin('event_venues', 'events.venue_id', '=', 'event_venues.id') |
172
|
1 |
|
->leftJoin('continents', 'event_venues.continent_id', '=', 'continents.id') |
173
|
1 |
|
->leftJoin('countries', 'event_venues.country_id', '=', 'countries.id') |
174
|
1 |
|
->leftJoin('regions', 'event_venues.region_id', '=', 'regions.id') |
175
|
|
|
|
176
|
1 |
|
->orderBy('event_repetitions.start_repeat', 'asc') |
177
|
1 |
|
->paginate($itemPerPage); |
178
|
|
|
|
179
|
|
|
// It works, but I don't use it now to develop |
180
|
|
|
/*$cacheExpireMinutes = 15; |
181
|
|
|
$events = Cache::remember('all_events', $cacheExpireTime, function () { |
182
|
|
|
return DB::table('events')->latest()->paginate(20); |
183
|
|
|
});*/ |
184
|
|
|
} |
185
|
|
|
|
186
|
2 |
|
return $ret; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|