|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\GoogleCalendar; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use DateTime; |
|
7
|
|
|
use Google_Service_Calendar_Event; |
|
8
|
|
|
use Google_Service_Calendar_EventDateTime; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
|
|
11
|
|
|
class Event |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var Google_Service_Calendar_Event */ |
|
14
|
|
|
public $googleEvent; |
|
15
|
|
|
|
|
16
|
|
|
/** @var int */ |
|
17
|
|
|
protected $calendarId; |
|
18
|
|
|
|
|
19
|
|
|
public static function createFromGoogleCalendarEvent(Google_Service_Calendar_Event $googleEvent, $calendarId) |
|
20
|
|
|
{ |
|
21
|
|
|
$event = new static(); |
|
22
|
|
|
|
|
23
|
|
|
$event->googleEvent = $googleEvent; |
|
24
|
|
|
|
|
25
|
|
|
$event->calendarId = $calendarId; |
|
26
|
|
|
|
|
27
|
|
|
return $event; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public static function create(array $properties, string $calendarId = null) |
|
31
|
|
|
{ |
|
32
|
|
|
$event = new static(); |
|
33
|
|
|
|
|
34
|
|
|
$event->calendarId = static::getGoogleCalendar($calendarId)->getCalendarId(); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
foreach ($properties as $name => $value) { |
|
37
|
|
|
$event->$name = $value; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $event->save(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function __construct() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->googleEvent = new Google_Service_Calendar_Event(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $name |
|
50
|
|
|
* |
|
51
|
|
|
* @return mixed |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __get($name) |
|
54
|
|
|
{ |
|
55
|
|
|
$name = $this->getFieldName($name); |
|
56
|
|
|
|
|
57
|
|
|
if ($name === 'sortDate') { |
|
58
|
|
|
return $this->getSortDate(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$value = array_get($this->googleEvent, $name); |
|
62
|
|
|
|
|
63
|
|
|
if (in_array($name, ['start.date', 'end.date']) && $value) { |
|
64
|
|
|
$value = Carbon::createFromFormat('Y-m-d', $value)->startOfDay(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (in_array($name, ['start.dateTime', 'end.dateTime']) && $value) { |
|
68
|
|
|
$value = Carbon::createFromFormat(DateTime::RFC3339, $value); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $value; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function __set($name, $value) |
|
75
|
|
|
{ |
|
76
|
|
|
$name = $this->getFieldName($name); |
|
77
|
|
|
|
|
78
|
|
|
if (in_array($name, ['start.date', 'end.date', 'start.dateTime', 'end.dateTime'])) { |
|
79
|
|
|
$this->setDateProperty($name, $value); |
|
80
|
|
|
|
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
array_set($this->googleEvent, $name, $value); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function exists(): bool |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->id != ''; |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function isAllDayEvent(): bool |
|
93
|
|
|
{ |
|
94
|
|
|
return is_null($this->googleEvent['start']['dateTime']); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param \Carbon\Carbon|null $startDateTime |
|
99
|
|
|
* @param \Carbon\Carbon|null $endDateTime |
|
100
|
|
|
* @param array $queryParameters |
|
101
|
|
|
* @param string|null $calendarId |
|
102
|
|
|
* |
|
103
|
|
|
* @return \Illuminate\Support\Collection |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function get( |
|
106
|
|
|
Carbon $startDateTime = null, |
|
107
|
|
|
Carbon $endDateTime = null, |
|
108
|
|
|
array $queryParameters = [], |
|
109
|
|
|
string $calendarId = null |
|
110
|
|
|
): Collection { |
|
111
|
|
|
$googleCalendar = static::getGoogleCalendar($calendarId); |
|
112
|
|
|
|
|
113
|
|
|
$googleEvents = $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters); |
|
114
|
|
|
|
|
115
|
|
|
return collect($googleEvents) |
|
116
|
|
|
->map(function (Google_Service_Calendar_Event $event) use ($calendarId) { |
|
117
|
|
|
return Event::createFromGoogleCalendarEvent($event, $calendarId); |
|
118
|
|
|
}) |
|
119
|
|
|
->sortBy(function (Event $event) { |
|
120
|
|
|
return $event->sortDate; |
|
|
|
|
|
|
121
|
|
|
}) |
|
122
|
|
|
->values(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $eventId |
|
127
|
|
|
* @param string $calendarId |
|
128
|
|
|
* |
|
129
|
|
|
* @return \Spatie\GoogleCalendar\Event |
|
130
|
|
|
*/ |
|
131
|
|
|
public static function find($eventId, $calendarId = null): Event |
|
132
|
|
|
{ |
|
133
|
|
|
$googleCalendar = static::getGoogleCalendar($calendarId); |
|
134
|
|
|
|
|
135
|
|
|
$googleEvent = $googleCalendar->getEvent($eventId); |
|
136
|
|
|
|
|
137
|
|
|
return static::createFromGoogleCalendarEvent($googleEvent, $calendarId); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function save(): Event |
|
141
|
|
|
{ |
|
142
|
|
|
$method = $this->exists() ? 'updateEvent' : 'insertEvent'; |
|
143
|
|
|
|
|
144
|
|
|
$googleCalendar = $this->getGoogleCalendar(); |
|
145
|
|
|
|
|
146
|
|
|
$googleEvent = $googleCalendar->$method($this); |
|
147
|
|
|
|
|
148
|
|
|
return static::createFromGoogleCalendarEvent($googleEvent, $googleCalendar->getCalendarId()); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param string $eventId |
|
153
|
|
|
*/ |
|
154
|
|
|
public function delete(string $eventId = null) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->getGoogleCalendar($this->calendarId)->deleteEvent($eventId ?? $this->id); |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param string $calendarId |
|
161
|
|
|
* |
|
162
|
|
|
* @return \Spatie\GoogleCalendar\GoogleCalendar |
|
163
|
|
|
*/ |
|
164
|
|
|
protected static function getGoogleCalendar($calendarId = null) |
|
165
|
|
|
{ |
|
166
|
|
|
$calendarId = $calendarId ?? config('laravel-google-calendar.calendar_id'); |
|
167
|
|
|
|
|
168
|
|
|
return GoogleCalendarFactory::createForCalendarId($calendarId); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param string $name |
|
173
|
|
|
* @param \Carbon\Carbon $date |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function setDateProperty(string $name, Carbon $date) |
|
176
|
|
|
{ |
|
177
|
|
|
$eventDateTime = new Google_Service_Calendar_EventDateTime(); |
|
178
|
|
|
|
|
179
|
|
|
if (in_array($name, ['start.date', 'end.date'])) { |
|
180
|
|
|
$eventDateTime->setDate($date->format('Y-m-d')); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
if (in_array($name, ['start.dateTime', 'end.dateTime'])) { |
|
184
|
|
|
$eventDateTime->setDateTime($date->format(DateTime::RFC3339)); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
if (starts_with($name, 'start')) { |
|
188
|
|
|
$this->googleEvent->setStart($eventDateTime); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
if (starts_with($name, 'end')) { |
|
192
|
|
|
$this->googleEvent->setEnd($eventDateTime); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
protected function getFieldName(string $name): string |
|
197
|
|
|
{ |
|
198
|
|
|
return [ |
|
199
|
|
|
'name' => 'summary', |
|
200
|
|
|
'startDate' => 'start.date', |
|
201
|
|
|
'endDate' => 'end.date', |
|
202
|
|
|
'startDateTime' => 'start.dateTime', |
|
203
|
|
|
'endDateTime' => 'end.dateTime', |
|
204
|
|
|
][$name] ?? $name; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function getSortDate(): string |
|
208
|
|
|
{ |
|
209
|
|
|
if ($this->startDate) { |
|
|
|
|
|
|
210
|
|
|
return $this->startDate; |
|
|
|
|
|
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
if ($this->startDateTime) { |
|
|
|
|
|
|
214
|
|
|
return $this->startDateTime; |
|
|
|
|
|
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return ''; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.