Completed
Pull Request — master (#1128)
by René
05:00
created

Contact::isEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 1
c 3
b 0
f 0
dl 0
loc 2
rs 10
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 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
	 * isEnabled
51
	 * @NoAdminRequired
52
	 * @return Boolean
53
	 */
54
	public static function isEnabled() {
55
		return \OC::$server->getAppManager()->isEnabledForUser('contacts');
56
	}
57
58
	/**
59
	 * resolveContact
60
	 * We just need the contact's UID, so make sure, the any prefix is removed
61
	 * @NoAdminRequired
62
	 */
63
	private function resolveContactId() {
64
		$parts = explode(":", $this->id);
65
		$this->id = end($parts);
66
	}
67
68
	/**
69
	 * loadContact
70
	 * @NoAdminRequired
71
	 * The contacts app just provides a search, so we have to load the contact
72
	 * after searching via the contact's id and use the first contact.
73
	 *
74
	 * Currently only the contact's first email address is supported
75
	 *
76
	 * From Version 1.5 on:
77
	 * For compatibility reasons, we have to search for the contacts name too.
78
	 * Before this implementation contacts where stored with their FN property.
79
	 *
80
	 * TODO: Remove FN as search range for loading a contact in a polls version
81
	 * later than 1.6.
82
	 */
83
	private function loadContact() {
84
		$contacts = self::listRaw($this->id, ['UID', 'FN']);
85
86
		if (count($contacts) > 1) {
87
			throw new MultipleContactsFound('Multiple contacts found for id ' . $this->id);
88
		}
89
90
		$this->contact = $contacts[0];
91
	}
92
93
	private function getContact() {
94
		if (\OC::$server->getAppManager()->isEnabledForUser('contacts')) {
95
			$this->resolveContactId();
96
			$this->loadContact();
97
98
			$this->id = $this->contact['UID'];
99
			$this->displayName = isset($this->contact['FN']) ? $this->contact['FN'] : $this->displayName;
100
			$this->emailAddress = isset($this->contact['EMAIL'][0]) ? $this->contact['EMAIL'][0] : $this->emailAddress;
101
			$this->organisation = isset($this->contact['ORG']) ? $this->contact['ORG'] : '';
102
			$this->categories = isset($this->contact['CATEGORIES']) ? explode(',', $this->contact['CATEGORIES']) : [];
103
104
105
			if (isset($this->contact['CATEGORIES'])) {
106
				$this->categories = explode(',', $this->contact['CATEGORIES']);
107
			} else {
108
				$this->categories = [];
109
			}
110
111
			$description = $this->categories;
112
113
			if (isset($this->contact['ORG'])) {
114
				array_unshift($description, $this->organisation);
115
			}
116
117
			if (count($description) > 0) {
118
				$this->description = implode(", ", $description);
119
			} else {
120
				$this->description = \OC::$server->getL10N('polls')->t('Contact');
121
			}
122
		} else {
123
			throw new ContactsNotEnabled();
124
		}
125
	}
126
127
	/**
128
	 * listRaw
129
	 * @NoAdminRequired
130
	 * List all contacts with email adresses
131
	 * excluding contacts from localSystemBook
132
	 * @param string $query
133
	 * @return Array
134
	 */
135
	public static function listRaw($query = '', $queryRange = ['FN', 'EMAIL', 'ORG', 'CATEGORIES']) {
136
		$contacts = [];
137
		if (\OC::$server->getAppManager()->isEnabledForUser('contacts')) {
138
			foreach (\OC::$server->getContactsManager()->search($query, $queryRange) as $contact) {
139
				if (!array_key_exists('isLocalSystemBook', $contact) && array_key_exists('EMAIL', $contact)) {
140
					$contacts[] = $contact;
141
				}
142
			}
143
		}
144
		return $contacts;
145
	}
146
147
	/**
148
	 * list
149
	 * @NoAdminRequired
150
	 * @param string $query
151
	 * @param array $queryRange
152
	 * @return self[]
153
	 */
154
	public static function search($query = '', $queryRange = ['FN', 'EMAIL', 'ORG', 'CATEGORIES']) {
155
		$contacts = [];
156
		foreach (self::listRaw($query, $queryRange) as $contact) {
157
			$contacts[] = new Self($contact['UID']);
158
		}
159
		return $contacts;
160
	}
161
}
162