1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace garethp\ews\Calendar; |
4
|
|
|
|
5
|
|
|
use garethp\ews\API\Enumeration\DisposalType; |
6
|
|
|
use garethp\ews\API\Type\CalendarItemType; |
7
|
|
|
use garethp\ews\API\Type; |
8
|
|
|
use garethp\ews\API; |
9
|
|
|
use garethp\ews\API\Enumeration; |
10
|
|
|
use DateTime; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* An API end point for Calendar items |
14
|
|
|
* |
15
|
|
|
* Class API |
16
|
|
|
* @package garethp\ews\Calendar |
17
|
|
|
*/ |
18
|
|
|
class CalendarAPI extends API |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Type\FolderIdType |
22
|
|
|
*/ |
23
|
|
|
protected $folderId; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Pick a Calendar based on it's name |
27
|
|
|
* |
28
|
|
|
* @param string|null $displayName |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
6 |
|
public function pickCalendar($displayName = null) |
32
|
|
|
{ |
33
|
6 |
|
if ($displayName == 'default.calendar' || $displayName == null) { |
|
|
|
|
34
|
1 |
|
$folder = $this->getFolderByDistinguishedId('calendar'); |
35
|
1 |
|
} else { |
36
|
6 |
|
$folder = $this->getFolderByDisplayName($displayName, 'calendar'); |
37
|
|
|
} |
38
|
|
|
|
39
|
6 |
|
$this->folderId = $folder->getFolderId(); |
|
|
|
|
40
|
|
|
|
41
|
6 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return Type\FolderIdType |
46
|
|
|
*/ |
47
|
6 |
|
public function getFolderId() |
48
|
|
|
{ |
49
|
6 |
|
if ($this->folderId === null) { |
50
|
|
|
$this->pickCalendar(); |
51
|
|
|
} |
52
|
|
|
|
53
|
6 |
|
return $this->folderId; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Type\FolderIdType $folderId |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setFolderId($folderId) |
61
|
|
|
{ |
62
|
|
|
$this->folderId = $folderId; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Create one or more calendar items |
69
|
|
|
* |
70
|
|
|
* @param $items CalendarItemType[]|CalendarItemType|array or more calendar items to create |
71
|
|
|
* @param $options array Options to merge in to the request |
72
|
|
|
* @return Type\ItemIdType[] |
73
|
|
|
*/ |
74
|
3 |
|
public function createCalendarItems($items, $options = array()) |
75
|
|
|
{ |
76
|
|
|
//If the item passed in is an object, or if it's an associative] |
77
|
|
|
// array waiting to be an object, let's put it in to an array |
78
|
3 |
|
if (!is_array($items) || Type::arrayIsAssoc($items)) { |
79
|
2 |
|
$items = array($items); |
80
|
2 |
|
} |
81
|
|
|
|
82
|
3 |
|
$item = array('CalendarItem' => $items); |
83
|
|
|
$defaultOptions = array( |
84
|
3 |
|
'SendMeetingInvitations' => Enumeration\CalendarItemCreateOrDeleteOperationType::SEND_TO_NONE, |
85
|
|
|
'SavedItemFolderId' => array( |
86
|
3 |
|
'FolderId' => $this->getFolderId()->toXmlObject() |
87
|
3 |
|
) |
88
|
3 |
|
); |
89
|
|
|
|
90
|
3 |
|
$options = array_replace_recursive($defaultOptions, $options); |
91
|
|
|
|
92
|
3 |
|
$items = $this->createItems($item, $options); |
93
|
|
|
|
94
|
3 |
|
if (!is_array($items)) { |
95
|
2 |
|
$items = array($items); |
96
|
2 |
|
} |
97
|
|
|
|
98
|
3 |
|
return $items; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get a list of calendar items between two dates/times |
103
|
|
|
* |
104
|
|
|
* @param string|DateTime $start |
105
|
|
|
* @param string|DateTime $end |
106
|
|
|
* @param array $options |
107
|
|
|
* @return CalendarItemType[]|Type\FindItemParentType |
108
|
|
|
*/ |
109
|
6 |
|
public function getCalendarItems($start = '12:00 AM', $end = '11:59 PM', $options = array()) |
110
|
|
|
{ |
111
|
6 |
|
if (!($start instanceof DateTime)) { |
112
|
6 |
|
$start = new DateTime($start); |
113
|
6 |
|
} |
114
|
|
|
|
115
|
6 |
|
if (!($end instanceof DateTime)) { |
116
|
6 |
|
$end = new DateTime($end); |
117
|
6 |
|
} |
118
|
|
|
|
119
|
|
|
$request = array( |
120
|
6 |
|
'Traversal' => 'Shallow', |
121
|
|
|
'ItemShape' => array( |
122
|
|
|
'BaseShape' => 'AllProperties' |
123
|
6 |
|
), |
124
|
|
|
'CalendarView' => array( |
125
|
6 |
|
'MaxEntriesReturned' => 100, |
126
|
6 |
|
'StartDate' => $start->format('c'), |
127
|
6 |
|
'EndDate' => $end->format('c') |
128
|
6 |
|
), |
129
|
|
|
'ParentFolderIds' => array( |
130
|
6 |
|
'FolderId' => $this->getFolderId()->toXmlObject() |
131
|
6 |
|
) |
132
|
6 |
|
); |
133
|
|
|
|
134
|
6 |
|
$request = array_replace_recursive($request, $options); |
135
|
|
|
|
136
|
6 |
|
$request = Type::buildFromArray($request); |
137
|
6 |
|
$response = $this->getClient()->FindItem($request); |
138
|
6 |
|
$items = $response; |
139
|
|
|
|
140
|
6 |
|
return $items; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param $id |
145
|
|
|
* @param $changeKey |
146
|
|
|
* @return Type\CalendarItemType |
147
|
|
|
*/ |
148
|
1 |
|
public function getCalendarItem($id, $changeKey) |
149
|
|
|
{ |
150
|
1 |
|
return $this->getItem(['Id' => $id, 'ChangeKey' => $changeKey]); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Updates a calendar item with changes |
155
|
|
|
* |
156
|
|
|
* @param $itemId Type\ItemIdType |
157
|
|
|
* @param $changes |
158
|
|
|
* @return Type\CalendarItemType[] |
159
|
|
|
*/ |
160
|
1 |
View Code Duplication |
public function updateCalendarItem(Type\ItemIdType $itemId, $changes) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
//Create the request |
163
|
|
|
$request = array( |
164
|
|
|
'ItemChange' => array( |
165
|
1 |
|
'ItemId' => $itemId->toArray(), |
166
|
1 |
|
'Updates' => $this->buildUpdateItemChanges('CalendarItem', 'calendar', $changes) |
167
|
1 |
|
) |
168
|
1 |
|
); |
169
|
|
|
|
170
|
|
|
$options = array( |
171
|
|
|
'SendMeetingInvitationsOrCancellations' => 'SendToNone' |
172
|
1 |
|
); |
173
|
|
|
|
174
|
1 |
|
$items = $this->updateItems($request, $options)->getCalendarItem(); |
175
|
|
|
|
176
|
1 |
|
if (!is_array($items)) { |
177
|
1 |
|
$items = array($items); |
178
|
1 |
|
} |
179
|
|
|
|
180
|
1 |
|
return $items; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param Type\ItemIdType $itemId |
185
|
|
|
* @param string $deleteType DisposalType::HARD_DELETE |
186
|
|
|
* DisposalType::MOVE_TO_DELETED_ITEMS |
187
|
|
|
* DisposalType::SOFT_DELETE |
188
|
|
|
* |
189
|
|
|
* @return bool |
190
|
|
|
*/ |
191
|
3 |
|
public function deleteCalendarItem(Type\ItemIdType $itemId, $deleteType = DisposalType::MOVE_TO_DELETED_ITEMS) |
192
|
|
|
{ |
193
|
3 |
|
return $this->deleteItems( |
194
|
3 |
|
$itemId, |
195
|
|
|
[ |
196
|
3 |
|
'SendMeetingCancellations' => 'SendToNone', |
197
|
3 |
|
], |
198
|
|
|
$deleteType |
199
|
3 |
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $start |
204
|
|
|
* @param string $end |
205
|
|
|
* @param array $options |
206
|
|
|
*/ |
207
|
6 |
|
public function deleteAllCalendarItems($start = '12:00 AM', $end = '11:59 PM', $options = array()) |
208
|
|
|
{ |
209
|
6 |
|
$items = $this->getCalendarItems($start, $end, $options); |
210
|
6 |
|
foreach ($items as $item) { |
|
|
|
|
211
|
2 |
|
$this->deleteCalendarItem($item->getItemId()); |
212
|
6 |
|
} |
213
|
6 |
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get a list of changes on the calendar items |
217
|
|
|
* |
218
|
|
|
* @param null $syncState |
219
|
|
|
* @param array $options |
220
|
|
|
* @return API\Message\SyncFolderItemsResponseMessageType |
221
|
|
|
*/ |
222
|
1 |
|
public function listChanges($syncState = null, $options = array()) |
223
|
|
|
{ |
224
|
1 |
|
return parent::listItemChanges($this->getFolderId(), $syncState, $options); |
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param Type\ItemIdType $itemId |
229
|
|
|
* @param string $message |
230
|
|
|
* @param string $sensitivity |
231
|
|
|
* @param array $options |
232
|
|
|
* |
233
|
|
|
* @return Type\ItemIdType[] |
234
|
|
|
*/ |
235
|
|
View Code Duplication |
public function acceptMeeting($itemId, $message, $sensitivity = 'Private', $options = array()) |
|
|
|
|
236
|
|
|
{ |
237
|
|
|
$request = array( |
238
|
|
|
'AcceptItem' => array( |
239
|
|
|
'Sensitivity' => $sensitivity, |
240
|
|
|
'Body' => array('BodyType' => 'HTML', '_value' => $message), |
241
|
|
|
'ReferenceItemId' => $itemId->toArray() |
242
|
|
|
) |
243
|
|
|
); |
244
|
|
|
|
245
|
|
|
$defaultOptions = array('MessageDisposition' => 'SendOnly'); |
246
|
|
|
$options = array_replace_recursive($defaultOptions, $options); |
247
|
|
|
|
248
|
|
|
$return = $this->createItems($request, $options)->getCalendarItem(); |
|
|
|
|
249
|
|
|
if (!is_array($request)) { |
250
|
|
|
$return = array($return); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $return; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @param $itemId |
258
|
|
|
* @param $message |
259
|
|
|
* @param string $sensitivity |
260
|
|
|
* @param array $options |
261
|
|
|
* @return Type\ItemIdType[] |
262
|
|
|
*/ |
263
|
|
View Code Duplication |
public function declineMeeting($itemId, $message, $sensitivity = 'Private', $options = array()) |
|
|
|
|
264
|
|
|
{ |
265
|
|
|
$request = array( |
266
|
|
|
'DeclineItem' => array( |
267
|
|
|
'Sensitivity' => $sensitivity, |
268
|
|
|
'Body' => array('BodyType' => 'HTML', '_value' => $message), |
269
|
|
|
'ReferenceItemId' => $itemId->toArray() |
270
|
|
|
) |
271
|
|
|
); |
272
|
|
|
|
273
|
|
|
$defaultOptions = array('MessageDisposition' => 'SendOnly'); |
274
|
|
|
$options = array_replace_recursive($defaultOptions, $options); |
275
|
|
|
|
276
|
|
|
$return = $this->createItems($request, $options)->getCalendarItem(); |
|
|
|
|
277
|
|
|
if (!is_array($request)) { |
278
|
|
|
$return = array($return); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return $return; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|