1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelEventsCalendar; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
|
8
|
|
|
use DavideCasiraghi\LaravelEventsCalendar\Models\Event; |
9
|
|
|
use DavideCasiraghi\LaravelEventsCalendar\Models\EventRepetition; |
10
|
|
|
|
11
|
|
|
class LaravelEventsCalendar |
12
|
|
|
{ |
13
|
|
|
/***************************************************************************/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Format a date from datepicker (d/m/Y) to a format ready to be stored on DB (Y-m-d). |
17
|
|
|
* If the date picker date is null return today's date. |
18
|
|
|
* the PARAM is a date in the d/m/Y format - the RETURN is a date in the Y-m-d format. |
19
|
|
|
* If $todaysDateIfNull = 1, when the date is null return the date of today. |
20
|
|
|
* |
21
|
|
|
* @param string $DatePickerDate |
22
|
|
|
* @param bool $todaysDateIfNull |
23
|
|
|
* @return string $ret |
24
|
|
|
*/ |
25
|
1 |
|
public static function formatDatePickerDateForMysql($DatePickerDate, bool $todaysDateIfNull = null) |
26
|
|
|
{ |
27
|
1 |
|
if ($DatePickerDate) { |
28
|
1 |
|
[$tid, $tim, $tiy] = explode('/', $DatePickerDate); |
29
|
1 |
|
$ret = "$tiy-$tim-$tid"; |
30
|
1 |
|
} elseif ($todaysDateIfNull) { |
31
|
1 |
|
date_default_timezone_set('Europe/Rome'); |
32
|
1 |
|
$ret = date('Y-m-d', time()); |
33
|
|
|
} else { |
34
|
1 |
|
$ret = ''; |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
return $ret; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/***************************************************************************/ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* It returns a string that is composed by the array values separated by a comma. |
44
|
|
|
* |
45
|
|
|
* @param iterable $items |
46
|
|
|
* @return string $ret |
47
|
|
|
*/ |
48
|
4 |
|
public static function getStringFromArraySeparatedByComma(iterable $items) |
49
|
|
|
{ |
50
|
4 |
|
$ret = ''; |
51
|
4 |
|
$i = 0; |
52
|
4 |
|
$len = count($items); // to put "," to all items except the last |
53
|
|
|
|
54
|
4 |
|
foreach ($items as $key => $item) { |
55
|
4 |
|
$ret .= $item; |
56
|
4 |
|
if ($i != $len - 1) { // not last |
57
|
2 |
|
$ret .= ', '; |
58
|
|
|
} |
59
|
4 |
|
$i++; |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
return $ret; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/***************************************************************************/ |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Check the date and return true if the weekday is the one specified in $dayOfTheWeek. eg. if $dayOfTheWeek = 3, is true if the date is a Wednesday |
69
|
|
|
* $dayOfTheWeek: 1|2|3|4|5|6|7 (MONDAY-SUNDAY) |
70
|
|
|
* https://stackoverflow.com/questions/2045736/getting-all-dates-for-mondays-and-tuesdays-for-the-next-year. |
71
|
|
|
* |
72
|
|
|
* @param string $date |
73
|
|
|
* @param int $dayOfTheWeek |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
4 |
|
public static function isWeekDay(string $date, int $dayOfTheWeek) |
77
|
|
|
{ |
78
|
|
|
// Fix the bug that was avoiding to save Sunday. Date 'w' identify sunday as 0 and not 7. |
79
|
4 |
|
if ($dayOfTheWeek == 7) { |
80
|
1 |
|
$dayOfTheWeek = 0; |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
return date('w', strtotime($date)) == $dayOfTheWeek; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/***************************************************************************/ |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* GET number of the specified weekday in this month (1 for the first). |
90
|
|
|
* $dateTimestamp - unix timestramp of the date specified |
91
|
|
|
* $dayOfWeekValue - 1 (for Monday) through 7 (for Sunday) |
92
|
|
|
* Return the number of the week in the month of the weekday specified. |
93
|
|
|
* @param string $dateTimestamp |
94
|
|
|
* @param string $dayOfWeekValue |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
2 |
|
public static function weekdayNumberOfMonth(string $dateTimestamp, string $dayOfWeekValue) |
98
|
|
|
{ |
99
|
2 |
|
$cut = substr($dateTimestamp, 0, 8); |
100
|
2 |
|
$daylen = 86400; |
101
|
2 |
|
$timestamp = strtotime($dateTimestamp); |
102
|
2 |
|
$first = strtotime($cut.'01'); |
103
|
2 |
|
$elapsed = (($timestamp - $first) / $daylen) + 1; |
104
|
2 |
|
$i = 1; |
105
|
2 |
|
$weeks = 0; |
106
|
2 |
|
for ($i == 1; $i <= $elapsed; $i++) { |
107
|
2 |
|
$dayfind = $cut.(strlen($i) < 2 ? '0'.$i : $i); |
108
|
2 |
|
$daytimestamp = strtotime($dayfind); |
109
|
2 |
|
$day = strtolower(date('N', $daytimestamp)); |
110
|
2 |
|
if ($day == strtolower($dayOfWeekValue)) { |
111
|
1 |
|
$weeks++; |
112
|
|
|
} |
113
|
|
|
} |
114
|
2 |
|
if ($weeks == 0) { |
|
|
|
|
115
|
1 |
|
$weeks++; |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
return $weeks; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/***************************************************************************/ |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* GET number of week from the end of the month - https://stackoverflow.com/questions/5853380/php-get-number-of-week-for-month |
125
|
|
|
* Week of the month = Week of the year - Week of the year of first day of month + 1. |
126
|
|
|
* Return the number of the week in the month of the day specified |
127
|
|
|
* $when - unix timestramp of the date specified. |
128
|
|
|
* |
129
|
|
|
* @param int $when |
130
|
|
|
* @return int |
131
|
|
|
*/ |
132
|
2 |
|
public static function weekOfMonthFromTheEnd(int $when) |
133
|
|
|
{ |
134
|
2 |
|
$numberOfDayOfTheMonth = strftime('%e', $when); // Day of the month 1-31 |
135
|
2 |
|
$lastDayOfMonth = strftime('%e', strtotime(date('Y-m-t', $when))); // the last day of the month of the specified date |
136
|
2 |
|
$dayDifference = $lastDayOfMonth - $numberOfDayOfTheMonth; |
137
|
|
|
|
138
|
|
|
switch (true) { |
139
|
2 |
|
case $dayDifference < 7: |
140
|
|
|
$weekFromTheEnd = 1; |
141
|
|
|
break; |
142
|
|
|
|
143
|
2 |
|
case $dayDifference < 14: |
144
|
|
|
$weekFromTheEnd = 2; |
145
|
|
|
break; |
146
|
|
|
|
147
|
2 |
|
case $dayDifference < 21: |
148
|
1 |
|
$weekFromTheEnd = 3; |
149
|
1 |
|
break; |
150
|
|
|
|
151
|
1 |
|
case $dayDifference < 28: |
152
|
1 |
|
$weekFromTheEnd = 4; |
153
|
1 |
|
break; |
154
|
|
|
|
155
|
|
|
default: |
156
|
|
|
$weekFromTheEnd = 5; |
157
|
|
|
break; |
158
|
|
|
} |
159
|
|
|
|
160
|
2 |
|
return $weekFromTheEnd; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/***************************************************************************/ |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* GET number of day from the end of the month. |
167
|
|
|
* $when - unix timestramp of the date specified |
168
|
|
|
* Return the number of day of the month from end. |
169
|
|
|
* |
170
|
|
|
* @param int $when |
171
|
|
|
* @return int |
172
|
|
|
*/ |
173
|
2 |
|
public static function dayOfMonthFromTheEnd(int $when) |
174
|
|
|
{ |
175
|
2 |
|
$numberOfDayOfTheMonth = strftime('%e', $when); // Day of the month 1-31 |
176
|
2 |
|
$lastDayOfMonth = strftime('%e', strtotime(date('Y-m-t', $when))); // the last day of the month of the specified date |
177
|
2 |
|
$dayDifference = $lastDayOfMonth - $numberOfDayOfTheMonth; |
178
|
|
|
|
179
|
2 |
|
return $dayDifference; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/***************************************************************************/ |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* GET the ordinal indicator - for the day of the month. |
186
|
|
|
* Return the ordinal indicator (st, nd, rd, th). |
187
|
|
|
* @param int $number |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
1 |
|
public static function getOrdinalIndicator($number) |
191
|
|
|
{ |
192
|
|
|
switch ($number) { |
193
|
1 |
|
case $number == 1 || $number == 21 || $number == 31: |
194
|
1 |
|
$ret = 'st'; |
195
|
1 |
|
break; |
196
|
1 |
|
case $number == 2 || $number == 22: |
197
|
|
|
$ret = 'nd'; |
198
|
|
|
break; |
199
|
1 |
|
case $number == 3 || $number == 23: |
200
|
|
|
$ret = 'rd'; |
201
|
|
|
break; |
202
|
|
|
default: |
203
|
1 |
|
$ret = 'th'; |
204
|
1 |
|
break; |
205
|
|
|
} |
206
|
|
|
|
207
|
1 |
|
return $ret; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/***************************************************************************/ |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Decode the event repeat_weekly_on field - used in event.show. |
214
|
|
|
* Return a string like "Monday". |
215
|
|
|
* |
216
|
|
|
* @param string $repeatWeeklyOn |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
2 |
|
public static function decodeRepeatWeeklyOn(string $repeatWeeklyOn) |
220
|
|
|
{ |
221
|
|
|
$weekdayArray = [ |
222
|
2 |
|
'', |
223
|
2 |
|
__('laravel-events-calendar::general.monday'), |
224
|
2 |
|
__('laravel-events-calendar::general.tuesday'), |
225
|
2 |
|
__('laravel-events-calendar::general.wednesday'), |
226
|
2 |
|
__('laravel-events-calendar::general.thursday'), |
227
|
2 |
|
__('laravel-events-calendar::general.friday'), |
228
|
2 |
|
__('laravel-events-calendar::general.saturday'), |
229
|
2 |
|
__('laravel-events-calendar::general.sunday'), |
230
|
|
|
]; |
231
|
2 |
|
$ret = $weekdayArray[$repeatWeeklyOn]; |
232
|
|
|
|
233
|
2 |
|
return $ret; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/***************************************************************************/ |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Decode the event on_monthly_kind field - used in event.show. |
240
|
|
|
* Return a string like "the 4th to last Thursday of the month". |
241
|
|
|
* |
242
|
|
|
* @param string $onMonthlyKindCode |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
2 |
|
public static function decodeOnMonthlyKind(string $onMonthlyKindCode) |
246
|
|
|
{ |
247
|
2 |
|
$onMonthlyKindCodeArray = explode('|', $onMonthlyKindCode); |
248
|
|
|
$weekDays = [ |
249
|
2 |
|
'', |
250
|
2 |
|
__('laravel-events-calendar::general.monday'), |
251
|
2 |
|
__('laravel-events-calendar::general.tuesday'), |
252
|
2 |
|
__('laravel-events-calendar::general.wednesday'), |
253
|
2 |
|
__('laravel-events-calendar::general.thursday'), |
254
|
2 |
|
__('laravel-events-calendar::general.friday'), |
255
|
2 |
|
__('laravel-events-calendar::general.saturday'), |
256
|
2 |
|
__('laravel-events-calendar::general.sunday'), |
257
|
|
|
]; |
258
|
|
|
|
259
|
|
|
//dd($onMonthlyKindCodeArray); |
260
|
2 |
|
switch ($onMonthlyKindCodeArray[0]) { |
261
|
2 |
|
case '0': // 0|7 eg. the 7th day of the month |
262
|
2 |
|
$dayNumber = $onMonthlyKindCodeArray[1]; |
263
|
2 |
|
$format = __('laravel-events-calendar::ordinalDays.the_'.($dayNumber).'_x_of_the_month'); |
264
|
2 |
|
$ret = sprintf($format, __('laravel-events-calendar::general.day')); |
|
|
|
|
265
|
2 |
|
break; |
266
|
1 |
|
case '1': // 1|2|4 eg. the 2nd Thursday of the month |
267
|
1 |
|
$dayNumber = $onMonthlyKindCodeArray[1]; |
268
|
1 |
|
$weekDay = $weekDays[$onMonthlyKindCodeArray[2]]; // Monday, Tuesday, Wednesday |
269
|
1 |
|
$format = __('laravel-events-calendar::ordinalDays.the_'.($dayNumber).'_x_of_the_month'); |
270
|
1 |
|
$ret = sprintf($format, $weekDay); |
271
|
1 |
|
break; |
272
|
1 |
|
case '2': // 2|20 eg. the 21st to last day of the month |
273
|
1 |
|
$dayNumber = $onMonthlyKindCodeArray[1] + 1; |
274
|
1 |
|
$format = __('laravel-events-calendar::ordinalDays.the_'.($dayNumber).'_to_last_x_of_the_month'); |
275
|
1 |
|
$ret = sprintf($format, __('laravel-events-calendar::general.day')); |
276
|
1 |
|
break; |
277
|
1 |
|
case '3': // 3|3|4 eg. the 4th to last Thursday of the month |
278
|
1 |
|
$dayNumber = $onMonthlyKindCodeArray[1] + 1; |
279
|
1 |
|
$weekDay = $weekDays[$onMonthlyKindCodeArray[2]]; // Monday, Tuesday, Wednesday |
280
|
1 |
|
$format = __('laravel-events-calendar::ordinalDays.the_'.($dayNumber).'_to_last_x_of_the_month'); |
281
|
1 |
|
$ret = sprintf($format, $weekDay); |
282
|
1 |
|
break; |
283
|
|
|
} |
284
|
|
|
|
285
|
2 |
|
return $ret; |
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/***************************************************************************/ |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Return the GPS coordinates of the venue |
292
|
|
|
* https://developer.mapquest.com/. |
293
|
|
|
* |
294
|
|
|
* @param string $address |
295
|
|
|
* @return array $ret |
296
|
|
|
*/ |
297
|
4 |
|
public static function getVenueGpsCoordinates(string $address) |
298
|
|
|
{ |
299
|
4 |
|
$key = 'Ad5KVnAISxX6aHyj6fAnHcKeh30n4W60'; |
300
|
4 |
|
$response = @file_get_contents('http://open.mapquestapi.com/geocoding/v1/address?key='.$key.'&location='.$address); |
301
|
4 |
|
$response = json_decode($response, true); |
302
|
|
|
|
303
|
4 |
|
$ret = []; |
304
|
4 |
|
$ret['lat'] = $response['results'][0]['locations'][0]['latLng']['lat']; |
305
|
4 |
|
$ret['lng'] = $response['results'][0]['locations'][0]['latLng']['lng']; |
306
|
|
|
|
307
|
4 |
|
return $ret; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/***************************************************************************/ |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Return a string with the list of the collection id separated by comma, |
314
|
|
|
* without any space. eg. "354,320,310". |
315
|
|
|
* |
316
|
|
|
* @param iterable $items |
317
|
|
|
* @return string $ret |
318
|
|
|
*/ |
319
|
1 |
|
public static function getCollectionIdsSeparatedByComma(iterable $items) |
320
|
|
|
{ |
321
|
1 |
|
$itemsIds = []; |
322
|
1 |
|
foreach ($items as $item) { |
323
|
|
|
array_push($itemsIds, $item->id); |
324
|
|
|
} |
325
|
1 |
|
$ret = implode(',', $itemsIds); |
326
|
|
|
|
327
|
1 |
|
return $ret; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/***************************************************************************/ |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Return a string that describe the report misuse reason. |
334
|
|
|
* |
335
|
|
|
* @param int $reason |
336
|
|
|
* @return string $ret |
337
|
|
|
*/ |
338
|
|
|
public static function getReportMisuseReasonDescription(int $reason) |
339
|
|
|
{ |
340
|
|
|
switch ($reason) { |
341
|
|
|
case '1': |
342
|
|
|
$ret = 'Not about Contact Improvisation'; |
343
|
|
|
break; |
344
|
|
|
case '2': |
345
|
|
|
$ret = 'Contains wrong informations'; |
346
|
|
|
break; |
347
|
|
|
case '3': |
348
|
|
|
$ret = 'It is not translated in english'; |
349
|
|
|
break; |
350
|
|
|
case '4': |
351
|
|
|
$ret = 'Other (specify in the message)'; |
352
|
|
|
break; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
return $ret; |
|
|
|
|
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/***************************************************************************/ |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Return a string that describe repetition kind in the event show view |
362
|
|
|
* |
363
|
|
|
* @param \DavideCasiraghi\LaravelEventsCalendar\Models\Event $event |
364
|
|
|
* @param \DavideCasiraghi\LaravelEventsCalendar\Models\EventRepetition $firstRpDates |
365
|
|
|
* @return string $ret |
366
|
|
|
*/ |
367
|
5 |
|
public static function getRepetitionTextString(Event $event, EventRepetition $firstRpDates) |
368
|
|
|
{ |
369
|
5 |
|
switch ($event->repeat_type) { |
370
|
5 |
|
case '1': // noRepeat |
371
|
2 |
|
$ret = null; |
372
|
2 |
|
break; |
373
|
3 |
|
case '2': // repeatWeekly |
374
|
1 |
|
$repeatUntil = new DateTime($event->repeat_until); |
375
|
|
|
|
376
|
|
|
// Get the name of the weekly day when the event repeat, if two days, return like "Thursday and Sunday" |
377
|
1 |
|
$repetitonWeekdayNumbersArray = explode(',', $event->repeat_weekly_on); |
378
|
1 |
|
$repetitonWeekdayNamesArray = []; |
379
|
1 |
|
foreach ($repetitonWeekdayNumbersArray as $key => $repetitonWeekdayNumber) { |
380
|
1 |
|
$repetitonWeekdayNamesArray[] = LaravelEventsCalendar::decodeRepeatWeeklyOn($repetitonWeekdayNumber); |
381
|
|
|
} |
382
|
|
|
// create from an array a string with all the values divided by " and " |
383
|
1 |
|
$nameOfTheRepetitionWeekDays = implode(' and ', $repetitonWeekdayNamesArray); |
384
|
|
|
|
385
|
|
|
//$ret = 'The event happens every '.$nameOfTheRepetitionWeekDays.' until '.$repeatUntil->format('d/m/Y'); |
386
|
1 |
|
$format = __('laravel-events-calendar::event.the_event_happens_every_x_until_x'); |
387
|
1 |
|
$ret = sprintf($format, $nameOfTheRepetitionWeekDays, $repeatUntil->format('d/m/Y')); |
|
|
|
|
388
|
1 |
|
break; |
389
|
2 |
|
case '3': //repeatMonthly |
390
|
1 |
|
$repeatUntil = new DateTime($event->repeat_until); |
391
|
1 |
|
$repetitionFrequency = LaravelEventsCalendar::decodeOnMonthlyKind($event->on_monthly_kind); |
392
|
|
|
|
393
|
|
|
//$ret = 'The event happens '.$repetitionFrequency.' until '.$repeatUntil->format('d/m/Y'); |
394
|
1 |
|
$format = __('laravel-events-calendar::event.the_event_happens_x_until_x'); |
395
|
1 |
|
$ret = sprintf($format, $repetitionFrequency, $repeatUntil->format('d/m/Y')); |
396
|
1 |
|
break; |
397
|
|
|
|
398
|
1 |
|
case '4': //repeatMultipleDays |
399
|
1 |
|
$dateStart = date('d/m/Y', strtotime($firstRpDates->start_repeat)); |
400
|
1 |
|
$singleDaysRepeatDatas = explode(',', $event->multiple_dates); |
401
|
|
|
|
402
|
|
|
// Sort the datas |
403
|
|
|
usort($singleDaysRepeatDatas, function ($a, $b) { |
404
|
1 |
|
$a = Carbon::createFromFormat('d/m/Y', $a); |
405
|
1 |
|
$b = Carbon::createFromFormat('d/m/Y', $b); |
406
|
|
|
|
407
|
1 |
|
return strtotime($a) - strtotime($b); |
408
|
1 |
|
}); |
409
|
|
|
|
410
|
|
|
//$ret = 'The event happens on this dates: '; |
411
|
1 |
|
$ret = __('laravel-events-calendar::event.the_event_happens_on_this_dates'); |
412
|
|
|
|
413
|
1 |
|
$ret .= $dateStart.', '; |
414
|
1 |
|
$ret .= LaravelEventsCalendar::getStringFromArraySeparatedByComma($singleDaysRepeatDatas); |
415
|
|
|
|
416
|
1 |
|
break; |
417
|
|
|
} |
418
|
|
|
|
419
|
5 |
|
return $ret; |
|
|
|
|
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
|
423
|
|
|
} |
424
|
|
|
|