@@ -37,177 +37,177 @@ |
||
| 37 | 37 | |
| 38 | 38 | class ContactsManager implements IManager { |
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * This function is used to search and find contacts within the users address books. |
|
| 42 | - * In case $pattern is empty all contacts will be returned. |
|
| 43 | - * |
|
| 44 | - * @param string $pattern which should match within the $searchProperties |
|
| 45 | - * @param array $searchProperties defines the properties within the query pattern should match |
|
| 46 | - * @param array $options = array() to define the search behavior |
|
| 47 | - * - 'escape_like_param' - If set to false wildcards _ and % are not escaped |
|
| 48 | - * - 'limit' - Set a numeric limit for the search results |
|
| 49 | - * - 'offset' - Set the offset for the limited search results |
|
| 50 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
| 51 | - */ |
|
| 52 | - public function search($pattern, $searchProperties = [], $options = []) { |
|
| 53 | - $this->loadAddressBooks(); |
|
| 54 | - $result = []; |
|
| 55 | - foreach ($this->addressBooks as $addressBook) { |
|
| 56 | - $r = $addressBook->search($pattern, $searchProperties, $options); |
|
| 57 | - $contacts = []; |
|
| 58 | - foreach ($r as $c) { |
|
| 59 | - $c['addressbook-key'] = $addressBook->getKey(); |
|
| 60 | - $contacts[] = $c; |
|
| 61 | - } |
|
| 62 | - $result = array_merge($result, $contacts); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - return $result; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * This function can be used to delete the contact identified by the given id |
|
| 70 | - * |
|
| 71 | - * @param object $id the unique identifier to a contact |
|
| 72 | - * @param string $addressBookKey identifier of the address book in which the contact shall be deleted |
|
| 73 | - * @return bool successful or not |
|
| 74 | - */ |
|
| 75 | - public function delete($id, $addressBookKey) { |
|
| 76 | - $addressBook = $this->getAddressBook($addressBookKey); |
|
| 77 | - if (!$addressBook) { |
|
| 78 | - return null; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - if ($addressBook->getPermissions() & Constants::PERMISSION_DELETE) { |
|
| 82 | - return $addressBook->delete($id); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - return null; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * This function is used to create a new contact if 'id' is not given or not present. |
|
| 90 | - * Otherwise the contact will be updated by replacing the entire data set. |
|
| 91 | - * |
|
| 92 | - * @param array $properties this array if key-value-pairs defines a contact |
|
| 93 | - * @param string $addressBookKey identifier of the address book in which the contact shall be created or updated |
|
| 94 | - * @return array representing the contact just created or updated |
|
| 95 | - */ |
|
| 96 | - public function createOrUpdate($properties, $addressBookKey) { |
|
| 97 | - $addressBook = $this->getAddressBook($addressBookKey); |
|
| 98 | - if (!$addressBook) { |
|
| 99 | - return null; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - if ($addressBook->getPermissions() & Constants::PERMISSION_CREATE) { |
|
| 103 | - return $addressBook->createOrUpdate($properties); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - return null; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Check if contacts are available (e.g. contacts app enabled) |
|
| 111 | - * |
|
| 112 | - * @return bool true if enabled, false if not |
|
| 113 | - */ |
|
| 114 | - public function isEnabled() { |
|
| 115 | - return !empty($this->addressBooks) || !empty($this->addressBookLoaders); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @param IAddressBook $addressBook |
|
| 120 | - */ |
|
| 121 | - public function registerAddressBook(IAddressBook $addressBook) { |
|
| 122 | - $this->addressBooks[$addressBook->getKey()] = $addressBook; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @param IAddressBook $addressBook |
|
| 127 | - */ |
|
| 128 | - public function unregisterAddressBook(IAddressBook $addressBook) { |
|
| 129 | - unset($this->addressBooks[$addressBook->getKey()]); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Return a list of the user's addressbooks display names |
|
| 134 | - * ! The addressBook displayName are not unique, please use getUserAddressBooks |
|
| 135 | - * |
|
| 136 | - * @return IAddressBook[] |
|
| 137 | - * @since 6.0.0 |
|
| 138 | - * @deprecated 16.0.0 - Use `$this->getUserAddressBooks()` instead |
|
| 139 | - */ |
|
| 140 | - public function getAddressBooks() { |
|
| 141 | - $this->loadAddressBooks(); |
|
| 142 | - $result = []; |
|
| 143 | - foreach ($this->addressBooks as $addressBook) { |
|
| 144 | - $result[$addressBook->getKey()] = $addressBook->getDisplayName(); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - return $result; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Return a list of the user's addressbooks |
|
| 152 | - * |
|
| 153 | - * @return IAddressBook[] |
|
| 154 | - * @since 16.0.0 |
|
| 155 | - */ |
|
| 156 | - public function getUserAddressBooks(): array { |
|
| 157 | - $this->loadAddressBooks(); |
|
| 158 | - return $this->addressBooks; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * removes all registered address book instances |
|
| 163 | - */ |
|
| 164 | - public function clear() { |
|
| 165 | - $this->addressBooks = []; |
|
| 166 | - $this->addressBookLoaders = []; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @var IAddressBook[] which holds all registered address books |
|
| 171 | - */ |
|
| 172 | - private $addressBooks = []; |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @var \Closure[] to call to load/register address books |
|
| 176 | - */ |
|
| 177 | - private $addressBookLoaders = []; |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * In order to improve lazy loading a closure can be registered which will be called in case |
|
| 181 | - * address books are actually requested |
|
| 182 | - * |
|
| 183 | - * @param \Closure $callable |
|
| 184 | - */ |
|
| 185 | - public function register(\Closure $callable) { |
|
| 186 | - $this->addressBookLoaders[] = $callable; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Get (and load when needed) the address book for $key |
|
| 191 | - * |
|
| 192 | - * @param string $addressBookKey |
|
| 193 | - * @return IAddressBook |
|
| 194 | - */ |
|
| 195 | - protected function getAddressBook($addressBookKey) { |
|
| 196 | - $this->loadAddressBooks(); |
|
| 197 | - if (!array_key_exists($addressBookKey, $this->addressBooks)) { |
|
| 198 | - return null; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - return $this->addressBooks[$addressBookKey]; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Load all address books registered with 'register' |
|
| 206 | - */ |
|
| 207 | - protected function loadAddressBooks() { |
|
| 208 | - foreach ($this->addressBookLoaders as $callable) { |
|
| 209 | - $callable($this); |
|
| 210 | - } |
|
| 211 | - $this->addressBookLoaders = []; |
|
| 212 | - } |
|
| 40 | + /** |
|
| 41 | + * This function is used to search and find contacts within the users address books. |
|
| 42 | + * In case $pattern is empty all contacts will be returned. |
|
| 43 | + * |
|
| 44 | + * @param string $pattern which should match within the $searchProperties |
|
| 45 | + * @param array $searchProperties defines the properties within the query pattern should match |
|
| 46 | + * @param array $options = array() to define the search behavior |
|
| 47 | + * - 'escape_like_param' - If set to false wildcards _ and % are not escaped |
|
| 48 | + * - 'limit' - Set a numeric limit for the search results |
|
| 49 | + * - 'offset' - Set the offset for the limited search results |
|
| 50 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
| 51 | + */ |
|
| 52 | + public function search($pattern, $searchProperties = [], $options = []) { |
|
| 53 | + $this->loadAddressBooks(); |
|
| 54 | + $result = []; |
|
| 55 | + foreach ($this->addressBooks as $addressBook) { |
|
| 56 | + $r = $addressBook->search($pattern, $searchProperties, $options); |
|
| 57 | + $contacts = []; |
|
| 58 | + foreach ($r as $c) { |
|
| 59 | + $c['addressbook-key'] = $addressBook->getKey(); |
|
| 60 | + $contacts[] = $c; |
|
| 61 | + } |
|
| 62 | + $result = array_merge($result, $contacts); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + return $result; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * This function can be used to delete the contact identified by the given id |
|
| 70 | + * |
|
| 71 | + * @param object $id the unique identifier to a contact |
|
| 72 | + * @param string $addressBookKey identifier of the address book in which the contact shall be deleted |
|
| 73 | + * @return bool successful or not |
|
| 74 | + */ |
|
| 75 | + public function delete($id, $addressBookKey) { |
|
| 76 | + $addressBook = $this->getAddressBook($addressBookKey); |
|
| 77 | + if (!$addressBook) { |
|
| 78 | + return null; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + if ($addressBook->getPermissions() & Constants::PERMISSION_DELETE) { |
|
| 82 | + return $addressBook->delete($id); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + return null; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * This function is used to create a new contact if 'id' is not given or not present. |
|
| 90 | + * Otherwise the contact will be updated by replacing the entire data set. |
|
| 91 | + * |
|
| 92 | + * @param array $properties this array if key-value-pairs defines a contact |
|
| 93 | + * @param string $addressBookKey identifier of the address book in which the contact shall be created or updated |
|
| 94 | + * @return array representing the contact just created or updated |
|
| 95 | + */ |
|
| 96 | + public function createOrUpdate($properties, $addressBookKey) { |
|
| 97 | + $addressBook = $this->getAddressBook($addressBookKey); |
|
| 98 | + if (!$addressBook) { |
|
| 99 | + return null; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + if ($addressBook->getPermissions() & Constants::PERMISSION_CREATE) { |
|
| 103 | + return $addressBook->createOrUpdate($properties); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + return null; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Check if contacts are available (e.g. contacts app enabled) |
|
| 111 | + * |
|
| 112 | + * @return bool true if enabled, false if not |
|
| 113 | + */ |
|
| 114 | + public function isEnabled() { |
|
| 115 | + return !empty($this->addressBooks) || !empty($this->addressBookLoaders); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @param IAddressBook $addressBook |
|
| 120 | + */ |
|
| 121 | + public function registerAddressBook(IAddressBook $addressBook) { |
|
| 122 | + $this->addressBooks[$addressBook->getKey()] = $addressBook; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @param IAddressBook $addressBook |
|
| 127 | + */ |
|
| 128 | + public function unregisterAddressBook(IAddressBook $addressBook) { |
|
| 129 | + unset($this->addressBooks[$addressBook->getKey()]); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Return a list of the user's addressbooks display names |
|
| 134 | + * ! The addressBook displayName are not unique, please use getUserAddressBooks |
|
| 135 | + * |
|
| 136 | + * @return IAddressBook[] |
|
| 137 | + * @since 6.0.0 |
|
| 138 | + * @deprecated 16.0.0 - Use `$this->getUserAddressBooks()` instead |
|
| 139 | + */ |
|
| 140 | + public function getAddressBooks() { |
|
| 141 | + $this->loadAddressBooks(); |
|
| 142 | + $result = []; |
|
| 143 | + foreach ($this->addressBooks as $addressBook) { |
|
| 144 | + $result[$addressBook->getKey()] = $addressBook->getDisplayName(); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + return $result; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Return a list of the user's addressbooks |
|
| 152 | + * |
|
| 153 | + * @return IAddressBook[] |
|
| 154 | + * @since 16.0.0 |
|
| 155 | + */ |
|
| 156 | + public function getUserAddressBooks(): array { |
|
| 157 | + $this->loadAddressBooks(); |
|
| 158 | + return $this->addressBooks; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * removes all registered address book instances |
|
| 163 | + */ |
|
| 164 | + public function clear() { |
|
| 165 | + $this->addressBooks = []; |
|
| 166 | + $this->addressBookLoaders = []; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @var IAddressBook[] which holds all registered address books |
|
| 171 | + */ |
|
| 172 | + private $addressBooks = []; |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @var \Closure[] to call to load/register address books |
|
| 176 | + */ |
|
| 177 | + private $addressBookLoaders = []; |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * In order to improve lazy loading a closure can be registered which will be called in case |
|
| 181 | + * address books are actually requested |
|
| 182 | + * |
|
| 183 | + * @param \Closure $callable |
|
| 184 | + */ |
|
| 185 | + public function register(\Closure $callable) { |
|
| 186 | + $this->addressBookLoaders[] = $callable; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Get (and load when needed) the address book for $key |
|
| 191 | + * |
|
| 192 | + * @param string $addressBookKey |
|
| 193 | + * @return IAddressBook |
|
| 194 | + */ |
|
| 195 | + protected function getAddressBook($addressBookKey) { |
|
| 196 | + $this->loadAddressBooks(); |
|
| 197 | + if (!array_key_exists($addressBookKey, $this->addressBooks)) { |
|
| 198 | + return null; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + return $this->addressBooks[$addressBookKey]; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Load all address books registered with 'register' |
|
| 206 | + */ |
|
| 207 | + protected function loadAddressBooks() { |
|
| 208 | + foreach ($this->addressBookLoaders as $callable) { |
|
| 209 | + $callable($this); |
|
| 210 | + } |
|
| 211 | + $this->addressBookLoaders = []; |
|
| 212 | + } |
|
| 213 | 213 | } |