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

ContactGroup::listRaw()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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