Passed
Pull Request — master (#1216)
by René
04:01
created

Contact::search()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 4
c 3
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Polls\Model;
25
26
use OCA\Polls\Exceptions\MultipleContactsFound;
27
use OCA\Polls\Exceptions\ContactsNotEnabled;
28
29
class Contact extends UserGroupClass {
30
	public const TYPE = 'contact';
31
	public const ICON = 'icon-mail';
32
33
	/** @var Array */
34
	private $contact = [];
35
36
	/**
37
	 * Contact constructor.
38
	 * @param $id
39
	 * @param $displayName
40
	 */
41
	public function __construct(
42
		$id
43
	) {
44
		parent::__construct($id, self::TYPE);
45
		$this->icon = self::ICON;
46
		$this->getContact();
47
	}
48
49
	/**
50
	 * getPublicId
51
	 * must use displayName for contact's user id, because contact id
52
	 * is not accessable outside the owners's scope
53
	 * @NoAdminRequired
54
	 * @return String
55
	 */
56
	public function getPublicId() {
57
		return $this->displayName;
58
	}
59
60
	/**
61
	 * isEnabled
62
	 * @NoAdminRequired
63
	 * @return Boolean
64
	 */
65
	public static function isEnabled() {
66
		return \OC::$server->getAppManager()->isEnabledForUser('contacts');
67
	}
68
69
	/**
70
	 * resolveContact
71
	 * We just need the contact's UID, so make sure, the any prefix is removed
72
	 * @NoAdminRequired
73
	 */
74
	private function resolveContactId() {
75
		$parts = explode(":", $this->id);
76
		$this->id = end($parts);
77
	}
78
79
	/**
80
	 * loadContact
81
	 * @NoAdminRequired
82
	 * The contacts app just provides a search, so we have to load the contact
83
	 * after searching via the contact's id and use the first contact.
84
	 *
85
	 * Currently only the contact's first email address is supported
86
	 *
87
	 * From Version 1.5 on:
88
	 * For compatibility reasons, we have to search for the contacts name too.
89
	 * Before this implementation contacts where stored with their FN property.
90
	 *
91
	 * TODO: Remove FN as search range for loading a contact in a polls version
92
	 * later than 1.6.
93
	 */
94
	private function loadContact() {
95
		$contacts = self::listRaw($this->id, ['UID', 'FN']);
96
97
		if (count($contacts) > 1) {
98
			throw new MultipleContactsFound('Multiple contacts found for id ' . $this->id);
99
		}
100
101
		$this->contact = $contacts[0];
102
	}
103
104
	private function getContact() {
105
		if (\OC::$server->getAppManager()->isEnabledForUser('contacts')) {
106
			$this->resolveContactId();
107
			$this->loadContact();
108
109
			$this->id = $this->contact['UID'];
110
			$this->displayName = isset($this->contact['FN']) ? $this->contact['FN'] : $this->displayName;
111
			$this->emailAddress = isset($this->contact['EMAIL'][0]) ? $this->contact['EMAIL'][0] : $this->emailAddress;
112
			$this->organisation = isset($this->contact['ORG']) ? $this->contact['ORG'] : '';
113
			$this->categories = isset($this->contact['CATEGORIES']) ? explode(',', $this->contact['CATEGORIES']) : [];
114
115
116
			if (isset($this->contact['CATEGORIES'])) {
117
				$this->categories = explode(',', $this->contact['CATEGORIES']);
118
			} else {
119
				$this->categories = [];
120
			}
121
122
			$description = $this->categories;
123
124
			if (isset($this->contact['ORG'])) {
125
				array_unshift($description, $this->organisation);
126
			}
127
128
			if (count($description) > 0) {
129
				$this->description = implode(", ", $description);
130
			} else {
131
				$this->description = \OC::$server->getL10N('polls')->t('Contact');
132
			}
133
		} else {
134
			throw new ContactsNotEnabled();
135
		}
136
	}
137
138
	/**
139
	 * listRaw
140
	 * @NoAdminRequired
141
	 * List all contacts with email adresses
142
	 * excluding contacts from localSystemBook
143
	 * @param string $query
144
	 * @return Array
145
	 */
146
	public static function listRaw($query = '', $queryRange = ['FN', 'EMAIL', 'ORG', 'CATEGORIES']) {
147
		$contacts = [];
148
		if (\OC::$server->getAppManager()->isEnabledForUser('contacts')) {
149
			foreach (\OC::$server->getContactsManager()->search($query, $queryRange) as $contact) {
150
				if (!array_key_exists('isLocalSystemBook', $contact) && array_key_exists('EMAIL', $contact)) {
151
					$contacts[] = $contact;
152
				}
153
			}
154
		}
155
		return $contacts;
156
	}
157
158
	/**
159
	 * list
160
	 * @NoAdminRequired
161
	 * @param string $query
162
	 * @param array $queryRange
163
	 * @return self[]
164
	 */
165
	public static function search($query = '', $queryRange = ['FN', 'EMAIL', 'ORG', 'CATEGORIES']) {
166
		$contacts = [];
167
		foreach (self::listRaw($query, $queryRange) as $contact) {
168
			$contacts[] = new Self($contact['UID']);
169
		}
170
		return $contacts;
171
	}
172
}
173