<?php
	/**
	 * Contact ItemModule
	 * Module which openes, creates, saves and deletes an item. It 
	 * extends the Module class.
	 */
	class ContactItemModule extends ItemModule
	{

		var $plaintext;

		/**
		 * Constructor
		 * @param int $id unique id.
		 * @param array $data list of all actions.
		 */
		function ContactItemModule($id, $data)
		{
			$this->properties = $GLOBALS['properties']->getContactProperties();
			
			$this->plaintext = true;

			parent::ItemModule($id, $data);
		}
		
		/**
		 * Function which saves an item. It sets the right properties for a contact
		 * item (address book properties).		 
		 * @param object $store MAPI Message Store Object
		 * @param string $parententryid parent entryid of the message
		 * @param string $entryid entryid of the message
		 * @param array $action the action data, sent by the client
		 * @return boolean true on success or false on failure		 		 
		 */		 		
		function save($store, $parententryid, $entryid, $action)
		{
			$properiesToDelete = array();		// create an array of properties which should be deleted 
												// this array is passed to $GLOBALS['operations']->saveMessage() function

			if(!$store && !$parententryid) {
				if(isset($action['props']['message_class'])) {
					$store = $GLOBALS['mapisession']->getDefaultMessageStore();
					$parententryid = $this->getDefaultFolderEntryID($store, $action['props']['message_class']);
				}
			} else if(!$parententryid) {
				if(isset($action['props']['message_class']))
					$parententryid = $this->getDefaultFolderEntryID($store, $action['props']['message_class']);
			}

			if($store && $parententryid && isset($action['props'])) {
				if(isset($action['members'])) {
					// DistList

					// for distlist we need to use different set of properties
					$this->properties = $GLOBALS['properties']->getDistlistProperties();

					// do conversion of client data
					$props = Conversion::mapXML2MAPI($this->properties, $action['props']);

					// collect members
					$members = array();
					$oneoff_members = array();

					$items = $action['members'];

					foreach($items as $item) {
						if(empty($item['email_address'])) {
							// if no email address is given then mapi_parseoneoff fails, so always give
							// email address, OL07 uses Unknown as email address so we do same here
							$item['email_address'] = 'Unknown';
						}

						$oneoff = mapi_createoneoff($item['display_name'], $item['address_type'], $item['email_address']);

						if ($item['distlist_type'] == DL_EXTERNAL_MEMBER) {
							$member = $oneoff;
						} else {
							$parts = array();
							$parts['distlist_guid'] = DL_GUID;
							$parts['distlist_type'] = $item['distlist_type'];
							$parts['entryid'] = hex2bin($item['entryid']);
							$member = pack('VA16CA*', 0, $parts['distlist_guid'], $parts['distlist_type'], $parts['entryid']);
						}

						$oneoff_members[] = $oneoff;
						$members[] = $member;
					}

					if (!empty($members) && !empty($oneoff_members)) {
						$props[$this->properties['members']] = $members;
						$props[$this->properties['oneoff_members']] = $oneoff_members;
					} else {
						$properiesToDelete[] = $this->properties['members'];
						$properiesToDelete[] = $this->properties['oneoff_members'];
					}

					unset($action['members']);
				} else {
					// Contact

					// generate one-off entryids for email addresses
					for($index = 1; $index < 4; $index++)
					{
						if(!empty($action['props']['email_address_' . $index]) && !empty($action['props']['email_address_display_name_' . $index])) {
							$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]));
						}
					}

					// set properties for primary fax number
					if(isset($action['props']['fax_1_email_address']) && !empty($action['props']['fax_1_email_address'])) {
						$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));
					} else {
						// delete properties to remove previous values
						$properiesToDelete[] = $this->properties['fax_1_address_type'];
						$properiesToDelete[] = $this->properties['fax_1_original_display_name'];
						$properiesToDelete[] = $this->properties['fax_1_email_address'];
						$properiesToDelete[] = $this->properties['fax_1_original_entryid'];
					}

					// set properties for business fax number
					if(isset($action['props']['fax_2_email_address']) && !empty($action['props']['fax_2_email_address'])) {
						$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));
					} else {
						$properiesToDelete[] = $this->properties['fax_2_address_type'];
						$properiesToDelete[] = $this->properties['fax_2_original_display_name'];
						$properiesToDelete[] = $this->properties['fax_2_email_address'];
						$properiesToDelete[] = $this->properties['fax_2_original_entryid'];
					}

					// set properties for home fax number
					if(isset($action['props']['fax_3_email_address']) && !empty($action['props']['fax_3_email_address'])) {
						$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));
					} else {
						$properiesToDelete[] = $this->properties['fax_3_address_type'];
						$properiesToDelete[] = $this->properties['fax_3_original_display_name'];
						$properiesToDelete[] = $this->properties['fax_3_email_address'];
						$properiesToDelete[] = $this->properties['fax_3_original_entryid'];
					}

					// check for properties which should be deleted
					if(isset($action['entryid']) && !empty($action['entryid'])) {
						// check for empty email address properties
						for($i = 1; $i < 4; $i++)
						{
							if(isset($action['props']['email_address_' . $i]) && empty($action['props']['email_address_' . $i])) {
								array_push($properiesToDelete, $this->properties['email_address_entryid_' . $i]);
								array_push($properiesToDelete, $this->properties['email_address_' . $i]);
								array_push($properiesToDelete, $this->properties['email_address_display_name_' . $i]);
								array_push($properiesToDelete, $this->properties['email_address_display_name_email_' . $i]);
								array_push($properiesToDelete, $this->properties['email_address_type_' . $i]);
							}
						}

						// check for empty address_book_mv and address_book_long properties
						if(isset($action['props']['address_book_long']) && $action['props']['address_book_long'] === 0) {
							$properiesToDelete[] = $this->properties['address_book_mv'];
							$properiesToDelete[] = $this->properties['address_book_long'];
						}

						// Check if the birthday and anniversary properties are empty. If so delete them.
						if(array_key_exists('birthday', $action['props']) && empty($action['props']['birthday'])){
							array_push($properiesToDelete, $this->properties['birthday']);
							array_push($properiesToDelete, $this->properties['birthday_eventid']);
							if(!empty($action['props']['birthday_eventid'])) {
								$this->deleteSpecialDateAppointment($store, $action['props']['birthday_eventid']);
							}
						}

						if(array_key_exists('wedding_anniversary', $action['props']) && empty($action['props']['wedding_anniversary'])){
							array_push($properiesToDelete, $this->properties['wedding_anniversary']);
							array_push($properiesToDelete, $this->properties['anniversary_eventid']);
							if(!empty($action['props']['anniversary_eventid'])) {
								$this->deleteSpecialDateAppointment($store, $action['props']['anniversary_eventid']);
							}
						}
					}

					/** 
					 * convert all line endings(LF) into CRLF
					 * XML parser will normalize all CR, LF and CRLF into LF
					 * but outlook(windows) uses CRLF as line ending
					 */
					if(isset($action['props']['business_address'])) {
						$action['props']['business_address'] = str_replace('\n', '\r\n', $action['props']['business_address']);
					}

					if(isset($action['props']['home_address'])) {
						$action['props']['home_address'] = str_replace('\n', '\r\n', $action['props']['home_address']);
					}

					if(isset($action['props']['other_address'])) {
						$action['props']['other_address'] = str_replace('\n', '\r\n', $action['props']['other_address']);
					}

					// check birthday props to make an appointment
					if(!empty($action['props']['birthday'])){
						$action['props']['birthday_eventid'] = $this->updateAppointments($store, $action, 'birthday');
					}

					// check anniversary props to make an appointment
					if(!empty($action['props']['wedding_anniversary'])){
						$action['props']['anniversary_eventid'] = $this->updateAppointments($store, $action, 'wedding_anniversary');
					}

					// do the conversion when all processing has been finished
					$props = Conversion::mapXML2MAPI($this->properties, $action['props']);
				}

				$messageProps = array();

				$result = $GLOBALS['operations']->saveMessage($store, $entryid, $parententryid, $props, $messageProps, array(), isset($action['attachments']) ? $action['attachments'] : array(), $properiesToDelete);

				if($result) {
					$GLOBALS['bus']->notify(bin2hex($parententryid), TABLE_SAVE, $messageProps);

					$this->addActionData('update', array('item' => Conversion::mapMAPI2XML($this->properties, $messageProps)));
					$GLOBALS['bus']->addData($this->getResponseData());
				}
			}
		}

		/**
		 * Function will create/update a yearly recurring appointment on the respective date of birthday or anniversary in user's calendar.	 
		 * @param object $store MAPI Message Store Object
		 * @param array $action the action data, sent by the client
		 * @param string $type type of appointment that should be created/updated, valid values are 'birthday' and 'wedding_anniversary'.
		 * @return HexString entryid of the newly created appointment in hex format.
		 */	
		function updateAppointments($store, $action, $type)
		{
			$result = false;

			$inbox = mapi_msgstore_getreceivefolder($store);
			if(mapi_last_hresult() == NOERROR) {
				$inboxProps = mapi_getprops($inbox, Array(PR_IPM_APPOINTMENT_ENTRYID, PR_STORE_ENTRYID));
				$parentEntryId = bin2hex($inboxProps[PR_IPM_APPOINTMENT_ENTRYID]);
				$storeEntryId = bin2hex($inboxProps[PR_STORE_ENTRYID]);
			}

			$actionProps = $action['props'];
			$subject = ($type === 'birthday' ? sprintf(_('%s\'s Birthday'), $actionProps['subject']) : sprintf(_('%s\'s Anniversary'), $actionProps['subject']));

			$startDate = $actionProps[$type];
			$dueDate = $actionProps[$type] + (24 * 60 * 60);	// ONE DAY is added to set duedate of item.

			// here the start of appointment is always set in localtime, so the unixtime is added with timezone value. 
			// (-1) is used for the correct direction ie. as we move eastward from GMT the timezone value is in positive and vicaversa when
			// moving westward.
			$startDateUTC = $startDate + ($actionProps['timezone'] * 60 * (-1));
			$dueDateUTC = $dueDate + ($actionProps['timezone'] * 60 * (-1));

			// Find the number of minute since the starting of the year to the given month
			// This is hardcoded here, but should also consider leap years
			switch(strftime('%m', $startDateUTC)){
				case 1:	$month = '0'; break;
				case 2:	$month = '44640'; break;	// 31 (no of days in jan) * 24 (hours in a day) * 60 (minutes in an hour)
				case 3:	$month = '84960'; break;	// 44640 (value of previous month) + 28 (no of days in feb) * 24 (hours in a day) * 60 (minutes in an hour)
				case 4:	$month = '129600'; break;
				case 5:	$month = '172800'; break;
				case 6:	$month = '217440'; break;
				case 7:	$month = '260640'; break;
				case 8:	$month = '305280'; break;
				case 9:	$month = '348480'; break;
				case 10: $month = '393120'; break;
				case 11: $month = '437760'; break;
				case 12: $month = '480960'; break;
			}

			$props = array (
				'message_class' => 'IPM.Appointment',
				'icon_index' => 1025,
				'busystatus' => fbFree,
				'meeting' => olNonMeeting,
				'object_type' => MAPI_MESSAGE,
				'message_flags' => MSGFLAG_READ | MSGFLAG_UNSENT,
				'subject' => $subject,

				'startdate' => $startDate,				
				'duedate' => $dueDate,
				'commonstart' => $startDate,
				'commonend' => $dueDate,
				'alldayevent' => true,
				'duration' => 1440,
				'reminder' => true, 
				'reminder_minutes' => 1080,
				'reminder_time' => $startDate,
				'flagdueby' => $startDate - (1080 * 60),

				'recurring' => true,
				'recurring_reset' => true,
				'startocc' => 0,
				'endocc' => 1440,
				'start' => $startDateUTC,
				'end' => $dueDateUTC,
				'term' => 35,
				'everyn' => 12,
				'subtype' => 2,
				'type' => 13,
				'regen' => 0,
				'month' => $month,
				'monthday' => strftime('%e', $startDateUTC),
				'timezone' => $actionProps['timezone'],
				'timezonedst' => $actionProps['timezonedst'],
				'dststartmonth' => $actionProps['dststartmonth'],
				'dststartweek' => $actionProps['dststartweek'],
				'dststartday' => $actionProps['dststartday'],
				'dststarthour' => $actionProps['dststarthour'],
				'dstendmonth' => $actionProps['dstendmonth'],
				'dstendweek' => $actionProps['dstendweek'],
				'dstendday' => $actionProps['dstendday'],
				'dstendhour' => $actionProps['dstendhour']
			);

			$data = array();
			$data['store'] = $storeEntryId;
			$data['parententryid'] = $parentEntryId;

			$entryid = false;
			// if entryid is provided then update existing appointment, else create new one
			if($type === 'birthday' && !empty($actionProps['birthday_eventid'])) {
				$entryid = $actionProps['birthday_eventid'];
			} else if($type === 'wedding_anniversary' && !empty($actionProps['anniversary_eventid'])) {
				$entryid = $actionProps['anniversary_eventid'];
			}

			if($entryid !== false) {
				$data['entryid'] = $entryid;
			}

			$data['props'] = $props;

			// Save appointment (saveAppointment takes care of creating/modifying exceptions to recurring
			// items if necessary)
			try {
				$messageProps = $GLOBALS['operations']->saveAppointment($store, hex2bin($entryid), hex2bin($parentEntryId), $data);
			} catch (MAPIException $e) {
				// if the appointment is deleted then create a new one
				if($e->getCode() == MAPI_E_NOT_FOUND) {
					$e->setHandled();
					$messageProps = $GLOBALS['operations']->saveAppointment($store, false, hex2bin($parentEntryId), $data);
				}
			}

			// Notify the bus if the save was OK
			if($messageProps && !(is_array($messageProps) && isset($messageProps['error'])) ){
				$GLOBALS['bus']->notify($parentEntryId, TABLE_SAVE, $messageProps);
				$result = bin2hex($messageProps[PR_ENTRYID]);
			}

			return $result;
		}
		
		/**
		 * Function will delete the appointment on the respective date of birthday or anniversary in user's calendar.	 
		 * @param object $store MAPI Message Store Object
		 * @param $entryid of the message with will be deleted,sent by the client
		 */	
		function deleteSpecialDateAppointment($store, $entryid)
		{
			$inbox = mapi_msgstore_getreceivefolder($store);
			if(mapi_last_hresult() == NOERROR) {
				$inboxProps = mapi_getprops($inbox, Array(PR_IPM_APPOINTMENT_ENTRYID, PR_STORE_ENTRYID));
				$parentEntryId = $inboxProps[PR_IPM_APPOINTMENT_ENTRYID];
				$storeEntryId = $inboxProps[PR_STORE_ENTRYID];
			}

			$props = array();
			$props[PR_PARENT_ENTRYID] = $parentEntryId;
			$props[PR_ENTRYID] = hex2bin($entryid);
			$props[PR_STORE_ENTRYID] = $storeEntryId;
			
			$result = $GLOBALS['operations']->deleteMessages($store, $parentEntryId, $props[PR_ENTRYID]);
			
			if($result) {
				$GLOBALS['bus']->notify(bin2hex($parentEntryId), TABLE_DELETE, $props);
			}
		}	
	}
?>