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

ContactsService::getContactsGroupMembers()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 15
dl 0
loc 26
rs 8.0555
c 2
b 0
f 2
cc 9
nc 7
nop 1
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\Service;
25
26
use OCA\Polls\Model\User;
27
28
class ContactsService {
29
	public function __construct(
30
	) {
31
		$this->enabled = \OC::$server->getContactsManager()->isEnabled();
0 ignored issues
show
Bug Best Practice introduced by
The property enabled does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32
	}
33
34
	/**
35
	 * Get a list of contacts
36
	 * @NoAdminRequired
37
	 * @param string $query
38
	 * @return User[]
39
	 */
40
	public function getContacts($query = '', $queryRange = ['FN', 'EMAIL', 'ORG', 'CATEGORIES']) {
41
		if (!$this->enabled || $query === '') {
42
			return [];
43
		}
44
45
		$contacts = [];
46
47
		foreach (\OC::$server->getContactsManager()->search($query, $queryRange) as $contact) {
48
			if (!array_key_exists('isLocalSystemBook', $contact) && array_key_exists('EMAIL', $contact)) {
49
				$contacts[] = new User(User::TYPE_CONTACT, $contact['UID']);
50
			}
51
		}
52
		return $contacts;
53
	}
54
55
	/**
56
	 * Get a list of contacts
57
	 * @NoAdminRequired
58
	 * @param string $query
59
	 * @return User[]
60
	 */
61
	public function getContactsGroupMembers($query = '') {
62
		if (!$this->enabled || $query === '') {
63
			return [];
64
		}
65
66
		$contacts = [];
67
		foreach (\OC::$server->getContactsManager()->search($query, ['CATEGORIES']) as $contact) {
68
			if (!array_key_exists('isLocalSystemBook', $contact)
69
				&& array_key_exists('EMAIL', $contact)
70
				&& in_array($query, explode(',', $contact['CATEGORIES']))
71
			) {
72
				$emailAdresses = $contact['EMAIL'];
73
74
				if (!is_array($emailAdresses)) {
75
					$emailAdresses = [$emailAdresses];
76
				} else {
77
					// take the first eMail address for now
78
					$emailAdresses = [$emailAdresses[0]];
79
				}
80
81
				foreach ($emailAdresses as $emailAddress) {
82
					$contacts[] = new User(User::TYPE_CONTACT, $contact['UID']);
83
				}
84
			}
85
		}
86
		return $contacts;
87
	}
88
89
	/**
90
	 * Get a list of contact groups
91
	 * @NoAdminRequired
92
	 * @param string $query
93
	 * @return Array
94
	 */
95
	public function getContactsGroups($query = '') {
96
		if (!$this->enabled || $query === '') {
97
			return [];
98
		}
99
100
		$contactGroups = [];
101
		$foundContacts = [];
102
		foreach (\OC::$server->getContactsManager()->search($query, ['CATEGORIES']) as $contact) {
103
			foreach (explode(',', $contact['CATEGORIES']) as $contactGroup) {
104
				if (strpos($contactGroup, $query) === 0 && !in_array($contactGroup, $foundContacts)) {
105
					$foundContacts[] = $contactGroup;
106
					$contactGroups[] = new User(User::TYPE_CONTACTGROUP, $contactGroup);
107
				};
108
			}
109
		}
110
		return $contactGroups;
111
	}
112
}
113