| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | namespace Aurora\Modules\Contacts; |
| 9: | |
| 10: | use Afterlogic\DAV\Backend; |
| 11: | use Afterlogic\DAV\Constants; |
| 12: | use Aurora\Api; |
| 13: | use Aurora\Modules\Contacts\Enums\Access; |
| 14: | use Aurora\Modules\Contacts\Enums\StorageType; |
| 15: | use Aurora\Modules\Contacts\Enums\SortField; |
| 16: | use Aurora\System\Enums\SortOrder; |
| 17: | use Aurora\Modules\Contacts\Classes\Contact; |
| 18: | use Aurora\Modules\Contacts\Classes\VCard\Helper; |
| 19: | use Aurora\Modules\Contacts\Models\ContactCard; |
| 20: | use Aurora\Modules\Contacts\Classes\Group; |
| 21: | use Aurora\Modules\Core\Module as CoreModule; |
| 22: | use Aurora\System\Exceptions\ApiException; |
| 23: | use Aurora\System\Notifications; |
| 24: | use Illuminate\Database\Eloquent\Builder; |
| 25: | use Illuminate\Database\Capsule\Manager as Capsule; |
| 26: | use Sabre\DAV\UUIDUtil; |
| 27: | use Sabre\DAV\PropPatch; |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | class Module extends \Aurora\System\Module\AbstractModule |
| 39: | { |
| 40: | protected $aImportExportFormats = ['csv', 'vcf']; |
| 41: | |
| 42: | protected $userPublicIdToDelete = null; |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | public static function getInstance() |
| 48: | { |
| 49: | return parent::getInstance(); |
| 50: | } |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | public static function Decorator() |
| 56: | { |
| 57: | return parent::Decorator(); |
| 58: | } |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | public function getModuleSettings() |
| 64: | { |
| 65: | return $this->oModuleSettings; |
| 66: | } |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | public function init() |
| 74: | { |
| 75: | $this->subscribeEvent('Mail::AfterUseEmails', array($this, 'onAfterUseEmails')); |
| 76: | $this->subscribeEvent('Mail::GetBodyStructureParts', array($this, 'onGetBodyStructureParts')); |
| 77: | $this->subscribeEvent('Core::DeleteUser::before', array($this, 'onBeforeDeleteUser')); |
| 78: | $this->subscribeEvent('Core::DeleteUser::after', array($this, 'onAfterDeleteUser')); |
| 79: | |
| 80: | $this->subscribeEvent('System::toResponseArray::after', array($this, 'onContactToResponseArray')); |
| 81: | |
| 82: | $this->denyMethodsCallByWebApi([ |
| 83: | 'UpdateContactObject', |
| 84: | 'CheckAccessToAddressBook', |
| 85: | 'CheckAccessToObject' |
| 86: | ]); |
| 87: | } |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | public function GetSettings() |
| 155: | { |
| 156: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 157: | $oUser = \Aurora\System\Api::getAuthenticatedUser(); |
| 158: | |
| 159: | $aResult = [ |
| 160: | 'AllowAddressBooksManagement' => $this->oModuleSettings->AllowAddressBooksManagement, |
| 161: | 'ImportContactsLink' => $this->oModuleSettings->ImportContactsLink, |
| 162: | 'PrimaryEmail' => (new Enums\PrimaryEmail())->getMap(), |
| 163: | 'PrimaryPhone' => (new Enums\PrimaryPhone())->getMap(), |
| 164: | 'PrimaryAddress' => (new Enums\PrimaryAddress())->getMap(), |
| 165: | 'SortField' => (new SortField())->getMap(), |
| 166: | 'ImportExportFormats' => $this->aImportExportFormats, |
| 167: | 'SaveVcfServerModuleName' => \Aurora\System\Api::GetModuleManager()->ModuleExists('DavContacts') ? 'DavContacts' : '', |
| 168: | 'ContactsPerPage' => $this->oModuleSettings->ContactsPerPage, |
| 169: | 'ContactsSortBy' => $this->oModuleSettings->ContactsSortBy |
| 170: | ]; |
| 171: | |
| 172: | if ($oUser && $oUser->isNormalOrTenant()) { |
| 173: | if (null !== $oUser->getExtendedProp(self::GetName() . '::ContactsPerPage')) { |
| 174: | $aResult['ContactsPerPage'] = $oUser->getExtendedProp(self::GetName() . '::ContactsPerPage'); |
| 175: | } |
| 176: | |
| 177: | $aResult['Storages'] = self::Decorator()->GetStorages(); |
| 178: | } |
| 179: | |
| 180: | return $aResult; |
| 181: | } |
| 182: | |
| 183: | public function IsDisplayedStorage($Storage) |
| 184: | { |
| 185: | return true; |
| 186: | } |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | public function GetContactStorages() |
| 192: | { |
| 193: | return $this->Decorator()->GetStorages(); |
| 194: | } |
| 195: | |
| 196: | public function GetStorageDisplayName($Storage) |
| 197: | { |
| 198: | $result = ''; |
| 199: | |
| 200: | switch($Storage) { |
| 201: | case Enums\StorageType::All: |
| 202: | $result = $this->i18N('LABEL_STORAGE_ALL'); |
| 203: | break; |
| 204: | case Enums\StorageType::Personal: |
| 205: | $result = $this->i18N('LABEL_STORAGE_PERSONAL'); |
| 206: | break; |
| 207: | case Enums\StorageType::Collected: |
| 208: | $result = $this->i18N('LABEL_STORAGE_COLLECTED'); |
| 209: | break; |
| 210: | case Enums\StorageType::Team: |
| 211: | $result = $this->i18N('LABEL_STORAGE_TEAM'); |
| 212: | break; |
| 213: | case Enums\StorageType::Shared: |
| 214: | $result = $this->i18N('LABEL_STORAGE_SHARED'); |
| 215: | break; |
| 216: | } |
| 217: | |
| 218: | return $result; |
| 219: | } |
| 220: | |
| 221: | protected function GetStorageDisplayNameOverride($sStorageName, $sSotrageId) |
| 222: | { |
| 223: | $result = $sStorageName; |
| 224: | |
| 225: | switch(true) { |
| 226: | case $sSotrageId === Enums\StorageType::Personal && $sStorageName === Constants::ADDRESSBOOK_DEFAULT_DISPLAY_NAME: |
| 227: | $result = $this->i18N('LABEL_STORAGE_PERSONAL'); |
| 228: | break; |
| 229: | case $sSotrageId === Enums\StorageType::Collected && $sStorageName === Constants::ADDRESSBOOK_COLLECTED_DISPLAY_NAME: |
| 230: | $result = $this->i18N('LABEL_STORAGE_COLLECTED'); |
| 231: | break; |
| 232: | case $sSotrageId === Enums\StorageType::Team && $sStorageName === Constants::ADDRESSBOOK_TEAM_DISPLAY_NAME: |
| 233: | $result = $this->i18N('LABEL_STORAGE_TEAM'); |
| 234: | break; |
| 235: | case $sSotrageId === Enums\StorageType::Shared && $sStorageName === Constants::ADDRESSBOOK_SHARED_WITH_ALL_DISPLAY_NAME: |
| 236: | $result = $this->i18N('LABEL_STORAGE_SHARED'); |
| 237: | break; |
| 238: | } |
| 239: | |
| 240: | return $result; |
| 241: | } |
| 242: | |
| 243: | public function GetStorages() |
| 244: | { |
| 245: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 246: | |
| 247: | $iUserId = \Aurora\System\Api::getAuthenticatedUserId(); |
| 248: | |
| 249: | $aAddressBooks = $this->Decorator()->GetAddressBooks($iUserId); |
| 250: | |
| 251: | foreach ($aAddressBooks as &$oAddressBook) { |
| 252: | $oAddressBook['DisplayName'] = $this->GetStorageDisplayNameOverride($oAddressBook['DisplayName'], $oAddressBook['Id']); |
| 253: | } |
| 254: | |
| 255: | $aStoragesOrder = [ |
| 256: | StorageType::Personal, |
| 257: | StorageType::Collected, |
| 258: | StorageType::Shared, |
| 259: | StorageType::Team |
| 260: | ]; |
| 261: | return $this->sortAddressBooks($aAddressBooks, $aStoragesOrder); |
| 262: | } |
| 263: | |
| 264: | protected function sortAddressBooks($aAddressBooks, $aOrder = []) |
| 265: | { |
| 266: | $priority_books = array(); |
| 267: | $non_priority_books = array(); |
| 268: | |
| 269: | |
| 270: | foreach ($aAddressBooks as $book) { |
| 271: | $id = $book['Id']; |
| 272: | |
| 273: | if (in_array($id, $aOrder)) { |
| 274: | $priority_books[] = $book; |
| 275: | } else { |
| 276: | $non_priority_books[] = $book; |
| 277: | } |
| 278: | } |
| 279: | |
| 280: | |
| 281: | usort($priority_books, function ($a, $b) use ($aOrder) { |
| 282: | |
| 283: | $index_a = array_search($a['Id'], $aOrder); |
| 284: | $index_b = array_search($b['Id'], $aOrder); |
| 285: | |
| 286: | |
| 287: | return $index_a - $index_b; |
| 288: | }); |
| 289: | |
| 290: | |
| 291: | usort($non_priority_books, function ($a, $b) { |
| 292: | |
| 293: | return strcmp($a['DisplayName'], $b['DisplayName']); |
| 294: | }); |
| 295: | |
| 296: | |
| 297: | return array_merge($priority_books, $non_priority_books); |
| 298: | } |
| 299: | |
| 300: | protected function getContactsCollection($iSortField = SortField::Name, $iSortOrder = SortOrder::ASC, $iOffset = 0, $iLimit = 20, $oFilters = null) |
| 301: | { |
| 302: | $sSortField = 'FullName'; |
| 303: | $sSortFieldSecond = 'ViewEmail'; |
| 304: | $sSortOrder = $iSortOrder === SortOrder::ASC ? 'asc' : 'desc'; |
| 305: | switch ($iSortField) { |
| 306: | case SortField::Email: |
| 307: | $sSortField = 'ViewEmail'; |
| 308: | $sSortFieldSecond = 'FullName'; |
| 309: | break; |
| 310: | case SortField::Frequency: |
| 311: | $sSortField = 'AgeScore'; |
| 312: | |
| 313: | break; |
| 314: | case SortField::FirstName: |
| 315: | $sSortField = 'FirstName'; |
| 316: | break; |
| 317: | case SortField::LastName: |
| 318: | $sSortField = 'LastName'; |
| 319: | break; |
| 320: | case SortField::Name: |
| 321: | $sSortField = 'FullName'; |
| 322: | break; |
| 323: | } |
| 324: | if ($iOffset > 0) { |
| 325: | $oFilters->offset($iOffset); |
| 326: | } |
| 327: | if ($iLimit > 0) { |
| 328: | $oFilters->limit($iLimit); |
| 329: | } |
| 330: | |
| 331: | $oFilters |
| 332: | ->orderBy(Capsule::connection()->raw("CASE WHEN `$sSortField` = '' THEN 1 ELSE 0 END")) |
| 333: | ->orderBy($sSortField, $sSortOrder) |
| 334: | ->orderBy($sSortFieldSecond, $sSortOrder) |
| 335: | ; |
| 336: | |
| 337: | return $oFilters->get(); |
| 338: | } |
| 339: | |
| 340: | |
| 341: | |
| 342: | |
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: | protected function resolveAddressbooksIdsForContacts($oUser, &$aContactsCollection) |
| 348: | { |
| 349: | $aAddressbooksMap = self::Decorator()->GetStoragesMapToAddressbooks(); |
| 350: | $aAddressBooks = []; |
| 351: | $aPersonalAddressBooks = Backend::Carddav()->getAddressBooksForUser(Constants::PRINCIPALS_PREFIX . $oUser->PublicId); |
| 352: | foreach ($aPersonalAddressBooks as $oAddressBook) { |
| 353: | $aAddressBooks[$oAddressBook['id']] = $oAddressBook; |
| 354: | } |
| 355: | |
| 356: | $aContactsCollection->each(function (&$contact) use ($aAddressBooks, $aAddressbooksMap) { |
| 357: | $contact->UUID = (string) $contact->UUID; |
| 358: | if (!isset($aAddressBooks[$contact->Storage])) { |
| 359: | $aAddressBooks[$contact->Storage] = Backend::Carddav()->getAddressBookById($contact->Storage); |
| 360: | } |
| 361: | $StorageTextId = false; |
| 362: | if ($aAddressBooks[$contact->Storage]) { |
| 363: | $StorageTextId = array_search($aAddressBooks[$contact->Storage]['uri'], $aAddressbooksMap); |
| 364: | } |
| 365: | |
| 366: | $contact->AddressBookId = (int) $contact->Storage; |
| 367: | $contact->Storage = $StorageTextId ? $StorageTextId : (StorageType::AddressBook . '-' . $contact->Storage); |
| 368: | }); |
| 369: | } |
| 370: | |
| 371: | |
| 372: | |
| 373: | |
| 374: | |
| 375: | |
| 376: | |
| 377: | |
| 378: | |
| 379: | |
| 380: | |
| 381: | |
| 382: | |
| 383: | |
| 384: | |
| 385: | |
| 386: | |
| 387: | |
| 388: | |
| 389: | |
| 390: | |
| 391: | |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | |
| 397: | |
| 398: | |
| 399: | |
| 400: | |
| 401: | |
| 402: | |
| 403: | |
| 404: | |
| 405: | |
| 406: | |
| 407: | |
| 408: | |
| 409: | |
| 410: | |
| 411: | |
| 412: | |
| 413: | |
| 414: | |
| 415: | |
| 416: | |
| 417: | |
| 418: | |
| 419: | |
| 420: | |
| 421: | |
| 422: | |
| 423: | |
| 424: | public function UpdateSettings($ContactsPerPage) |
| 425: | { |
| 426: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 427: | |
| 428: | $bResult = false; |
| 429: | |
| 430: | $oUser = \Aurora\System\Api::getAuthenticatedUser(); |
| 431: | if ($oUser) { |
| 432: | if ($oUser->isNormalOrTenant()) { |
| 433: | $oUser->setExtendedProp(self::GetName() . '::ContactsPerPage', $ContactsPerPage); |
| 434: | return CoreModule::Decorator()->UpdateUserObject($oUser); |
| 435: | } |
| 436: | if ($oUser->isAdmin()) { |
| 437: | $this->setConfig('ContactsPerPage', $ContactsPerPage); |
| 438: | $bResult = $this->saveModuleConfig(); |
| 439: | } |
| 440: | } |
| 441: | |
| 442: | return $bResult; |
| 443: | } |
| 444: | |
| 445: | |
| 446: | |
| 447: | |
| 448: | |
| 449: | |
| 450: | |
| 451: | |
| 452: | |
| 453: | |
| 454: | |
| 455: | |
| 456: | |
| 457: | |
| 458: | |
| 459: | |
| 460: | |
| 461: | |
| 462: | |
| 463: | |
| 464: | |
| 465: | |
| 466: | |
| 467: | |
| 468: | |
| 469: | |
| 470: | |
| 471: | |
| 472: | |
| 473: | |
| 474: | |
| 475: | |
| 476: | |
| 477: | |
| 478: | |
| 479: | |
| 480: | |
| 481: | |
| 482: | |
| 483: | |
| 484: | |
| 485: | |
| 486: | |
| 487: | |
| 488: | |
| 489: | |
| 490: | |
| 491: | |
| 492: | |
| 493: | |
| 494: | |
| 495: | |
| 496: | |
| 497: | |
| 498: | |
| 499: | |
| 500: | public function Export($UserId, $Storage, $Format, Builder $Filters = null, $GroupUUID = '', $ContactUUIDs = [], $AddressBookId = null) |
| 501: | { |
| 502: | Api::CheckAccess($UserId); |
| 503: | |
| 504: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 505: | |
| 506: | $sOutput = ''; |
| 507: | |
| 508: | if (!empty($GroupUUID)) { |
| 509: | $oGroup = self::Decorator()->GetGroup($UserId, $GroupUUID); |
| 510: | if ($oGroup) { |
| 511: | $ContactUUIDs = (is_array($ContactUUIDs) && count($ContactUUIDs) > 0) ? array_intersect( |
| 512: | $oGroup->Contacts, |
| 513: | $ContactUUIDs |
| 514: | ) : $oGroup->Contacts; |
| 515: | } |
| 516: | } |
| 517: | |
| 518: | if (is_array($ContactUUIDs)) { |
| 519: | $query = $this->getGetContactsQueryBuilder($UserId, $Storage, $AddressBookId, $Filters, false, true); |
| 520: | if ($Format === 'vcf') { |
| 521: | if (count($ContactUUIDs) > 0) { |
| 522: | $query = $query->whereIn('contacts_cards.CardId', $ContactUUIDs); |
| 523: | } |
| 524: | $rows = $query->select('carddata')->pluck('carddata')->toArray(); |
| 525: | foreach ($rows as $row) { |
| 526: | $sOutput .= $row; |
| 527: | } |
| 528: | } elseif ($Format === 'csv') { |
| 529: | $oSync = new Classes\Csv\Sync(); |
| 530: | if (count($ContactUUIDs) === 0) { |
| 531: | $ContactUUIDs = $query->select('CardId')->pluck('CardId')->toArray(); |
| 532: | } |
| 533: | $aContacts = self::Decorator()->GetContactsByUids($UserId, $ContactUUIDs); |
| 534: | $sOutput = $oSync->Export($aContacts); |
| 535: | } |
| 536: | } |
| 537: | |
| 538: | if (is_string($sOutput) && !empty($sOutput)) { |
| 539: | $fileName = 'export'; |
| 540: | $aStorages = self::Decorator()->GetStorages(); |
| 541: | foreach ($aStorages as $aStorage) { |
| 542: | if ($aStorage['Id'] === $Storage) { |
| 543: | $fileName = isset($aStorage['DisplayName']) ? $aStorage['DisplayName'] : $aStorage['Id']; |
| 544: | break; |
| 545: | } |
| 546: | } |
| 547: | |
| 548: | header('Pragma: public'); |
| 549: | header('Content-Type: text/csv'); |
| 550: | header('Content-Disposition: attachment; filename="' . $fileName . '.' . $Format . '";'); |
| 551: | header('Content-Transfer-Encoding: binary'); |
| 552: | } |
| 553: | |
| 554: | echo $sOutput; |
| 555: | } |
| 556: | |
| 557: | public function GetContactAsVCF($UserId, $Contact) |
| 558: | { |
| 559: | Api::CheckAccess($UserId); |
| 560: | $oVCard = new \Sabre\VObject\Component\VCard(); |
| 561: | Classes\VCard\Helper::UpdateVCardFromContact($Contact, $oVCard); |
| 562: | return $oVCard->serialize(); |
| 563: | } |
| 564: | |
| 565: | |
| 566: | |
| 567: | |
| 568: | |
| 569: | |
| 570: | |
| 571: | |
| 572: | |
| 573: | |
| 574: | |
| 575: | |
| 576: | |
| 577: | |
| 578: | |
| 579: | |
| 580: | |
| 581: | |
| 582: | |
| 583: | |
| 584: | |
| 585: | |
| 586: | |
| 587: | |
| 588: | |
| 589: | |
| 590: | |
| 591: | |
| 592: | |
| 593: | |
| 594: | |
| 595: | |
| 596: | |
| 597: | |
| 598: | |
| 599: | |
| 600: | |
| 601: | |
| 602: | |
| 603: | |
| 604: | |
| 605: | |
| 606: | |
| 607: | |
| 608: | |
| 609: | |
| 610: | |
| 611: | |
| 612: | |
| 613: | |
| 614: | public function GetGroups($UserId = null, $UUIDs = [], $Search = '') |
| 615: | { |
| 616: | $result = []; |
| 617: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 618: | |
| 619: | Api::CheckAccess($UserId); |
| 620: | |
| 621: | $aArgs = [ |
| 622: | 'UserId' => $UserId, |
| 623: | 'Storage' => StorageType::Personal, |
| 624: | 'AddressBookId' => 0 |
| 625: | ]; |
| 626: | |
| 627: | if ($this->populateContactArguments($aArgs)) { |
| 628: | $query = Capsule::connection()->table('contacts_cards') |
| 629: | ->join('adav_cards', 'contacts_cards.CardId', '=', 'adav_cards.id') |
| 630: | ->select('adav_cards.id as card_id', 'carddata'); |
| 631: | |
| 632: | $query->where(function ($whereQuery) use ($UserId, $aArgs, $query) { |
| 633: | $this->prepareFiltersFromStorage($UserId, StorageType::Personal, $aArgs['AddressBookId'], $query, $whereQuery); |
| 634: | })->where('IsGroup', true); |
| 635: | |
| 636: | if (is_array($UUIDs) && count($UUIDs) > 0) { |
| 637: | $query->whereIn('adav_cards.id', $UUIDs); |
| 638: | } |
| 639: | |
| 640: | if (!empty($Search)) { |
| 641: | $query->where('FullName', 'LIKE', "%$Search%"); |
| 642: | } |
| 643: | |
| 644: | $groups = $query->get(); |
| 645: | |
| 646: | foreach ($groups as $group) { |
| 647: | $groupObj = new Group(); |
| 648: | $groupObj->Id = (int) $group->card_id; |
| 649: | $groupObj->IdUser = $UserId; |
| 650: | $groupObj->populate(Helper::GetGroupDataFromVcard( |
| 651: | \Sabre\VObject\Reader::read( |
| 652: | $group->carddata, |
| 653: | \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES |
| 654: | ), |
| 655: | $group->card_id |
| 656: | )); |
| 657: | $result[] = $groupObj; |
| 658: | } |
| 659: | } |
| 660: | |
| 661: | return $result; |
| 662: | } |
| 663: | |
| 664: | |
| 665: | |
| 666: | |
| 667: | |
| 668: | |
| 669: | |
| 670: | |
| 671: | |
| 672: | |
| 673: | |
| 674: | |
| 675: | |
| 676: | |
| 677: | |
| 678: | |
| 679: | |
| 680: | |
| 681: | |
| 682: | |
| 683: | |
| 684: | |
| 685: | |
| 686: | |
| 687: | |
| 688: | |
| 689: | |
| 690: | |
| 691: | |
| 692: | |
| 693: | |
| 694: | |
| 695: | |
| 696: | |
| 697: | |
| 698: | |
| 699: | |
| 700: | |
| 701: | |
| 702: | |
| 703: | |
| 704: | |
| 705: | |
| 706: | |
| 707: | |
| 708: | |
| 709: | |
| 710: | |
| 711: | |
| 712: | |
| 713: | |
| 714: | |
| 715: | |
| 716: | |
| 717: | |
| 718: | |
| 719: | |
| 720: | |
| 721: | |
| 722: | |
| 723: | |
| 724: | |
| 725: | |
| 726: | |
| 727: | |
| 728: | |
| 729: | |
| 730: | |
| 731: | |
| 732: | public function GetGroup($UserId, $UUID) |
| 733: | { |
| 734: | $mResult = false; |
| 735: | |
| 736: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 737: | |
| 738: | Api::CheckAccess($UserId); |
| 739: | |
| 740: | $oUser = Api::getUserById($UserId); |
| 741: | if ($oUser instanceof \Aurora\Modules\Core\Models\User) { |
| 742: | $query = Capsule::connection()->table('contacts_cards') |
| 743: | ->join('adav_cards', 'contacts_cards.CardId', '=', 'adav_cards.id') |
| 744: | ->join('adav_addressbooks', 'adav_cards.addressbookid', '=', 'adav_addressbooks.id') |
| 745: | ->select('adav_cards.id as card_id', 'adav_cards.uri as card_uri', 'adav_addressbooks.id as addressbook_id', 'carddata'); |
| 746: | |
| 747: | $aArgs = [ |
| 748: | 'UUID' => $UUID, |
| 749: | 'UserId' => $UserId |
| 750: | ]; |
| 751: | |
| 752: | $query->where(function ($q) use ($aArgs, $query) { |
| 753: | $aArgs['Query'] = $query; |
| 754: | $this->broadcastEvent(self::GetName() . '::ContactQueryBuilder', $aArgs, $q); |
| 755: | }); |
| 756: | |
| 757: | $row = $query->where('contacts_cards.IsGroup', true)->first(); |
| 758: | if ($row) { |
| 759: | if (!self::Decorator()->CheckAccessToAddressBook($oUser, $row->addressbook_id, Access::Read)) { |
| 760: | throw new ApiException(Notifications::AccessDenied, null, 'AccessDenied'); |
| 761: | } |
| 762: | |
| 763: | $mResult = new Group(); |
| 764: | $mResult->IdUser = $UserId; |
| 765: | $mResult->Id = $row->card_id; |
| 766: | |
| 767: | $mResult->populate( |
| 768: | Helper::GetGroupDataFromVcard( |
| 769: | \Sabre\VObject\Reader::read( |
| 770: | $row->carddata, |
| 771: | \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES |
| 772: | ), |
| 773: | $row->card_uri |
| 774: | ) |
| 775: | ); |
| 776: | |
| 777: | $mResult->UUID = $UUID; |
| 778: | } |
| 779: | } |
| 780: | |
| 781: | return $mResult; |
| 782: | } |
| 783: | |
| 784: | |
| 785: | |
| 786: | |
| 787: | |
| 788: | |
| 789: | |
| 790: | |
| 791: | |
| 792: | |
| 793: | |
| 794: | |
| 795: | |
| 796: | |
| 797: | |
| 798: | |
| 799: | |
| 800: | |
| 801: | |
| 802: | |
| 803: | |
| 804: | |
| 805: | |
| 806: | |
| 807: | |
| 808: | |
| 809: | |
| 810: | |
| 811: | |
| 812: | |
| 813: | |
| 814: | |
| 815: | |
| 816: | |
| 817: | |
| 818: | |
| 819: | |
| 820: | |
| 821: | |
| 822: | |
| 823: | |
| 824: | |
| 825: | |
| 826: | |
| 827: | |
| 828: | |
| 829: | |
| 830: | |
| 831: | |
| 832: | |
| 833: | |
| 834: | |
| 835: | |
| 836: | |
| 837: | |
| 838: | |
| 839: | |
| 840: | |
| 841: | |
| 842: | |
| 843: | |
| 844: | |
| 845: | |
| 846: | |
| 847: | |
| 848: | |
| 849: | |
| 850: | |
| 851: | |
| 852: | |
| 853: | |
| 854: | |
| 855: | |
| 856: | |
| 857: | |
| 858: | |
| 859: | public function GetContacts($UserId, $Storage = '', $Offset = 0, $Limit = 20, $SortField = SortField::Name, $SortOrder = SortOrder::ASC, $Search = '', $GroupUUID = '', Builder $Filters = null, $WithGroups = false, $WithoutTeamContactsDuplicates = false, $Suggestions = false, $AddressBookId = null) |
| 860: | { |
| 861: | |
| 862: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 863: | |
| 864: | Api::CheckAccess($UserId); |
| 865: | |
| 866: | $oUser = Api::getUserById($UserId); |
| 867: | $aContacts = []; |
| 868: | if (self::Decorator()->CheckAccessToAddressBook($oUser, $AddressBookId, Access::Read)) { |
| 869: | $query = $this->getGetContactsQueryBuilder($UserId, $Storage, $AddressBookId, $Filters, $Suggestions); |
| 870: | |
| 871: | if (!empty($Search)) { |
| 872: | $query = $query->where(function ($query) use ($Search) { |
| 873: | $query->where('FullName', 'LIKE', "%$Search%") |
| 874: | ->orWhere('PersonalEmail', 'LIKE', "%$Search%") |
| 875: | ->orWhere('BusinessEmail', 'LIKE', "%$Search%") |
| 876: | ->orWhere('OtherEmail', 'LIKE', "%$Search%") |
| 877: | ->orWhere('BusinessCompany', 'LIKE', "%$Search%"); |
| 878: | }); |
| 879: | } |
| 880: | |
| 881: | if (!empty($GroupUUID)) { |
| 882: | $oGroup = self::Decorator()->GetGroup($UserId, $GroupUUID); |
| 883: | if ($oGroup) { |
| 884: | $contacts = $oGroup->Contacts; |
| 885: | if (count($contacts) === 0) { |
| 886: | $contacts = [null]; |
| 887: | } |
| 888: | $query->whereIn('adav_cards.id', $contacts); |
| 889: | } |
| 890: | } |
| 891: | |
| 892: | $count = $query->count(); |
| 893: | |
| 894: | $aContactsCollection = $this->getContactsCollection($SortField, $SortOrder, $Offset, $Limit, $query); |
| 895: | |
| 896: | if ($Storage === StorageType::All) { |
| 897: | $personalContacsCollection = $aContactsCollection->filter(function ($contact) { |
| 898: | return !$contact->IsTeam && !$contact->Shared; |
| 899: | }); |
| 900: | |
| 901: | if ($WithoutTeamContactsDuplicates) { |
| 902: | $aContactsCollection->each(function ($contact, $key) use (&$aContactsCollection, $personalContacsCollection) { |
| 903: | if ($contact->IsTeam && $personalContacsCollection->unique()->contains('ViewEmail', $contact->ViewEmail)) { |
| 904: | $aContactsCollection->forget($key); |
| 905: | } elseif ($contact->Auto) { |
| 906: | $aContactsCollection->each(function (&$subContact) use (&$aContactsCollection, $contact, $key) { |
| 907: | if ($subContact->IsTeam && $subContact->ViewEmail === $contact->ViewEmail) { |
| 908: | $subContact->AgeScore = $contact->AgeScore; |
| 909: | $aContactsCollection->forget($key); |
| 910: | } |
| 911: | if (!$contact->IsTeam && !$contact->Shared && !$contact->Auto && $subContact->ViewEmail === $contact->ViewEmail) { |
| 912: | $aContactsCollection->forget($key); |
| 913: | } |
| 914: | }); |
| 915: | } |
| 916: | }); |
| 917: | } else { |
| 918: | $aContactsCollection->each(function (&$contact, $key) use (&$aContactsCollection, $personalContacsCollection) { |
| 919: | if ($contact->IsTeam) { |
| 920: | $personalContact = $personalContacsCollection->unique()->filter(function ($subContact) use (&$contact) { |
| 921: | return strtolower($contact->ViewEmail) === strtolower($subContact->ViewEmail); |
| 922: | })->first(); |
| 923: | |
| 924: | if ($personalContact) { |
| 925: | $contact->Frequency = $personalContact->Frequency; |
| 926: | if ($contact->Auto) { |
| 927: | $aContactsCollection = $aContactsCollection->filter(function ($subContact) use ($contact) { |
| 928: | return (strtolower($subContact->ViewEmail) === strtolower($contact->ViewEmail) && !$contact->Auto) || |
| 929: | strtolower($subContact->ViewEmail) !== strtolower($contact->ViewEmail); |
| 930: | }); |
| 931: | } |
| 932: | } |
| 933: | } |
| 934: | }); |
| 935: | } |
| 936: | } |
| 937: | |
| 938: | $this->resolveAddressbooksIdsForContacts($oUser, $aContactsCollection); |
| 939: | |
| 940: | |
| 941: | $aContactsCollection->each(function ($contact) use ($UserId) { |
| 942: | if (!$contact->UserId) { |
| 943: | $contact->UserId = $UserId; |
| 944: | } |
| 945: | }); |
| 946: | |
| 947: | $aContacts = $aContactsCollection->toArray(); |
| 948: | if ($WithGroups) { |
| 949: | $groups = self::Decorator()->GetGroups($UserId, [], $Search); |
| 950: | |
| 951: | if (is_array($groups) && count($groups) > 0) { |
| 952: | $groupContactsUuids = []; |
| 953: | $contactsUuids = []; |
| 954: | array_map(function ($item) use (&$groupContactsUuids, &$contactsUuids) { |
| 955: | if (is_array($item->Contacts) && count($item->Contacts) > 0) { |
| 956: | $groupContactsUuids[$item->UUID] = $item->Contacts; |
| 957: | $contactsUuids = array_merge($contactsUuids, $item->Contacts); |
| 958: | } |
| 959: | }, $groups); |
| 960: | |
| 961: | $groupContacts = []; |
| 962: | $contactsUuids = array_unique($contactsUuids); |
| 963: | |
| 964: | if (count($contactsUuids) > 0) { |
| 965: | foreach (self::Decorator()->GetContactsByUids($UserId, $contactsUuids) as $groupContact) { |
| 966: | $groupContacts[$groupContact->UUID] = $groupContact; |
| 967: | } |
| 968: | |
| 969: | $aGroupUsersList = []; |
| 970: | |
| 971: | foreach ($groups as $group) { |
| 972: | $aGroupContactsEmails = []; |
| 973: | if (is_array($group->Contacts)) { |
| 974: | foreach ($group->Contacts as $contactUuid) { |
| 975: | if (isset($groupContacts[$contactUuid])) { |
| 976: | $oContact = $groupContacts[$contactUuid]; |
| 977: | $aGroupContactsEmails[] = $oContact->FullName ? "\"{$oContact->FullName}\" <{$oContact->ViewEmail}>" : $oContact->ViewEmail; |
| 978: | } |
| 979: | } |
| 980: | |
| 981: | $aGroupUsersList[] = [ |
| 982: | 'UUID' => (string)$group->UUID, |
| 983: | 'IdUser' => $group->IdUser, |
| 984: | 'FullName' => $group->Name, |
| 985: | 'FirstName' => '', |
| 986: | 'LastName' => '', |
| 987: | 'ViewEmail' => implode(', ', $aGroupContactsEmails), |
| 988: | 'Storage' => '', |
| 989: | 'Frequency' => 0, |
| 990: | 'DateModified' => '', |
| 991: | 'IsGroup' => true, |
| 992: | ]; |
| 993: | } |
| 994: | } |
| 995: | $aContacts = array_merge($aContacts, $aGroupUsersList); |
| 996: | } |
| 997: | } |
| 998: | } |
| 999: | } else { |
| 1000: | throw new ApiException(Notifications::AccessDenied, null, 'AccessDenied'); |
| 1001: | } |
| 1002: | |
| 1003: | return [ |
| 1004: | 'ContactCount' => $count, |
| 1005: | 'List' => \Aurora\System\Managers\Response::GetResponseObject(array_values($aContacts)) |
| 1006: | ]; |
| 1007: | } |
| 1008: | |
| 1009: | public function GetContactSuggestions($UserId, $Storage, $Limit = 20, $SortField = SortField::Name, $SortOrder = SortOrder::ASC, $Search = '', $WithGroups = false, $WithoutTeamContactsDuplicates = false, $WithUserGroups = false) |
| 1010: | { |
| 1011: | $WithoutTeamContactsDuplicates = false; |
| 1012: | |
| 1013: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 1014: | |
| 1015: | Api::CheckAccess($UserId); |
| 1016: | |
| 1017: | $aResult = array( |
| 1018: | 'ContactCount' => 0, |
| 1019: | 'List' => [] |
| 1020: | ); |
| 1021: | |
| 1022: | $aContacts = $this->Decorator()->GetContacts($UserId, $Storage, 0, $Limit, $SortField, $SortOrder, $Search, '', null, $WithGroups, $WithoutTeamContactsDuplicates, true); |
| 1023: | $aResultList = $aContacts['List']; |
| 1024: | |
| 1025: | $aResult['List'] = $aResultList; |
| 1026: | $aResult['ContactCount'] = count($aResultList); |
| 1027: | |
| 1028: | if ($WithUserGroups) { |
| 1029: | $oUser = CoreModule::Decorator()->GetUserWithoutRoleCheck($UserId); |
| 1030: | if ($oUser) { |
| 1031: | $aGroups = CoreModule::Decorator()->GetGroups($oUser->IdTenant, $Search); |
| 1032: | foreach ($aGroups['Items'] as $aGroup) { |
| 1033: | $aGroup['IsGroup'] = true; |
| 1034: | $aResult['List'][] = $aGroup; |
| 1035: | |
| 1036: | $aResult['ContactCount']++; |
| 1037: | } |
| 1038: | } |
| 1039: | } |
| 1040: | |
| 1041: | return $aResult; |
| 1042: | } |
| 1043: | |
| 1044: | |
| 1045: | |
| 1046: | |
| 1047: | public function CheckAccessToObject($User, $Contact, $Access = null) |
| 1048: | { |
| 1049: | return true; |
| 1050: | } |
| 1051: | |
| 1052: | public function CheckAccessToAddressBook($User, $AddressBookId, $Access = null) |
| 1053: | { |
| 1054: | return true; |
| 1055: | } |
| 1056: | |
| 1057: | |
| 1058: | |
| 1059: | |
| 1060: | |
| 1061: | |
| 1062: | |
| 1063: | |
| 1064: | |
| 1065: | |
| 1066: | |
| 1067: | |
| 1068: | |
| 1069: | |
| 1070: | |
| 1071: | |
| 1072: | |
| 1073: | |
| 1074: | |
| 1075: | |
| 1076: | |
| 1077: | |
| 1078: | |
| 1079: | |
| 1080: | |
| 1081: | |
| 1082: | |
| 1083: | |
| 1084: | |
| 1085: | |
| 1086: | |
| 1087: | |
| 1088: | |
| 1089: | |
| 1090: | |
| 1091: | |
| 1092: | |
| 1093: | |
| 1094: | |
| 1095: | |
| 1096: | |
| 1097: | |
| 1098: | |
| 1099: | |
| 1100: | |
| 1101: | |
| 1102: | |
| 1103: | |
| 1104: | |
| 1105: | |
| 1106: | |
| 1107: | |
| 1108: | |
| 1109: | |
| 1110: | |
| 1111: | |
| 1112: | |
| 1113: | |
| 1114: | |
| 1115: | |
| 1116: | |
| 1117: | |
| 1118: | public function GetContact($UUID, $UserId = null) |
| 1119: | { |
| 1120: | $mResult = false; |
| 1121: | |
| 1122: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 1123: | |
| 1124: | Api::CheckAccess($UserId); |
| 1125: | |
| 1126: | $aContacts = self::Decorator()->GetContactsByUids($UserId, [$UUID]); |
| 1127: | |
| 1128: | if (count($aContacts) > 0) { |
| 1129: | $mResult = $aContacts[0]; |
| 1130: | } |
| 1131: | |
| 1132: | return $mResult; |
| 1133: | } |
| 1134: | |
| 1135: | |
| 1136: | |
| 1137: | |
| 1138: | |
| 1139: | |
| 1140: | |
| 1141: | |
| 1142: | |
| 1143: | |
| 1144: | |
| 1145: | |
| 1146: | |
| 1147: | |
| 1148: | |
| 1149: | |
| 1150: | |
| 1151: | |
| 1152: | |
| 1153: | |
| 1154: | |
| 1155: | |
| 1156: | |
| 1157: | |
| 1158: | |
| 1159: | |
| 1160: | |
| 1161: | |
| 1162: | |
| 1163: | |
| 1164: | |
| 1165: | |
| 1166: | |
| 1167: | |
| 1168: | |
| 1169: | |
| 1170: | |
| 1171: | |
| 1172: | |
| 1173: | |
| 1174: | |
| 1175: | |
| 1176: | |
| 1177: | |
| 1178: | |
| 1179: | |
| 1180: | |
| 1181: | |
| 1182: | |
| 1183: | |
| 1184: | |
| 1185: | |
| 1186: | |
| 1187: | |
| 1188: | |
| 1189: | |
| 1190: | |
| 1191: | |
| 1192: | |
| 1193: | |
| 1194: | |
| 1195: | |
| 1196: | |
| 1197: | |
| 1198: | public function GetContactsByEmails($UserId, $Storage, $Emails, $AddressBookId = null) |
| 1199: | { |
| 1200: | $result = []; |
| 1201: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 1202: | |
| 1203: | Api::CheckAccess($UserId); |
| 1204: | $oUser = Api::getUserById($UserId); |
| 1205: | |
| 1206: | if (self::Decorator()->CheckAccessToAddressBook($oUser, $AddressBookId, Access::Read)) { |
| 1207: | $filter = ContactCard::whereIn('ViewEmail', $Emails); |
| 1208: | $query = $this->getGetContactsQueryBuilder($UserId, $Storage, $AddressBookId, $filter); |
| 1209: | $result = $query->get(); |
| 1210: | $this->resolveAddressbooksIdsForContacts($oUser, $result); |
| 1211: | } else { |
| 1212: | throw new ApiException(Notifications::AccessDenied, null, 'AccessDenied'); |
| 1213: | } |
| 1214: | |
| 1215: | return $result; |
| 1216: | } |
| 1217: | |
| 1218: | |
| 1219: | |
| 1220: | |
| 1221: | |
| 1222: | |
| 1223: | |
| 1224: | public function GetContactsByUids($UserId, $Uids) |
| 1225: | { |
| 1226: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 1227: | |
| 1228: | Api::CheckAccess($UserId); |
| 1229: | |
| 1230: | $oUser = Api::getUserById($UserId); |
| 1231: | |
| 1232: | $mResult = []; |
| 1233: | |
| 1234: | if ($oUser instanceof \Aurora\Modules\Core\Models\User) { |
| 1235: | $query = Capsule::connection()->table('contacts_cards') |
| 1236: | ->select('adav_cards.id as CardId', 'adav_cards.uri as card_uri', 'adav_addressbooks.id as addressbook_id', 'contacts_cards.Properties', 'carddata', 'etag', 'core_users.Id as UserId') |
| 1237: | ->join('adav_cards', 'contacts_cards.CardId', '=', 'adav_cards.id') |
| 1238: | ->join('adav_addressbooks', 'adav_cards.addressbookid', '=', 'adav_addressbooks.id') |
| 1239: | ->leftJoin('core_users', 'adav_addressbooks.principaluri', '=', Capsule::connection()->raw("CONCAT('principals/', " . Capsule::connection()->getTablePrefix() . "core_users.PublicId)")); |
| 1240: | |
| 1241: | $aArgs = [ |
| 1242: | 'UUID' => $Uids, |
| 1243: | 'UserId' => $UserId |
| 1244: | ]; |
| 1245: | $query->where(function ($q) use ($aArgs, $query) { |
| 1246: | $aArgs['Query'] = $query; |
| 1247: | $this->broadcastEvent(self::GetName() . '::ContactQueryBuilder', $aArgs, $q); |
| 1248: | }); |
| 1249: | |
| 1250: | $rows = $query->get(); |
| 1251: | foreach($rows as $row) { |
| 1252: | if (!self::Decorator()->CheckAccessToAddressBook($oUser, $row->addressbook_id, Access::Read)) { |
| 1253: | continue; |
| 1254: | } |
| 1255: | |
| 1256: | $oContact = new Contact(); |
| 1257: | $oContact->Id = $row->CardId; |
| 1258: | $oContact->InitFromVCardStr($row->UserId, $row->carddata); |
| 1259: | $oContact->ETag = \trim($row->etag, '"'); |
| 1260: | |
| 1261: | $storagesMapToAddressbooks = self::Decorator()->GetStoragesMapToAddressbooks(); |
| 1262: | $addressbook = Backend::Carddav()->getAddressBookById($row->addressbook_id); |
| 1263: | |
| 1264: | $key = false; |
| 1265: | if ($addressbook) { |
| 1266: | $key = array_search($addressbook['uri'], $storagesMapToAddressbooks); |
| 1267: | } |
| 1268: | |
| 1269: | $oContact->Storage = $key !== false ? $key : StorageType::AddressBook; |
| 1270: | $oContact->AddressBookId = (int) $row->addressbook_id; |
| 1271: | if ($row->Properties) { |
| 1272: | $oContact->Properties = \json_decode($row->Properties, true); |
| 1273: | } |
| 1274: | $groups = self::Decorator()->GetGroups($UserId); |
| 1275: | foreach ($groups as $group) { |
| 1276: | if (in_array($row->CardId, $group->Contacts)) { |
| 1277: | $oContact->GroupUUIDs[] = $group->UUID; |
| 1278: | } |
| 1279: | } |
| 1280: | |
| 1281: | $mResult[] = $oContact; |
| 1282: | } |
| 1283: | } |
| 1284: | |
| 1285: | return $mResult; |
| 1286: | } |
| 1287: | |
| 1288: | |
| 1289: | |
| 1290: | |
| 1291: | |
| 1292: | |
| 1293: | |
| 1294: | |
| 1295: | public function GetContactsInfo($Storage, $UserId = null, Builder $Filters = null) |
| 1296: | { |
| 1297: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 1298: | |
| 1299: | Api::CheckAccess($UserId); |
| 1300: | |
| 1301: | $aResult = [ |
| 1302: | 'CTag' => 0, |
| 1303: | 'Info' => [] |
| 1304: | ]; |
| 1305: | |
| 1306: | $aArgs = [ |
| 1307: | 'UserId' => $UserId, |
| 1308: | 'Storage' => $Storage, |
| 1309: | 'AddressBookId' => 0 |
| 1310: | ]; |
| 1311: | |
| 1312: | if ($this->populateContactArguments($aArgs)) { |
| 1313: | if ((int) $aArgs['AddressBookId'] > 0) { |
| 1314: | $addressbook = Backend::Carddav()->getAddressBookById($aArgs['AddressBookId']); |
| 1315: | |
| 1316: | if ($addressbook) { |
| 1317: | $aResult['CTag'] = (int) $addressbook['{http://sabredav.org/ns}sync-token']; |
| 1318: | } |
| 1319: | } |
| 1320: | $query = $this->getGetContactsQueryBuilder($UserId, $Storage, $aArgs['AddressBookId'], $Filters); |
| 1321: | |
| 1322: | $aContacts = $query->get(['UUID', 'ETag', 'Auto', 'Storage']); |
| 1323: | |
| 1324: | $storagesMapToAddressbooks = self::Decorator()->GetStoragesMapToAddressbooks(); |
| 1325: | |
| 1326: | foreach ($aContacts as $oContact) { |
| 1327: | $StorageTextId = false; |
| 1328: | if (!empty($addressbook)) { |
| 1329: | $StorageTextId = array_search($addressbook['uri'], $storagesMapToAddressbooks); |
| 1330: | } |
| 1331: | |
| 1332: | |
| 1333: | |
| 1334: | |
| 1335: | $aResult['Info'][] = [ |
| 1336: | 'UUID' => (string) $oContact->UUID, |
| 1337: | 'ETag' => $oContact->ETag, |
| 1338: | 'Storage' => $StorageTextId ? $StorageTextId : (string) $oContact->Storage, |
| 1339: | 'IsTeam' => $oContact->IsTeam, |
| 1340: | 'Shared' => $oContact->Shared, |
| 1341: | ]; |
| 1342: | } |
| 1343: | } |
| 1344: | |
| 1345: | return $aResult; |
| 1346: | } |
| 1347: | |
| 1348: | |
| 1349: | |
| 1350: | |
| 1351: | |
| 1352: | |
| 1353: | |
| 1354: | |
| 1355: | |
| 1356: | |
| 1357: | |
| 1358: | |
| 1359: | |
| 1360: | |
| 1361: | |
| 1362: | |
| 1363: | |
| 1364: | |
| 1365: | |
| 1366: | |
| 1367: | |
| 1368: | |
| 1369: | |
| 1370: | |
| 1371: | |
| 1372: | |
| 1373: | |
| 1374: | |
| 1375: | |
| 1376: | |
| 1377: | |
| 1378: | |
| 1379: | |
| 1380: | |
| 1381: | |
| 1382: | |
| 1383: | |
| 1384: | |
| 1385: | |
| 1386: | |
| 1387: | |
| 1388: | |
| 1389: | |
| 1390: | |
| 1391: | |
| 1392: | |
| 1393: | |
| 1394: | |
| 1395: | |
| 1396: | |
| 1397: | |
| 1398: | |
| 1399: | |
| 1400: | |
| 1401: | |
| 1402: | |
| 1403: | |
| 1404: | |
| 1405: | |
| 1406: | |
| 1407: | |
| 1408: | |
| 1409: | |
| 1410: | |
| 1411: | public function CreateContact($Contact, $UserId = null) |
| 1412: | { |
| 1413: | Api::CheckAccess($UserId); |
| 1414: | |
| 1415: | $oUser = CoreModule::getInstance()->GetUserWithoutRoleCheck($UserId); |
| 1416: | |
| 1417: | $mResult = false; |
| 1418: | |
| 1419: | if ($oUser instanceof \Aurora\Modules\Core\Models\User) { |
| 1420: | $oContact = new Classes\Contact(); |
| 1421: | $oContact->IdUser = $oUser->Id; |
| 1422: | $oContact->IdTenant = $oUser->IdTenant; |
| 1423: | $oContact->populate($Contact); |
| 1424: | |
| 1425: | $oContact->Frequency = $this->getAutocreatedContactFrequencyAndDeleteIt($oUser->Id, $oContact->ViewEmail); |
| 1426: | |
| 1427: | $oVCard = new \Sabre\VObject\Component\VCard(); |
| 1428: | Helper::UpdateVCardFromContact($oContact, $oVCard); |
| 1429: | |
| 1430: | if (self::Decorator()->CheckAccessToAddressBook($oUser, $oContact->AddressBookId, Access::Write)) { |
| 1431: | $cardUri = $oContact->UUID . '.vcf'; |
| 1432: | $cardETag = Backend::Carddav()->createCard($oContact->AddressBookId, $cardUri, $oVCard->serialize()); |
| 1433: | |
| 1434: | if ($cardETag) { |
| 1435: | $newCard = Backend::Carddav()->getCard($oContact->AddressBookId, $cardUri); |
| 1436: | if ($newCard) { |
| 1437: | ContactCard::where('CardId', $newCard['id'])->update(['Frequency' => $oContact->Frequency]); |
| 1438: | |
| 1439: | if (is_array($oContact->GroupUUIDs) && count($oContact->GroupUUIDs) > 0) { |
| 1440: | $oGroups = self::Decorator()->GetGroups($UserId, $oContact->GroupUUIDs); |
| 1441: | if ($oGroups) { |
| 1442: | foreach ($oGroups as $oGroup) { |
| 1443: | $oGroup->Contacts = array_merge($oGroup->Contacts, [(string) $newCard['id']]); |
| 1444: | |
| 1445: | $this->UpdateGroupObject($UserId, $oGroup); |
| 1446: | } |
| 1447: | } |
| 1448: | } |
| 1449: | |
| 1450: | $mResult = [ |
| 1451: | 'UUID' => (string) $newCard['id'], |
| 1452: | 'ETag' => \trim($newCard['etag'], '"') |
| 1453: | ]; |
| 1454: | } |
| 1455: | } |
| 1456: | } else { |
| 1457: | throw new ApiException(Notifications::AccessDenied, null, 'AccessDenied'); |
| 1458: | } |
| 1459: | } |
| 1460: | |
| 1461: | return $mResult; |
| 1462: | } |
| 1463: | |
| 1464: | |
| 1465: | |
| 1466: | |
| 1467: | |
| 1468: | |
| 1469: | |
| 1470: | private function getAutocreatedContactFrequencyAndDeleteIt($UserId, $sViewEmail) |
| 1471: | { |
| 1472: | Api::CheckAccess($UserId); |
| 1473: | |
| 1474: | $iFrequency = 0; |
| 1475: | |
| 1476: | $aArgs = [ |
| 1477: | 'UserId' => $UserId, |
| 1478: | 'Storage' => StorageType::Collected, |
| 1479: | 'AddressBookId' => 0 |
| 1480: | ]; |
| 1481: | |
| 1482: | if ($this->populateContactArguments($aArgs)) { |
| 1483: | $oQuery = ContactCard::where([ |
| 1484: | ['AddressBookId', '=', $aArgs['AddressBookId']], |
| 1485: | ['ViewEmail', '=', $sViewEmail] |
| 1486: | ]); |
| 1487: | |
| 1488: | $oAutocreatedContacts = $this->getContactsCollection( |
| 1489: | SortField::Name, |
| 1490: | SortOrder::ASC, |
| 1491: | 0, |
| 1492: | 1, |
| 1493: | $oQuery |
| 1494: | ); |
| 1495: | $oContact = $oAutocreatedContacts->first(); |
| 1496: | if ($oContact instanceof ContactCard) { |
| 1497: | $card_uri = Capsule::connection()->table('adav_cards') |
| 1498: | ->where('id', $oContact->CardId) |
| 1499: | ->pluck('uri')->first(); |
| 1500: | |
| 1501: | Backend::Carddav()->deleteCard($oContact->AddressBookId, $card_uri); |
| 1502: | $iFrequency = $oContact->Frequency; |
| 1503: | } |
| 1504: | } |
| 1505: | |
| 1506: | return $iFrequency; |
| 1507: | } |
| 1508: | |
| 1509: | |
| 1510: | |
| 1511: | |
| 1512: | |
| 1513: | |
| 1514: | |
| 1515: | |
| 1516: | |
| 1517: | |
| 1518: | |
| 1519: | |
| 1520: | |
| 1521: | |
| 1522: | |
| 1523: | |
| 1524: | |
| 1525: | |
| 1526: | |
| 1527: | |
| 1528: | |
| 1529: | |
| 1530: | |
| 1531: | |
| 1532: | |
| 1533: | |
| 1534: | |
| 1535: | |
| 1536: | |
| 1537: | |
| 1538: | |
| 1539: | |
| 1540: | |
| 1541: | |
| 1542: | |
| 1543: | |
| 1544: | |
| 1545: | |
| 1546: | |
| 1547: | |
| 1548: | |
| 1549: | |
| 1550: | |
| 1551: | |
| 1552: | |
| 1553: | |
| 1554: | |
| 1555: | |
| 1556: | |
| 1557: | |
| 1558: | |
| 1559: | |
| 1560: | |
| 1561: | |
| 1562: | |
| 1563: | |
| 1564: | |
| 1565: | |
| 1566: | |
| 1567: | |
| 1568: | |
| 1569: | |
| 1570: | public function UpdateContact($UserId, $Contact) |
| 1571: | { |
| 1572: | Api::CheckAccess($UserId); |
| 1573: | |
| 1574: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 1575: | |
| 1576: | $oContact = self::Decorator()->GetContact($Contact['UUID'], $UserId); |
| 1577: | $oUser = Api::getUserById($UserId); |
| 1578: | if ($oContact && self::Decorator()->CheckAccessToObject($oUser, $oContact, Access::Write)) { |
| 1579: | $oContact->populate($Contact); |
| 1580: | $result = self::Decorator()->UpdateContactObject($oContact); |
| 1581: | if ($result) { |
| 1582: | if (is_array($oContact->GroupUUIDs)) { |
| 1583: | $groups = self::Decorator()->GetGroups($UserId); |
| 1584: | foreach ($groups as $group) { |
| 1585: | if ($group) { |
| 1586: | if (!in_array($group->UUID, $oContact->GroupUUIDs)) { |
| 1587: | $group->Contacts = array_diff($group->Contacts, [$oContact->UUID]); |
| 1588: | } else { |
| 1589: | $group->Contacts = array_merge($group->Contacts, [$oContact->UUID]); |
| 1590: | } |
| 1591: | $this->UpdateGroupObject($UserId, $group); |
| 1592: | } |
| 1593: | } |
| 1594: | } |
| 1595: | |
| 1596: | return [ |
| 1597: | 'UUID' => (string) $oContact->UUID, |
| 1598: | 'ETag' => $result |
| 1599: | ]; |
| 1600: | } else { |
| 1601: | return false; |
| 1602: | } |
| 1603: | } else { |
| 1604: | throw new ApiException(Notifications::AccessDenied, null, 'AccessDenied'); |
| 1605: | } |
| 1606: | |
| 1607: | return false; |
| 1608: | } |
| 1609: | |
| 1610: | public function MoveContactsToStorage($UserId, $FromStorage, $ToStorage, $UUIDs) |
| 1611: | { |
| 1612: | $result = false; |
| 1613: | |
| 1614: | if ($ToStorage === StorageType::Team) { |
| 1615: | return false; |
| 1616: | } |
| 1617: | |
| 1618: | $query = Capsule::connection() |
| 1619: | ->table('contacts_cards') |
| 1620: | ->join('adav_cards', 'contacts_cards.CardId', '=', 'adav_cards.id') |
| 1621: | ->join('adav_addressbooks', 'adav_cards.addressbookid', '=', 'adav_addressbooks.id') |
| 1622: | ->select('adav_cards.uri as card_uri', 'adav_cards.id as card_id', 'adav_addressbooks.id as addressbook_id'); |
| 1623: | |
| 1624: | $aArgs = [ |
| 1625: | 'UserId' => $UserId, |
| 1626: | 'UUID' => $UUIDs |
| 1627: | ]; |
| 1628: | |
| 1629: | |
| 1630: | $cardsUris = $query->where(function ($q) use ($aArgs, $query) { |
| 1631: | $aArgs['Query'] = $query; |
| 1632: | $this->broadcastEvent(self::GetName() . '::ContactQueryBuilder', $aArgs, $q); |
| 1633: | })->pluck('card_uri', 'card_id')->toArray(); |
| 1634: | |
| 1635: | $aArgsTo = [ |
| 1636: | 'UserId' => $UserId, |
| 1637: | 'Storage' => $ToStorage, |
| 1638: | 'AddressBookId' => 0 |
| 1639: | ]; |
| 1640: | |
| 1641: | $resultFrom = true; |
| 1642: | $resultTo = $this->populateContactArguments($aArgsTo); |
| 1643: | |
| 1644: | $ToAddressBookId = (int) $aArgsTo['AddressBookId']; |
| 1645: | |
| 1646: | foreach ($cardsUris as $cardId => $cardUri) { |
| 1647: | $FromAddressBookId = 0; |
| 1648: | if ($FromStorage === StorageType::All) { |
| 1649: | $oContact = self::Decorator()->GetContact($cardId, $UserId); |
| 1650: | if ($oContact instanceof Contact) { |
| 1651: | if ($oContact->Storage === StorageType::Team) { |
| 1652: | continue; |
| 1653: | } |
| 1654: | $FromAddressBookId = (int) $oContact->AddressBookId; |
| 1655: | } |
| 1656: | } else { |
| 1657: | $aArgsFrom = [ |
| 1658: | 'UserId' => $UserId, |
| 1659: | 'Storage' => $FromStorage, |
| 1660: | 'AddressBookId' => 0 |
| 1661: | ]; |
| 1662: | |
| 1663: | $resultFrom = $this->populateContactArguments($aArgsFrom); |
| 1664: | |
| 1665: | $FromAddressBookId = (int) $aArgsFrom['AddressBookId']; |
| 1666: | } |
| 1667: | if ($FromAddressBookId != $ToAddressBookId && $resultFrom && $resultTo) { |
| 1668: | $result = Backend::Carddav()->updateCardAddressBook($FromAddressBookId, $ToAddressBookId, $cardUri); |
| 1669: | } |
| 1670: | } |
| 1671: | |
| 1672: | return $result; |
| 1673: | } |
| 1674: | |
| 1675: | |
| 1676: | |
| 1677: | |
| 1678: | |
| 1679: | |
| 1680: | |
| 1681: | public function UpdateContactObject($Contact) |
| 1682: | { |
| 1683: | $mResult = false; |
| 1684: | |
| 1685: | $oUser = \Aurora\System\Api::getAuthenticatedUser(); |
| 1686: | $aStorageParts = \explode('-', $Contact->Storage); |
| 1687: | if (isset($aStorageParts[0], $aStorageParts[1]) && $aStorageParts[0] === StorageType::AddressBook) { |
| 1688: | $Contact->AddressBookId = (int) $aStorageParts[1]; |
| 1689: | $Contact->Storage = StorageType::AddressBook; |
| 1690: | } |
| 1691: | |
| 1692: | $query = Capsule::connection()->table('contacts_cards') |
| 1693: | ->join('adav_cards', 'contacts_cards.CardId', '=', 'adav_cards.id') |
| 1694: | ->join('adav_addressbooks', 'adav_cards.addressbookid', '=', 'adav_addressbooks.id') |
| 1695: | ->select('adav_cards.uri as card_uri', 'adav_addressbooks.id as addressbook_id', 'carddata'); |
| 1696: | |
| 1697: | $aArgs = [ |
| 1698: | 'UserId' => $oUser->Id, |
| 1699: | 'UUID' => $Contact->Id |
| 1700: | ]; |
| 1701: | |
| 1702: | |
| 1703: | $query->where(function ($q) use ($aArgs, $query) { |
| 1704: | $aArgs['Query'] = $query; |
| 1705: | $this->broadcastEvent(self::GetName() . '::ContactQueryBuilder', $aArgs, $q); |
| 1706: | }); |
| 1707: | |
| 1708: | $row = $query->first(); |
| 1709: | if ($row) { |
| 1710: | $oVCard = \Sabre\VObject\Reader::read($row->carddata); |
| 1711: | $uidVal = $oVCard->UID->getValue(); |
| 1712: | if (empty($uidVal) || is_numeric($uidVal)) { |
| 1713: | $uriInfo = pathinfo($row->card_uri); |
| 1714: | if (isset($uriInfo['filename'])) { |
| 1715: | $oVCard->UID = $uriInfo['filename']; |
| 1716: | } |
| 1717: | } |
| 1718: | |
| 1719: | Helper::UpdateVCardFromContact($Contact, $oVCard); |
| 1720: | $mResult = Backend::Carddav()->updateCard($row->addressbook_id, $row->card_uri, $oVCard->serialize()); |
| 1721: | $mResult = str_replace('"', '', $mResult); |
| 1722: | } |
| 1723: | |
| 1724: | return $mResult; |
| 1725: | } |
| 1726: | |
| 1727: | |
| 1728: | |
| 1729: | |
| 1730: | |
| 1731: | |
| 1732: | |
| 1733: | |
| 1734: | |
| 1735: | |
| 1736: | |
| 1737: | |
| 1738: | |
| 1739: | |
| 1740: | |
| 1741: | |
| 1742: | |
| 1743: | |
| 1744: | |
| 1745: | |
| 1746: | |
| 1747: | |
| 1748: | |
| 1749: | |
| 1750: | |
| 1751: | |
| 1752: | |
| 1753: | |
| 1754: | |
| 1755: | |
| 1756: | |
| 1757: | |
| 1758: | |
| 1759: | |
| 1760: | |
| 1761: | |
| 1762: | |
| 1763: | |
| 1764: | |
| 1765: | |
| 1766: | |
| 1767: | |
| 1768: | |
| 1769: | |
| 1770: | |
| 1771: | |
| 1772: | |
| 1773: | |
| 1774: | |
| 1775: | |
| 1776: | |
| 1777: | |
| 1778: | |
| 1779: | |
| 1780: | |
| 1781: | |
| 1782: | public function DeleteContacts($UserId, $Storage, $UUIDs) |
| 1783: | { |
| 1784: | $mResult = false; |
| 1785: | Api::CheckAccess($UserId); |
| 1786: | $oUser = Api::getUserById($UserId); |
| 1787: | |
| 1788: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 1789: | |
| 1790: | $AddressBookId = $Storage; |
| 1791: | if (self::Decorator()->CheckAccessToAddressBook($oUser, $AddressBookId, Enums\Access::Write)) { |
| 1792: | $query = Capsule::connection()->table('contacts_cards') |
| 1793: | ->join('adav_cards', 'contacts_cards.CardId', '=', 'adav_cards.id') |
| 1794: | ->join('adav_addressbooks', 'adav_cards.addressbookid', '=', 'adav_addressbooks.id') |
| 1795: | ->select('adav_cards.id as card_id', 'adav_cards.uri as card_uri', 'adav_addressbooks.id as addressbook_id') |
| 1796: | ->where('adav_cards.addressbookid', '=', $AddressBookId); |
| 1797: | |
| 1798: | $aArgs = [ |
| 1799: | 'UUID' => $UUIDs, |
| 1800: | 'UserId' => $UserId |
| 1801: | ]; |
| 1802: | $query->where(function ($q) use ($aArgs, $query) { |
| 1803: | $aArgs['Query'] = $query; |
| 1804: | $this->broadcastEvent(self::GetName() . '::ContactQueryBuilder', $aArgs, $q); |
| 1805: | }); |
| 1806: | |
| 1807: | $rows = $query->distinct()->get()->all(); |
| 1808: | |
| 1809: | $groups = self::Decorator()->GetGroups($UserId); |
| 1810: | $groupsToUpdate = []; |
| 1811: | |
| 1812: | foreach ($rows as $row) { |
| 1813: | Backend::Carddav()->deleteCard($row->addressbook_id, $row->card_uri); |
| 1814: | foreach ($groups as $group) { |
| 1815: | if (($key = array_search($row->card_id, $group->Contacts)) !== false) { |
| 1816: | unset($group->Contacts[$key]); |
| 1817: | if (!in_array($group->UUID, $groupsToUpdate)) { |
| 1818: | $groupsToUpdate[] = $group->UUID; |
| 1819: | } |
| 1820: | } |
| 1821: | } |
| 1822: | } |
| 1823: | |
| 1824: | foreach ($groups as $group) { |
| 1825: | if (in_array($group->UUID, $groupsToUpdate)) { |
| 1826: | $this->UpdateGroupObject($UserId, $group); |
| 1827: | } |
| 1828: | } |
| 1829: | |
| 1830: | $mResult = true; |
| 1831: | } else { |
| 1832: | throw new ApiException(Notifications::AccessDenied, null, 'AccessDenied'); |
| 1833: | } |
| 1834: | |
| 1835: | return $mResult; |
| 1836: | } |
| 1837: | |
| 1838: | |
| 1839: | |
| 1840: | |
| 1841: | |
| 1842: | |
| 1843: | |
| 1844: | |
| 1845: | |
| 1846: | |
| 1847: | |
| 1848: | |
| 1849: | |
| 1850: | |
| 1851: | |
| 1852: | |
| 1853: | |
| 1854: | |
| 1855: | |
| 1856: | |
| 1857: | |
| 1858: | |
| 1859: | |
| 1860: | |
| 1861: | |
| 1862: | |
| 1863: | |
| 1864: | |
| 1865: | |
| 1866: | |
| 1867: | |
| 1868: | |
| 1869: | |
| 1870: | |
| 1871: | |
| 1872: | |
| 1873: | |
| 1874: | |
| 1875: | |
| 1876: | |
| 1877: | |
| 1878: | |
| 1879: | |
| 1880: | |
| 1881: | |
| 1882: | |
| 1883: | |
| 1884: | |
| 1885: | |
| 1886: | |
| 1887: | |
| 1888: | |
| 1889: | |
| 1890: | |
| 1891: | |
| 1892: | |
| 1893: | public function CreateGroup($Group, $UserId = null) |
| 1894: | { |
| 1895: | $mResult = false; |
| 1896: | |
| 1897: | Api::CheckAccess($UserId); |
| 1898: | |
| 1899: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 1900: | |
| 1901: | if (is_array($Group)) { |
| 1902: | \Aurora\System\Validator::validate($Group, [ |
| 1903: | 'Name' => 'required' |
| 1904: | ], [ |
| 1905: | 'required' => 'The :attribute field is required.' |
| 1906: | ]); |
| 1907: | |
| 1908: | $oGroup = new Classes\Group(); |
| 1909: | $oGroup->IdUser = (int) $UserId; |
| 1910: | |
| 1911: | $oGroup->populate($Group); |
| 1912: | if (isset($Group['Contacts']) && is_array($Group['Contacts'])) { |
| 1913: | $oGroup->Contacts = $this->getContactsUUIDsFromIds($UserId, $Group['Contacts']); |
| 1914: | } |
| 1915: | |
| 1916: | $oVCard = new \Sabre\VObject\Component\VCard(); |
| 1917: | Helper::UpdateVCardFromGroup($oGroup, $oVCard); |
| 1918: | |
| 1919: | $userPublicId = Api::getUserPublicIdById($UserId); |
| 1920: | $addressBook = Backend::Carddav()->getAddressBookForUser(Constants::PRINCIPALS_PREFIX . $userPublicId, Constants::ADDRESSBOOK_DEFAULT_NAME); |
| 1921: | $cardUri = $oGroup->UUID . '.vcf'; |
| 1922: | |
| 1923: | if ($addressBook) { |
| 1924: | $cardETag = Backend::Carddav()->createCard($addressBook['id'], $cardUri, $oVCard->serialize()); |
| 1925: | if ($cardETag) { |
| 1926: | $newCard = Backend::Carddav()->getCard($addressBook['id'], $cardUri); |
| 1927: | if ($newCard) { |
| 1928: | $mResult = [ |
| 1929: | 'UUID' => (string) $newCard['id'], |
| 1930: | 'ETag' => \trim($newCard['etag'], '"') |
| 1931: | ]; |
| 1932: | } |
| 1933: | } |
| 1934: | } |
| 1935: | } |
| 1936: | |
| 1937: | return $mResult; |
| 1938: | } |
| 1939: | |
| 1940: | protected function getContactsUUIDsFromIds($UserId, $Ids) |
| 1941: | { |
| 1942: | if (is_array($Ids) && count($Ids) > 0) { |
| 1943: | $query = Capsule::connection()->table('contacts_cards') |
| 1944: | ->join('adav_cards', 'contacts_cards.CardId', '=', 'adav_cards.id') |
| 1945: | ->join('adav_addressbooks', 'adav_cards.addressbookid', '=', 'adav_addressbooks.id') |
| 1946: | ->select('adav_cards.uri as card_uri'); |
| 1947: | |
| 1948: | $aArgs = [ |
| 1949: | 'UserId' => $UserId, |
| 1950: | 'UUID' => $Ids |
| 1951: | ]; |
| 1952: | |
| 1953: | |
| 1954: | $query->where(function ($q) use ($aArgs, $query) { |
| 1955: | $aArgs['Query'] = $query; |
| 1956: | $this->broadcastEvent(self::GetName() . '::ContactQueryBuilder', $aArgs, $q); |
| 1957: | }); |
| 1958: | |
| 1959: | $contactsIds = $query->pluck('card_uri')->all(); |
| 1960: | |
| 1961: | return array_map(function ($item) { |
| 1962: | $pathInfo = pathinfo($item); |
| 1963: | return $pathInfo['filename']; |
| 1964: | }, $contactsIds); |
| 1965: | } else { |
| 1966: | return []; |
| 1967: | } |
| 1968: | } |
| 1969: | |
| 1970: | protected function getContactsIdsFromUUIDs($UserId, $UUIDs) |
| 1971: | { |
| 1972: | $Uris = array_map(function ($item) { |
| 1973: | return $item . '.vcf'; |
| 1974: | }, $UUIDs); |
| 1975: | |
| 1976: | $contactsIds = Capsule::connection()->table('adav_cards') |
| 1977: | ->join('adav_addressbooks', 'adav_cards.addressbookid', '=', 'adav_addressbooks.id') |
| 1978: | ->select('adav_cards.id as card_id') |
| 1979: | ->where('principaluri', Constants::PRINCIPALS_PREFIX . Api::getUserPublicIdById($UserId)) |
| 1980: | ->whereIn('adav_cards.uri', $Uris)->get()->all(); |
| 1981: | |
| 1982: | return array_map(function ($item) { |
| 1983: | return $item->card_id; |
| 1984: | }, $contactsIds); |
| 1985: | } |
| 1986: | |
| 1987: | |
| 1988: | |
| 1989: | |
| 1990: | |
| 1991: | |
| 1992: | |
| 1993: | |
| 1994: | |
| 1995: | |
| 1996: | |
| 1997: | |
| 1998: | |
| 1999: | |
| 2000: | |
| 2001: | |
| 2002: | |
| 2003: | |
| 2004: | |
| 2005: | |
| 2006: | |
| 2007: | |
| 2008: | |
| 2009: | |
| 2010: | |
| 2011: | |
| 2012: | |
| 2013: | |
| 2014: | |
| 2015: | |
| 2016: | |
| 2017: | |
| 2018: | |
| 2019: | |
| 2020: | |
| 2021: | |
| 2022: | |
| 2023: | |
| 2024: | |
| 2025: | |
| 2026: | |
| 2027: | |
| 2028: | |
| 2029: | |
| 2030: | |
| 2031: | |
| 2032: | |
| 2033: | |
| 2034: | |
| 2035: | |
| 2036: | |
| 2037: | protected function UpdateGroupObject($UserId, $oGroup) |
| 2038: | { |
| 2039: | $mResult = false; |
| 2040: | |
| 2041: | if (is_array($oGroup->Contacts) && count($oGroup->Contacts)) { |
| 2042: | $oGroup->Contacts = $this->getContactsUUIDsFromIds($UserId, $oGroup->Contacts); |
| 2043: | } |
| 2044: | |
| 2045: | $query = Capsule::connection()->table('contacts_cards') |
| 2046: | ->join('adav_cards', 'contacts_cards.CardId', '=', 'adav_cards.id') |
| 2047: | ->join('adav_addressbooks', 'adav_cards.addressbookid', '=', 'adav_addressbooks.id') |
| 2048: | ->select('adav_cards.uri as card_uri', 'adav_addressbooks.id as addressbook_id', 'carddata'); |
| 2049: | |
| 2050: | $aArgs = [ |
| 2051: | 'UserId' => $UserId, |
| 2052: | 'UUID' => $oGroup->UUID |
| 2053: | ]; |
| 2054: | |
| 2055: | |
| 2056: | $query->where(function ($q) use ($aArgs, $query) { |
| 2057: | $aArgs['Query'] = $query; |
| 2058: | $this->broadcastEvent(self::GetName() . '::ContactQueryBuilder', $aArgs, $q); |
| 2059: | }); |
| 2060: | |
| 2061: | $row = $query->first(); |
| 2062: | if ($row) { |
| 2063: | $oVCard = \Sabre\VObject\Reader::read($row->carddata); |
| 2064: | $uidVal = $oVCard->UID->getValue(); |
| 2065: | if (empty($uidVal) || is_numeric($uidVal)) { |
| 2066: | $uriInfo = pathinfo($row->card_uri); |
| 2067: | if (isset($uriInfo['filename'])) { |
| 2068: | $oVCard->UID = $uriInfo['filename']; |
| 2069: | } |
| 2070: | } |
| 2071: | Helper::UpdateVCardFromGroup($oGroup, $oVCard); |
| 2072: | $mResult = !!Backend::Carddav()->updateCard($row->addressbook_id, $row->card_uri, $oVCard->serialize()); |
| 2073: | } |
| 2074: | |
| 2075: | return $mResult; |
| 2076: | } |
| 2077: | |
| 2078: | |
| 2079: | |
| 2080: | |
| 2081: | |
| 2082: | |
| 2083: | public function UpdateGroup($UserId, $Group) |
| 2084: | { |
| 2085: | $mResult = false; |
| 2086: | |
| 2087: | Api::CheckAccess($UserId); |
| 2088: | |
| 2089: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 2090: | |
| 2091: | $oGroup = self::Decorator()->GetGroup($UserId, $Group['UUID']); |
| 2092: | if ($oGroup) { |
| 2093: | $oGroup->populate($Group); |
| 2094: | $mResult = $this->UpdateGroupObject($UserId, $oGroup); |
| 2095: | } |
| 2096: | |
| 2097: | return $mResult; |
| 2098: | } |
| 2099: | |
| 2100: | |
| 2101: | |
| 2102: | |
| 2103: | |
| 2104: | |
| 2105: | |
| 2106: | |
| 2107: | |
| 2108: | |
| 2109: | |
| 2110: | |
| 2111: | |
| 2112: | |
| 2113: | |
| 2114: | |
| 2115: | |
| 2116: | |
| 2117: | |
| 2118: | |
| 2119: | |
| 2120: | |
| 2121: | |
| 2122: | |
| 2123: | |
| 2124: | |
| 2125: | |
| 2126: | |
| 2127: | |
| 2128: | |
| 2129: | |
| 2130: | |
| 2131: | |
| 2132: | |
| 2133: | |
| 2134: | |
| 2135: | |
| 2136: | |
| 2137: | |
| 2138: | |
| 2139: | |
| 2140: | |
| 2141: | |
| 2142: | |
| 2143: | |
| 2144: | |
| 2145: | |
| 2146: | |
| 2147: | |
| 2148: | |
| 2149: | |
| 2150: | |
| 2151: | |
| 2152: | |
| 2153: | public function DeleteGroup($UserId, $UUID) |
| 2154: | { |
| 2155: | Api::CheckAccess($UserId); |
| 2156: | |
| 2157: | return self::Decorator()->DeleteContacts($UserId, StorageType::Personal, [$UUID]); |
| 2158: | } |
| 2159: | |
| 2160: | |
| 2161: | |
| 2162: | |
| 2163: | |
| 2164: | |
| 2165: | |
| 2166: | |
| 2167: | |
| 2168: | |
| 2169: | |
| 2170: | |
| 2171: | |
| 2172: | |
| 2173: | |
| 2174: | |
| 2175: | |
| 2176: | |
| 2177: | |
| 2178: | |
| 2179: | |
| 2180: | |
| 2181: | |
| 2182: | |
| 2183: | |
| 2184: | |
| 2185: | |
| 2186: | |
| 2187: | |
| 2188: | |
| 2189: | |
| 2190: | |
| 2191: | |
| 2192: | |
| 2193: | |
| 2194: | |
| 2195: | |
| 2196: | |
| 2197: | |
| 2198: | |
| 2199: | |
| 2200: | |
| 2201: | |
| 2202: | |
| 2203: | |
| 2204: | |
| 2205: | |
| 2206: | |
| 2207: | |
| 2208: | |
| 2209: | |
| 2210: | |
| 2211: | |
| 2212: | |
| 2213: | |
| 2214: | |
| 2215: | |
| 2216: | |
| 2217: | public function AddContactsToGroup($UserId, $GroupUUID, $ContactUUIDs) |
| 2218: | { |
| 2219: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 2220: | |
| 2221: | $mResult = false; |
| 2222: | |
| 2223: | Api::CheckAccess($UserId); |
| 2224: | |
| 2225: | |
| 2226: | $GroupId = $GroupUUID; |
| 2227: | $ContactIds = $ContactUUIDs; |
| 2228: | |
| 2229: | if (is_array($ContactIds) && !empty($ContactIds)) { |
| 2230: | |
| 2231: | $oGroup = self::Decorator()->GetGroup($UserId, $GroupId); |
| 2232: | if ($oGroup) { |
| 2233: | |
| 2234: | $aContacts = self::Decorator()->GetContactsByUids($UserId, $ContactIds); |
| 2235: | $newContactIds = array_map(function ($item) { |
| 2236: | return $item->Id; |
| 2237: | }, $aContacts); |
| 2238: | $oGroup->Contacts = array_merge($oGroup->Contacts, $newContactIds); |
| 2239: | |
| 2240: | $mResult = $this->UpdateGroupObject($UserId, $oGroup); |
| 2241: | } |
| 2242: | } |
| 2243: | |
| 2244: | return $mResult; |
| 2245: | } |
| 2246: | |
| 2247: | |
| 2248: | |
| 2249: | |
| 2250: | |
| 2251: | |
| 2252: | |
| 2253: | |
| 2254: | |
| 2255: | |
| 2256: | |
| 2257: | |
| 2258: | |
| 2259: | |
| 2260: | |
| 2261: | |
| 2262: | |
| 2263: | |
| 2264: | |
| 2265: | |
| 2266: | |
| 2267: | |
| 2268: | |
| 2269: | |
| 2270: | |
| 2271: | |
| 2272: | |
| 2273: | |
| 2274: | |
| 2275: | |
| 2276: | |
| 2277: | |
| 2278: | |
| 2279: | |
| 2280: | |
| 2281: | |
| 2282: | |
| 2283: | |
| 2284: | |
| 2285: | |
| 2286: | |
| 2287: | |
| 2288: | |
| 2289: | |
| 2290: | |
| 2291: | |
| 2292: | |
| 2293: | |
| 2294: | |
| 2295: | |
| 2296: | |
| 2297: | |
| 2298: | |
| 2299: | |
| 2300: | |
| 2301: | |
| 2302: | public function RemoveContactsFromGroup($UserId, $GroupUUID, $ContactUUIDs) |
| 2303: | { |
| 2304: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 2305: | |
| 2306: | Api::CheckAccess($UserId); |
| 2307: | $mResult = false; |
| 2308: | |
| 2309: | |
| 2310: | $GroupId = $GroupUUID; |
| 2311: | $ContactIds = $ContactUUIDs; |
| 2312: | |
| 2313: | if (is_array($ContactIds) && !empty($ContactIds)) { |
| 2314: | $oGroup = self::Decorator()->GetGroup($UserId, $GroupId); |
| 2315: | if ($oGroup) { |
| 2316: | $ContactIds = array_map(function ($id) { |
| 2317: | return (int) $id; |
| 2318: | }, $ContactIds); |
| 2319: | $oGroup->Contacts = array_diff($oGroup->Contacts, $ContactIds); |
| 2320: | $mResult = $this->UpdateGroupObject($UserId, $oGroup); |
| 2321: | } |
| 2322: | } |
| 2323: | |
| 2324: | return $mResult; |
| 2325: | } |
| 2326: | |
| 2327: | |
| 2328: | |
| 2329: | |
| 2330: | |
| 2331: | |
| 2332: | |
| 2333: | |
| 2334: | |
| 2335: | |
| 2336: | |
| 2337: | |
| 2338: | |
| 2339: | |
| 2340: | |
| 2341: | |
| 2342: | |
| 2343: | |
| 2344: | |
| 2345: | |
| 2346: | |
| 2347: | |
| 2348: | |
| 2349: | |
| 2350: | |
| 2351: | |
| 2352: | |
| 2353: | |
| 2354: | |
| 2355: | |
| 2356: | |
| 2357: | |
| 2358: | |
| 2359: | |
| 2360: | |
| 2361: | |
| 2362: | |
| 2363: | |
| 2364: | |
| 2365: | |
| 2366: | |
| 2367: | |
| 2368: | |
| 2369: | |
| 2370: | |
| 2371: | |
| 2372: | |
| 2373: | |
| 2374: | |
| 2375: | |
| 2376: | |
| 2377: | |
| 2378: | |
| 2379: | |
| 2380: | |
| 2381: | |
| 2382: | |
| 2383: | |
| 2384: | |
| 2385: | public function Import($UserId, $UploadData, $GroupUUID, $Storage = null) |
| 2386: | { |
| 2387: | Api::CheckAccess($UserId); |
| 2388: | |
| 2389: | $oUser = CoreModule::getInstance()->GetUserWithoutRoleCheck($UserId); |
| 2390: | |
| 2391: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 2392: | |
| 2393: | $aResponse = array( |
| 2394: | 'ImportedCount' => 0, |
| 2395: | 'ParsedCount' => 0 |
| 2396: | ); |
| 2397: | |
| 2398: | if (is_array($UploadData)) { |
| 2399: | $oApiFileCacheManager = new \Aurora\System\Managers\Filecache(); |
| 2400: | $sTempFileName = 'import-post-' . md5($UploadData['name'] . $UploadData['tmp_name']); |
| 2401: | if ($oApiFileCacheManager->moveUploadedFile($oUser->UUID, $sTempFileName, $UploadData['tmp_name'], '', self::GetName())) { |
| 2402: | $sTempFilePath = $oApiFileCacheManager->generateFullFilePath($oUser->UUID, $sTempFileName, '', self::GetName()); |
| 2403: | |
| 2404: | $aImportResult = array(); |
| 2405: | |
| 2406: | $sFileExtension = strtolower(\Aurora\System\Utils::GetFileExtension($UploadData['name'])); |
| 2407: | switch ($sFileExtension) { |
| 2408: | case 'csv': |
| 2409: | $oSync = new Classes\Csv\Sync(); |
| 2410: | $aImportResult = $oSync->Import($oUser->Id, $sTempFilePath, $GroupUUID, $Storage); |
| 2411: | break; |
| 2412: | case 'vcf': |
| 2413: | $aImportResult = $this->importVcf($oUser->Id, $sTempFilePath, $Storage); |
| 2414: | break; |
| 2415: | } |
| 2416: | |
| 2417: | if (is_array($aImportResult) && isset($aImportResult['ImportedCount']) && isset($aImportResult['ParsedCount'])) { |
| 2418: | $aResponse['ImportedCount'] = $aImportResult['ImportedCount']; |
| 2419: | $aResponse['ParsedCount'] = $aImportResult['ParsedCount']; |
| 2420: | } else { |
| 2421: | throw new ApiException(Notifications::IncorrectFileExtension); |
| 2422: | } |
| 2423: | |
| 2424: | $oApiFileCacheManager->clear($oUser->UUID, $sTempFileName, '', self::GetName()); |
| 2425: | } else { |
| 2426: | throw new ApiException(Notifications::UnknownError); |
| 2427: | } |
| 2428: | } else { |
| 2429: | throw new ApiException(Notifications::UnknownError); |
| 2430: | } |
| 2431: | |
| 2432: | return $aResponse; |
| 2433: | } |
| 2434: | |
| 2435: | public function UpdateSharedContacts($UserId, $UUIDs) |
| 2436: | { |
| 2437: | Api::CheckAccess($UserId); |
| 2438: | |
| 2439: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 2440: | return true; |
| 2441: | } |
| 2442: | |
| 2443: | public function AddContactsFromFile($UserId, $File) |
| 2444: | { |
| 2445: | Api::CheckAccess($UserId); |
| 2446: | |
| 2447: | $oUser = CoreModule::getInstance()->GetUserWithoutRoleCheck($UserId); |
| 2448: | |
| 2449: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 2450: | |
| 2451: | if (empty($File)) { |
| 2452: | throw new ApiException(Notifications::InvalidInputParameter); |
| 2453: | } |
| 2454: | |
| 2455: | $oApiFileCache = new \Aurora\System\Managers\Filecache(); |
| 2456: | |
| 2457: | $sTempFilePath = $oApiFileCache->generateFullFilePath($oUser->UUID, $File); |
| 2458: | $aImportResult = $this->importVcf($oUser->Id, $sTempFilePath); |
| 2459: | |
| 2460: | return $aImportResult; |
| 2461: | } |
| 2462: | |
| 2463: | |
| 2464: | |
| 2465: | |
| 2466: | |
| 2467: | |
| 2468: | |
| 2469: | public function SaveContactAsTempFile($UserId, $UUID, $FileName) |
| 2470: | { |
| 2471: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 2472: | |
| 2473: | Api::CheckAccess($UserId); |
| 2474: | |
| 2475: | $mResult = false; |
| 2476: | |
| 2477: | $oContact = self::Decorator()->GetContact($UUID, $UserId); |
| 2478: | if ($oContact) { |
| 2479: | $oVCard = new \Sabre\VObject\Component\VCard(); |
| 2480: | Helper::UpdateVCardFromContact($oContact, $oVCard); |
| 2481: | $sVCardData = $oVCard->serialize(); |
| 2482: | if ($sVCardData) { |
| 2483: | $sUUID = \Aurora\System\Api::getUserUUIDById($UserId); |
| 2484: | $sTempName = md5($sUUID . $UUID); |
| 2485: | $oApiFileCache = new \Aurora\System\Managers\Filecache(); |
| 2486: | |
| 2487: | $oApiFileCache->put($sUUID, $sTempName, $sVCardData); |
| 2488: | if ($oApiFileCache->isFileExists($sUUID, $sTempName)) { |
| 2489: | $mResult = \Aurora\System\Utils::GetClientFileResponse( |
| 2490: | null, |
| 2491: | $UserId, |
| 2492: | $FileName, |
| 2493: | $sTempName, |
| 2494: | $oApiFileCache->fileSize($sUUID, $sTempName) |
| 2495: | ); |
| 2496: | } |
| 2497: | } |
| 2498: | } |
| 2499: | |
| 2500: | return $mResult; |
| 2501: | } |
| 2502: | |
| 2503: | |
| 2504: | |
| 2505: | private function importVcf($iUserId, $sTempFilePath, $sStorage = null) |
| 2506: | { |
| 2507: | $aImportResult = array( |
| 2508: | 'ParsedCount' => 0, |
| 2509: | 'ImportedCount' => 0, |
| 2510: | 'ImportedUids' => [] |
| 2511: | ); |
| 2512: | |
| 2513: | $oHandler = fopen($sTempFilePath, 'r'); |
| 2514: | $oSplitter = new \Sabre\VObject\Splitter\VCard($oHandler, \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES); |
| 2515: | $oContactsDecorator = Module::Decorator(); |
| 2516: | |
| 2517: | $aGroupsData = []; |
| 2518: | $aContactsData = []; |
| 2519: | while ($oVCard = $oSplitter->getNext()) { |
| 2520: | set_time_limit(30); |
| 2521: | |
| 2522: | $Uid = (string) $oVCard->UID; |
| 2523: | if (empty($Uid)) { |
| 2524: | $Uid = UUIDUtil::getUUID(); |
| 2525: | } |
| 2526: | if ((isset($oVCard->KIND) && (string) $oVCard->KIND === 'GROUP') || |
| 2527: | (isset($oVCard->{'X-ADDRESSBOOKSERVER-KIND'}) && (string) $oVCard->{'X-ADDRESSBOOKSERVER-KIND'} === 'GROUP')) { |
| 2528: | $aGroupsData[] = Classes\VCard\Helper::GetGroupDataFromVcard($oVCard, $Uid); |
| 2529: | } else { |
| 2530: | $aContactData = Classes\VCard\Helper::GetContactDataFromVcard($oVCard, $Uid); |
| 2531: | $oContact = self::Decorator()->GetContact($Uid, $iUserId); |
| 2532: | $aImportResult['ParsedCount']++; |
| 2533: | if (!$oContact) { |
| 2534: | if (isset($sStorage)) { |
| 2535: | $aContactData['Storage'] = $sStorage; |
| 2536: | } |
| 2537: | $aContactsData[$Uid] = $aContactData; |
| 2538: | } |
| 2539: | } |
| 2540: | } |
| 2541: | |
| 2542: | foreach ($aContactsData as $key => $aContactData) { |
| 2543: | $CreatedContactData = $oContactsDecorator->CreateContact($aContactData, $iUserId); |
| 2544: | if ($CreatedContactData) { |
| 2545: | $aImportResult['ImportedCount']++; |
| 2546: | $aImportResult['ImportedUids'][] = $CreatedContactData['UUID']; |
| 2547: | $aContactsData[$key]['NewUUID'] = $CreatedContactData['UUID']; |
| 2548: | } |
| 2549: | } |
| 2550: | |
| 2551: | foreach ($aGroupsData as $aGroupData) { |
| 2552: | if (isset($aGroupData['Contacts'])) { |
| 2553: | $aUuids = $aGroupData['Contacts']; |
| 2554: | $aGroupData['Contacts'] = []; |
| 2555: | foreach ($aUuids as $value) { |
| 2556: | if (isset($aContactsData[$value])) { |
| 2557: | $aGroupData['Contacts'][] = $aContactsData[$value]['NewUUID']; |
| 2558: | } |
| 2559: | } |
| 2560: | } |
| 2561: | $oContactsDecorator->CreateGroup($aGroupData, $iUserId); |
| 2562: | } |
| 2563: | |
| 2564: | return $aImportResult; |
| 2565: | } |
| 2566: | |
| 2567: | protected function populateContactArguments(&$aArgs) |
| 2568: | { |
| 2569: | $mResult = false; |
| 2570: | $this->broadcastEvent('PopulateContactArguments', $aArgs, $mResult); |
| 2571: | return $mResult; |
| 2572: | } |
| 2573: | |
| 2574: | private function prepareFiltersFromStorage($UserId, $Storage = '', $AddressBookId = 0, &$Query = null, &$WhereQuery = null, $Suggestions = false) |
| 2575: | { |
| 2576: | $aArgs = [ |
| 2577: | 'UserId' => $UserId, |
| 2578: | 'Storage' => $Storage, |
| 2579: | 'AddressBookId' => $AddressBookId, |
| 2580: | 'IsValid' => false, |
| 2581: | 'Query' => $Query, |
| 2582: | 'Suggestions' => $Suggestions |
| 2583: | ]; |
| 2584: | |
| 2585: | $this->broadcastEvent('PrepareFiltersFromStorage', $aArgs, $WhereQuery); |
| 2586: | if (!$aArgs['IsValid']) { |
| 2587: | throw new ApiException(Notifications::InvalidInputParameter, null, 'Invalid Storage parameter value'); |
| 2588: | } |
| 2589: | return $WhereQuery; |
| 2590: | } |
| 2591: | |
| 2592: | public function onAfterUseEmails($Args, &$Result) |
| 2593: | { |
| 2594: | $aAddresses = $Args['Emails']; |
| 2595: | $iUserId = $Args['IdUser']; |
| 2596: | foreach ($aAddresses as $sEmail => $sName) { |
| 2597: | try { |
| 2598: | $contactsColl = self::Decorator()->GetContactsByEmails($iUserId, StorageType::Personal, [$sEmail]); |
| 2599: | |
| 2600: | $oContact = $contactsColl->first(); |
| 2601: | if (!$oContact) { |
| 2602: | $contactsColl = self::Decorator()->GetContactsByEmails($iUserId, StorageType::Collected, [$sEmail]); |
| 2603: | $oContact = $contactsColl->first(); |
| 2604: | } |
| 2605: | |
| 2606: | if ($oContact) { |
| 2607: | ContactCard::where('CardId', $oContact->Id)->update(['Frequency' => $oContact->Frequency + 1]); |
| 2608: | } else { |
| 2609: | self::Decorator()->CreateContact([ |
| 2610: | 'FullName' => $sName, |
| 2611: | 'PersonalEmail' => $sEmail, |
| 2612: | 'Auto' => true, |
| 2613: | 'Storage' => StorageType::Collected, |
| 2614: | ], $iUserId); |
| 2615: | } |
| 2616: | } catch (\Exception $ex) { |
| 2617: | } |
| 2618: | } |
| 2619: | } |
| 2620: | |
| 2621: | public function onGetBodyStructureParts($aParts, &$aResultParts) |
| 2622: | { |
| 2623: | foreach ($aParts as $oPart) { |
| 2624: | if ($oPart instanceof \MailSo\Imap\BodyStructure && |
| 2625: | ($oPart->ContentType() === 'text/vcard' || $oPart->ContentType() === 'text/x-vcard')) { |
| 2626: | $aResultParts[] = $oPart; |
| 2627: | break; |
| 2628: | } |
| 2629: | } |
| 2630: | } |
| 2631: | |
| 2632: | public function onBeforeDeleteUser(&$aArgs, &$mResult) |
| 2633: | { |
| 2634: | if (isset($aArgs['UserId'])) { |
| 2635: | $this->userPublicIdToDelete = Api::getUserPublicIdById($aArgs['UserId']); |
| 2636: | } |
| 2637: | } |
| 2638: | |
| 2639: | public function onAfterDeleteUser(&$aArgs, &$mResult) |
| 2640: | { |
| 2641: | if ($mResult && $this->userPublicIdToDelete) { |
| 2642: | $abooks = Backend::Carddav()->getAddressBooksForUser(Constants::PRINCIPALS_PREFIX . $this->userPublicIdToDelete); |
| 2643: | if ($abooks) { |
| 2644: | foreach ($abooks as $book) { |
| 2645: | Backend::Carddav()->deleteAddressBook($book['id']); |
| 2646: | } |
| 2647: | } |
| 2648: | } |
| 2649: | } |
| 2650: | |
| 2651: | public function onContactToResponseArray($aArgs, &$mResult) |
| 2652: | { |
| 2653: | if (isset($aArgs[0]) && $aArgs[0] instanceof Contact && is_array($mResult)) { |
| 2654: | $mResult['UUID'] = (string) $mResult['Id']; |
| 2655: | } |
| 2656: | } |
| 2657: | |
| 2658: | |
| 2659: | public function GetAddressBook($UserId, $UUID) |
| 2660: | { |
| 2661: | Api::CheckAccess($UserId); |
| 2662: | |
| 2663: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 2664: | |
| 2665: | $principalUri = Constants::PRINCIPALS_PREFIX . \Aurora\System\Api::getUserPublicIdById($UserId); |
| 2666: | |
| 2667: | return Backend::Carddav()->getAddressBookForUser($principalUri, $UUID); |
| 2668: | } |
| 2669: | |
| 2670: | public function GetAddressBooks($UserId = null) |
| 2671: | { |
| 2672: | $aResult = []; |
| 2673: | |
| 2674: | Api::CheckAccess($UserId); |
| 2675: | |
| 2676: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 2677: | |
| 2678: | return $aResult; |
| 2679: | } |
| 2680: | |
| 2681: | public function CreateAddressBook($AddressBookName, $UserId = null, $UUID = null) |
| 2682: | { |
| 2683: | $mResult = false; |
| 2684: | |
| 2685: | Api::CheckAccess($UserId); |
| 2686: | |
| 2687: | if (isset($UUID)) { |
| 2688: | $sAddressBookUUID = $UUID; |
| 2689: | } else { |
| 2690: | $sAddressBookUUID = UUIDUtil::getUUID(); |
| 2691: | } |
| 2692: | |
| 2693: | $userPublicId = Api::getUserPublicIdById($UserId); |
| 2694: | |
| 2695: | $iAddressBookId = Backend::Carddav()->createAddressBook(Constants::PRINCIPALS_PREFIX . $userPublicId, $sAddressBookUUID, ['{DAV:}displayname' => $AddressBookName]); |
| 2696: | |
| 2697: | if (is_numeric($iAddressBookId)) { |
| 2698: | $oAddressBook = Backend::Carddav()->getAddressBookById($iAddressBookId); |
| 2699: | if ($oAddressBook) { |
| 2700: | return [ |
| 2701: | 'Id' => StorageType::AddressBook . '-' . $oAddressBook['id'], |
| 2702: | 'EntityId' => (int) $oAddressBook['id'], |
| 2703: | 'CTag' => (int) $oAddressBook['{http://sabredav.org/ns}sync-token'], |
| 2704: | 'Display' => true, |
| 2705: | 'Owner' => basename($oAddressBook['principaluri']), |
| 2706: | 'Order' => 1, |
| 2707: | 'DisplayName' => $oAddressBook['{DAV:}displayname'], |
| 2708: | 'Uri' => $oAddressBook['uri'] |
| 2709: | ]; |
| 2710: | |
| 2711: | } |
| 2712: | } |
| 2713: | |
| 2714: | return $mResult; |
| 2715: | } |
| 2716: | |
| 2717: | public function UpdateAddressBook($EntityId, $AddressBookName, $UserId = null) |
| 2718: | { |
| 2719: | $mResult = false; |
| 2720: | |
| 2721: | Api::CheckAccess($UserId); |
| 2722: | |
| 2723: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 2724: | |
| 2725: | if ($this->CheckAccessToAddressBook($UserId, $EntityId, Access::Write)) { |
| 2726: | $propParch = new PropPatch([ |
| 2727: | '{DAV:}displayname' => $AddressBookName |
| 2728: | ]); |
| 2729: | Backend::Carddav()->updateAddressBook($EntityId, $propParch); |
| 2730: | $mResult = $propParch->commit(); |
| 2731: | } else { |
| 2732: | throw new ApiException(Notifications::AccessDenied, null, 'AccessDenied'); |
| 2733: | } |
| 2734: | |
| 2735: | return $mResult; |
| 2736: | } |
| 2737: | |
| 2738: | public function DeleteAddressBook($EntityId, $UserId = null) |
| 2739: | { |
| 2740: | $mResult = false; |
| 2741: | |
| 2742: | Api::CheckAccess($UserId); |
| 2743: | |
| 2744: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 2745: | |
| 2746: | $userPublicId = Api::getUserPublicIdById($UserId); |
| 2747: | |
| 2748: | $abook = Capsule::connection()->table('adav_addressbooks') |
| 2749: | ->where('id', $EntityId) |
| 2750: | ->where('principaluri', Constants::PRINCIPALS_PREFIX . $userPublicId) |
| 2751: | ->first(); |
| 2752: | |
| 2753: | if ($abook) { |
| 2754: | Backend::Carddav()->deleteAddressBook($EntityId); |
| 2755: | $mResult = true; |
| 2756: | } |
| 2757: | |
| 2758: | return $mResult; |
| 2759: | } |
| 2760: | |
| 2761: | public function DeleteUsersAddressBooks($UserId = null) |
| 2762: | { |
| 2763: | $mResult = false; |
| 2764: | |
| 2765: | Api::CheckAccess($UserId); |
| 2766: | |
| 2767: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 2768: | |
| 2769: | $userPublicId = Api::getUserPublicIdById($UserId); |
| 2770: | |
| 2771: | $abooks = Capsule::connection()->table('adav_addressbooks') |
| 2772: | ->where('principaluri', Constants::PRINCIPALS_PREFIX . $userPublicId) |
| 2773: | ->get(); |
| 2774: | |
| 2775: | foreach ($abooks as $abook) { |
| 2776: | Backend::Carddav()->deleteAddressBook($abook->id); |
| 2777: | $mResult = true; |
| 2778: | } |
| 2779: | |
| 2780: | return $mResult; |
| 2781: | } |
| 2782: | |
| 2783: | public function GetStoragesMapToAddressbooks() |
| 2784: | { |
| 2785: | return []; |
| 2786: | } |
| 2787: | |
| 2788: | protected function getGetContactsQueryBuilder($UserId, $Storage = '', $AddressBookId = null, Builder $Filters = null, $Suggestions = false, $withGroups = false) |
| 2789: | { |
| 2790: | if ($Filters instanceof Builder) { |
| 2791: | $query = & $Filters; |
| 2792: | } else { |
| 2793: | $query = ContactCard::query(); |
| 2794: | } |
| 2795: | |
| 2796: | $con = Capsule::connection(); |
| 2797: | $query->select( |
| 2798: | 'adav_cards.id as Id', |
| 2799: | 'adav_cards.id as UUID', |
| 2800: | 'adav_cards.uri as Uri', |
| 2801: | 'adav_cards.addressbookid as Storage', |
| 2802: | 'etag as ETag', |
| 2803: | $con->raw('FROM_UNIXTIME(lastmodified) as DateModified'), |
| 2804: | 'contacts_cards.PrimaryEmail', |
| 2805: | 'contacts_cards.PersonalEmail', |
| 2806: | 'contacts_cards.BusinessEmail', |
| 2807: | 'contacts_cards.OtherEmail', |
| 2808: | 'contacts_cards.BusinessCompany', |
| 2809: | 'contacts_cards.FullName', |
| 2810: | 'contacts_cards.FirstName', |
| 2811: | 'contacts_cards.LastName', |
| 2812: | 'contacts_cards.Frequency', |
| 2813: | 'contacts_cards.Properties', |
| 2814: | $con->raw('(Frequency/CEIL(DATEDIFF(CURDATE() + INTERVAL 1 DAY, FROM_UNIXTIME(lastmodified))/30)) as AgeScore'), |
| 2815: | 'core_users.Id as UserId' |
| 2816: | ) |
| 2817: | ->join('adav_cards', 'contacts_cards.CardId', '=', 'adav_cards.id') |
| 2818: | ->where(function ($wherQuery) use ($UserId, $Storage, $AddressBookId, $query, $Suggestions) { |
| 2819: | $this->prepareFiltersFromStorage($UserId, $Storage, $AddressBookId, $query, $wherQuery, $Suggestions); |
| 2820: | }); |
| 2821: | if (!$withGroups) { |
| 2822: | $query->where('IsGroup', false); |
| 2823: | } |
| 2824: | if ($Suggestions) { |
| 2825: | $query->where('Frequency', '>=', 0); |
| 2826: | } |
| 2827: | |
| 2828: | $query->leftJoin('core_users', 'adav_addressbooks.principaluri', '=', Capsule::connection()->raw("CONCAT('principals/', " . Capsule::connection()->getTablePrefix() . "core_users.PublicId)")); |
| 2829: | |
| 2830: | return $query; |
| 2831: | } |
| 2832: | } |
| 2833: | |