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

ContactGroup::getMembers()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 22
rs 8.8333
c 1
b 0
f 0
cc 7
nc 5
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
25
namespace OCA\Polls\Model;
26
27
use OCP\IL10N;
28
use OCA\Polls\Interfaces\IUserObj;
29
30
class ContactGroup implements \JsonSerializable, IUserObj {
31
	public const TYPE = 'contactGroup';
32
33
	/** @var IL10N */
34
	private $l10n;
0 ignored issues
show
introduced by
The private property $l10n is not used, and could be removed.
Loading history...
35
36
	/** @var string */
37
	private $id;
38
39
	/** @var string */
40
	private $displayName = '';
41
42
	private $group;
0 ignored issues
show
introduced by
The private property $group is not used, and could be removed.
Loading history...
43
44
	/**
45
	 * Group constructor.
46
	 * @param $id
47
	 * @param $displayName
48
	 */
49
	public function __construct(
50
		$id,
51
		$displayName = ''
52
	) {
53
		$this->id = $id;
54
		$this->displayName = $displayName;
55
	}
56
57
	/**
58
	 * getId
59
	 * @NoAdminRequired
60
	 * @return String
61
	 */
62
	public function getId() {
63
		return $this->id;
64
	}
65
66
	/**
67
	 * getType
68
	 * @NoAdminRequired
69
	 * @return String
70
	 */
71
	public function getType() {
72
		return self::TYPE;
73
	}
74
75
	/**
76
	 * getUser
77
	 * @NoAdminRequired
78
	 * @return String
79
	 */
80
	public function getUser() {
81
		return $this->id;
82
	}
83
84
	/**
85
	 * getlanguage
86
	 * @NoAdminRequired
87
	 * @return String
88
	 */
89
	public function getLanguage() {
90
		return '';
91
	}
92
93
	/**
94
	 * getDisplayName
95
	 * @NoAdminRequired
96
	 * @return String
97
	 */
98
	public function getDisplayName() {
99
		return $this->id;
100
	}
101
102
	/**
103
	 * getOrganisation
104
	 * @NoAdminRequired
105
	 * @return String
106
	 */
107
	public function getOrganisation() {
108
		return '';
109
	}
110
111
	/**
112
	 * getEmailAddress
113
	 * @NoAdminRequired
114
	 * @return String
115
	 */
116
	public function getEmailAddress() {
117
		return '';
118
	}
119
120
	/**
121
	 * getDesc
122
	 * @NoAdminRequired
123
	 * @return String
124
	 */
125
	public function getDesc() {
126
		return \OC::$server->getL10N('polls')->t('Contact group');
127
	}
128
129
	/**
130
	 * getIcon
131
	 * @NoAdminRequired
132
	 * @return String
133
	 */
134
	public function getIcon() {
135
		return 'icon-group';
136
	}
137
138
	/**
139
	 * listRaw
140
	 * @NoAdminRequired
141
	 * @param string $query
142
	 * @return Array
143
	 */
144
	public static function listRaw($query = '') {
145
		$contactGroups = [];
146
		if (\OC::$server->getContactsManager()->isEnabled()) {
147
			// find contact, which are member of the requested Group
148
			foreach (\OC::$server->getContactsManager()->search($query, ['CATEGORIES']) as $contact) {
149
				// get all groups from the found contact and explode to array
150
				$temp = explode(',', $contact['CATEGORIES']);
151
				foreach ($temp as $contactGroup) {
152
					if (stripos($contactGroup, $query) === 0) {
153
						$contactGroups[] = $contactGroup;
154
					}
155
				}
156
			}
157
		}
158
		return array_unique($contactGroups);
159
	}
160
161
	/**
162
	 * Get a list of contact groups
163
	 * @NoAdminRequired
164
	 * @param string $query
165
	 * @return Array
166
	 */
167
	public static function search($query = '') {
168
		if (\OC::$server->getContactsManager()->isEnabled() && $query) {
169
			$contactGroups = [];
170
			foreach (self::listRaw($query) as $contactGroup) {
171
				$contactGroups[] = new self($contactGroup);
172
			}
173
		}
174
		return $contactGroups;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $contactGroups does not seem to be defined for all execution paths leading up to this point.
Loading history...
175
	}
176
177
	/**
178
	 * Get a list of contacts group members
179
	 * @NoAdminRequired
180
	 * @param string $query
181
	 * @return Contact[]
182
	 */
183
	public function getMembers() {
184
		if (\OC::$server->getContactsManager()->isEnabled()) {
185
			$contacts = [];
186
			foreach (\OC::$server->getContactsManager()->search($this->id, ['CATEGORIES']) as $contact) {
187
				if (!array_key_exists('isLocalSystemBook', $contact)
188
					&& array_key_exists('EMAIL', $contact)
189
					&& in_array($query, explode(',', $contact['CATEGORIES']))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $query seems to be never defined.
Loading history...
190
				) {
191
					$emailAdresses = $contact['EMAIL'];
192
193
					if (!is_array($emailAdresses)) {
194
						$emailAdress = $emailAdresses;
0 ignored issues
show
Unused Code introduced by
The assignment to $emailAdress is dead and can be removed.
Loading history...
195
					} else {
196
						// take the first eMail address for now
197
						$emailAdress = $emailAdresses[0];
198
					}
199
					$contacts[] = new Contact($contact['UID']);
200
				}
201
			}
202
			return $contacts;
203
		}
204
		return [];
205
	}
206
207
208
	/**
209
	 * @return array
210
	 */
211
	public function jsonSerialize(): array {
212
		return	[
213
			'id'        	=> $this->id,
214
			'user'          => $this->id,
215
			'type'       	=> $this->getType(),
216
			'displayName'	=> $this->getDisplayName(),
217
			'organisation'	=> $this->getOrganisation(),
218
			'emailAddress'	=> $this->getEmailAddress(),
219
			'desc' 			=> $this->getDesc(),
220
			'icon'			=> $this->getIcon(),
221
			'isNoUser'		=> true,
222
			'isGuest'		=> true,
223
		];
224
	}
225
}
226