Skip to content

Commit

Permalink
Adding ability to delete item fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Garethp committed May 6, 2016
1 parent da9af7f commit c2f8577
Show file tree
Hide file tree
Showing 18 changed files with 201 additions and 41 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## 0.8.2 - 2016-05-06
* Add the ability to delete fields when updating an item

## 0.8.1 - 2016-05-02
* Fixing a segmentation fault

## 0.8.0 - 2016-05-02
* Changes the namespace of this package from `jamesiarmes\PEWS` to `garethp\ews`

## 0.7.9
* Add the ability to delete fields when updating an item

## 0.7.8 - 2016-05-02
* Fixing a segmentation fault

Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"statusCode":200,"headers":{"Cache-Control":["private"],"Transfer-Encoding":["chunked"],"Content-Type":["text\/xml; charset=utf-8"],"Server":["Microsoft-IIS\/7.5"],"Set-Cookie":["exchangecookie=14801a464dee4487940d347916088856; expires=Tue, 28-Feb-2017 10:42:26 GMT; path=\/; HttpOnly"],"X-AspNet-Version":["2.0.50727"],"Persistent-Auth":["true"],"X-Powered-By":["ASP.NET"],"Date":["Mon, 29 Feb 2016 10:42:26 GMT"]},"body":"<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\"><s:Header><h:ServerVersionInfo MajorVersion=\"14\" MinorVersion=\"3\" MajorBuildNumber=\"195\" MinorBuildNumber=\"1\" Version=\"Exchange2010_SP2\" xmlns:h=\"http:\/\/schemas.microsoft.com\/exchange\/services\/2006\/types\" xmlns=\"http:\/\/schemas.microsoft.com\/exchange\/services\/2006\/types\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\"\/><\/s:Header><s:Body xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\"><m:GetFolderResponse xmlns:m=\"http:\/\/schemas.microsoft.com\/exchange\/services\/2006\/messages\" xmlns:t=\"http:\/\/schemas.microsoft.com\/exchange\/services\/2006\/types\"><m:ResponseMessages><m:GetFolderResponseMessage ResponseClass=\"Success\"><m:ResponseCode>NoError<\/m:ResponseCode><m:Folders><t:CalendarFolder><t:FolderId Id=\"AQMkADhiZjhlMDZjLTc0MDItNDI5OQAtOTcyMC02M2U2NDZhZmI3MTMALgAAA6i8KDve4qpIkQ6MdHAyIZ4BAPkqvLM2ftlLgyCv9ALxwvYAAAIBDQAAAA==\" ChangeKey=\"AgAAABYAAAD5KryzNn7ZS4Mgr\/QC8cL2AABpp01c\"\/><t:DisplayName>Calendar<\/t:DisplayName><t:ChildFolderCount>1<\/t:ChildFolderCount><\/t:CalendarFolder><\/m:Folders><\/m:GetFolderResponseMessage><\/m:ResponseMessages><\/m:GetFolderResponse><\/s:Body><\/s:Envelope>"}]
[{"statusCode":200,"headers":{"Cache-Control":["private"],"Transfer-Encoding":["chunked"],"Content-Type":["text\/xml; charset=utf-8"],"Server":["Microsoft-IIS\/8.0"],"request-id":["d2e6ea1b-4f3d-49ae-b9bc-965d13ebe851"],"X-CalculatedBETarget":["HE1PR0401MB1884.eurprd04.prod.outlook.com"],"X-BackEndHttpStatus":["200"],"Set-Cookie":["exchangecookie=0594fa42c17244f28526afecc478824d; expires=Sat, 06-May-2017 10:15:05 GMT; path=\/; HttpOnly"],"x-EwsHandler":["GetFolder"],"X-AspNet-Version":["4.0.30319"],"X-DiagInfo":["HE1PR0401MB1884"],"X-BEServer":["HE1PR0401MB1884"],"X-Powered-By":["ASP.NET"],"X-FEServer":["AM2PR07CA0032"],"Date":["Fri, 06 May 2016 10:15:05 GMT"]},"body":"<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\"><s:Header><h:ServerVersionInfo MajorVersion=\"15\" MinorVersion=\"1\" MajorBuildNumber=\"485\" MinorBuildNumber=\"14\" Version=\"V2016_01_06\" xmlns:h=\"http:\/\/schemas.microsoft.com\/exchange\/services\/2006\/types\" xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\/><\/s:Header><s:Body><m:GetFolderResponse xmlns:m=\"http:\/\/schemas.microsoft.com\/exchange\/services\/2006\/messages\" xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xmlns:t=\"http:\/\/schemas.microsoft.com\/exchange\/services\/2006\/types\"><m:ResponseMessages><m:GetFolderResponseMessage ResponseClass=\"Success\"><m:ResponseCode>NoError<\/m:ResponseCode><m:Folders><t:CalendarFolder><t:FolderId Id=\"AAMkAGRmOGJjZTc0LWQ0ZWUtNDliZC1hYjQ1LTkyODM1NjFhMGIzNwAuAAAAAAAdsEfFUCwSRaf4+E1agVdfAQA4JiVRwOF7SbQybW1zR3q0AAAAAAENAAA=\" ChangeKey=\"AgAAABYAAAA4JiVRwOF7SbQybW1zR3q0AAAAAAA3\"\/><t:DisplayName>Calendar<\/t:DisplayName><t:TotalCount>2<\/t:TotalCount><t:ChildFolderCount>3<\/t:ChildFolderCount><\/t:CalendarFolder><\/m:Folders><\/m:GetFolderResponseMessage><\/m:ResponseMessages><\/m:GetFolderResponse><\/s:Body><\/s:Envelope>"}]

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# To Do List

### Contact Item Type
* Make EmailAddresses and PhoneNumbers return an associative array of values

### EWSAutoDiscover
* Fix this shit up some day
* Unit test what's fixed
Expand All @@ -9,10 +12,11 @@

### All Types
* Add a dirty/clean state to objects, so you can do `$api->updateItem($items)` and have it know what to update
* Make IndexedFieldURI's return an associative array of values

### Test Coverage
* Increase to at least 90%

### Code Generation
* Modify generator to include the SOAP functions as Doc Blocks on ExchangeWebServices class

Expand Down
14 changes: 14 additions & 0 deletions examples/contacts/deleteContactField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use garethp\ews\Contacts\ContactsAPI as API;

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

$contact = $api->getContacts();

$api->updateContactItem($contact[0]->getItemId(), array(
'deleteFields' => array (
'GivenName',
'PhoneNumber:HomePhone'
)
));
56 changes: 35 additions & 21 deletions src/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,38 +284,52 @@ public function updateItems($items, $options = array())
return $this->getClient()->UpdateItem($request)->getItems();
}

protected function getFieldURI($uriType, $key = null, $value = null)
{
if (strpos($key, ':') !== false) {
try {
$fieldUri = $this->getIndexedFieldUriByName(substr($key, 0, strpos($key, ':')), $uriType);

list ($key, $index) = explode(':', $key);

if (is_array($value)) {
$key = key($value);
$value = $value[$key];
}

return ['IndexedFieldURI', ['FieldURI' => $fieldUri, 'FieldIndex' => $index], $key, $value];
} catch (\Exception $e) {
}
}

$fullName = $this->getFieldUriByName($key, $uriType);
return ['FieldURI', ['FieldURI' => $fullName], $key, $value];
}

protected function buildUpdateItemChanges($itemType, $uriType, $changes)
{
$setItemFields = array();
$deleteItemFields = array();

//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) {
}
if (isset($changes['deleteFields'])) {
foreach ($changes['deleteFields'] as $key) {
list($fieldUriType, $fieldKey) = $this->getFieldURI($uriType, $key);
$deleteItemFields[] = [$fieldUriType => $fieldKey];
}

$fullName = $this->getFieldUriByName($key, $uriType);
unset($changes['deleteFields']);
}

//Add each property to a setItemField
foreach ($changes as $key => $value) {
list ($fieldUriType, $fieldKey, $valueKey, $value) = $this->getFieldURI($uriType, $key, $value);
$setItemFields[] = array(
'FieldURI' => array('FieldURI' => $fullName),
$itemType => array($key => $value)
$fieldUriType => $fieldKey,
$itemType => [$valueKey => $value]
);
}

return $setItemFields;
return array('SetItemField' => $setItemFields, 'DeleteItemField' => $deleteItemFields);
}

public function createFolders($names, Type\FolderIdType $parentFolder, $options = array())
Expand Down
1 change: 0 additions & 1 deletion src/API/Type/ArrayOfFoldersType.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public function getAllFolders()
$folders,
(is_array($this->tasksFolder) ? $this->tasksFolder : array($this->tasksFolder))
);
$folders = array_merge($folders, $this->tasksFolder);
}

$this->allFolders = $folders;
Expand Down
4 changes: 1 addition & 3 deletions src/Calendar/CalendarAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,7 @@ public function updateCalendarItem(Type\ItemIdType $itemId, $changes)
$request = array(
'ItemChange' => array(
'ItemId' => $itemId->toArray(),
'Updates' => array(
'SetItemField' => $this->buildUpdateItemChanges('CalendarItem', 'calendar', $changes)
)
'Updates' => $this->buildUpdateItemChanges('CalendarItem', 'calendar', $changes)
)
);

Expand Down
13 changes: 10 additions & 3 deletions src/Contacts/ContactsAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public function getContacts($folderId = null, $options = array())
return $this->getClient()->FindItem($request);
}

/**
* @param Type\ItemIdType $itemId
* @return Type\ContactItemType
*/
public function getContact($itemId)
{
return $this->getItem($itemId);
}

/**
* @param $contacts
* @param array $options
Expand Down Expand Up @@ -90,9 +99,7 @@ public function updateContactItem(Type\ItemIdType $itemId, $changes)
$request = array(
'ItemChange' => array(
'ItemId' => $itemId->toArray(),
'Updates' => array(
'SetItemField' => $this->buildUpdateItemChanges('Contact', 'contacts', $changes)
)
'Updates' => $this->buildUpdateItemChanges('Contact', 'contacts', $changes)
)
);

Expand Down
4 changes: 1 addition & 3 deletions src/Mail/MailAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ public function updateMailItem($itemId, $changes)
$request = array(
'ItemChange' => array(
'ItemId' => $itemId->toArray(),
'Updates' => array(
'SetItemField' => $this->buildUpdateItemChanges('Message', 'message', $changes)
)
'Updates' => $this->buildUpdateItemChanges('Message', 'message', $changes)
)
);

Expand Down
114 changes: 114 additions & 0 deletions tests/src/Contacts/ContactsAPI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace garethp\ews\Test\Contacts;

use garethp\ews\API\Type\ContactItemType;
use garethp\ews\API\Type\ItemIdType;
use PHPUnit_Framework_TestCase;
use garethp\ews\Contacts\ContactsAPI as API;

class ContactsAPI extends PHPUnit_Framework_TestCase
{
/**
* @return \garethp\ews\Contacts\ContactsAPI
*/
public function getClient()
{
$mode = getenv('HttpPlayback');
if ($mode == false) {
$mode = 'playback';
}

$auth = [
'server' => 'server',
'user' => 'user',
'password' => 'password'
];

if (is_file(getcwd() . '/Resources/auth.json')) {
$auth = json_decode(file_get_contents(getcwd() . '/Resources/auth.json'), true);
}

$client = API::withUsernameAndPassword(
$auth['server'],
$auth['user'],
$auth['password'],
[
'httpPlayback' => [
'mode' => $mode,
'recordFileName' => self::class . '.' . $this->getName() . '.json'
]
]
);

$testFolder = $client->getFolderByDisplayName('Test', $client->getFolderId());
$client->setFolderId($testFolder->getFolderId());

return $client;
}

public function testCreateContact()
{
$api = $this->getClient();

$contact = $api->createContacts(array (
'GivenName' => 'John',
'Surname' => 'Smith',
'EmailAddresses' => array(
'Entry' => array('Key' => 'EmailAddress1', '_value' => 'john.smith@gmail.com')
),
'PhoneNumbers' => array(
'Entry' => array('Key' => 'HomePhone', '_value' => '000')
)
));

$this->assertArrayHasKey(0, $contact);

$contact = $contact[0];
$this->assertInstanceOf(ItemIdType::class, $contact);

$contact = $api->getContact($contact);
$this->assertNotNull($contact);
$this->assertEquals('John', $contact->getGivenName());

$api->deleteItems($contact->getItemId());
}

public function testUpdateContact()
{
$api = $this->getClient();

$contact = $api->createContacts(array (
'GivenName' => 'John',
'Surname' => 'Smith',
'EmailAddresses' => array(
'Entry' => array('Key' => 'EmailAddress1', '_value' => 'john.smith@gmail.com')
),
'PhoneNumbers' => array(
'Entry' => array('Key' => 'HomePhone', '_value' => '000')
)
));

$contact = $contact[0];
$api->updateContactItem($contact, array(
'GivenName' => 'Jane',
'EmailAddress:EmailAddress1' => array (
'EmailAddresses' => array (
'Entry' => array('Key' => 'EmailAddress1', '_value' => 'jane.smith@gmail.com')
)
),
'PhoneNumber:HomePhone' => array (
'PhoneNumbers' => array (
'Entry' => array('Key' => 'HomePhone', '_value' => '111')
)
)
));

$contact = $api->getContact($contact);
$this->assertEquals('Jane', $contact->getGivenName());
$this->assertEquals('jane.smith@gmail.com', $contact->getEmailAddresses()->Entry);
$this->assertEquals('111', $contact->getPhoneNumbers()->Entry);

$api->deleteItems($contact->getItemId());
}
}

0 comments on commit c2f8577

Please sign in to comment.