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

ContactsService::getContactsGroups()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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