1: | <?php |
2: | |
3: | use Afterlogic\DAV\Constants; |
4: | use Aurora\Modules\Contacts\Models\ContactCard; |
5: | use Aurora\System\Api; |
6: | use Illuminate\Database\Migrations\Migration; |
7: | use Illuminate\Database\Capsule\Manager as Capsule; |
8: | |
9: | class CopyDataToContactsCardsTable extends Migration |
10: | { |
11: | protected $filterByAddressBooks = false; |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | public function up() |
18: | { |
19: | Api::Init(); |
20: | Capsule::connection()->table('contacts')->orderBy('Id')->chunk(100000, function ($rows) { |
21: | foreach ($rows as $row) { |
22: | try { |
23: | $properties = null; |
24: | $uid = $row->UUID; |
25: | if (isset($row->Properties)) { |
26: | $properties = json_decode($row->Properties, true); |
27: | if (isset($properties['DavContacts::UID'])) { |
28: | $uid = $properties['DavContacts::UID']; |
29: | unset($properties['DavContacts::UID']); |
30: | } |
31: | if (isset($properties['DavContacts::VCardUID'])) { |
32: | unset($properties['DavContacts::VCardUID']); |
33: | } |
34: | } |
35: | |
36: | $userPublicId = Api::getUserPublicIdById($row->IdUser); |
37: | $userPrincipal = Constants::PRINCIPALS_PREFIX . $userPublicId; |
38: | |
39: | Api::Log('Source contact: ', \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
40: | Api::Log(' Contact.Id: ' . $row->Id, \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
41: | |
42: | if ($row->Frequency > 0 || (isset($properties) && is_array($properties) && count($properties) > 0)) { |
43: | Api::Log(' Contact.UUID: ' . $uid, \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
44: | Api::Log(' Contact.ViewEmail: ' . $row->ViewEmail, \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
45: | Api::Log(' User.PublicId: ' . $userPublicId, \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
46: | |
47: | $query = Capsule::connection()->table('contacts_cards') |
48: | ->select('contacts_cards.Id', 'contacts_cards.CardId', 'contacts_cards.Properties') |
49: | ->join('adav_addressbooks', 'contacts_cards.AddressBookId', '=', 'adav_addressbooks.id') |
50: | ->join('adav_cards', 'contacts_cards.CardId', '=', 'adav_cards.id') |
51: | ->where('adav_addressbooks.principaluri', $userPrincipal) |
52: | ->where('contacts_cards.ViewEmail', $row->ViewEmail) |
53: | ->where('adav_cards.uri', $uid . '.vcf'); |
54: | |
55: | if ($this->filterByAddressBooks) { |
56: | $storage = $row->Storage; |
57: | $storagesMapToAddressbooks = \Aurora\Modules\Contacts\Module::Decorator()->GetStoragesMapToAddressbooks(); |
58: | $addressbookUri = null; |
59: | $isCustomAddressBook = false; |
60: | if (isset($storagesMapToAddressbooks[$storage])) { |
61: | $addressbookUri = $storagesMapToAddressbooks[$storage]; |
62: | } elseif ($row->AddressBookId) { |
63: | $isCustomAddressBook = true; |
64: | } |
65: | |
66: | if ($isCustomAddressBook) { |
67: | Api::Log(' Custom addressbook (id): ' . $row->AddressBookId, \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
68: | $query->join('contacts_addressbooks', 'contacts_addressbooks.UUID', '=', 'adav_addressbooks.uri') |
69: | ->where('contacts_addressbooks.Id', $row->AddressBookId); |
70: | } elseif ($addressbookUri) { |
71: | Api::Log(' Dav addressbook (uri): ' . $addressbookUri, \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
72: | $query->where('adav_addressbooks.uri', $addressbookUri); |
73: | } |
74: | } |
75: | $cardsInfo = $query->get(); |
76: | |
77: | if (count($cardsInfo) > 0) { |
78: | foreach ($cardsInfo as $info) { |
79: | Api::Log('Destination contact card:', \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
80: | Api::Log(' ContactCard.Id: ' . $info->Id, \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
81: | Api::Log(' ContactCard.CardId: ' . $info->CardId, \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
82: | |
83: | $update = []; |
84: | if (isset($properties) && is_array($properties) && count($properties) > 0) { |
85: | Api::Log(' Source properties (count): ' . count($properties), \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
86: | if (isset($info->Properties)) { |
87: | $oldProperties = \json_decode($info->Properties, true); |
88: | $properties = array_merge($oldProperties, $properties); |
89: | } |
90: | Api::Log(' Result properties (count): ' . count($properties), \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
91: | $update['Properties'] = \json_encode($properties); |
92: | } |
93: | if ($row->Frequency > 0) { |
94: | Api::Log(' Frequency: ' . $row->Frequency, \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
95: | $update['Frequency'] = $row->Frequency; |
96: | } |
97: | if (count($update) > 0) { |
98: | if (!!ContactCard::where('Id', $info->Id)->update($update)) { |
99: | Api::Log('Contact data migrated successfuly!', \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
100: | } |
101: | } |
102: | } |
103: | } else { |
104: | Api::Log('No destination contact cards were found', \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
105: | } |
106: | } else { |
107: | Api::Log('No contact data to migrate', \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
108: | } |
109: | } catch (\Exception $e) { |
110: | Api::Log('Contact migration exception', \Aurora\System\Enums\LogLevel::Error, 'contacts-migration-'); |
111: | Api::LogObject($row, \Aurora\System\Enums\LogLevel::Error, 'contacts-migration-'); |
112: | Api::Log($e->getMessage(), \Aurora\System\Enums\LogLevel::Error, 'contacts-migration-'); |
113: | |
114: | } |
115: | |
116: | Api::Log('', \Aurora\System\Enums\LogLevel::Full, 'contacts-migration-'); |
117: | } |
118: | }); |
119: | } |
120: | |
121: | |
122: | |
123: | |
124: | |
125: | |
126: | public function down() {} |
127: | } |
128: | |