Passed
Pull Request — master (#1128)
by René
04:45
created

Contact::getCategories()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 5
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 0
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 OCP\IL10N;
27
use OCA\Polls\Exceptions\MultipleContactsFound;
28
use OCA\Polls\Exceptions\ContactsNotEnabled;
29
use OCA\Polls\Interfaces\IUserObj;
30
31
class Contact implements \JsonSerializable, IUserObj {
32
	public const TYPE = 'contact';
33
34
	/** @var IL10N */
35
	private $l10n;
36
37
	/** @var string */
38
	private $id;
39
40
	/** @var string */
41
	private $type;
0 ignored issues
show
introduced by
The private property $type is not used, and could be removed.
Loading history...
42
43
	/** @var string */
44
	private $displayName = '';
45
46
	/** @var string */
47
	private $desc = '';
0 ignored issues
show
introduced by
The private property $desc is not used, and could be removed.
Loading history...
48
49
	/** @var array */
50
	private $contact;
51
52
	/**
53
	 * User constructor.
54
	 * @param $type
55
	 * @param $id
56
	 * @param $displayName
57
	 */
58
	public function __construct(
59
		$id,
60
		$displayName = ''
61
	) {
62
		$this->id = $id;
63
		$this->displayName = $displayName;
64
65
		$this->l10n = \OC::$server->getL10N('polls');
66
		$this->load();
67
	}
68
69
	/**
70
	 * Get id
71
	 * @NoAdminRequired
72
	 * @return String
73
	 */
74
	public function getId() {
75
		return $this->id;
76
	}
77
78
	/**
79
	 * getUser
80
	 * @NoAdminRequired
81
	 * @return String
82
	 */
83
	public function getUser() {
84
		return $this->id;
85
	}
86
87
	/**
88
	 * Get user type
89
	 * @NoAdminRequired
90
	 * @return String
91
	 */
92
	public function getType() {
93
		return self::TYPE;
94
	}
95
96
	/**
97
	 * Get language of user, if type = TYPE_USER
98
	 * @NoAdminRequired
99
	 * @return String
100
	 */
101
	public function getLanguage() {
102
		return '';
103
	}
104
105
	/**
106
	 * Get displayName
107
	 * @NoAdminRequired
108
	 * @return String
109
	 */
110
	public function getDisplayName() {
111
		return isset($this->contact['FN']) ? $this->contact['FN'] : $this->displayName;
112
	}
113
114
	/**
115
	 * Get additional description, if available
116
	 * @NoAdminRequired
117
	 * @return String
118
	 */
119
	public function getDesc() {
120
		$desc = $this->getCategories();
121
		if (isset($this->contact['ORG'])) {
122
			array_unshift($desc, $this->getOrganisation());
123
		}
124
		if (count($desc) > 0) {
125
			return implode(", ", $desc);
126
		} else {
127
			return \OC::$server->getL10N('polls')->t('Contact');
128
		}
129
	}
130
131
	/**
132
	 * Get email address
133
	 * @NoAdminRequired
134
	 * @return String
135
	 */
136
	public function getEmailAddress() {
137
		return isset($this->contact['EMAIL'][0]) ? $this->contact['EMAIL'][0] : '';
138
	}
139
140
	/**
141
	 * Get organisation, if type = TYPE_CONTACT
142
	 * @NoAdminRequired
143
	 * @return String
144
	 */
145
	public function getOrganisation() {
146
		return isset($this->contact['ORG']) ? $this->contact['ORG'] : '';
147
	}
148
149
	/**
150
	 * getCategories
151
	 * @NoAdminRequired
152
	 * @return Array
153
	 */
154
	public function getCategories() {
155
		if (isset($this->contact['CATEGORIES'])) {
156
			return explode(',', $this->contact['CATEGORIES']);
157
		} else {
158
			return [];
159
		}
160
	}
161
162
	/**
163
	 * Get icon class
164
	 * @NoAdminRequired
165
	 * @return String
166
	 */
167
	public function getIcon() {
168
		return 'icon-mail';
169
	}
170
171
	/**
172
	 * Load contact, if type = TYPE_CONTACT
173
	 * @NoAdminRequired
174
	 * @return String
175
	 * @throws MultipleContactsFound
176
	 * @throws ContactsNotEnabled
177
	 */
178
	private function load() {
179
		$this->contact = [];
180
		$parts = explode(":", $this->id);
181
		$this->id = end($parts);
182
		if (\OC::$server->getAppManager()->isEnabledForUser('contacts')) {
183
			// Search for UID and FN
184
			// Before this implementation contacts where stored with their FN property
185
			// From now on, the contact's UID is used as identifier
186
			// TODO: Remove FN as search range for loading a contact in a polls version later than 1.6
187
			$contacts = \OC::$server->getContactsManager()->search($this->id, ['UID', 'FN']);
188
189
			if (count($contacts) === 1) {
190
				$this->contact = $contacts[0];
191
				$this->id = $this->contact['UID'];
192
			} elseif (count($contacts) > 1) {
193
				throw new MultipleContactsFound('Multiple contacts found for id '. $this->id);
194
			}
195
		} else {
196
			throw new ContactsNotEnabled();
197
		}
198
	}
199
200
	/**
201
	 * isEnabled
202
	 * @NoAdminRequired
203
	 * @return Boolean
204
	 */
205
	public static function isEnabled() {
206
		return \OC::$server->getAppManager()->isEnabledForUser('contacts');
207
	}
208
209
	/**
210
	 * listRaw
211
	 * @NoAdminRequired
212
	 * @param string $query
213
	 * @return Array
214
	 */
215
	public static function listRaw($query = '', $queryRange = ['FN', 'EMAIL', 'ORG', 'CATEGORIES']) {
216
		$contacts = [];
217
		if (\OC::$server->getAppManager()->isEnabledForUser('contacts')) {
218
			foreach (\OC::$server->getContactsManager()->search($query, $queryRange) as $contact) {
219
				if (!array_key_exists('isLocalSystemBook', $contact) && array_key_exists('EMAIL', $contact)) {
220
					$contacts[] = $contact;
221
				}
222
			}
223
		}
224
		return $contacts;
225
	}
226
227
	/**
228
	 * list
229
	 * @NoAdminRequired
230
	 * @param string $query
231
	 * @param array $queryRange
232
	 * @return self[]
233
	 */
234
	public static function search($query = '', $queryRange = ['FN', 'EMAIL', 'ORG', 'CATEGORIES']) {
235
		$contacts = [];
236
		foreach (self::listRaw($query, $queryRange) as $contact) {
237
			$contacts[] = new Self($contact['UID']);
238
		}
239
		return $contacts;
240
	}
241
242
243
	/**
244
	 * @return array
245
	 */
246
	public function jsonSerialize(): array {
247
		return	[
248
			'id'        	=> $this->getId(),
249
			'user'          => $this->id,
250
			'type'       	=> $this->getType(),
251
			'displayName'	=> $this->getDisplayName(),
252
			'organisation'	=> $this->getOrganisation(),
253
			'emailAddress'	=> $this->getEmailAddress(),
254
			'desc' 			=> $this->getDesc(),
255
			'icon'			=> $this->getIcon(),
256
			'categories'	=> $this->getCategories(),
257
			'isNoUser'		=> true,
258
			'isGuest'		=> true,
259
		];
260
	}
261
}
262