1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | namespace Aurora\Modules\Contacts; |
9: | |
10: | use Aurora\Api; |
11: | use Aurora\Modules\Contacts\Enums\Access; |
12: | use Aurora\Modules\Contacts\Enums\StorageType; |
13: | use Aurora\Modules\Contacts\Models\AddressBook; |
14: | use Aurora\Modules\Contacts\Models\Contact; |
15: | use Aurora\Modules\Contacts\Models\Group; |
16: | use Aurora\Modules\Core\Module as CoreModule; |
17: | use Aurora\System\Exceptions\ApiException; |
18: | use Aurora\System\Notifications; |
19: | use Illuminate\Database\Eloquent\Builder; |
20: | use Sabre\DAV\UUIDUtil; |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | class Module extends \Aurora\System\Module\AbstractModule |
30: | { |
31: | public $oManager = null; |
32: | |
33: | protected $aImportExportFormats = ['csv', 'vcf']; |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | public static function getInstance() |
40: | { |
41: | return \Aurora\System\Api::GetModule(self::GetName()); |
42: | } |
43: | |
44: | public function getManager() |
45: | { |
46: | if ($this->oManager === null) { |
47: | $this->oManager = new Manager($this); |
48: | } |
49: | |
50: | return $this->oManager; |
51: | } |
52: | |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | public function init() |
59: | { |
60: | $this->subscribeEvent('Mail::AfterUseEmails', array($this, 'onAfterUseEmails')); |
61: | $this->subscribeEvent('Mail::GetBodyStructureParts', array($this, 'onGetBodyStructureParts')); |
62: | $this->subscribeEvent('Core::DeleteUser::after', array($this, 'onAfterDeleteUser')); |
63: | |
64: | $this->subscribeEvent('Calendar::CreateEvent', array($this, 'onCreateOrUpdateEvent')); |
65: | $this->subscribeEvent('Calendar::UpdateEvent', array($this, 'onCreateOrUpdateEvent')); |
66: | } |
67: | |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | public function GetApiContactsManager() |
74: | { |
75: | return $this->getManager(); |
76: | } |
77: | |
78: | |
79: | |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | |
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: | public function GetSettings() |
145: | { |
146: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
147: | $oUser = \Aurora\System\Api::getAuthenticatedUser(); |
148: | $aResult = [ |
149: | 'AllowAddressBooksManagement' => $this->getConfig('AllowAddressBooksManagement', false), |
150: | 'ImportContactsLink' => $this->getConfig('ImportContactsLink', ''), |
151: | 'PrimaryEmail' => (new Enums\PrimaryEmail())->getMap(), |
152: | 'PrimaryPhone' => (new Enums\PrimaryPhone())->getMap(), |
153: | 'PrimaryAddress' => (new Enums\PrimaryAddress())->getMap(), |
154: | 'SortField' => (new Enums\SortField())->getMap(), |
155: | 'ImportExportFormats' => $this->aImportExportFormats, |
156: | 'SaveVcfServerModuleName' => \Aurora\System\Api::GetModuleManager()->ModuleExists('DavContacts') ? 'DavContacts' : '', |
157: | 'ContactsPerPage' => $this->getConfig('ContactsPerPage', 20), |
158: | 'ContactsSortBy' => $this->getConfig('ContactsSortBy', []) |
159: | ]; |
160: | |
161: | if ($oUser && $oUser->isNormalOrTenant()) { |
162: | if (isset($oUser->{self::GetName().'::ContactsPerPage'})) { |
163: | $aResult['ContactsPerPage'] = $oUser->{self::GetName().'::ContactsPerPage'}; |
164: | } |
165: | |
166: | $aResult['Storages'] = self::Decorator()->GetStorages(); |
167: | } |
168: | |
169: | return $aResult; |
170: | } |
171: | |
172: | public function IsDisplayedStorage($Storage) |
173: | { |
174: | return true; |
175: | } |
176: | |
177: | |
178: | public function GetContactStorages() |
179: | { |
180: | return $this->Decorator()->GetStorages(); |
181: | } |
182: | |
183: | public function GetStorages() |
184: | { |
185: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
186: | |
187: | $aStorages = []; |
188: | $aStorageNames = []; |
189: | $this->broadcastEvent('GetStorages', $aStorageNames); |
190: | \ksort($aStorageNames); |
191: | |
192: | $iUserId = \Aurora\System\Api::getAuthenticatedUserId(); |
193: | |
194: | foreach ($aStorageNames as $iIndex => $sStorageName) { |
195: | $aStorages[] = [ |
196: | 'Id' => $sStorageName, |
197: | 'CTag' => $this->Decorator()->GetCTag($iUserId, $sStorageName), |
198: | 'Display' => $this->Decorator()->IsDisplayedStorage($sStorageName), |
199: | 'Order' => $iIndex |
200: | ]; |
201: | } |
202: | |
203: | return array_merge($aStorages, $this->Decorator()->GetAddressBooks($iUserId)); |
204: | } |
205: | |
206: | |
207: | |
208: | |
209: | |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | |
216: | |
217: | |
218: | |
219: | |
220: | |
221: | |
222: | |
223: | |
224: | |
225: | |
226: | |
227: | |
228: | |
229: | |
230: | |
231: | |
232: | |
233: | |
234: | |
235: | |
236: | |
237: | |
238: | |
239: | |
240: | |
241: | |
242: | |
243: | |
244: | |
245: | |
246: | |
247: | |
248: | |
249: | |
250: | |
251: | |
252: | |
253: | |
254: | |
255: | |
256: | |
257: | |
258: | |
259: | public function UpdateSettings($ContactsPerPage) |
260: | { |
261: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
262: | |
263: | $bResult = false; |
264: | |
265: | $oUser = \Aurora\System\Api::getAuthenticatedUser(); |
266: | if ($oUser) { |
267: | if ($oUser->isNormalOrTenant()) { |
268: | $oUser->setExtendedProp(self::GetName().'::ContactsPerPage', $ContactsPerPage); |
269: | return \Aurora\Modules\Core\Module::Decorator()->UpdateUserObject($oUser); |
270: | } |
271: | if ($oUser->isAdmin()) { |
272: | $this->setConfig('ContactsPerPage', $ContactsPerPage); |
273: | $bResult = $this->saveModuleConfig(); |
274: | } |
275: | } |
276: | |
277: | return $bResult; |
278: | } |
279: | |
280: | |
281: | |
282: | |
283: | |
284: | |
285: | |
286: | |
287: | |
288: | |
289: | |
290: | |
291: | |
292: | |
293: | |
294: | |
295: | |
296: | |
297: | |
298: | |
299: | |
300: | |
301: | |
302: | |
303: | |
304: | |
305: | |
306: | |
307: | |
308: | |
309: | |
310: | |
311: | |
312: | |
313: | |
314: | |
315: | |
316: | |
317: | |
318: | |
319: | |
320: | |
321: | |
322: | |
323: | |
324: | |
325: | |
326: | |
327: | |
328: | |
329: | |
330: | |
331: | |
332: | |
333: | |
334: | public function Export($UserId, $Storage, $Format, Builder $Filters = null, $GroupUUID = '', $ContactUUIDs = []) |
335: | { |
336: | Api::CheckAccess($UserId); |
337: | |
338: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
339: | |
340: | $sOutput = ''; |
341: | |
342: | if (empty($GroupUUID) && $Format === 'vcf') { |
343: | $aGroups = $this->getManager()->getGroups($UserId); |
344: | foreach ($aGroups as $oGroup) { |
345: | $oVCard = new \Sabre\VObject\Component\VCard(); |
346: | \Aurora\Modules\Contacts\Classes\VCard\Helper::UpdateVCardFromGroup($oGroup, $oVCard); |
347: | foreach ($oGroup->Contacts as $oContact) { |
348: | if ($oContact) { |
349: | $sVCardUID = null; |
350: | if ($oContact->Storage !== 'team') { |
351: | if (!empty($oContact->{'DavContacts::VCardUID'})) { |
352: | $sVCardUID = $oContact->{'DavContacts::VCardUID'}; |
353: | } |
354: | } else { |
355: | $sVCardUID = $oContact->UUID; |
356: | } |
357: | if (isset($sVCardUID)) { |
358: | $oVCard->add('X-ADDRESSBOOKSERVER-MEMBER', 'urn:uuid:' . $sVCardUID); |
359: | } |
360: | } |
361: | } |
362: | |
363: | $sOutput .= $oVCard->serialize(); |
364: | } |
365: | } |
366: | |
367: | $oQuery = ($Filters instanceof Builder) ? $Filters : Models\Contact::query(); |
368: | $oQuery->where(function ($query) use ($UserId, $Storage) { |
369: | $this->prepareFiltersFromStorage($UserId, $Storage, Enums\SortField::Name, $query); |
370: | }); |
371: | |
372: | if (empty($ContactUUIDs) && !empty($GroupUUID)) { |
373: | $oGroup = Group::firstWhere('UUID', $GroupUUID); |
374: | if ($oGroup) { |
375: | $oQuery->whereHas('Groups', function ($oSubQuery) use ($oGroup) { |
376: | return $oSubQuery->where('Groups.Id', $oGroup->Id); |
377: | }); |
378: | } |
379: | } |
380: | if (count($ContactUUIDs) > 0) { |
381: | $oQuery->whereIn('UUID', $ContactUUIDs); |
382: | } |
383: | |
384: | $aContacts = $this->getManager()->getContacts(Enums\SortField::Name, \Aurora\System\Enums\SortOrder::ASC, 0, 0, $oQuery); |
385: | |
386: | switch ($Format) { |
387: | case 'csv': |
388: | $oSync = new Classes\Csv\Sync(); |
389: | $sOutput = $oSync->Export($aContacts); |
390: | break; |
391: | case 'vcf': |
392: | foreach ($aContacts as $oContact) { |
393: | |
394: | |
395: | $sOutput .= self::Decorator()->GetContactAsVCF($UserId, $oContact); |
396: | } |
397: | break; |
398: | } |
399: | |
400: | if (is_string($sOutput) && !empty($sOutput)) { |
401: | header('Pragma: public'); |
402: | header('Content-Type: text/csv'); |
403: | header('Content-Disposition: attachment; filename="export.' . $Format . '";'); |
404: | header('Content-Transfer-Encoding: binary'); |
405: | } |
406: | |
407: | echo $sOutput; |
408: | } |
409: | |
410: | public function GetContactAsVCF($UserId, $Contact) |
411: | { |
412: | Api::CheckAccess($UserId); |
413: | $oVCard = new \Sabre\VObject\Component\VCard(); |
414: | Classes\VCard\Helper::UpdateVCardFromContact($Contact, $oVCard); |
415: | return $oVCard->serialize(); |
416: | } |
417: | |
418: | |
419: | |
420: | |
421: | |
422: | |
423: | |
424: | |
425: | |
426: | |
427: | |
428: | |
429: | |
430: | |
431: | |
432: | |
433: | |
434: | |
435: | |
436: | |
437: | |
438: | |
439: | |
440: | |
441: | |
442: | |
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: | public function GetGroups($UserId = null) |
468: | { |
469: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
470: | |
471: | Api::CheckAccess($UserId); |
472: | |
473: | return $this->getManager()->getGroups($UserId)->toArray(); |
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: | |
501: | |
502: | |
503: | |
504: | |
505: | |
506: | |
507: | |
508: | |
509: | |
510: | |
511: | |
512: | |
513: | |
514: | |
515: | |
516: | |
517: | |
518: | |
519: | |
520: | |
521: | |
522: | |
523: | |
524: | |
525: | |
526: | |
527: | |
528: | |
529: | |
530: | |
531: | |
532: | |
533: | |
534: | |
535: | |
536: | |
537: | |
538: | |
539: | |
540: | |
541: | |
542: | |
543: | |
544: | public function GetGroup($UserId, $UUID) |
545: | { |
546: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
547: | |
548: | Api::CheckAccess($UserId); |
549: | |
550: | return $this->getManager()->getGroup($UUID); |
551: | } |
552: | |
553: | |
554: | |
555: | |
556: | |
557: | |
558: | public function GetGroupByName($Name, $UserId = null) |
559: | { |
560: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
561: | |
562: | if (isset($UserId) && $UserId !== \Aurora\System\Api::getAuthenticatedUserId()) { |
563: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin); |
564: | } else { |
565: | $UserId = \Aurora\System\Api::getAuthenticatedUserId(); |
566: | } |
567: | |
568: | Api::CheckAccess($UserId); |
569: | |
570: | return $this->getManager()->getGroupByName($Name, $UserId); |
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: | |
615: | |
616: | |
617: | |
618: | |
619: | |
620: | |
621: | |
622: | |
623: | |
624: | |
625: | |
626: | |
627: | |
628: | |
629: | |
630: | |
631: | |
632: | |
633: | |
634: | |
635: | |
636: | |
637: | |
638: | |
639: | |
640: | |
641: | |
642: | |
643: | |
644: | |
645: | public function GetContacts($UserId, $Storage = '', $Offset = 0, $Limit = 20, $SortField = Enums\SortField::Name, $SortOrder = \Aurora\System\Enums\SortOrder::ASC, $Search = '', $GroupUUID = '', Builder $Filters = null, $WithGroups = false, $WithoutTeamContactsDuplicates = false, $Suggestions = false) |
646: | { |
647: | |
648: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
649: | |
650: | Api::CheckAccess($UserId); |
651: | |
652: | $oQuery = ($Filters instanceof Builder) ? $Filters : Models\Contact::query(); |
653: | $oQuery->where(function ($query) use ($UserId, $Storage, $SortField, $Suggestions) { |
654: | $this->prepareFiltersFromStorage($UserId, $Storage, $SortField, $query, $Suggestions); |
655: | }); |
656: | |
657: | if (!empty($GroupUUID)) { |
658: | $oGroup = Group::firstWhere('UUID', $GroupUUID); |
659: | if ($oGroup) { |
660: | $oQuery = $oQuery->whereHas('Groups', function ($oSubQuery) use ($oGroup) { |
661: | return $oSubQuery->where('contacts_groups.Id', $oGroup->Id); |
662: | }); |
663: | } |
664: | } |
665: | if (!empty($Search)) { |
666: | $oQuery = $oQuery->where(function ($query) use ($Search) { |
667: | $query->where('FullName', 'LIKE', '%'.$Search.'%') |
668: | ->orWhere('ViewEmail', 'LIKE', '%'.$Search.'%') |
669: | ->orWhere('PersonalEmail', 'LIKE', '%'.$Search.'%') |
670: | ->orWhere('BusinessEmail', 'LIKE', '%'.$Search.'%') |
671: | ->orWhere('OtherEmail', 'LIKE', '%'.$Search.'%') |
672: | ->orWhere('BusinessCompany', 'LIKE', '%'.$Search.'%'); |
673: | }); |
674: | } |
675: | |
676: | $aGroupUsersList = []; |
677: | |
678: | if ($WithGroups) { |
679: | $oUser = \Aurora\System\Api::getAuthenticatedUser(); |
680: | if ($oUser instanceof \Aurora\Modules\Core\Models\User) { |
681: | $aGroups = $this->getManager()->getGroups($oUser->Id, Group::where('Name', 'LIKE', "%{$Search}%")); |
682: | if ($aGroups) { |
683: | foreach ($aGroups as $oGroup) { |
684: | $aGroupContactsEmails = $oGroup->Contacts->map(function ($oContact) { |
685: | return $oContact->FullName ? "\"{$oContact->FullName}\" <{$oContact->ViewEmail}>" : $oContact->ViewEmail; |
686: | })->toArray(); |
687: | |
688: | $aGroupUsersList[] = [ |
689: | 'UUID' => $oGroup->UUID, |
690: | 'IdUser' => $oGroup->IdUser, |
691: | 'FullName' => $oGroup->Name, |
692: | 'FirstName' => '', |
693: | 'LastName' => '', |
694: | 'ViewEmail' => implode(', ', $aGroupContactsEmails), |
695: | 'Storage' => '', |
696: | 'Frequency' => 0, |
697: | 'DateModified' => '', |
698: | 'IsGroup' => true, |
699: | ]; |
700: | } |
701: | } |
702: | } |
703: | } |
704: | |
705: | $iCount = $this->getManager()->getContactsCount($oQuery); |
706: | $aContactsCol = $this->getManager()->getContacts($SortField, $SortOrder, $Offset, $Limit, $oQuery); |
707: | $aContacts = $aContactsCol->toArray(); |
708: | |
709: | if ($Storage === StorageType::All && $WithoutTeamContactsDuplicates) { |
710: | $aPersonalContacsCol = $aContactsCol->map(function ($oContact) { |
711: | if ($oContact->Storage === StorageType::Personal && $oContact->Auto === false) { |
712: | return $oContact; |
713: | } |
714: | }); |
715: | |
716: | foreach ($aContacts as $key => $aContact) { |
717: | $sViewEmail = $aContact['ViewEmail']; |
718: | $sStorage = $aContact['Storage']; |
719: | if ($sStorage === StorageType::Team && $aPersonalContacsCol->unique()->contains('ViewEmail', $sViewEmail)) { |
720: | unset($aContacts[$key]); |
721: | } elseif ($sStorage === StorageType::Personal && $aContact['Auto'] === true) { |
722: | foreach ($aContacts as $subKey => $aSubContact) { |
723: | if ($aSubContact['Storage'] === StorageType::Team && $aSubContact['ViewEmail'] === $sViewEmail) { |
724: | $aContacts[$subKey]['AgeScore'] = $aContacts[$key]['AgeScore']; |
725: | unset($aContacts[$key]); |
726: | } |
727: | if ($aSubContact['Storage'] === StorageType::Personal && $aSubContact['Auto'] === false && $aSubContact['ViewEmail'] === $sViewEmail) { |
728: | unset($aContacts[$key]); |
729: | } |
730: | } |
731: | } |
732: | } |
733: | } |
734: | $aList = array_map(function ($aContact) { |
735: | if ($aContact['Storage'] === StorageType::AddressBook) { |
736: | $aContact['Storage'] = $aContact['Storage'] . $aContact['AddressBookId']; |
737: | } |
738: | return [ |
739: | 'UUID' => $aContact['UUID'], |
740: | 'IdUser' => $aContact['IdUser'], |
741: | 'FullName' => $aContact['FullName'], |
742: | 'FirstName' => isset($aContact['FirstName']) ? $aContact['FirstName'] : '', |
743: | 'LastName' => isset($aContact['LastName']) ? $aContact['LastName'] : '', |
744: | 'ViewEmail' => $aContact['ViewEmail'], |
745: | 'Storage' => $aContact['Storage'], |
746: | 'Frequency' => $aContact['Frequency'], |
747: | 'DateModified' => isset($aContact['DateModified']) ? $aContact['DateModified'] : 0, |
748: | 'ETag' => isset($aContact['ETag']) ? $aContact['ETag'] : '', |
749: | 'AgeScore' => isset($aContact['AgeScore']) ? (float) $aContact['AgeScore'] : 0 |
750: | ]; |
751: | }, $aContacts); |
752: | |
753: | $aList = array_merge($aList, $aGroupUsersList); |
754: | return [ |
755: | 'ContactCount' => $iCount + count($aGroupUsersList), |
756: | 'List' => \Aurora\System\Managers\Response::GetResponseObject($aList) |
757: | ]; |
758: | } |
759: | |
760: | protected function _getContactSuggestions($UserId, $Storage, $Limit = 20, $SortField = Enums\SortField::Name, $SortOrder = \Aurora\System\Enums\SortOrder::ASC, $Search = '', $WithGroups = false, $WithoutTeamContactsDuplicates = false) |
761: | { |
762: | |
763: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
764: | |
765: | Api::CheckAccess($UserId); |
766: | |
767: | $aResult = array( |
768: | 'ContactCount' => 0, |
769: | 'List' => [] |
770: | ); |
771: | |
772: | $aContacts = $this->Decorator()->GetContacts($UserId, $Storage, 0, $Limit, $SortField, $SortOrder, $Search, '', null, $WithGroups, $WithoutTeamContactsDuplicates, true); |
773: | $aResultList = $aContacts['List']; |
774: | |
775: | $contactsColl = collect($aContacts['List']); |
776: | |
777: | |
778: | |
779: | $aResult['List'] = $aResultList; |
780: | $aResult['ContactCount'] = count($aResultList); |
781: | return $aResult; |
782: | } |
783: | |
784: | public function GetContactSuggestions($UserId, $Storage, $Limit = 20, $SortField = Enums\SortField::Name, $SortOrder = \Aurora\System\Enums\SortOrder::ASC, $Search = '', $WithGroups = false, $WithoutTeamContactsDuplicates = false, $WithUserGroups = false) |
785: | { |
786: | $aResult = $this->_getContactSuggestions($UserId, $Storage, $Limit, $SortField, $SortOrder, $Search, $WithGroups, $WithoutTeamContactsDuplicates); |
787: | |
788: | if ($WithUserGroups) { |
789: | $oUser = CoreModule::Decorator()->GetUserUnchecked($UserId); |
790: | if ($oUser) { |
791: | $aGroups = CoreModule::Decorator()->GetGroups($oUser->IdTenant, $Search); |
792: | foreach ($aGroups['Items'] as $aGroup) { |
793: | $aGroup['IsGroup'] = true; |
794: | $aResult['List'][] = $aGroup; |
795: | |
796: | $aResult['ContactCount']++; |
797: | } |
798: | } |
799: | } |
800: | |
801: | return $aResult; |
802: | } |
803: | |
804: | |
805: | |
806: | |
807: | |
808: | public function CheckAccessToObject($User, $Contact, $Access = null) |
809: | { |
810: | return true; |
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: | |
860: | |
861: | |
862: | |
863: | |
864: | |
865: | |
866: | |
867: | |
868: | |
869: | |
870: | |
871: | |
872: | |
873: | |
874: | public function GetContact($UUID, $UserId = null) |
875: | { |
876: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
877: | |
878: | Api::CheckAccess($UserId); |
879: | |
880: | $oUser = \Aurora\Modules\Core\Module::getInstance()->GetUserUnchecked($UserId); |
881: | |
882: | $mResult = false; |
883: | |
884: | if ($oUser instanceof \Aurora\Modules\Core\Models\User) { |
885: | $oContact = $this->getManager()->getContact($UUID); |
886: | if ($oContact->Storage === StorageType::AddressBook) { |
887: | $oContact->Storage = $oContact->Storage . '-' . $oContact->AddressBookId; |
888: | } |
889: | if (self::Decorator()->CheckAccessToObject($oUser, $oContact)) { |
890: | $mResult = $oContact; |
891: | } |
892: | } |
893: | |
894: | return $mResult; |
895: | } |
896: | |
897: | |
898: | |
899: | |
900: | |
901: | |
902: | |
903: | |
904: | |
905: | |
906: | |
907: | |
908: | |
909: | |
910: | |
911: | |
912: | |
913: | |
914: | |
915: | |
916: | |
917: | |
918: | |
919: | |
920: | |
921: | |
922: | |
923: | |
924: | |
925: | |
926: | |
927: | |
928: | |
929: | |
930: | |
931: | |
932: | |
933: | |
934: | |
935: | |
936: | |
937: | |
938: | |
939: | |
940: | |
941: | |
942: | |
943: | |
944: | |
945: | |
946: | |
947: | |
948: | |
949: | |
950: | |
951: | |
952: | |
953: | |
954: | |
955: | |
956: | |
957: | |
958: | |
959: | public function GetContactsByEmails($UserId, $Storage, $Emails, $Filters = null, $AsArray = true) |
960: | { |
961: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
962: | $aContacts = []; |
963: | |
964: | Api::CheckAccess($UserId); |
965: | |
966: | $oQuery = ($Filters instanceof Builder) ? $Filters : Contact::query(); |
967: | $oQuery->where(function ($query) use ($UserId, $Storage) { |
968: | $this->prepareFiltersFromStorage($UserId, $Storage, Enums\SortField::Name, $query); |
969: | }); |
970: | $oQuery = $oQuery->whereIn('ViewEmail', $Emails); |
971: | if ($Storage !== StorageType::All) { |
972: | $oQuery->where('Storage', $Storage); |
973: | } |
974: | |
975: | if ($AsArray) { |
976: | $aContacts = $this->getManager()->getContactsAsArray(Enums\SortField::Name, \Aurora\System\Enums\SortOrder::ASC, 0, 0, $oQuery); |
977: | } else { |
978: | $aContacts = $this->getManager()->getContacts(Enums\SortField::Name, \Aurora\System\Enums\SortOrder::ASC, 0, 0, $oQuery); |
979: | } |
980: | return $aContacts; |
981: | } |
982: | |
983: | |
984: | |
985: | |
986: | |
987: | |
988: | |
989: | public function GetContactsByUids($UserId, $Uids) |
990: | { |
991: | $aResult = []; |
992: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
993: | |
994: | Api::CheckAccess($UserId); |
995: | |
996: | $oUser = \Aurora\Modules\Core\Module::getInstance()->GetUserUnchecked($UserId); |
997: | |
998: | if (is_array($Uids) && count($Uids) > 0) { |
999: | $aContacts = $this->getManager()->getContacts( |
1000: | Enums\SortField::Name, |
1001: | \Aurora\System\Enums\SortOrder::ASC, |
1002: | 0, |
1003: | 0, |
1004: | Contact::whereIn('UUID', $Uids) |
1005: | ); |
1006: | |
1007: | foreach ($aContacts as $oContact) { |
1008: | if ($oContact instanceof \Aurora\Modules\Contacts\Models\Contact) { |
1009: | if (self::Decorator()->CheckAccessToObject($oUser, $oContact)) { |
1010: | |
1011: | $oContact->Storage = ($oContact->Auto) ? 'collected' : $oContact->Storage; |
1012: | $aResult[] = $oContact; |
1013: | } |
1014: | } |
1015: | } |
1016: | } else { |
1017: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); |
1018: | } |
1019: | |
1020: | return $aResult; |
1021: | } |
1022: | |
1023: | |
1024: | |
1025: | |
1026: | |
1027: | |
1028: | |
1029: | public function GetContactsInfo($Storage, $UserId = null, Builder $Filters = null) |
1030: | { |
1031: | $aResult = [ |
1032: | 'CTag' => $this->GetCTag($UserId, $Storage), |
1033: | 'Info' => array() |
1034: | ]; |
1035: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
1036: | |
1037: | Api::CheckAccess($UserId); |
1038: | |
1039: | $oQuery = ($Filters instanceof Builder) ? $Filters : Models\Contact::query(); |
1040: | $oQuery->where(function ($query) use ($UserId, $Storage) { |
1041: | $this->prepareFiltersFromStorage($UserId, $Storage, Enums\SortField::Name, $query); |
1042: | }); |
1043: | |
1044: | $aContacts = $oQuery->get(['UUID', 'ETag', 'Auto', 'Storage']); |
1045: | |
1046: | foreach ($aContacts as $oContact) { |
1047: | $aResult['Info'][] = [ |
1048: | 'UUID' => $oContact->UUID, |
1049: | 'ETag' => $oContact->ETag, |
1050: | 'Storage' => $oContact->Auto ? 'collected' : $oContact->getStorageWithId() |
1051: | ]; |
1052: | } |
1053: | |
1054: | return $aResult; |
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: | |
1119: | |
1120: | public function CreateContact($Contact, $UserId = null) |
1121: | { |
1122: | Api::CheckAccess($UserId); |
1123: | |
1124: | $oUser = \Aurora\Modules\Core\Module::getInstance()->GetUserUnchecked($UserId); |
1125: | |
1126: | $mResult = false; |
1127: | |
1128: | if ($oUser instanceof \Aurora\Modules\Core\Models\User) { |
1129: | $oContact = new Models\Contact(); |
1130: | $oContact->IdUser = $oUser->Id; |
1131: | $oContact->IdTenant = $oUser->IdTenant; |
1132: | $oContact->populate($Contact, true); |
1133: | |
1134: | if (self::Decorator()->CheckAccessToObject($oUser, $oContact, Access::Write)) { |
1135: | $oContact->Frequency = $this->getAutocreatedContactFrequencyAndDeleteIt($oUser->Id, $oContact->ViewEmail); |
1136: | if ($this->getManager()->createContact($oContact)) { |
1137: | $oContact->addGroups( |
1138: | isset($Contact['GroupUUIDs']) ? $Contact['GroupUUIDs'] : null, |
1139: | isset($Contact['GroupNames']) ? $Contact['GroupNames'] : null, |
1140: | true |
1141: | ); |
1142: | $mResult = ['UUID' => $oContact->UUID, 'ETag' => $oContact->ETag]; |
1143: | } |
1144: | } |
1145: | } |
1146: | |
1147: | return $mResult; |
1148: | } |
1149: | |
1150: | |
1151: | |
1152: | |
1153: | |
1154: | |
1155: | |
1156: | private function getAutocreatedContactFrequencyAndDeleteIt($UserId, $sViewEmail) |
1157: | { |
1158: | Api::CheckAccess($UserId); |
1159: | |
1160: | $iFrequency = 0; |
1161: | $sStorage = 'personal'; |
1162: | $oQuery = Contact::where([ |
1163: | ['ViewEmail', '=', $sViewEmail], |
1164: | ['IdUser', '=', $UserId], |
1165: | ['Auto', '=', true], |
1166: | ['Storage', '=', $sStorage] |
1167: | ]); |
1168: | $oAutocreatedContacts = $this->getManager()->getContacts( |
1169: | \Aurora\Modules\Contacts\Enums\SortField::Name, |
1170: | \Aurora\System\Enums\SortOrder::ASC, |
1171: | 0, |
1172: | 1, |
1173: | $oQuery |
1174: | ); |
1175: | if (is_array($oAutocreatedContacts) && isset($oAutocreatedContacts[0])) { |
1176: | $iFrequency = $oAutocreatedContacts[0]->Frequency; |
1177: | $this->getManager()->deleteContacts($UserId, $sStorage, [$oAutocreatedContacts[0]->UUID]); |
1178: | $this->getManager()->updateCTag($UserId, $sStorage); |
1179: | } |
1180: | return $iFrequency; |
1181: | } |
1182: | |
1183: | |
1184: | |
1185: | |
1186: | |
1187: | |
1188: | |
1189: | |
1190: | |
1191: | |
1192: | |
1193: | |
1194: | |
1195: | |
1196: | |
1197: | |
1198: | |
1199: | |
1200: | |
1201: | |
1202: | |
1203: | |
1204: | |
1205: | |
1206: | |
1207: | |
1208: | |
1209: | |
1210: | |
1211: | |
1212: | |
1213: | |
1214: | |
1215: | |
1216: | |
1217: | |
1218: | |
1219: | |
1220: | |
1221: | |
1222: | |
1223: | |
1224: | |
1225: | |
1226: | |
1227: | |
1228: | |
1229: | |
1230: | |
1231: | |
1232: | |
1233: | |
1234: | |
1235: | |
1236: | |
1237: | |
1238: | |
1239: | |
1240: | |
1241: | |
1242: | |
1243: | |
1244: | public function UpdateContact($UserId, $Contact) |
1245: | { |
1246: | Api::CheckAccess($UserId); |
1247: | |
1248: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
1249: | |
1250: | $oContact = $this->getManager()->getContact($Contact['UUID']); |
1251: | if ($oContact) { |
1252: | $oContact->populate($Contact, true); |
1253: | if ($this->UpdateContactObject($oContact)) { |
1254: | $oContact->addGroups( |
1255: | isset($Contact['GroupUUIDs']) ? $Contact['GroupUUIDs'] : null, |
1256: | isset($Contact['GroupNames']) ? $Contact['GroupNames'] : null, |
1257: | true |
1258: | ); |
1259: | return [ |
1260: | 'UUID' => $oContact->UUID, |
1261: | 'ETag' => $oContact->ETag |
1262: | ]; |
1263: | } else { |
1264: | return false; |
1265: | } |
1266: | } |
1267: | |
1268: | return false; |
1269: | } |
1270: | |
1271: | public function UpdateContactObject($Contact) |
1272: | { |
1273: | $mResult = false; |
1274: | |
1275: | $oUser = \Aurora\System\Api::getAuthenticatedUser(); |
1276: | $aStorageParts = \explode('-', $Contact->Storage); |
1277: | if (isset($aStorageParts[0], $aStorageParts[1]) && $aStorageParts[0] === StorageType::AddressBook) { |
1278: | $Contact->AddressBookId = (int) $aStorageParts[1]; |
1279: | $Contact->Storage = StorageType::AddressBook; |
1280: | } |
1281: | if (self::Decorator()->CheckAccessToObject($oUser, $Contact, Enums\Access::Write)) { |
1282: | $mResult = $this->getManager()->updateContact($Contact); |
1283: | } |
1284: | |
1285: | return $mResult; |
1286: | } |
1287: | |
1288: | |
1289: | |
1290: | |
1291: | |
1292: | |
1293: | |
1294: | |
1295: | |
1296: | |
1297: | |
1298: | |
1299: | |
1300: | |
1301: | |
1302: | |
1303: | |
1304: | |
1305: | |
1306: | |
1307: | |
1308: | |
1309: | |
1310: | |
1311: | |
1312: | |
1313: | |
1314: | |
1315: | |
1316: | |
1317: | |
1318: | |
1319: | |
1320: | |
1321: | |
1322: | |
1323: | |
1324: | |
1325: | |
1326: | |
1327: | |
1328: | |
1329: | |
1330: | |
1331: | |
1332: | |
1333: | |
1334: | |
1335: | |
1336: | |
1337: | |
1338: | |
1339: | |
1340: | |
1341: | |
1342: | public function DeleteContacts($UserId, $Storage, $UUIDs) |
1343: | { |
1344: | $mResult = false; |
1345: | Api::CheckAccess($UserId); |
1346: | $oUser = Api::getUserById($UserId); |
1347: | |
1348: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
1349: | |
1350: | $sStorage = $Storage; |
1351: | $aStorageParts = \explode('-', $sStorage); |
1352: | if (isset($aStorageParts[0]) && $aStorageParts[0] === StorageType::AddressBook) { |
1353: | $sStorage = StorageType::AddressBook; |
1354: | } |
1355: | |
1356: | $aContacts = Contact::whereIn('UUID', $UUIDs)->get(); |
1357: | $bCheck = true; |
1358: | foreach ($aContacts as $oContact) { |
1359: | if (!self::Decorator()->CheckAccessToObject($oUser, $oContact, Enums\Access::Write)) { |
1360: | $bCheck = false; |
1361: | break; |
1362: | } |
1363: | } |
1364: | |
1365: | if ($bCheck && $this->getManager()->deleteContacts($UserId, $sStorage, $UUIDs)) { |
1366: | $this->getManager()->updateCTag($UserId, $Storage); |
1367: | $mResult = true; |
1368: | } |
1369: | |
1370: | return $mResult; |
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: | |
1412: | |
1413: | |
1414: | |
1415: | |
1416: | |
1417: | |
1418: | |
1419: | |
1420: | |
1421: | |
1422: | |
1423: | |
1424: | |
1425: | |
1426: | |
1427: | |
1428: | public function CreateGroup($Group, $UserId = null) |
1429: | { |
1430: | Api::CheckAccess($UserId); |
1431: | |
1432: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
1433: | |
1434: | if (is_array($Group)) { |
1435: | \Aurora\System\Validator::validate($Group, [ |
1436: | 'Name' => 'required' |
1437: | ]); |
1438: | |
1439: | $oGroup = new Models\Group(); |
1440: | $oGroup->IdUser = (int) $UserId; |
1441: | |
1442: | $oGroup->populate($Group); |
1443: | |
1444: | $this->getManager()->createGroup($oGroup); |
1445: | $this->getManager()->updateCTag($UserId, 'personal'); |
1446: | |
1447: | if (isset($Group['Contacts']) && is_array($Group['Contacts'])) { |
1448: | $oGroup->Contacts()->sync( |
1449: | Models\Contact::whereIn('UUID', $Group['Contacts'])->get() |
1450: | ->map(function ($oContact) { |
1451: | return $oContact->Id; |
1452: | }) |
1453: | ); |
1454: | } |
1455: | |
1456: | return $oGroup ? $oGroup->UUID : false; |
1457: | } else { |
1458: | return false; |
1459: | } |
1460: | } |
1461: | |
1462: | |
1463: | |
1464: | |
1465: | |
1466: | |
1467: | |
1468: | |
1469: | |
1470: | |
1471: | |
1472: | |
1473: | |
1474: | |
1475: | |
1476: | |
1477: | |
1478: | |
1479: | |
1480: | |
1481: | |
1482: | |
1483: | |
1484: | |
1485: | |
1486: | |
1487: | |
1488: | |
1489: | |
1490: | |
1491: | |
1492: | |
1493: | |
1494: | |
1495: | |
1496: | |
1497: | |
1498: | |
1499: | |
1500: | |
1501: | |
1502: | |
1503: | |
1504: | |
1505: | |
1506: | |
1507: | |
1508: | |
1509: | |
1510: | |
1511: | |
1512: | |
1513: | |
1514: | |
1515: | |
1516: | |
1517: | public function UpdateGroup($UserId, $Group) |
1518: | { |
1519: | Api::CheckAccess($UserId); |
1520: | |
1521: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
1522: | |
1523: | $oGroup = $this->getManager()->getGroup($Group['UUID']); |
1524: | if ($oGroup) { |
1525: | $oGroup->populate($Group); |
1526: | |
1527: | if (isset($Group['Contacts']) && is_array($Group['Contacts'])) { |
1528: | $oGroup->Contacts()->sync( |
1529: | Models\Contact::whereIn('UUID', $Group['Contacts'])->get() |
1530: | ->map(function ($oContact) { |
1531: | return $oContact->Id; |
1532: | }) |
1533: | ); |
1534: | } |
1535: | |
1536: | return $oGroup->save(); |
1537: | } |
1538: | |
1539: | return false; |
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: | |
1571: | |
1572: | |
1573: | |
1574: | |
1575: | |
1576: | |
1577: | |
1578: | |
1579: | |
1580: | |
1581: | |
1582: | |
1583: | |
1584: | |
1585: | |
1586: | |
1587: | |
1588: | |
1589: | |
1590: | |
1591: | |
1592: | |
1593: | |
1594: | |
1595: | public function DeleteGroup($UserId, $UUID) |
1596: | { |
1597: | Api::CheckAccess($UserId); |
1598: | |
1599: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
1600: | $this->getManager()->updateCTag($UserId, 'personal'); |
1601: | return $this->getManager()->deleteGroups([$UUID]); |
1602: | } |
1603: | |
1604: | |
1605: | |
1606: | |
1607: | |
1608: | |
1609: | |
1610: | |
1611: | |
1612: | |
1613: | |
1614: | |
1615: | |
1616: | |
1617: | |
1618: | |
1619: | |
1620: | |
1621: | |
1622: | |
1623: | |
1624: | |
1625: | |
1626: | |
1627: | |
1628: | |
1629: | |
1630: | |
1631: | |
1632: | |
1633: | |
1634: | |
1635: | |
1636: | |
1637: | |
1638: | |
1639: | |
1640: | |
1641: | |
1642: | |
1643: | |
1644: | |
1645: | |
1646: | |
1647: | |
1648: | |
1649: | |
1650: | |
1651: | |
1652: | |
1653: | |
1654: | |
1655: | |
1656: | |
1657: | |
1658: | |
1659: | public function AddContactsToGroup($UserId, $GroupUUID, $ContactUUIDs) |
1660: | { |
1661: | Api::CheckAccess($UserId); |
1662: | |
1663: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
1664: | |
1665: | if (is_array($ContactUUIDs) && !empty($ContactUUIDs)) { |
1666: | return $this->getManager()->addContactsToGroup($GroupUUID, $ContactUUIDs); |
1667: | } |
1668: | |
1669: | return true; |
1670: | } |
1671: | |
1672: | |
1673: | |
1674: | |
1675: | |
1676: | |
1677: | |
1678: | |
1679: | |
1680: | |
1681: | |
1682: | |
1683: | |
1684: | |
1685: | |
1686: | |
1687: | |
1688: | |
1689: | |
1690: | |
1691: | |
1692: | |
1693: | |
1694: | |
1695: | |
1696: | |
1697: | |
1698: | |
1699: | |
1700: | |
1701: | |
1702: | |
1703: | |
1704: | |
1705: | |
1706: | |
1707: | |
1708: | |
1709: | |
1710: | |
1711: | |
1712: | |
1713: | |
1714: | |
1715: | |
1716: | |
1717: | |
1718: | |
1719: | |
1720: | |
1721: | |
1722: | |
1723: | |
1724: | |
1725: | |
1726: | |
1727: | public function RemoveContactsFromGroup($UserId, $GroupUUID, $ContactUUIDs) |
1728: | { |
1729: | Api::CheckAccess($UserId); |
1730: | |
1731: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
1732: | |
1733: | if (is_array($ContactUUIDs) && !empty($ContactUUIDs)) { |
1734: | return $this->getManager()->removeContactsFromGroup($GroupUUID, $ContactUUIDs); |
1735: | } |
1736: | |
1737: | return true; |
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: | |
1783: | |
1784: | |
1785: | |
1786: | |
1787: | |
1788: | |
1789: | |
1790: | |
1791: | |
1792: | |
1793: | |
1794: | |
1795: | |
1796: | |
1797: | |
1798: | public function Import($UserId, $UploadData, $GroupUUID, $Storage = null) |
1799: | { |
1800: | Api::CheckAccess($UserId); |
1801: | |
1802: | $oUser = \Aurora\Modules\Core\Module::getInstance()->GetUserUnchecked($UserId); |
1803: | |
1804: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
1805: | |
1806: | $aResponse = array( |
1807: | 'ImportedCount' => 0, |
1808: | 'ParsedCount' => 0 |
1809: | ); |
1810: | |
1811: | if (is_array($UploadData)) { |
1812: | $oApiFileCacheManager = new \Aurora\System\Managers\Filecache(); |
1813: | $sTempFileName = 'import-post-' . md5($UploadData['name'] . $UploadData['tmp_name']); |
1814: | if ($oApiFileCacheManager->moveUploadedFile($oUser->UUID, $sTempFileName, $UploadData['tmp_name'], '', self::GetName())) { |
1815: | $sTempFilePath = $oApiFileCacheManager->generateFullFilePath($oUser->UUID, $sTempFileName, '', self::GetName()); |
1816: | |
1817: | $aImportResult = array(); |
1818: | |
1819: | $sFileExtension = strtolower(\Aurora\System\Utils::GetFileExtension($UploadData['name'])); |
1820: | switch ($sFileExtension) { |
1821: | case 'csv': |
1822: | $oSync = new Classes\Csv\Sync(); |
1823: | $aImportResult = $oSync->Import($oUser->Id, $sTempFilePath, $GroupUUID, $Storage); |
1824: | break; |
1825: | case 'vcf': |
1826: | $aImportResult = $this->importVcf($oUser->Id, $sTempFilePath, $Storage); |
1827: | break; |
1828: | } |
1829: | |
1830: | if (is_array($aImportResult) && isset($aImportResult['ImportedCount']) && isset($aImportResult['ParsedCount'])) { |
1831: | $aResponse['ImportedCount'] = $aImportResult['ImportedCount']; |
1832: | $aResponse['ParsedCount'] = $aImportResult['ParsedCount']; |
1833: | } else { |
1834: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::IncorrectFileExtension); |
1835: | } |
1836: | |
1837: | $oApiFileCacheManager->clear($oUser->UUID, $sTempFileName, '', self::GetName()); |
1838: | } else { |
1839: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::UnknownError); |
1840: | } |
1841: | } else { |
1842: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::UnknownError); |
1843: | } |
1844: | |
1845: | return $aResponse; |
1846: | } |
1847: | |
1848: | public function GetGroupEvents($UserId, $UUID) |
1849: | { |
1850: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
1851: | |
1852: | Api::CheckAccess($UserId); |
1853: | |
1854: | $aResult = []; |
1855: | $aEvents = $this->_getGroupEvents($UUID); |
1856: | if (is_array($aEvents) && 0 < count($aEvents)) { |
1857: | foreach ($aEvents as $oEvent) { |
1858: | $oCalendarModule = Api::GetModule('Calendar'); |
1859: | if ($oCalendarModule) { |
1860: | $aResult[] = $oCalendarModule->GetBaseEvent($UserId, $oEvent->CalendarUUID, $oEvent->EventUUID); |
1861: | } |
1862: | } |
1863: | } |
1864: | |
1865: | return $aResult; |
1866: | } |
1867: | |
1868: | public function UpdateSharedContacts($UserId, $UUIDs) |
1869: | { |
1870: | Api::CheckAccess($UserId); |
1871: | |
1872: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
1873: | return true; |
1874: | } |
1875: | |
1876: | public function AddContactsFromFile($UserId, $File) |
1877: | { |
1878: | Api::CheckAccess($UserId); |
1879: | |
1880: | $oUser = \Aurora\Modules\Core\Module::getInstance()->GetUserUnchecked($UserId); |
1881: | |
1882: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
1883: | |
1884: | if (empty($File)) { |
1885: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); |
1886: | } |
1887: | |
1888: | $oApiFileCache = new \Aurora\System\Managers\Filecache(); |
1889: | |
1890: | $sTempFilePath = $oApiFileCache->generateFullFilePath($oUser->UUID, $File); |
1891: | $aImportResult = $this->importVcf($oUser->Id, $sTempFilePath); |
1892: | |
1893: | return $aImportResult; |
1894: | } |
1895: | |
1896: | public function GetCTag($UserId, $Storage) |
1897: | { |
1898: | Api::CheckAccess($UserId); |
1899: | |
1900: | $oUser = \Aurora\Modules\Core\Module::getInstance()->GetUserUnchecked($UserId); |
1901: | |
1902: | $iResult = 0; |
1903: | if ($oUser instanceof \Aurora\Modules\Core\Models\User) { |
1904: | $aStorageParts = \explode('-', $Storage); |
1905: | $iUserId = $Storage === StorageType::Personal || $Storage === StorageType::Collected || (isset($aStorageParts[0]) && $aStorageParts[0] === StorageType::AddressBook) ? $oUser->Id : $oUser->IdTenant; |
1906: | |
1907: | $oCTag = $this->getManager()->getCTag($iUserId, $Storage); |
1908: | if ($oCTag instanceof Models\CTag) { |
1909: | $iResult = $oCTag->CTag; |
1910: | } |
1911: | } |
1912: | |
1913: | return $iResult; |
1914: | } |
1915: | |
1916: | |
1917: | |
1918: | |
1919: | |
1920: | |
1921: | |
1922: | |
1923: | public function SaveContactAsTempFile($UserId, $UUID, $FileName) |
1924: | { |
1925: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
1926: | |
1927: | Api::CheckAccess($UserId); |
1928: | |
1929: | $mResult = false; |
1930: | |
1931: | $oContact = self::Decorator()->GetContact($UUID, $UserId); |
1932: | if ($oContact) { |
1933: | $oVCard = new \Sabre\VObject\Component\VCard(); |
1934: | \Aurora\Modules\Contacts\Classes\VCard\Helper::UpdateVCardFromContact($oContact, $oVCard); |
1935: | $sVCardData = $oVCard->serialize(); |
1936: | if ($sVCardData) { |
1937: | $sUUID = \Aurora\System\Api::getUserUUIDById($UserId); |
1938: | $sTempName = md5($sUUID.$UUID); |
1939: | $oApiFileCache = new \Aurora\System\Managers\Filecache(); |
1940: | |
1941: | $oApiFileCache->put($sUUID, $sTempName, $sVCardData); |
1942: | if ($oApiFileCache->isFileExists($sUUID, $sTempName)) { |
1943: | $mResult = \Aurora\System\Utils::GetClientFileResponse( |
1944: | null, |
1945: | $UserId, |
1946: | $FileName, |
1947: | $sTempName, |
1948: | $oApiFileCache->fileSize($sUUID, $sTempName) |
1949: | ); |
1950: | } |
1951: | } |
1952: | } |
1953: | |
1954: | return $mResult; |
1955: | } |
1956: | |
1957: | |
1958: | |
1959: | private function importVcf($iUserId, $sTempFilePath, $sStorage = null) |
1960: | { |
1961: | $aImportResult = array( |
1962: | 'ParsedCount' => 0, |
1963: | 'ImportedCount' => 0, |
1964: | 'ImportedUids' => [] |
1965: | ); |
1966: | |
1967: | $oHandler = fopen($sTempFilePath, 'r'); |
1968: | $oSplitter = new \Sabre\VObject\Splitter\VCard($oHandler, \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES); |
1969: | $oContactsDecorator = Module::Decorator(); |
1970: | $oApiContactsManager = $oContactsDecorator ? $oContactsDecorator->GetApiContactsManager() : null; |
1971: | if ($oApiContactsManager) { |
1972: | while ($oVCard = $oSplitter->getNext()) { |
1973: | set_time_limit(30); |
1974: | |
1975: | $aContactData = Classes\VCard\Helper::GetContactDataFromVcard($oVCard); |
1976: | $oContact = isset($aContactData['UUID']) ? $oApiContactsManager->getContact($aContactData['UUID']) : null; |
1977: | $aImportResult['ParsedCount']++; |
1978: | if (!isset($oContact) || empty($oContact)) { |
1979: | if (isset($sStorage)) { |
1980: | $aContactData['Storage'] = $sStorage; |
1981: | |
1982: | |
1983: | |
1984: | |
1985: | |
1986: | } |
1987: | $CreatedContactData = $oContactsDecorator->CreateContact($aContactData, $iUserId); |
1988: | if ($CreatedContactData) { |
1989: | $aImportResult['ImportedCount']++; |
1990: | $aImportResult['ImportedUids'][] = $CreatedContactData['UUID']; |
1991: | } |
1992: | } |
1993: | } |
1994: | } |
1995: | return $aImportResult; |
1996: | } |
1997: | |
1998: | private function prepareFiltersFromStorage($UserId, $Storage = '', $SortField = Enums\SortField::Name, $oQuery = null, $bSuggesions = false) |
1999: | { |
2000: | $aArgs = [ |
2001: | 'UserId' => $UserId, |
2002: | 'Storage' => $Storage, |
2003: | 'SortField' => $SortField, |
2004: | 'Suggestions' => $bSuggesions, |
2005: | 'IsValid' => false, |
2006: | ]; |
2007: | |
2008: | $this->broadcastEvent('PrepareFiltersFromStorage', $aArgs, $oQuery); |
2009: | if (!$aArgs['IsValid']) { |
2010: | throw new ApiException(Notifications::InvalidInputParameter, null, 'Invalid Storage parameter value'); |
2011: | } |
2012: | return $oQuery; |
2013: | } |
2014: | |
2015: | public function onAfterUseEmails($Args, &$Result) |
2016: | { |
2017: | $aAddresses = $Args['Emails']; |
2018: | $iUserId = $Args['IdUser']; |
2019: | foreach ($aAddresses as $sEmail => $sName) { |
2020: | $oContact = $this->getManager()->getContactByEmail($iUserId, $sEmail); |
2021: | if ($oContact) { |
2022: | if ($oContact->Frequency !== -1) { |
2023: | $oContact->Frequency = $oContact->Frequency + 1; |
2024: | $this->getManager()->updateContact($oContact); |
2025: | } |
2026: | } else { |
2027: | self::Decorator()->CreateContact([ |
2028: | 'FullName' => $sName, |
2029: | 'PersonalEmail' => $sEmail, |
2030: | 'Auto' => true, |
2031: | ], $iUserId); |
2032: | } |
2033: | $this->getManager()->updateCTag($iUserId, 'collected'); |
2034: | } |
2035: | } |
2036: | |
2037: | public function onGetBodyStructureParts($aParts, &$aResultParts) |
2038: | { |
2039: | foreach ($aParts as $oPart) { |
2040: | if ($oPart instanceof \MailSo\Imap\BodyStructure && |
2041: | ($oPart->ContentType() === 'text/vcard' || $oPart->ContentType() === 'text/x-vcard')) { |
2042: | $aResultParts[] = $oPart; |
2043: | break; |
2044: | } |
2045: | } |
2046: | } |
2047: | |
2048: | public function onAfterDeleteUser(&$aArgs, &$mResult) |
2049: | { |
2050: | $this->getManager()->deleteGroupsByUserId($aArgs['UserId']); |
2051: | $this->getManager()->deleteCTagsByUserId($aArgs['UserId']); |
2052: | $this->getManager()->deleteContactsByUserId($aArgs['UserId']); |
2053: | AddressBook::where('UserId', $aArgs['UserId'])->delete(); |
2054: | } |
2055: | |
2056: | public function onCreateOrUpdateEvent(&$aArgs) |
2057: | { |
2058: | $oEvent = $aArgs['Event']; |
2059: | $aGroups = self::findGroupsHashTagsFromString($oEvent->Name); |
2060: | $aGroupsDescription = self::findGroupsHashTagsFromString($oEvent->Description); |
2061: | $aGroups = array_merge($aGroups, $aGroupsDescription); |
2062: | $aGroupsLocation = self::findGroupsHashTagsFromString($oEvent->Location); |
2063: | $aGroups = array_merge($aGroups, $aGroupsLocation); |
2064: | $oUser = \Aurora\System\Api::getAuthenticatedUser(); |
2065: | |
2066: | if ($oUser instanceof \Aurora\Modules\Core\Models\User) { |
2067: | foreach ($aGroups as $sGroup) { |
2068: | $sGroupName = ltrim($sGroup, '#'); |
2069: | $oGroup = $this->Decorator()->GetGroupByName($sGroupName, $oUser->Id); |
2070: | if (!$oGroup) { |
2071: | $sGroupUUID = $this->Decorator()->CreateGroup(['Name' => $sGroupName], $oUser->Id); |
2072: | if ($sGroupUUID) { |
2073: | $oGroup = $this->GetGroup($oUser->Id, $sGroupUUID); |
2074: | } |
2075: | } |
2076: | |
2077: | if ($oGroup instanceof Models\Group) { |
2078: | $this->removeEventFromGroup($oGroup->UUID, $oEvent->IdCalendar, $oEvent->Id); |
2079: | $this->addEventToGroup($oGroup->UUID, $oEvent->IdCalendar, $oEvent->Id); |
2080: | } |
2081: | } |
2082: | } |
2083: | } |
2084: | |
2085: | |
2086: | |
2087: | |
2088: | |
2089: | |
2090: | protected function _getGroupEvents($sGroupUUID) |
2091: | { |
2092: | $mResult = false; |
2093: | try { |
2094: | $mResult = \Aurora\Modules\Contacts\Models\GroupEvent::where(['GroupUUID' => $sGroupUUID])->get(); |
2095: | } catch (\Aurora\System\Exceptions\BaseException $oException) { |
2096: | $mResult = false; |
2097: | } |
2098: | return $mResult; |
2099: | } |
2100: | |
2101: | |
2102: | |
2103: | |
2104: | |
2105: | |
2106: | |
2107: | protected function getGroupEvent($sCalendarUUID, $sEventUUID) |
2108: | { |
2109: | $mResult = false; |
2110: | try { |
2111: | $mResult = \Aurora\Modules\Contacts\Models\GroupEvent::where('CalendarUUID', $sCalendarUUID) |
2112: | ->where('EventUUID', $sEventUUID)->first(); |
2113: | } catch (\Aurora\System\Exceptions\BaseException $oException) { |
2114: | $mResult = false; |
2115: | } |
2116: | return $mResult; |
2117: | } |
2118: | |
2119: | |
2120: | |
2121: | |
2122: | |
2123: | |
2124: | |
2125: | |
2126: | protected function addEventToGroup($sGroupUUID, $sCalendarUUID, $sEventUUID) |
2127: | { |
2128: | $bResult = false; |
2129: | try { |
2130: | $oGroupEvent = new Models\GroupEvent(); |
2131: | $oGroupEvent->GroupUUID = $sGroupUUID; |
2132: | $oGroupEvent->CalendarUUID = $sCalendarUUID; |
2133: | $oGroupEvent->EventUUID = $sEventUUID; |
2134: | $bResult = $oGroupEvent->save(); |
2135: | } catch (\Aurora\System\Exceptions\BaseException $oException) { |
2136: | $bResult = false; |
2137: | } |
2138: | return $bResult; |
2139: | } |
2140: | |
2141: | |
2142: | |
2143: | |
2144: | |
2145: | |
2146: | |
2147: | |
2148: | protected function removeEventFromGroup($sGroupUUID, $sCalendarUUID, $sEventUUID) |
2149: | { |
2150: | $mResult = false; |
2151: | try { |
2152: | $mResult = \Aurora\Modules\Contacts\Models\GroupEvent::where('GroupUUID', $sGroupUUID) |
2153: | ->where('CalendarUUID', $sCalendarUUID) |
2154: | ->where('EventUUID', $sEventUUID)->first(); |
2155: | |
2156: | if ($mResult instanceof Models\GroupEvent) { |
2157: | $mResult = $mResult->delete(); |
2158: | } |
2159: | } catch (\Aurora\System\Exceptions\BaseException $oException) { |
2160: | $mResult = false; |
2161: | } |
2162: | return $mResult; |
2163: | } |
2164: | |
2165: | |
2166: | |
2167: | |
2168: | |
2169: | |
2170: | protected static function findGroupsHashTagsFromString($sString) |
2171: | { |
2172: | $aResult = array(); |
2173: | |
2174: | preg_match_all("/[#]([^#\s]+)/", $sString, $aMatches); |
2175: | |
2176: | if (\is_array($aMatches) && isset($aMatches[0]) && \is_array($aMatches[0]) && 0 < \count($aMatches[0])) { |
2177: | $aResult = $aMatches[0]; |
2178: | } |
2179: | |
2180: | return $aResult; |
2181: | } |
2182: | |
2183: | |
2184: | |
2185: | |
2186: | |
2187: | |
2188: | |
2189: | public function removeEventFromAllGroups($sCalendarUUID, $sEventUUID) |
2190: | { |
2191: | $mResult = false; |
2192: | try { |
2193: | $mResult = \Aurora\Modules\Contacts\Models\GroupEvent::where('CalendarUUID', $sCalendarUUID) |
2194: | ->where('EventUUID', $sEventUUID)->first(); |
2195: | |
2196: | if (is_array($mResult)) { |
2197: | foreach ($mResult as $oGroupEvent) { |
2198: | if ($mResult instanceof Models\GroupEvent) { |
2199: | $mResult->delete(); |
2200: | } |
2201: | } |
2202: | } |
2203: | $mResult = true; |
2204: | } catch (\Aurora\System\Exceptions\BaseException $oException) { |
2205: | $mResult = false; |
2206: | } |
2207: | return $mResult; |
2208: | } |
2209: | |
2210: | |
2211: | public function GetAddressBook($UserId, $UUID) |
2212: | { |
2213: | Api::CheckAccess($UserId); |
2214: | |
2215: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
2216: | |
2217: | return AddressBook::where('UserId', $UserId) |
2218: | ->where('UUID', $UUID)->first(); |
2219: | } |
2220: | |
2221: | public function GetAddressBooks($UserId = null) |
2222: | { |
2223: | $aResult = []; |
2224: | |
2225: | Api::CheckAccess($UserId); |
2226: | |
2227: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
2228: | |
2229: | $aAddressBooks = AddressBook::where('UserId', $UserId)->get(); |
2230: | |
2231: | foreach ($aAddressBooks as $oAddressBook) { |
2232: | $aResult[] = [ |
2233: | 'Id' => StorageType::AddressBook . '-' . $oAddressBook->Id, |
2234: | 'EntityId' => $oAddressBook->Id, |
2235: | 'CTag' => $this->Decorator()->GetCTag($UserId, StorageType::AddressBook . '-' . $oAddressBook->Id), |
2236: | 'Display' => true, |
2237: | 'Order' => 1, |
2238: | 'DisplayName' => $oAddressBook->Name |
2239: | ]; |
2240: | } |
2241: | |
2242: | |
2243: | |
2244: | |
2245: | |
2246: | |
2247: | |
2248: | |
2249: | return $aResult; |
2250: | } |
2251: | |
2252: | public function CreateAddressBook($AddressBookName, $UserId = null, $UUID = null) |
2253: | { |
2254: | $mResult = false; |
2255: | |
2256: | Api::CheckAccess($UserId); |
2257: | |
2258: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
2259: | |
2260: | $oAddressBook = new AddressBook(); |
2261: | $oAddressBook->UserId = (int) $UserId; |
2262: | $oAddressBook->Name = $AddressBookName; |
2263: | |
2264: | if (isset($UUID)) { |
2265: | $oAddressBook->UUID = $UUID; |
2266: | } else { |
2267: | $oAddressBook->UUID = UUIDUtil::getUUID(); |
2268: | } |
2269: | if ($oAddressBook->save()) { |
2270: | $mResult = $oAddressBook->Id; |
2271: | } |
2272: | |
2273: | return $mResult; |
2274: | } |
2275: | |
2276: | public function UpdateAddressBook($EntityId, $AddressBookName, $UserId = null) |
2277: | { |
2278: | $mResult = false; |
2279: | |
2280: | Api::CheckAccess($UserId); |
2281: | |
2282: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
2283: | |
2284: | $oAddressBook = AddressBook::where('UserId', $UserId) |
2285: | ->where('Id', $EntityId)->first(); |
2286: | |
2287: | if ($oAddressBook) { |
2288: | $oAddressBook->Name = $AddressBookName; |
2289: | |
2290: | $mResult = $oAddressBook->save(); |
2291: | } |
2292: | |
2293: | return $mResult; |
2294: | } |
2295: | |
2296: | public function DeleteAddressBook($EntityId, $UserId = null) |
2297: | { |
2298: | $mResult = false; |
2299: | |
2300: | Api::CheckAccess($UserId); |
2301: | |
2302: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
2303: | |
2304: | $oAddressBook = AddressBook::where('UserId', $UserId) |
2305: | ->where('Id', $EntityId)->first(); |
2306: | |
2307: | if ($oAddressBook) { |
2308: | Contact::where('AddressBookId', $EntityId)->delete(); |
2309: | $mResult = $oAddressBook->delete(); |
2310: | } |
2311: | |
2312: | return $mResult; |
2313: | } |
2314: | |
2315: | public function DeleteUsersAddressBooks($UserId = null) |
2316: | { |
2317: | $mResult = false; |
2318: | |
2319: | Api::CheckAccess($UserId); |
2320: | |
2321: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
2322: | |
2323: | $mResult = AddressBook::where('UserId', $UserId)->delete(); |
2324: | |
2325: | return $mResult; |
2326: | } |
2327: | } |
2328: | |