1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelEventsCalendar\Models; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Carbon\CarbonInterval; |
7
|
|
|
use Carbon\CarbonPeriod; |
8
|
|
|
use DavideCasiraghi\LaravelEventsCalendar\Facades\LaravelEventsCalendar; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
10
|
|
|
|
11
|
|
|
class EventRepetition extends Model |
12
|
|
|
{ |
13
|
|
|
/***************************************************************************/ |
14
|
|
|
/** |
15
|
|
|
* The table associated with the model. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $table = 'event_repetitions'; |
20
|
|
|
|
21
|
|
|
/***************************************************************************/ |
22
|
|
|
|
23
|
|
|
protected $fillable = [ |
24
|
|
|
'event_id', 'start_repeat', 'end_repeat', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/*public function user() |
28
|
|
|
{ |
29
|
|
|
return $this->belongsTo('DavideCasiraghi\LaravelEventsCalendar\Models\Event', 'event_id', 'id'); |
30
|
|
|
}*/ |
31
|
|
|
|
32
|
|
|
/***************************************************************************/ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get for each event the first event repetition in the near future (JUST THE QUERY to use as SUBQUERY). |
36
|
|
|
* Parameters are Start date and End date of the interval |
37
|
|
|
* Return the query string,. |
38
|
|
|
* @param string $searchStartDate |
39
|
|
|
* @param string $searchEndDate |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
5 |
|
public static function getLastestEventsRepetitionsQuery($searchStartDate, $searchEndDate) |
43
|
|
|
{ |
44
|
|
|
$ret = self:: |
45
|
5 |
|
selectRaw('event_id, MIN(id) AS rp_id, start_repeat, end_repeat') |
46
|
|
|
->when($searchStartDate, function ($query, $searchStartDate) { |
47
|
5 |
|
return $query->where('event_repetitions.start_repeat', '>=', $searchStartDate); |
48
|
5 |
|
}) |
49
|
|
|
->when($searchEndDate, function ($query, $searchEndDate) { |
50
|
1 |
|
return $query->where('event_repetitions.end_repeat', '<=', $searchEndDate); |
51
|
5 |
|
}) |
52
|
5 |
|
->groupBy('event_id'); |
53
|
|
|
|
54
|
5 |
|
return $ret; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/***************************************************************************/ |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Save event repetition in the DB. |
61
|
|
|
* $dateStart and $dateEnd are in the format Y-m-d |
62
|
|
|
* $timeStart and $timeEnd are in the format H:i:s. |
63
|
|
|
* @param int $eventId |
64
|
|
|
* @param string $dateStart |
65
|
|
|
* @param string $dateEnd |
66
|
|
|
* @param string $timeStart |
67
|
|
|
* @param string $timeEnd |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
15 |
|
public static function saveEventRepetitionOnDB(int $eventId, string $dateStart, string $dateEnd, string $timeStart, string $timeEnd) |
71
|
|
|
{ |
72
|
15 |
|
$eventRepetition = new self(); |
73
|
15 |
|
$eventRepetition->event_id = $eventId; |
74
|
|
|
|
75
|
15 |
|
$eventRepetition->start_repeat = Carbon::createFromFormat('Y-m-d H:i', $dateStart.' '.$timeStart); |
76
|
15 |
|
$eventRepetition->end_repeat = Carbon::createFromFormat('Y-m-d H:i', $dateEnd.' '.$timeEnd); |
77
|
|
|
|
78
|
15 |
|
$eventRepetition->save(); |
79
|
15 |
|
} |
80
|
|
|
|
81
|
|
|
/***************************************************************************/ |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Save all the weekly repetitions in the event_repetitions table. |
85
|
|
|
* $dateStart and $dateEnd are in the format Y-m-d |
86
|
|
|
* $timeStart and $timeEnd are in the format H:i:s. |
87
|
|
|
* $weekDays - $request->get('repeat_weekly_on_day'). |
88
|
|
|
* @param \DavideCasiraghi\LaravelEventsCalendar\Models\Event $event |
89
|
|
|
* @param array|null $weekDays |
90
|
|
|
* @param string $startDate |
91
|
|
|
* @param string $repeatUntilDate |
92
|
|
|
* @param string $timeStart |
93
|
|
|
* @param string $timeEnd |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
3 |
|
public static function saveWeeklyRepeatDates(int $eventId, array $weekDays, string $startDate, string $repeatUntilDate, string $timeStart, string $timeEnd) |
97
|
|
|
{ |
98
|
3 |
|
$beginPeriod = Carbon::createFromFormat('Y-m-d', $startDate); |
99
|
3 |
|
$endPeriod = Carbon::createFromFormat('Y-m-d', $repeatUntilDate); |
100
|
3 |
|
$interval = CarbonInterval::days(1); |
|
|
|
|
101
|
3 |
|
$period = CarbonPeriod::create($beginPeriod, $interval, $endPeriod); |
102
|
3 |
|
foreach ($period as $day) { // Iterate for each day of the period |
103
|
3 |
|
foreach ($weekDays as $weekDayNumber) { // Iterate for every day of the week (1:Monday, 2:Tuesday, 3:Wednesday ...) |
104
|
3 |
|
if (LaravelEventsCalendar::isWeekDay($day->format('Y-m-d'), $weekDayNumber)) { |
|
|
|
|
105
|
3 |
|
self::saveEventRepetitionOnDB($eventId, $day->format('Y-m-d'), $day->format('Y-m-d'), $timeStart, $timeEnd); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
3 |
|
} |
110
|
|
|
|
111
|
|
|
/***************************************************************************/ |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Save multiple single dates in the event_repetitions table |
115
|
|
|
* useful: http://thisinterestsme.com/php-get-first-monday-of-month/. |
116
|
|
|
* $singleDaysRepeatDatas - explode of $request->get('multiple_dates') - eg. ["19/03/2020","20/05/2020","29/05/2020"] |
117
|
|
|
* $startDate (Y-m-d) |
118
|
|
|
* $timeStart (H:i:s) |
119
|
|
|
* $timeEnd (H:i:s) |
120
|
|
|
* |
121
|
|
|
* @param int $eventId |
122
|
|
|
* @param array $singleDaysRepeatDatas |
123
|
|
|
* @param string $startDate |
124
|
|
|
* @param string $timeStart |
125
|
|
|
* @param string $timeEnd |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
1 |
|
public static function saveMultipleRepeatDates(int $eventId, array $singleDaysRepeatDatas, string $startDate, string $timeStart, string $timeEnd) |
129
|
|
|
{ |
130
|
1 |
|
$day = Carbon::createFromFormat('Y-m-d', $startDate); |
131
|
|
|
|
132
|
1 |
|
EventRepetition::saveEventRepetitionOnDB($eventId, $day->format('Y-m-d'), $day->format('Y-m-d'), $timeStart, $timeEnd); |
133
|
|
|
|
134
|
1 |
|
foreach ($singleDaysRepeatDatas as $key => $singleDayRepeatDatas) { |
135
|
1 |
|
$day = Carbon::createFromFormat('d/m/Y', $singleDayRepeatDatas); |
136
|
|
|
|
137
|
1 |
|
EventRepetition::saveEventRepetitionOnDB($eventId, $day->format('Y-m-d'), $day->format('Y-m-d'), $timeStart, $timeEnd); |
138
|
|
|
} |
139
|
1 |
|
} |
140
|
|
|
} |
141
|
|
|
|