Skip to content

Commit

Permalink
Adding Contact creating and updating. Adding support for indexed URI'…
Browse files Browse the repository at this point in the history
…s. Adding examples for contacts. Updating changelog
  • Loading branch information
Garethp committed Apr 18, 2016
1 parent db1d867 commit 8cfad36
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* Removed `NTLMSoapClient\Exchange`. Folded the few lines of functionality in to `NTLMSoapClient`
* Added `API::getServerTimezones($timezoneIDs = array(), $fullTimezoneData = false)`
* Added `CalendarAPI::acceptMeeting($itemId, $message, $sensitivity = 'Private', $options = array()`
* Added `ContactsAPI::createContacts($contact, $options=array())`
* Added `ContactsAPI::updateContactItem($itemId, $changes)`
* Added some contact examples

## 0.7.4 - 2016-03-02
* Introduced the `API::deleteFolder($folderId)` method
Expand Down
3 changes: 3 additions & 0 deletions examples/contacts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ Please note that you can do much more if you query against the SOAP client direc
that have been made simpler

* [Get a list of contacts](listContacts.php)
* [Create a contact](createContacts.php)
* [Update a contact](updateContact.php)
* [Delete a contact](deleteContact.php)
13 changes: 13 additions & 0 deletions examples/contacts/createContacts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use jamesiarmes\PEWS\Contacts\ContactsAPI as API;

$api = API::withUsernameAndPassword('server', 'username', 'password');

$api->createContacts(array (
'GivenName' => 'John',
'Surname' => 'Smith',
'EmailAddresses' => array(
'Entry' => array('Key' => 'EmailAddress1', '_value' => '[email protected]')
)
));
9 changes: 9 additions & 0 deletions examples/contacts/deleteContact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use jamesiarmes\PEWS\Contacts\ContactsAPI as API;

$api = API::withUsernameAndPassword('server', 'username', 'password');

$contact = $api->getContacts();

$api->deleteItems($contact[0]->getItemId());
23 changes: 23 additions & 0 deletions examples/contacts/updateContact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use jamesiarmes\PEWS\Contacts\ContactsAPI as API;

$api = API::withUsernameAndPassword('server', 'username', 'password');

$contact = $api->getContacts();

//EmailAddress is a bit different from most field updates. It's a what's known as an "Indexed Field URI", which means
//that each email address has an "index". This is the "Key" that you used when creating this EmailAddress. So in order
//to update it, we need to tell EWS what the key of the item we want to update is. For this, we use "EmailAddress:key".

//However, we know that we want to update an email address, but the actual field we need to update is "EmailAddresses",
//hence why instead of a simple $key => $value array, we have this multi-dimensional array. They value of
//"EmailAddress:key" need to have the same structure as when we created the value
$api->updateContactItem($contact[0]->getItemId(), array(
'GivenName' => 'Jane',
'EmailAddress:EmailAddress1' => array (
'EmailAddresses' => array (
'Entry' => array('Key' => 'EmailAddress1', '_value' => '[email protected]')
)
)
));
75 changes: 66 additions & 9 deletions src/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace jamesiarmes\PEWS;

use jamesiarmes\PEWS\API\Enumeration\DictionaryURIType;
use jamesiarmes\PEWS\API\Enumeration\UnindexedFieldURIType;
use jamesiarmes\PEWS\API\ExchangeWebServices;
use jamesiarmes\PEWS\API\Message\GetServerTimeZonesType;
use jamesiarmes\PEWS\API\Message\SyncFolderItemsResponseMessageType;
Expand Down Expand Up @@ -36,18 +38,23 @@ public function getPrimarySmtpMailbox()
return $this->getClient()->getPrimarySmtpMailbox();
}

private $fieldUris = array();
private $unIndexedFieldUris = array();
private $dictionaryFieldUris = array();

/**
* Storing the API client
* @var ExchangeWebServices
*/
private $client;

public function setupFieldUris()
/**
* @param $className
* @return array
*/
public function getFieldUrisFromClass($className)
{
//So, since we have to pass in URI's of everything we update, we need to fetch them
$reflection = new \ReflectionClass('jamesiarmes\PEWS\API\Enumeration\UnindexedFieldURIType');
$reflection = new \ReflectionClass($className);
$constants = $reflection->getConstants();
$constantsFound = array();

Expand All @@ -67,31 +74,64 @@ public function setupFieldUris()
$constantsFound[$name][$category] = $constant;
}

$this->fieldUris = $constantsFound;
return $constantsFound;
}

public function setupFieldUris()
{
$this->unIndexedFieldUris = $this
->getFieldUrisFromClass(UnindexedFieldURIType::class);

$this->dictionaryFieldUris = $this
->getFieldUrisFromClass(DictionaryURIType::class);
}

public function getFieldUriByName($fieldName, $preference = 'item')
{
$fieldName = strtolower($fieldName);
$preference = strtolower($preference);

if (empty($this->fieldUris)) {
if (empty($this->unIndexedFieldUris)) {
$this->setupFieldUris();
}

if (!isset($this->unIndexedFieldUris[$fieldName])) {
return false;
}

if (!isset($this->unIndexedFieldUris[$fieldName][$preference])) {
$preference = 'item';
}

if (!isset($this->unIndexedFieldUris[$fieldName][$preference])) {
throw new \Exception("Could not find uri $preference:$fieldName");
}

return $this->unIndexedFieldUris[$fieldName][$preference];
}

public function getIndexedFieldUriByName($fieldName, $preference = 'item')
{
$fieldName = strtolower($fieldName);
$preference = strtolower($preference);

if (empty($this->dictionaryFieldUris)) {
$this->setupFieldUris();
}

if (!isset($this->fieldUris[$fieldName])) {
if (!isset($this->dictionaryFieldUris[$fieldName])) {
return false;
}

if (!isset($this->fieldUris[$fieldName][$preference])) {
if (!isset($this->dictionaryFieldUris[$fieldName][$preference])) {
$preference = 'item';
}

if (!isset($this->fieldUris[$fieldName][$preference])) {
if (!isset($this->dictionaryFieldUris[$fieldName][$preference])) {
throw new \Exception("Could not find uri $preference:$fieldName");
}

return $this->fieldUris[$fieldName][$preference];
return $this->dictionaryFieldUris[$fieldName][$preference];
}

/**
Expand Down Expand Up @@ -249,6 +289,23 @@ protected function buildUpdateItemChanges($itemType, $uriType, $changes)

//Add each property to a setItemField
foreach ($changes as $key => $value) {
if (strpos($key, ':') !== false) {
try {
$fieldUri = $this->getIndexedFieldUriByName(substr($key, 0, strpos($key, ':')), $uriType);

list ($key, $index) = explode(':', $key);
$fieldKey = key($value);
$value = $value[$fieldKey];

$setItemFields[] = array(
'IndexedFieldURI' => array('FieldURI' => $fieldUri, 'FieldIndex' => $index),
$itemType => array($fieldKey => $value)
);
continue;
} catch (\Exception $e) {
}
}

$fullName = $this->getFieldUriByName($key, $uriType);

$setItemFields[] = array(
Expand Down
50 changes: 49 additions & 1 deletion src/Contacts/ContactsAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function setFolderId($folderId)
/**
* @param Type\FolderIdType $folderId
* @param array $options
* @return Type
* @return Type\ContactItemType[]
*/
public function getContacts($folderId = null, $options = array())
{
Expand All @@ -56,6 +56,54 @@ public function getContacts($folderId = null, $options = array())
$request = array_replace_recursive($request, $options);

$request = Type::buildFromArray($request);

return $this->getClient()->FindItem($request);
}

/**
* @param $contacts
* @param array $options
* @return Type\ItemIdType[]
*/
public function createContacts($contacts, $options = array())
{
$request = array('Contact' => $contacts);

$defaultOptions = array(
'MessageDisposition' => 'SaveOnly',
'SavedItemFolderId' => array('FolderId' => $this->getFolderId()->toArray())
);
$options = array_replace_recursive($defaultOptions, $options);

$result = $this->createItems($request, $options);

if (!is_array($result)) {
$result = array($result);
}

return $result;
}

public function updateContactItem(Type\ItemIdType $itemId, $changes)
{
//Create the request
$request = array(
'ItemChange' => array(
'ItemId' => $itemId->toArray(),
'Updates' => array(
'SetItemField' => $this->buildUpdateItemChanges('Contact', 'contacts', $changes)
)
)
);

$options = array();

$items = $this->updateItems($request, $options);

if (!is_array($items)) {
$items = array($items);
}

return $items;
}
}

0 comments on commit 8cfad36

Please sign in to comment.