Passed
Push — master ( 47e36c...1b8f90 )
by
unknown
06:00 queued 18s
created

ContactItemModule::updateAppointments()   F

Complexity

Conditions 14
Paths 288

Size

Total Lines 122
Code Lines 85

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 14
eloc 85
c 1
b 1
f 0
nc 288
nop 3
dl 0
loc 122
rs 3.7939

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Contact ItemModule
5
 * Module which openes, creates, saves and deletes an item. It
6
 * extends the Module class.
7
 */
8
class ContactItemModule extends ItemModule {
9
	/**
10
	 * Constructor.
11
	 *
12
	 * @param int   $id   unique id
13
	 * @param array $data list of all actions
14
	 */
15
	public function __construct($id, $data) {
16
		$this->properties = $GLOBALS['properties']->getContactProperties();
17
18
		parent::__construct($id, $data);
19
20
		$this->plaintext = true;
21
	}
22
23
	/**
24
	 * Function which opens an item.
25
	 *
26
	 * @param object $store   MAPI Message Store Object
27
	 * @param string $entryid entryid of the message
28
	 * @param array  $action  the action data, sent by the client
29
	 */
30
	public function open($store, $entryid, $action) {
31
		$data = [];
32
33
		if ($entryid) {
34
			/* Check if given entryid is shared folder distlist then
35
			* get the store of distlist for fetching it's members.
36
			*/
37
			$storeData = $this->getStoreParentEntryIdFromEntryId($entryid);
38
			$store = $storeData["store"];
39
			$message = $storeData["message"];
40
		}
41
42
		if (empty($message)) {
43
			return;
44
		}
45
46
		// Open embedded message if requested
47
		$attachNum = !empty($action['attach_num']) ? $action['attach_num'] : false;
48
		if ($attachNum) {
49
			// get message props of sub message
50
			$parentMessage = $message;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.
Loading history...
51
			$message = $GLOBALS['operations']->openMessage($store, $entryid, $attachNum);
52
53
			if (empty($message)) {
54
				return;
55
			}
56
57
			// Check if message is distlist then we need to use different set of properties
58
			$props = mapi_getprops($message, [PR_MESSAGE_CLASS]);
59
60
			if (stripos($props[PR_MESSAGE_CLASS], 'IPM.Distlist') !== false) {
61
				// for distlist we need to use different set of properties
62
				$this->properties = $GLOBALS['properties']->getDistListProperties();
63
			}
64
65
			$data['item'] = $GLOBALS['operations']->getEmbeddedMessageProps($store, $message, $this->properties, $parentMessage, $attachNum);
66
		}
67
		else {
68
			// Check if message is distlist then we need to use different set of properties
69
			$props = mapi_getprops($message, [PR_MESSAGE_CLASS]);
70
71
			if (stripos($props[PR_MESSAGE_CLASS], 'IPM.Distlist') !== false) {
72
				// for distlist we need to use different set of properties
73
				$this->properties = $GLOBALS['properties']->getDistListProperties();
74
			}
75
76
			// get message props of the message
77
			$data['item'] = $GLOBALS['operations']->getMessageProps($store, $message, $this->properties, $this->plaintext);
78
		}
79
80
		// By openentry from address book, the entryid will differ, make it same as the origin
81
		$data['item']['entryid'] = bin2hex($entryid);
82
83
		// Allowing to hook in just before the data sent away to be sent to the client
84
		$GLOBALS['PluginManager']->triggerHook('server.module.contactitemmodule.open.after', [
85
			'moduleObject' => &$this,
86
			'store' => $store,
87
			'entryid' => $entryid,
88
			'action' => $action,
89
			'message' => &$message,
90
			'data' => &$data,
91
		]);
92
93
		$this->addActionData('item', $data);
94
		$GLOBALS['bus']->addData($this->getResponseData());
95
	}
96
97
	/**
98
	 * Function which saves an item. It sets the right properties for a contact
99
	 * item (address book properties).
100
	 *
101
	 * @param object $store         MAPI Message Store Object
102
	 * @param string $parententryid parent entryid of the message
103
	 * @param string $entryid       entryid of the message
104
	 * @param array  $action        the action data, sent by the client
105
	 */
106
	public function save($store, $parententryid, $entryid, $action) {
107
		$properiesToDelete = []; // create an array of properties which should be deleted
108
		// this array is passed to $GLOBALS['operations']->saveMessage() function
109
110
		if (!$store && !$parententryid) {
0 ignored issues
show
introduced by
$store is of type object, thus it always evaluated to true.
Loading history...
111
			if (isset($action['props']['message_class'])) {
112
				$store = $GLOBALS['mapisession']->getDefaultMessageStore();
113
				$parententryid = $this->getDefaultFolderEntryID($store, $action['props']['message_class']);
114
			}
115
			elseif ($entryid) {
116
				$data = $this->getStoreParentEntryIdFromEntryId($entryid);
117
				$store = $data["store"];
118
				$parententryid = $data["parent_entryid"];
119
			}
120
		}
121
		elseif (!$parententryid) {
122
			if (isset($action['props']['message_class'])) {
123
				$parententryid = $this->getDefaultFolderEntryID($store, $action['props']['message_class']);
124
			}
125
		}
126
127
		if ($store && $parententryid && isset($action['props'])) {
128
			if (isset($action['members'])) {
129
				// DistList
130
131
				// for distlist we need to use different set of properties
132
				$this->properties = $GLOBALS['properties']->getDistListProperties();
133
134
				// do conversion of client data
135
				$props = Conversion::mapXML2MAPI($this->properties, $action['props']);
136
137
				// collect members
138
				$members = [];
139
				$oneoff_members = [];
140
141
				$items = $action['members'];
142
143
				foreach ($items as $item) {
144
					if (empty($item['email_address'])) {
145
						// if no email address is given then mapi_parseoneoff fails, so always give
146
						// email address, OL07 uses Unknown as email address so we do same here
147
						$item['email_address'] = 'Unknown';
148
					}
149
150
					$oneoff = mapi_createoneoff($item['display_name'], $item['address_type'], $item['email_address']);
151
152
					if ($item['distlist_type'] == DL_EXTERNAL_MEMBER) {
153
						$member = $oneoff;
154
					}
155
					else {
156
						$parts = [];
157
						$parts['distlist_guid'] = WAB_GUID;
158
						$parts['distlist_type'] = $item['distlist_type'];
159
						$parts['entryid'] = hex2bin($item['entryid']);
160
						$member = pack('VA16CA*', 0, $parts['distlist_guid'], $parts['distlist_type'], $parts['entryid']);
161
					}
162
163
					$oneoff_members[] = $oneoff;
164
					$members[] = $member;
165
				}
166
167
				if (!empty($members) && !empty($oneoff_members)) {
168
					$props[$this->properties['members']] = $members;
169
					$props[$this->properties['oneoff_members']] = $oneoff_members;
170
				}
171
				else {
172
					$properiesToDelete[] = $this->properties['members'];
173
					$properiesToDelete[] = $this->properties['oneoff_members'];
174
				}
175
176
				unset($action['members']);
177
			}
178
			else {
179
				// Contact
180
181
				$isCopyGABToContact = isset($action["message_action"], $action["message_action"]["action_type"]) &&
182
183
				$action["message_action"]["action_type"] === "copyToContact";
184
185
				if ($isCopyGABToContact) {
186
					$this->copyGABRecordProps($action);
187
				}
188
				// generate one-off entryids for email addresses
189
				for ($index = 1; $index < 4; ++$index) {
190
					if (!empty($action['props']['email_address_' . $index]) && !empty($action['props']['email_address_display_name_' . $index])) {
191
						$action['props']['email_address_entryid_' . $index] = bin2hex(mapi_createoneoff($action['props']['email_address_display_name_' . $index], $action['props']['email_address_type_' . $index], $action['props']['email_address_' . $index]));
192
					}
193
				}
194
195
				// set properties for primary fax number
196
				if (isset($action['props']['fax_1_email_address']) && !empty($action['props']['fax_1_email_address'])) {
197
					$action['props']['fax_1_original_entryid'] = bin2hex(mapi_createoneoff($action['props']['fax_1_original_display_name'], $action['props']['fax_1_address_type'], $action['props']['fax_1_email_address'], MAPI_UNICODE));
198
				}
199
				else {
200
					// delete properties to remove previous values
201
					$properiesToDelete[] = $this->properties['fax_1_address_type'];
202
					$properiesToDelete[] = $this->properties['fax_1_original_display_name'];
203
					$properiesToDelete[] = $this->properties['fax_1_email_address'];
204
					$properiesToDelete[] = $this->properties['fax_1_original_entryid'];
205
				}
206
207
				// set properties for business fax number
208
				if (isset($action['props']['fax_2_email_address']) && !empty($action['props']['fax_2_email_address'])) {
209
					$action['props']['fax_2_original_entryid'] = bin2hex(mapi_createoneoff($action['props']['fax_2_original_display_name'], $action['props']['fax_2_address_type'], $action['props']['fax_2_email_address'], MAPI_UNICODE));
210
				}
211
				else {
212
					$properiesToDelete[] = $this->properties['fax_2_address_type'];
213
					$properiesToDelete[] = $this->properties['fax_2_original_display_name'];
214
					$properiesToDelete[] = $this->properties['fax_2_email_address'];
215
					$properiesToDelete[] = $this->properties['fax_2_original_entryid'];
216
				}
217
218
				// set properties for home fax number
219
				if (isset($action['props']['fax_3_email_address']) && !empty($action['props']['fax_3_email_address'])) {
220
					$action['props']['fax_3_original_entryid'] = bin2hex(mapi_createoneoff($action['props']['fax_3_original_display_name'], $action['props']['fax_3_address_type'], $action['props']['fax_3_email_address'], MAPI_UNICODE));
221
				}
222
				else {
223
					$properiesToDelete[] = $this->properties['fax_3_address_type'];
224
					$properiesToDelete[] = $this->properties['fax_3_original_display_name'];
225
					$properiesToDelete[] = $this->properties['fax_3_email_address'];
226
					$properiesToDelete[] = $this->properties['fax_3_original_entryid'];
227
				}
228
229
				// check for properties which should be deleted
230
				if (isset($action['entryid']) && !empty($action['entryid'])) {
231
					// check for empty email address properties
232
					for ($i = 1; $i < 4; ++$i) {
233
						if (isset($action['props']['email_address_' . $i]) && empty($action['props']['email_address_' . $i])) {
234
							array_push($properiesToDelete, $this->properties['email_address_entryid_' . $i]);
235
							array_push($properiesToDelete, $this->properties['email_address_' . $i]);
236
							array_push($properiesToDelete, $this->properties['email_address_display_name_' . $i]);
237
							array_push($properiesToDelete, $this->properties['email_address_display_name_email_' . $i]);
238
							array_push($properiesToDelete, $this->properties['email_address_type_' . $i]);
239
						}
240
					}
241
242
					// check for empty address_book_mv and address_book_long properties
243
					if (isset($action['props']['address_book_long']) && $action['props']['address_book_long'] === 0) {
244
						$properiesToDelete[] = $this->properties['address_book_mv'];
245
						$properiesToDelete[] = $this->properties['address_book_long'];
246
					}
247
248
					// Check if the birthday and anniversary properties are empty. If so delete them.
249
					if (array_key_exists('birthday', $action['props']) && empty($action['props']['birthday'])) {
250
						array_push($properiesToDelete, $this->properties['birthday']);
251
						array_push($properiesToDelete, $this->properties['birthday_eventid']);
252
						if (!empty($action['props']['birthday_eventid'])) {
253
							$this->deleteSpecialDateAppointment($store, $action['props']['birthday_eventid']);
254
						}
255
					}
256
257
					if (array_key_exists('wedding_anniversary', $action['props']) && empty($action['props']['wedding_anniversary'])) {
258
						array_push($properiesToDelete, $this->properties['wedding_anniversary']);
259
						array_push($properiesToDelete, $this->properties['anniversary_eventid']);
260
						if (!empty($action['props']['anniversary_eventid'])) {
261
							$this->deleteSpecialDateAppointment($store, $action['props']['anniversary_eventid']);
262
						}
263
					}
264
				}
265
266
				/*
267
				 * convert all line endings(LF) into CRLF
268
				 * XML parser will normalize all CR, LF and CRLF into LF
269
				 * but outlook(windows) uses CRLF as line ending
270
				 */
271
				if (isset($action['props']['business_address'])) {
272
					$action['props']['business_address'] = str_replace('\n', '\r\n', $action['props']['business_address']);
273
				}
274
275
				if (isset($action['props']['home_address'])) {
276
					$action['props']['home_address'] = str_replace('\n', '\r\n', $action['props']['home_address']);
277
				}
278
279
				if (isset($action['props']['other_address'])) {
280
					$action['props']['other_address'] = str_replace('\n', '\r\n', $action['props']['other_address']);
281
				}
282
283
				// check birthday props to make an appointment
284
				if (!empty($action['props']['birthday'])) {
285
					$action['props']['birthday_eventid'] = $this->updateAppointments($store, $action, 'birthday');
286
				}
287
288
				// check anniversary props to make an appointment
289
				if (!empty($action['props']['wedding_anniversary'])) {
290
					$action['props']['anniversary_eventid'] = $this->updateAppointments($store, $action, 'wedding_anniversary');
291
				}
292
293
				// do the conversion when all processing has been finished
294
				$props = Conversion::mapXML2MAPI($this->properties, $action['props']);
295
			}
296
297
			$messageProps = [];
298
299
			$result = $GLOBALS['operations']->saveMessage($store, $entryid, $parententryid, $props, $messageProps, [], isset($action['attachments']) ? $action['attachments'] : [], $properiesToDelete);
300
301
			if ($result) {
302
				$GLOBALS['bus']->notify(bin2hex($parententryid), TABLE_SAVE, $messageProps);
303
304
				if ($isCopyGABToContact) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $isCopyGABToContact does not seem to be defined for all execution paths leading up to this point.
Loading history...
305
					$message = mapi_msgstore_openentry($store, $messageProps[PR_ENTRYID]);
306
					$messageProps = mapi_getprops($message, $this->properties);
307
				}
308
309
				$this->addActionData('update', ['item' => Conversion::mapMAPI2XML($this->properties, $messageProps)]);
310
				$GLOBALS['bus']->addData($this->getResponseData());
311
			}
312
		}
313
	}
314
315
	/**
316
	 * Function copy the some property from address book record to contact props.
317
	 *
318
	 * @param array $action the action data, sent by the client
319
	 */
320
	public function copyGABRecordProps(&$action) {
321
		$addrbook = $GLOBALS["mapisession"]->getAddressbook();
322
		$abitem = mapi_ab_openentry($addrbook, hex2bin($action["message_action"]["source_entryid"]));
323
		$abItemProps = mapi_getprops($abitem, [
324
			PR_COMPANY_NAME,
325
			PR_ASSISTANT,
326
			PR_BUSINESS_TELEPHONE_NUMBER,
327
			PR_BUSINESS2_TELEPHONE_NUMBER,
328
			PR_HOME2_TELEPHONE_NUMBER,
329
			PR_STREET_ADDRESS,
330
			PR_LOCALITY,
331
			PR_STATE_OR_PROVINCE,
332
			PR_POSTAL_CODE,
333
			PR_COUNTRY,
334
			PR_MOBILE_TELEPHONE_NUMBER,
335
		]);
336
		$action["props"]["company_name"] = isset($abItemProps[PR_COMPANY_NAME]) ? $abItemProps[PR_COMPANY_NAME] : '';
337
		$action["props"]["assistant"] = isset($abItemProps[PR_ASSISTANT]) ? $abItemProps[PR_ASSISTANT] : '';
338
		$action["props"]["business_telephone_number"] = isset($abItemProps[PR_BUSINESS_TELEPHONE_NUMBER]) ? $abItemProps[PR_BUSINESS_TELEPHONE_NUMBER] : '';
339
		$action["props"]["business2_telephone_number"] = isset($abItemProps[PR_BUSINESS2_TELEPHONE_NUMBER]) ? $abItemProps[PR_BUSINESS2_TELEPHONE_NUMBER] : '';
340
		$action["props"]["home2_telephone_number"] = isset($abItemProps[PR_HOME2_TELEPHONE_NUMBER]) ? $abItemProps[PR_HOME2_TELEPHONE_NUMBER] : '';
341
		$action["props"]["home_address_street"] = isset($abItemProps[PR_STREET_ADDRESS]) ? $abItemProps[PR_STREET_ADDRESS] : '';
342
		$action["props"]["home_address_city"] = isset($abItemProps[PR_LOCALITY]) ? $abItemProps[PR_LOCALITY] : '';
343
		$action["props"]["home_address_state"] = isset($abItemProps[PR_STATE_OR_PROVINCE]) ? $abItemProps[PR_STATE_OR_PROVINCE] : '';
344
		$action["props"]["home_address_postal_code"] = isset($abItemProps[PR_POSTAL_CODE]) ? $abItemProps[PR_POSTAL_CODE] : '';
345
		$action["props"]["home_address_country"] = isset($abItemProps[PR_COUNTRY]) ? $abItemProps[PR_COUNTRY] : '';
346
347
		$action["props"]["cellular_telephone_number"] = isset($abItemProps[PR_MOBILE_TELEPHONE_NUMBER]) ? $abItemProps[PR_MOBILE_TELEPHONE_NUMBER] : '';
348
349
		// Set the home_address property value
350
		$props = ["street", "city", "state", "postal_code", "country"];
351
		$homeAddress = "";
352
		foreach ($props as $index => $prop) {
353
			if (isset($action["props"]["home_address_" . $prop]) && !empty($action["props"]["home_address_" . $prop])) {
354
				$homeAddress .= $action["props"]["home_address_" . $prop] . " ";
355
				if ($prop == "street" || $prop == "postal_code") {
356
					$homeAddress .= PHP_EOL;
357
				}
358
			}
359
		}
360
361
		$action["props"]["home_address"] = $homeAddress;
362
	}
363
364
	/**
365
	 * Function which deletes an item. Extended here to also delete corresponding birthday/anniversary
366
	 * appointments from calendar.
367
	 *
368
	 * @param object $store         MAPI Message Store Object
369
	 * @param string $parententryid parent entryid of the message
370
	 * @param string $entryid       entryid of the message
371
	 * @param array  $action        the action data, sent by the client
372
	 */
373
	public function delete($store, $parententryid, $entryid, $action) {
374
		$message = false;
375
		if (!$store && !$parententryid && $entryid) {
0 ignored issues
show
introduced by
$store is of type object, thus it always evaluated to true.
Loading history...
376
			$data = $this->getStoreParentEntryIdFromEntryId($entryid);
377
			$store = $data["store"];
378
			$message = $data["message"];
379
			$parententryid = $data["parent_entryid"];
380
		}
381
382
		if ($store && $entryid) {
383
			try {
384
				if ($message === false) {
0 ignored issues
show
introduced by
The condition $message === false is always true.
Loading history...
385
					$message = $GLOBALS["operations"]->openMessage($store, $entryid);
386
				}
387
388
				$props = mapi_getprops($message, [$this->properties['anniversary_eventid'], $this->properties['birthday_eventid']]);
389
390
				// if any of the appointment entryid exists then delete it
391
				if (!empty($props[$this->properties['birthday_eventid']])) {
392
					$this->deleteSpecialDateAppointment($store, bin2hex($props[$this->properties['birthday_eventid']]));
393
				}
394
395
				if (!empty($props[$this->properties['anniversary_eventid']])) {
396
					$this->deleteSpecialDateAppointment($store, bin2hex($props[$this->properties['anniversary_eventid']]));
397
				}
398
			}
399
			catch (MAPIException $e) {
400
				// if any error occurs in deleting appointments then we shouldn't block deletion of contact item
401
				// so ignore errors now
402
				$e->setHandled();
403
			}
404
405
			parent::delete($store, $parententryid, $entryid, $action);
406
		}
407
	}
408
409
	/**
410
	 * Function which retrieve the store, parent_entryid from record entryid.
411
	 *
412
	 * @param $entryid entryid of the message
413
	 *
414
	 * @return array which contains store and message object and parent entryid of that message
415
	 */
416
	public function getStoreParentEntryIdFromEntryId($entryid) {
417
		$message = $GLOBALS['mapisession']->openMessage($entryid);
418
		$messageStoreInfo = mapi_getprops($message, [PR_STORE_ENTRYID, PR_PARENT_ENTRYID]);
419
		$store = $GLOBALS['mapisession']->openMessageStore($messageStoreInfo[PR_STORE_ENTRYID]);
420
		$parentEntryid = $messageStoreInfo[PR_PARENT_ENTRYID];
421
422
		return ["message" => $message, "store" => $store, "parent_entryid" => $parentEntryid];
423
	}
424
425
	/**
426
	 * Function will create/update a yearly recurring appointment on the respective date of birthday or anniversary in user's calendar.
427
	 *
428
	 * @param object $store  MAPI Message Store Object
429
	 * @param array  $action the action data, sent by the client
430
	 * @param string $type   type of appointment that should be created/updated, valid values are 'birthday' and 'wedding_anniversary'
431
	 *
432
	 * @return HexString entryid of the newly created appointment in hex format
0 ignored issues
show
Bug introduced by
The type HexString was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
433
	 */
434
	public function updateAppointments($store, $action, $type) {
435
		$result = false;
436
437
		$root = mapi_msgstore_openentry($store);
438
		$rootProps = mapi_getprops($root, [PR_IPM_APPOINTMENT_ENTRYID, PR_STORE_ENTRYID]);
439
		$parentEntryId = bin2hex($rootProps[PR_IPM_APPOINTMENT_ENTRYID]);
440
		$storeEntryId = bin2hex($rootProps[PR_STORE_ENTRYID]);
441
442
		$actionProps = $action['props'];
443
		$subject = !empty($actionProps['subject']) ? $actionProps['subject'] : _('Untitled');
444
		$subject = ($type === 'birthday' ? sprintf(_('%s\'s Birthday'), $subject) : sprintf(_('%s\'s Anniversary'), $subject));
445
446
		// UTC time
447
		$startDateUTC = $actionProps[$type];
448
		$dueDateUTC = $actionProps[$type] + (24 * 60 * 60); // ONE DAY is added to set duedate of item.
449
450
		// get local time from UTC time
451
		$recur = new Recurrence($store, []);
0 ignored issues
show
Bug introduced by
The type Recurrence was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
452
		$startDate = $recur->fromGMT($actionProps, $startDateUTC);
453
		$dueDate = $recur->fromGMT($actionProps, $dueDateUTC);
454
455
		// Find the number of minutes since the start of the year to the given month,
456
		// taking leap years into account.
457
		$month = date('m', $startDate);
458
		$year = date('y', $startDate);
459
460
		$d1 = new DateTime();
461
		$d1->setDate($year, 1, 1);
0 ignored issues
show
Bug introduced by
$year of type string is incompatible with the type integer expected by parameter $year of DateTime::setDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

461
		$d1->setDate(/** @scrutinizer ignore-type */ $year, 1, 1);
Loading history...
462
		$d2 = new DateTime();
463
		$d2->setDate($year, $month, 1);
0 ignored issues
show
Bug introduced by
$month of type string is incompatible with the type integer expected by parameter $month of DateTime::setDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

463
		$d2->setDate($year, /** @scrutinizer ignore-type */ $month, 1);
Loading history...
464
465
		$diff = $d2->diff($d1);
466
		$month = $diff->days * 24 * 60;
467
468
		$props = [
469
			'message_class' => 'IPM.Appointment',
470
			'icon_index' => 1025,
471
			'busystatus' => fbFree,
472
			'meeting' => olNonMeeting,
473
			'object_type' => MAPI_MESSAGE,
474
			'message_flags' => MSGFLAG_READ | MSGFLAG_UNSENT,
475
			'subject' => $subject,
476
477
			'startdate' => $startDateUTC,
478
			'duedate' => $dueDateUTC,
479
			'commonstart' => $startDateUTC,
480
			'commonend' => $dueDateUTC,
481
			'alldayevent' => true,
482
			'duration' => 1440,
483
			'reminder' => true,
484
			'reminder_minutes' => 1080,
485
			'reminder_time' => $startDateUTC,
486
			'flagdueby' => $startDateUTC - (1080 * 60),
487
488
			'recurring' => true,
489
			'recurring_reset' => true,
490
			'startocc' => 0,
491
			'endocc' => 1440,
492
			'start' => $startDate,
493
			'end' => $dueDate,
494
			'term' => 35,
495
			'everyn' => 12,
496
			'subtype' => 2,
497
			'type' => 13,
498
			'regen' => 0,
499
			'month' => $month,
500
			'monthday' => date('j', $startDate),
501
			'timezone' => $actionProps['timezone'],
502
			'timezonedst' => $actionProps['timezonedst'],
503
			'dststartmonth' => $actionProps['dststartmonth'],
504
			'dststartweek' => $actionProps['dststartweek'],
505
			'dststartday' => $actionProps['dststartday'],
506
			'dststarthour' => $actionProps['dststarthour'],
507
			'dstendmonth' => $actionProps['dstendmonth'],
508
			'dstendweek' => $actionProps['dstendweek'],
509
			'dstendday' => $actionProps['dstendday'],
510
			'dstendhour' => $actionProps['dstendhour'],
511
		];
512
513
		$data = [];
514
		$data['store'] = $storeEntryId;
515
		$data['parententryid'] = $parentEntryId;
516
517
		$entryid = false;
518
		// if entryid is provided then update existing appointment, else create new one
519
		if ($type === 'birthday' && !empty($actionProps['birthday_eventid'])) {
520
			$entryid = $actionProps['birthday_eventid'];
521
		}
522
		elseif ($type === 'wedding_anniversary' && !empty($actionProps['anniversary_eventid'])) {
523
			$entryid = $actionProps['anniversary_eventid'];
524
		}
525
526
		if ($entryid !== false) {
527
			$data['entryid'] = $entryid;
528
		}
529
530
		if (isset($action['timezone_iana'])) {
531
			$props['timezone_iana'] = $action['timezone_iana'];
532
		}
533
534
		$data['props'] = $props;
535
536
		// Save appointment (saveAppointment takes care of creating/modifying exceptions to recurring
537
		// items if necessary)
538
		try {
539
			$messageProps = $GLOBALS['operations']->saveAppointment($store, hex2bin($entryid), hex2bin($parentEntryId), $data);
540
		}
541
		catch (MAPIException $e) {
542
			// if the appointment is deleted then create a new one
543
			if ($e->getCode() == MAPI_E_NOT_FOUND) {
544
				$e->setHandled();
545
				$messageProps = $GLOBALS['operations']->saveAppointment($store, false, hex2bin($parentEntryId), $data);
546
			}
547
		}
548
549
		// Notify the bus if the save was OK
550
		if ($messageProps && !(is_array($messageProps) && isset($messageProps['error']))) {
551
			$GLOBALS['bus']->notify($parentEntryId, TABLE_SAVE, $messageProps);
552
			$result = bin2hex($messageProps[PR_ENTRYID]);
553
		}
554
555
		return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type false|string which is incompatible with the documented return type HexString.
Loading history...
556
	}
557
558
	/**
559
	 * Function will delete the appointment on the respective date of birthday or anniversary in user's calendar.
560
	 *
561
	 * @param object $store   MAPI Message Store Object
562
	 * @param        $entryid of the message with will be deleted,sent by the client
0 ignored issues
show
Bug introduced by
The type of was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
563
	 */
564
	public function deleteSpecialDateAppointment($store, $entryid) {
565
		$root = mapi_msgstore_openentry($store);
566
		$rootProps = mapi_getprops($root, [PR_IPM_APPOINTMENT_ENTRYID, PR_STORE_ENTRYID]);
567
		$parentEntryId = $rootProps[PR_IPM_APPOINTMENT_ENTRYID];
568
		$storeEntryId = $rootProps[PR_STORE_ENTRYID];
569
570
		$props = [];
571
		$props[PR_PARENT_ENTRYID] = $parentEntryId;
572
		$props[PR_ENTRYID] = hex2bin($entryid);
573
		$props[PR_STORE_ENTRYID] = $storeEntryId;
574
575
		$result = $GLOBALS['operations']->deleteMessages($store, $parentEntryId, $props[PR_ENTRYID]);
576
577
		if ($result) {
578
			$GLOBALS['bus']->notify(bin2hex($parentEntryId), TABLE_DELETE, $props);
579
		}
580
	}
581
}
582