Passed
Pull Request — master (#1128)
by René
06:43
created

Contact::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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