From 276903b5902471099244ae600ae0e51e115189de Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sun, 11 Oct 2020 19:44:20 +0200 Subject: refactoring invitations --- lib/Model/Contact.php | 92 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 32 deletions(-) (limited to 'lib/Model/Contact.php') diff --git a/lib/Model/Contact.php b/lib/Model/Contact.php index 478515f2..3d0187f9 100644 --- a/lib/Model/Contact.php +++ b/lib/Model/Contact.php @@ -31,42 +31,76 @@ class Contact extends UserGroupClass { public const ICON = 'icon-mail'; /** @var Array */ - private $contact; + private $contact =[]; /** - * User constructor. + * Contact constructor. * @param $id * @param $displayName */ public function __construct( - $id, - $displayName = '' + $id ) { - parent::__construct($id, self::TYPE, $displayName); + parent::__construct($id, self::TYPE); $this->icon = self::ICON; + $this->getContact(); + } + /** + * isEnabled + * @NoAdminRequired + * @return Boolean + */ + public static function isEnabled() { + return \OC::$server->getAppManager()->isEnabledForUser('contacts'); + } + + /** + * resolveContact + * We just need the contact's UID, so make sure, the any prefix is removed + * @NoAdminRequired + */ + private function resolveContactId() { + $parts = explode(":", $this->id); + $this->id = end($parts); + } + + /** + * loadContact + * @NoAdminRequired + * The contacts app just provides a search, so we have to load the contact + * after searching via the contact's id and use the first contact. + * + * Currently only the contact's first email address is supported + * + * From Version 1.5 on: + * For compatibility reasons, we have to search for the contacts name too. + * Before this implementation contacts where stored with their FN property. + * + * TODO: Remove FN as search range for loading a contact in a polls version + * later than 1.6. + */ + private function loadContact() { + $contacts = self::listRaw($this->id, ['UID', 'FN']); + + if (count($contacts) > 1) { + throw new MultipleContactsFound('Multiple contacts found for id ' . $this->id); + } + + $this->contact = $contacts[0]; + } + + private function getContact() { if (\OC::$server->getAppManager()->isEnabledForUser('contacts')) { - $this->contact = []; - - $parts = explode(":", $this->id); - $this->id = end($parts); - - // Search for UID and FN - // Before this implementation contacts where stored with their FN property - // From now on, the contact's UID is used as identifier - // TODO: Remove FN as search range for loading a contact in a polls version later than 1.6 - $contacts = \OC::$server->getContactsManager()->search($this->id, ['UID', 'FN']); - - if (count($contacts) === 1) { - $this->contact = $contacts[0]; - $this->id = $this->contact['UID']; - $this->displayName = isset($this->contact['FN']) ? $this->contact['FN'] : $this->displayName; - $this->emailAddress = isset($this->contact['EMAIL'][0]) ? $this->contact['EMAIL'][0] : $this->emailAddress; - } elseif (count($contacts) > 1) { - throw new MultipleContactsFound('Multiple contacts found for id ' . $this->id); - } + $this->resolveContactId(); + $this->loadContact(); + $this->id = $this->contact['UID']; + $this->displayName = isset($this->contact['FN']) ? $this->contact['FN'] : $this->displayName; + $this->emailAddress = isset($this->contact['EMAIL'][0]) ? $this->contact['EMAIL'][0] : $this->emailAddress; $this->organisation = isset($this->contact['ORG']) ? $this->contact['ORG'] : ''; + $this->categories = isset($this->contact['CATEGORIES']) ? explode(',', $this->contact['CATEGORIES']) : []; + if (isset($this->contact['CATEGORIES'])) { $this->categories = explode(',', $this->contact['CATEGORIES']); @@ -88,20 +122,14 @@ class Contact extends UserGroupClass { } else { throw new ContactsNotEnabled(); } - } - /** - * isEnabled - * @NoAdminRequired - * @return Boolean - */ - public static function isEnabled() { - return \OC::$server->getAppManager()->isEnabledForUser('contacts'); } /** * listRaw * @NoAdminRequired + * List all contacts with email adresses + * excluding contacts from localSystemBook * @param string $query * @return Array */ -- cgit v1.2.3