1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace garethp\ews\API; |
4
|
|
|
|
5
|
|
|
class ItemUpdateBuilder |
6
|
|
|
{ |
7
|
3 |
|
public static function buildUpdateItemChanges($itemType, $uriType, $changes) |
8
|
|
|
{ |
9
|
3 |
|
$updateArray = []; |
10
|
|
|
|
11
|
3 |
|
if (isset($changes['deleteFields'])) { |
12
|
|
|
$updateArray['DeleteItemField'] = self::buildUpdateDeleteItemFields($uriType, $changes['deleteFields']); |
13
|
|
|
unset($changes['deleteFields']); |
14
|
|
|
} |
15
|
|
|
|
16
|
3 |
|
$updateArray['SetItemField'] = self::buildUpdateSetItemFields($itemType, $uriType, $changes); |
17
|
|
|
|
18
|
3 |
|
return $updateArray; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected static function buildUpdateDeleteItemFields($uriType, $changes) |
22
|
|
|
{ |
23
|
|
|
$deleteItemFields = []; |
24
|
|
|
foreach ($changes as $key) { |
25
|
|
|
if (strpos($key, 'PhysicalAddress:') === 0 && $uriType == "contacts") { |
26
|
|
|
$deleteItemFields = |
27
|
|
|
self::deleteContactPhysicalAddressField($key, $deleteItemFields); |
28
|
|
|
} else { |
29
|
|
|
list($fieldUriType, $fieldKey) = self::getFieldURI($uriType, $key); |
30
|
|
|
$deleteItemFields[] = [$fieldUriType => $fieldKey]; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $deleteItemFields; |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
protected static function buildUpdateSetItemFields($itemType, $uriType, $changes) |
38
|
|
|
{ |
39
|
3 |
|
$setItemFields = []; |
40
|
3 |
|
foreach ($changes as $key => $valueArray) { |
41
|
3 |
|
$valueArray = self::splitDictionaryUpdateEntries($valueArray); |
42
|
3 |
|
if (!is_array($valueArray) || Type::arrayIsAssoc($valueArray)) { |
43
|
3 |
|
$valueArray = array($valueArray); |
44
|
3 |
|
} |
45
|
|
|
|
46
|
3 |
|
foreach ($valueArray as $value) { |
47
|
3 |
|
list ($fieldUriType, $fieldKey, $valueKey, $value) = self::getFieldURI($uriType, $key, $value); |
48
|
3 |
|
$setItemFields[] = array( |
49
|
3 |
|
$fieldUriType => $fieldKey, |
50
|
3 |
|
$itemType => [$valueKey => $value] |
51
|
3 |
|
); |
52
|
3 |
|
} |
53
|
3 |
|
} |
54
|
|
|
|
55
|
3 |
|
return $setItemFields; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected static function deleteContactPhysicalAddressField($key, $deleteItemFields) |
59
|
|
|
{ |
60
|
|
|
$key = explode(":", $key); |
61
|
|
|
$dictionaryFields = FieldURIManager::getDictionaryURIFields(); |
62
|
|
|
if (count($key) == 2 && !isset($dictionaryFields['physicaladdress']['contacts'][strtolower($key[1])])) { |
63
|
|
|
foreach ($dictionaryFields['physicaladdress']['contacts'] as $uriKey => $uri) { |
64
|
|
|
$deleteItemFields[] = ['IndexedFieldURI' => ['FieldURI' => $uri, 'FieldIndex' => $key[1]]]; |
65
|
|
|
} |
66
|
|
|
} elseif (count($key) == 3 && isset($dictionaryFields['physicaladdress']['contacts'][strtolower($key[1])])) { |
67
|
|
|
$uri = $dictionaryFields['physicaladdress']['contacts'][strtolower($key[1])]; |
68
|
|
|
$deleteItemFields[] = ['IndexedFieldURI' => ['FieldURI' => $uri, 'FieldIndex' => $key[2]]]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $deleteItemFields; |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
protected static function splitDictionaryUpdateEntries($value) |
75
|
|
|
{ |
76
|
3 |
|
if (!is_array($value)) { |
77
|
3 |
|
return $value; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
reset($value); |
81
|
1 |
|
$fieldKey = key($value); |
82
|
|
|
|
83
|
1 |
|
if (!is_array($value[$fieldKey]) || empty($value[$fieldKey]['Entry'])) { |
84
|
|
|
return $value; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
$entryKey = $value[$fieldKey]['Entry']['Key']; |
88
|
1 |
|
unset($value[$fieldKey]['Entry']['Key']); |
89
|
|
|
|
90
|
1 |
|
$newValue = []; |
91
|
1 |
|
foreach ($value[$fieldKey]['Entry'] as $key => $updateValue) { |
92
|
1 |
|
$newValue[] = [$fieldKey => ['Entry' => ['Key' => $entryKey, $key => $updateValue]]]; |
93
|
1 |
|
} |
94
|
|
|
|
95
|
1 |
|
$value = $newValue; |
96
|
|
|
|
97
|
1 |
|
return $value; |
98
|
|
|
} |
99
|
|
|
|
100
|
3 |
|
protected static function getFieldURI($uriType, $key = null, $value = null) |
101
|
|
|
{ |
102
|
3 |
|
if (strpos($key, ':') !== false) { |
103
|
|
|
try { |
104
|
1 |
|
$fieldUriValue = substr($key, 0, strpos($key, ':')); |
105
|
|
|
|
106
|
1 |
|
list ($key, $index) = explode(':', $key); |
107
|
|
|
|
108
|
1 |
|
if (is_array($value)) { |
109
|
1 |
|
$key = key($value); |
110
|
1 |
|
$value = $value[$key]; |
111
|
1 |
|
} |
112
|
|
|
|
113
|
1 |
|
if (is_array($value) && !empty($value['Entry']) && is_array($value['Entry'])) { |
114
|
1 |
|
$entryKey = $value['Entry']['Key']; |
115
|
1 |
|
unset($value['Entry']['Key']); |
116
|
1 |
|
reset($value['Entry']); |
117
|
|
|
|
118
|
1 |
|
$fieldKey = key($value['Entry']); |
119
|
1 |
|
$value['Entry']['Key'] = $entryKey; |
120
|
1 |
|
$fieldUri = FieldURIManager::getIndexedFieldUriByName($fieldUriValue, $uriType, $fieldKey); |
121
|
1 |
|
} else { |
122
|
|
|
$fieldUri = FieldURIManager::getIndexedFieldUriByName($fieldUriValue, $uriType); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
return ['IndexedFieldURI', ['FieldURI' => $fieldUri, 'FieldIndex' => $index], $key, $value]; |
126
|
|
|
} catch (\Exception $e) { |
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
3 |
|
$fullName = FieldURIManager::getFieldUriByName($key, $uriType); |
131
|
|
|
|
132
|
3 |
|
return ['FieldURI', ['FieldURI' => $fullName], $key, $value]; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|