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