| 1: | <?php |
| 2: | |
| 3: | use Afterlogic\DAV\Backend; |
| 4: | use Aurora\Modules\Contacts\Enums\StorageType; |
| 5: | use Aurora\Modules\Contacts\Models\ContactCard; |
| 6: | use Illuminate\Database\Migrations\Migration; |
| 7: | use Illuminate\Database\Capsule\Manager as Capsule; |
| 8: | use Sabre\DAV\UUIDUtil; |
| 9: | |
| 10: | class MigrateTeamContacts extends Migration |
| 11: | { |
| 12: | protected function getTeamAddressBook($tenantId, $createIfNotExists = true) |
| 13: | { |
| 14: | $sPrincipalUri = \Afterlogic\DAV\Constants::PRINCIPALS_PREFIX . $tenantId . '_' . \Afterlogic\DAV\Constants::DAV_TENANT_PRINCIPAL; |
| 15: | $addressbook = Backend::Carddav()->getAddressBookForUser($sPrincipalUri, 'gab'); |
| 16: | if (!$addressbook && $createIfNotExists) { |
| 17: | if (Backend::Carddav()->createAddressBook($sPrincipalUri, 'gab', ['{DAV:}displayname' => \Afterlogic\DAV\Constants::ADDRESSBOOK_TEAM_DISPLAY_NAME])) { |
| 18: | $addressbook = Backend::Carddav()->getAddressBookForUser($sPrincipalUri, 'gab'); |
| 19: | } |
| 20: | } |
| 21: | |
| 22: | return $addressbook; |
| 23: | } |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | public function up() |
| 31: | { |
| 32: | Capsule::connection()->table('core_tenants')->orderBy('Id')->chunk(100000, function ($tenants) { |
| 33: | foreach ($tenants as $tenant) { |
| 34: | |
| 35: | $addressbook = $this->getTeamAddressBook($tenant->Id); |
| 36: | |
| 37: | if ($addressbook) { |
| 38: | Capsule::connection()->table('contacts')->where('Storage', StorageType::Team) |
| 39: | ->where('IdTenant', $tenant->Id) |
| 40: | ->orderBy('Id')->chunk(100000, function ($rows) use ($addressbook) { |
| 41: | foreach ($rows as $row) { |
| 42: | try { |
| 43: | $conactCard = ContactCard::where('AddressBookId', $addressbook['id'])->where('ViewEmail', $row->ViewEmail)->first(); |
| 44: | if (!$conactCard) { |
| 45: | $uid = UUIDUtil::getUUID(); |
| 46: | $vcard = new \Sabre\VObject\Component\VCard(['UID' => $uid]); |
| 47: | $vcard->add( |
| 48: | 'EMAIL', |
| 49: | $row->ViewEmail, |
| 50: | [ |
| 51: | 'type' => ['work'], |
| 52: | 'pref' => 1, |
| 53: | ] |
| 54: | ); |
| 55: | Backend::Carddav()->createCard($addressbook['id'], $uid . '.vcf', $vcard->serialize()); |
| 56: | } else { |
| 57: | \Aurora\System\Api::Log('Team contact already exists: ' . $row->ViewEmail, \Aurora\System\Enums\LogLevel::Error, 'contacts-migration-'); |
| 58: | } |
| 59: | } catch (\Exception $e) { |
| 60: | \Aurora\System\Api::Log('Contact migration exception', \Aurora\System\Enums\LogLevel::Error, 'contacts-migration-'); |
| 61: | \Aurora\System\Api::LogObject($row, \Aurora\System\Enums\LogLevel::Error, 'contacts-migration-'); |
| 62: | \Aurora\System\Api::Log($e->getMessage(), \Aurora\System\Enums\LogLevel::Error, 'contacts-migration-'); |
| 63: | } |
| 64: | } |
| 65: | }); |
| 66: | } |
| 67: | } |
| 68: | }); |
| 69: | } |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | public function down() |
| 77: | { |
| 78: | Capsule::connection()->table('core_tenants')->orderBy('Id')->chunk(100000, function ($tenants) { |
| 79: | foreach ($tenants as $tenant) { |
| 80: | $addressbook = $this->getTeamAddressBook($tenant->Id); |
| 81: | if ($addressbook) { |
| 82: | Backend::Carddav()->deleteAddressBook($addressbook['id']); |
| 83: | } |
| 84: | } |
| 85: | }); |
| 86: | } |
| 87: | } |
| 88: | |