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
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function __get($name) |
53
|
|
|
{ |
54
|
|
|
$name = $this->translateFieldName($name); |
55
|
|
|
|
56
|
|
|
if ($name === 'sortDate') { |
57
|
|
|
return $this->getSortDate(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$value = array_get($this->googleEvent, $name); |
61
|
|
|
|
62
|
|
|
if (in_array($name, ['start.date', 'end.date']) && $value) { |
63
|
|
|
$value = Carbon::createFromFormat('Y-m-d', $value); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (in_array($name, ['start.dateTime', 'end.dateTime']) && $value) { |
67
|
|
|
$value = Carbon::createFromFormat(DateTime::RFC3339, $value); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $value; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function __set($name, $value) |
74
|
|
|
{ |
75
|
|
|
$name = $this->translateFieldName($name); |
76
|
|
|
|
77
|
|
|
if (in_array($name, ['start.date', 'end.date', 'start.dateTime', 'end.dateTime'])) { |
78
|
|
|
$this->setDateProperty($name, $value); |
79
|
|
|
|
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
array_set($this->googleEvent, $name, $value); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function exists() : bool |
87
|
|
|
{ |
88
|
|
|
return $this->id != ''; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function isAllDayEvent() : bool |
92
|
|
|
{ |
93
|
|
|
return is_null($this->googleEvent['start']['dateTime']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param \Carbon\Carbon|null $startDateTime |
98
|
|
|
* @param \Carbon\Carbon|null $endDateTime |
99
|
|
|
* @param array $queryParameters |
100
|
|
|
* @param string|null $calendarId |
101
|
|
|
* |
102
|
|
|
* @return \Illuminate\Support\Collection |
103
|
|
|
*/ |
104
|
|
|
public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = [], string $calendarId = null) : Collection |
105
|
|
|
{ |
106
|
|
|
$googleCalendar = self::getGoogleCalendar($calendarId); |
107
|
|
|
|
108
|
|
|
$googleEvents = $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters); |
109
|
|
|
|
110
|
|
|
return collect($googleEvents) |
111
|
|
|
->map(function (Google_Service_Calendar_Event $event) use ($calendarId) { |
112
|
|
|
return Event::createFromGoogleCalendarEvent($event, $calendarId); |
113
|
|
|
}) |
114
|
|
|
->sortBy(function(Event $event) { |
115
|
|
|
return $event->sortDate; |
|
|
|
|
116
|
|
|
}); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $eventId |
121
|
|
|
* @param string $calendarId |
122
|
|
|
* |
123
|
|
|
* @return \Spatie\GoogleCalendar\Event |
124
|
|
|
*/ |
125
|
|
|
public static function find($eventId, $calendarId = null) : Event |
126
|
|
|
{ |
127
|
|
|
$googleCalendar = self::getGoogleCalendar($calendarId); |
128
|
|
|
|
129
|
|
|
$googleEvent = $googleCalendar->getEvent($eventId); |
130
|
|
|
|
131
|
|
|
return Event::createFromGoogleCalendarEvent($googleEvent, $calendarId); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function save() : Event |
135
|
|
|
{ |
136
|
|
|
$method = $this->exists() ? 'updateEvent' : 'insertEvent'; |
137
|
|
|
|
138
|
|
|
$googleCalendar = $this->getGoogleCalendar(); |
139
|
|
|
|
140
|
|
|
$googleEvent = $googleCalendar->$method($this); |
141
|
|
|
|
142
|
|
|
return Event::createFromGoogleCalendarEvent($googleEvent, $googleCalendar->getCalendarId()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $eventId |
147
|
|
|
* |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
|
|
public function delete(string $eventId = null) |
151
|
|
|
{ |
152
|
|
|
return $this->getGoogleCalendar($this->calendarId)->deleteEvent($eventId ?? $this->id); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $calendarId |
157
|
|
|
* |
158
|
|
|
* @return \Spatie\GoogleCalendar\GoogleCalendar |
159
|
|
|
*/ |
160
|
|
|
protected static function getGoogleCalendar($calendarId = null) : GoogleCalendar |
161
|
|
|
{ |
162
|
|
|
$calendarId = $calendarId ?? config('laravel-google-calendar.calendar_id'); |
163
|
|
|
|
164
|
|
|
return GoogleCalendarFactory::createForCalendarId($calendarId); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $name |
169
|
|
|
* @param \Carbon\Carbon $date |
170
|
|
|
*/ |
171
|
|
|
protected function setDateProperty(string $name, Carbon $date) |
172
|
|
|
{ |
173
|
|
|
$eventDateTime = new Google_Service_Calendar_EventDateTime(); |
174
|
|
|
|
175
|
|
|
if (in_array($name, ['start.date', 'end.date'])) { |
176
|
|
|
$eventDateTime->setDate($date->format('Y-m-d')); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if (in_array($name, ['start.dateTime', 'end.dateTime'])) { |
180
|
|
|
$eventDateTime->setDate($date->format(DateTime::RFC3339)); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
if (starts_with($name, 'start')) { |
184
|
|
|
$this->googleEvent->setStart($eventDateTime); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (starts_with($name, 'end')) { |
188
|
|
|
$this->googleEvent->setEnd($eventDateTime); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
protected function translateFieldName(string $name) : string |
193
|
|
|
{ |
194
|
|
|
$translations = [ |
195
|
|
|
'name' => 'summary', |
196
|
|
|
'startDate' => 'start.date', |
197
|
|
|
'endDate' => 'end.date', |
198
|
|
|
'startDateTime' => 'start.dateTime', |
199
|
|
|
'endDateTime' => 'endDateTime', |
200
|
|
|
]; |
201
|
|
|
|
202
|
|
|
return $translations[$name] ?? $name; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getSortDate() : Carbon |
206
|
|
|
{ |
207
|
|
|
if ($this->startDate) { |
|
|
|
|
208
|
|
|
return $this->startDate; |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $this->startDateTime; |
|
|
|
|
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
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.