Stefanius67 /
GContacts
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace SKien\Google; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Class to manage the contact groups of a google account. |
||
| 8 | * |
||
| 9 | * This class encapsulates the following Google People API resources: |
||
| 10 | * - contactGroups |
||
| 11 | * - contactGroups.members |
||
| 12 | * |
||
| 13 | * If one of the methods that calls the google API fails (if it returns `false`), |
||
| 14 | * the last responsecode and furter informations can be retrieved through |
||
| 15 | * following methods of the `GClient` instance this object was created with: |
||
| 16 | * - `$oClient->getLastResponseCode()` |
||
| 17 | * - `$oClient->getLastError()` |
||
| 18 | * - `$oClient->getLastStatus()` |
||
| 19 | * |
||
| 20 | * @see \SKien\Google\GClient::getLastResponseCode() |
||
| 21 | * @see \SKien\Google\GClient::getLastError() |
||
| 22 | * @see \SKien\Google\GClient::getLastStatus() |
||
| 23 | * |
||
| 24 | * @link https://developers.google.com/people/api/rest/v1/contactGroups |
||
| 25 | * @link https://developers.google.com/people/api/rest/v1/contactGroups.members |
||
| 26 | * |
||
| 27 | * @author Stefanius <[email protected]> |
||
| 28 | * @copyright MIT License - see the LICENSE file for details |
||
| 29 | */ |
||
| 30 | class GContactGroups |
||
| 31 | {
|
||
| 32 | /** Values for the groupFields */ |
||
| 33 | /** The group's client data |
||
| 34 | * @link https://developers.google.com/people/api/rest/v1/contactGroups#groupclientdata */ |
||
| 35 | public const GF_CLIENT_DATA = 'clientData'; |
||
| 36 | /** The contact group type |
||
| 37 | * @link https://developers.google.com/people/api/rest/v1/contactGroups#GroupType */ |
||
| 38 | public const GF_GROUP_TYPE = 'groupType'; |
||
| 39 | /** The total number of contacts in the group irrespective of max members in specified in the request */ |
||
| 40 | public const GF_MEMBER_COUNT = 'memberCount'; |
||
| 41 | /** The metadata about a contact group |
||
| 42 | * @link https://developers.google.com/people/api/rest/v1/contactGroups#contactgroupmetadata */ |
||
| 43 | public const GF_METADATA = 'metadata'; |
||
| 44 | /** name, formattedName, resourceName */ |
||
| 45 | public const GF_NAME = 'name'; |
||
| 46 | |||
| 47 | /** All available contact groups (User- and system groups) */ |
||
| 48 | public const GT_ALL_CONTACT_GROUPS = ''; |
||
| 49 | /** User defined contact group */ |
||
| 50 | public const GT_USER_CONTACT_GROUPS = 'USER_CONTACT_GROUP'; |
||
| 51 | /** System defined contact group */ |
||
| 52 | public const GT_SYSTEM_CONTACT_GROUPS = 'SYSTEM_CONTACT_GROUP'; |
||
| 53 | |||
| 54 | /** full data list */ |
||
| 55 | public const DATA_LIST = 0; |
||
| 56 | /** list as associative array resourceName -> groupName */ |
||
| 57 | public const RES_LIST = 1; |
||
| 58 | |||
| 59 | /** system group 'starred' */ |
||
| 60 | public const GRP_STARRED = 'contactGroups/starred'; |
||
| 61 | /** system group 'chatBuddies' */ |
||
| 62 | public const GRP_CHAT_BUDDIES = 'contactGroups/chatBuddies'; |
||
| 63 | /** system group 'all' */ |
||
| 64 | public const GRP_ALL = 'contactGroups/all'; |
||
| 65 | /** system group 'myContacts' */ |
||
| 66 | public const GRP_MY_CONTACTS = 'contactGroups/myContacts'; |
||
| 67 | /** system group 'friends' */ |
||
| 68 | public const GRP_FRIENDS = 'contactGroups/friends'; |
||
| 69 | /** system group 'family' */ |
||
| 70 | public const GRP_FAMILY = 'contactGroups/family'; |
||
| 71 | /** system group 'coworkers' */ |
||
| 72 | public const GRP_COWORKERS = 'contactGroups/coworkers'; |
||
| 73 | /** system group 'blocked' */ |
||
| 74 | public const GRP_BLOCKED = 'contactGroups/blocked'; |
||
| 75 | |||
| 76 | /** max. pagesize for the list request */ |
||
| 77 | protected const GROUPS_MAX_PAGESIZE = 1000; |
||
| 78 | |||
| 79 | /** @var array<string> groupFields to be retruned by the request */ |
||
| 80 | protected array $aGroupFields = []; |
||
| 81 | /** @var int pagesize for a request */ |
||
| 82 | protected int $iPageSize = 50; |
||
| 83 | /** @var GClient clients to perform the requests to the api */ |
||
| 84 | protected GClient $oClient; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Create an instance and pass the client to use |
||
| 88 | * for the API requests. |
||
| 89 | * @param GClient $oClient |
||
| 90 | */ |
||
| 91 | public function __construct(GClient $oClient) |
||
| 92 | {
|
||
| 93 | $this->oClient = $oClient; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Add groupFields for next request. |
||
| 98 | * Can be called multiple and/or by passing an array of groupFields. All |
||
| 99 | * const `self::GF_xxxx` can be specified. |
||
| 100 | * Normally the groupFields parameter is left blank, which means that |
||
| 101 | * all groupFields except `GF_CLIENT_DATA` are taken into account. |
||
| 102 | * @param string|array<string>|null $fields; if set to null, the internal list is cleared |
||
| 103 | */ |
||
| 104 | public function addGroupFields($fields) : void |
||
| 105 | {
|
||
| 106 | if ($fields === null) {
|
||
| 107 | $this->aGroupFields = []; |
||
| 108 | } else if (is_array($fields)) {
|
||
| 109 | $this->aGroupFields = array_merge($this->aGroupFields, $fields); |
||
| 110 | } else if (!in_array($fields, $this->aGroupFields)) {
|
||
| 111 | $this->aGroupFields[] = $fields; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Set the pagesize for reading lists. |
||
| 117 | * May be limited to the max. pagesize for the request. |
||
| 118 | * @param int $iPageSize |
||
| 119 | */ |
||
| 120 | public function setPageSize(int $iPageSize) : void |
||
| 121 | {
|
||
| 122 | $this->iPageSize = $iPageSize; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get the whole list of the contact groups. |
||
| 127 | * The type of the groups to list (All, User or System) can be specified by the |
||
| 128 | * `$strGroupType` parameter. <br/><br/> |
||
| 129 | * The content of the result array can be specified with the `$iListType` parameter. |
||
| 130 | * - `self::RES_LIST`: all groups as associative array `ressourceName` -> `groupName`. |
||
| 131 | * - `self::DATA_LIST`: an array of all ContactGroup - objects. |
||
| 132 | * @link https://developers.google.com/people/api/rest/v1/contactGroups/list |
||
| 133 | * @link https://developers.google.com/people/api/rest/v1/contactGroups#ContactGroup |
||
| 134 | * @param string $strGroupType use one of the `self::GT_xxx` constants |
||
| 135 | * @param int $iListType content of the result array (`self::RES_LIST` or `self::DATA_LIST`) |
||
| 136 | * @return array<mixed>|false |
||
| 137 | */ |
||
| 138 | public function list(string $strGroupType = self::GT_ALL_CONTACT_GROUPS, int $iListType = self::RES_LIST) |
||
| 139 | {
|
||
| 140 | $aHeader = [$this->oClient->getAuthHeader()]; |
||
| 141 | $aParams = []; |
||
| 142 | if (count($this->aGroupFields) > 0) {
|
||
| 143 | $aParams['groupFields'] = implode(',', $this->aGroupFields);
|
||
| 144 | } |
||
| 145 | $aParams['pageSize'] = ($this->iPageSize > self::GROUPS_MAX_PAGESIZE ? self::GROUPS_MAX_PAGESIZE : $this->iPageSize); |
||
| 146 | |||
| 147 | $bEndOfList = false; |
||
| 148 | $aGroupList = []; |
||
| 149 | while ($bEndOfList === false) {
|
||
| 150 | $strURI = 'https://people.googleapis.com/v1/contactGroups?' . http_build_query($aParams); |
||
| 151 | $strResponse = $this->oClient->fetchJsonResponse($strURI, GClient::GET, $aHeader); |
||
| 152 | if ($strResponse === false) {
|
||
| 153 | return false; |
||
| 154 | } |
||
| 155 | $oResponse = json_decode($strResponse, true); |
||
| 156 | if (isset($oResponse['nextPageToken']) && !empty($oResponse['nextPageToken'])) {
|
||
| 157 | $aParams['pageToken'] = $oResponse['nextPageToken']; |
||
| 158 | } else {
|
||
| 159 | $bEndOfList = true; |
||
| 160 | } |
||
| 161 | foreach ($oResponse['contactGroups'] as $aGroup) {
|
||
| 162 | if ($strGroupType == self::GT_ALL_CONTACT_GROUPS || $aGroup['groupType'] == $strGroupType) {
|
||
| 163 | if ($iListType == self::DATA_LIST) {
|
||
| 164 | $aGroupList[] = $aGroup; |
||
| 165 | } else {
|
||
| 166 | $aGroupList[$aGroup['resourceName']] = $aGroup['formattedName']; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | return $aGroupList; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get whole group information. |
||
| 176 | * @link https://developers.google.com/people/api/rest/v1/contactGroups/get |
||
| 177 | * @link https://developers.google.com/people/api/rest/v1/contactGroups#ContactGroup |
||
| 178 | * @param string $strResourceName |
||
| 179 | * @param int $iMaxMembers |
||
| 180 | * @return array<mixed>|false |
||
| 181 | */ |
||
| 182 | public function getGroup(string $strResourceName, int $iMaxMembers = 0) |
||
| 183 | {
|
||
| 184 | $aHeader = [$this->oClient->getAuthHeader()]; |
||
| 185 | |||
| 186 | $aParams = []; |
||
| 187 | if (count($this->aGroupFields) > 0) {
|
||
| 188 | $aParams['updatePersonFields'] = implode(',', $this->aGroupFields);
|
||
| 189 | } |
||
| 190 | if ($iMaxMembers > 0) {
|
||
| 191 | $aParams['maxMembers'] = $iMaxMembers; |
||
| 192 | } |
||
| 193 | |||
| 194 | $strURI = 'https://people.googleapis.com/v1/' . $strResourceName . '?' . http_build_query($aParams); |
||
| 195 | $strResponse = $this->oClient->fetchJsonResponse($strURI, GClient::GET, $aHeader); |
||
| 196 | |||
| 197 | $result = false; |
||
| 198 | if ($strResponse !== false) {
|
||
| 199 | $result = json_decode($strResponse, true); |
||
| 200 | } |
||
| 201 | return $result; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get the resourceName for a named contact group. |
||
| 206 | * If no group with requested name found, the method will return |
||
| 207 | * an empty string. |
||
| 208 | * @param string $strGroupName |
||
| 209 | * @return string|false resourceName or false in case of an error |
||
| 210 | */ |
||
| 211 | public function getGroupResourceName(string $strGroupName) |
||
| 212 | {
|
||
| 213 | // get grouplist and search for requested name |
||
| 214 | $aGroups = $this->list(self::GT_ALL_CONTACT_GROUPS, self::DATA_LIST); |
||
| 215 | if ($aGroups !== false) {
|
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 216 | foreach ($aGroups as $aGroup) {
|
||
| 217 | if ($aGroup['formattedName'] == $strGroupName) {
|
||
| 218 | return $aGroup['resourceName']; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | return ''; // ... no error, but group not found |
||
| 222 | } |
||
| 223 | return false; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Create a new contact group. |
||
| 228 | * The name must be unique to the users contact groups. Attempting to create |
||
| 229 | * a group with a duplicate name results in a 409 response code. |
||
| 230 | * @link https://developers.google.com/people/api/rest/v1/contactGroups/create |
||
| 231 | * @param string $strGroupName |
||
| 232 | * @return array<mixed>|false |
||
| 233 | */ |
||
| 234 | public function createGroup(string $strGroupName) |
||
| 235 | {
|
||
| 236 | $aHeader = [ |
||
| 237 | $this->oClient->getAuthHeader(), |
||
| 238 | 'Content-Type: application/json', |
||
| 239 | ]; |
||
| 240 | $data = json_encode(['contactGroup' => ['name' => $strGroupName]]); |
||
| 241 | |||
| 242 | $strURI = 'https://people.googleapis.com/v1/contactGroups'; |
||
| 243 | $strResponse = $this->oClient->fetchJsonResponse($strURI, GClient::POST, $aHeader, $data); |
||
| 244 | |||
| 245 | $result = false; |
||
| 246 | if ($strResponse !== false) {
|
||
| 247 | $result = json_decode($strResponse, true); |
||
| 248 | } |
||
| 249 | return $result; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Update the groupName for the group specified by the resourceName. |
||
| 254 | * The new name must be unique to the users contact groups. Attempting to rename |
||
| 255 | * a group with a duplicate name results in a 409 response code. |
||
| 256 | * @link https://developers.google.com/people/api/rest/v1/contactGroups/update |
||
| 257 | * @param string $strResourceName |
||
| 258 | * @param string $strGroupName |
||
| 259 | * @return array<mixed>|false |
||
| 260 | */ |
||
| 261 | public function updateGroup(string $strResourceName, string $strGroupName) |
||
| 262 | {
|
||
| 263 | $aHeader = [ |
||
| 264 | $this->oClient->getAuthHeader(), |
||
| 265 | 'Content-Type: application/json', |
||
| 266 | ]; |
||
| 267 | $data = json_encode(['contactGroup' => ['name' => $strGroupName]]); |
||
| 268 | |||
| 269 | $strURI = 'https://people.googleapis.com/v1/' . $strResourceName; |
||
| 270 | $strResponse = $this->oClient->fetchJsonResponse($strURI, GClient::PUT, $aHeader, $data); |
||
| 271 | |||
| 272 | $result = false; |
||
| 273 | if ($strResponse !== false) {
|
||
| 274 | $result = json_decode($strResponse, true); |
||
| 275 | } |
||
| 276 | return $result; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Delete the contact group specified by the resourceName. |
||
| 281 | * If the param `$bDeleteContacts` is set to true, ALL contacts that are asigned to |
||
| 282 | * the group to delete will be <b>deleted</b> to! |
||
| 283 | * @link https://developers.google.com/people/api/rest/v1/contactGroups/delete |
||
| 284 | * @param string $strResourceName |
||
| 285 | * @param bool $bDeleteContacts set to true, if all groupmembers should be deleted |
||
| 286 | * @return bool true on success, false on error. |
||
| 287 | */ |
||
| 288 | public function deleteGroup(string $strResourceName, bool $bDeleteContacts = false) : bool |
||
| 289 | {
|
||
| 290 | $aHeader = [$this->oClient->getAuthHeader()]; |
||
| 291 | |||
| 292 | $strURI = 'https://people.googleapis.com/v1/' . $strResourceName; |
||
| 293 | if ($bDeleteContacts) {
|
||
| 294 | $strURI .= '?' . http_build_query(['deleteContacts' => true]); |
||
| 295 | } |
||
| 296 | $strResponse = $this->oClient->fetchJsonResponse($strURI, GClient::DELETE, $aHeader); |
||
| 297 | return ($strResponse !== false); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Add one or more contacts to the specified group. |
||
| 302 | * @link https://developers.google.com/people/api/rest/v1/contactGroups.members/modify |
||
| 303 | * @param string $strGroupResourceName |
||
| 304 | * @param array<string> $aContactResourceNames |
||
| 305 | * @return array<mixed>|false |
||
| 306 | */ |
||
| 307 | public function addContactsToGroup(string $strGroupResourceName, array $aContactResourceNames) |
||
| 308 | {
|
||
| 309 | $aHeader = [ |
||
| 310 | $this->oClient->getAuthHeader(), |
||
| 311 | 'Content-Type: application/json', |
||
| 312 | ]; |
||
| 313 | $data = json_encode(['resourceNamesToAdd' => $aContactResourceNames]); |
||
| 314 | |||
| 315 | $strURI = 'https://people.googleapis.com/v1/' . $strGroupResourceName . '/members:modify'; |
||
| 316 | $strResponse = $this->oClient->fetchJsonResponse($strURI, GClient::POST, $aHeader, $data); |
||
| 317 | |||
| 318 | $result = false; |
||
| 319 | if ($strResponse !== false) {
|
||
| 320 | $result = json_decode($strResponse, true); |
||
| 321 | } |
||
| 322 | return $result; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Remove one or more contacts from the specified group. |
||
| 327 | * @link https://developers.google.com/people/api/rest/v1/contactGroups.members/modify |
||
| 328 | * @param string $strGroupResourceName |
||
| 329 | * @param array<string> $aContactResourceNames |
||
| 330 | * @return array<mixed>|false |
||
| 331 | */ |
||
| 332 | public function removeContactsFromGroup(string $strGroupResourceName, array $aContactResourceNames) |
||
| 333 | {
|
||
| 334 | $aHeader = [ |
||
| 335 | $this->oClient->getAuthHeader(), |
||
| 336 | 'Content-Type: application/json', |
||
| 337 | ]; |
||
| 338 | $data = json_encode(['resourceNamesToRemove' => $aContactResourceNames]); |
||
| 339 | |||
| 340 | $strURI = 'https://people.googleapis.com/v1/' . $strGroupResourceName . '/members:modify'; |
||
| 341 | $strResponse = $this->oClient->fetchJsonResponse($strURI, GClient::POST, $aHeader, $data); |
||
| 342 | |||
| 343 | $result = false; |
||
| 344 | if ($strResponse !== false) {
|
||
| 345 | $result = json_decode($strResponse, true); |
||
| 346 | } |
||
| 347 | return $result; |
||
| 348 | } |
||
| 349 | } |
||
| 350 |