Passed
Pull Request — master (#1128)
by René
08:09 queued 03:52
created

ContactGroup::getMembers()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 4
nc 4
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
class ContactGroup extends UserGroupClass {
28
	public const TYPE = 'contactGroup';
29
	public const ICON = 'icon-group';
30
31
	/**
32
	 * Group constructor.
33
	 * @param $id
34
	 * @param $displayName
35
	 */
36
	public function __construct(
37
		$id,
38
		$displayName = ''
39
	) {
40
		parent::__construct($id, self::TYPE, $id);
41
		$this->icon = self::ICON;
42
		$this->description = \OC::$server->getL10N('polls')->t('Contact group');
43
	}
44
45
	/**
46
	 * listRaw
47
	 * @NoAdminRequired
48
	 * @param string $query
49
	 * @return Array
50
	 */
51
	public static function listRaw($query = '') {
52
		$contactGroups = [];
53
		if (\OC::$server->getContactsManager()->isEnabled()) {
54
			// find contact, which are member of the requested Group
55
			foreach (\OC::$server->getContactsManager()->search($query, ['CATEGORIES']) as $contact) {
56
				// get all groups from the found contact and explode to array
57
				$temp = explode(',', $contact['CATEGORIES']);
58
				foreach ($temp as $contactGroup) {
59
					if (stripos($contactGroup, $query) === 0) {
60
						$contactGroups[] = $contactGroup;
61
					}
62
				}
63
			}
64
		}
65
		return array_unique($contactGroups);
66
	}
67
68
	/**
69
	 * Get a list of contact groups
70
	 * @NoAdminRequired
71
	 * @param string $query
72
	 * @return Array
73
	 */
74
	public static function search($query = '') {
75
		$contactGroups = [];
76
		if (\OC::$server->getContactsManager()->isEnabled() && $query) {
77
			foreach (self::listRaw($query) as $contactGroup) {
78
				$contactGroups[] = new self($contactGroup);
79
			}
80
		}
81
		return $contactGroups;
82
	}
83
84
	/**
85
	 * Get a list of contacts group members
86
	 * @NoAdminRequired
87
	 * @return Contact[]
88
	 */
89
	public function getMembers() {
90
		if (\OC::$server->getContactsManager()->isEnabled()) {
91
			$contacts = [];
92
			foreach (\OC::$server->getContactsManager()->search($this->id, ['CATEGORIES']) as $contact) {
93
				if (array_key_exists('EMAIL', $contact)) {
94
					$contacts[] = new Contact($contact['UID']);
95
				}
96
			}
97
			return $contacts;
98
		}
99
		return [];
100
	}
101
102
103
	/**
104
	 * @return array
105
	 */
106
	public function jsonSerialize(): array {
107
		return	[
108
			'id'        	=> $this->id,
109
			'user'          => $this->id,
110
			'type'       	=> $this->getType(),
111
			'displayName'	=> $this->getDisplayName(),
112
			'organisation'	=> $this->getOrganisation(),
113
			'emailAddress'	=> $this->getEmailAddress(),
114
			'desc' 			=> $this->getDescription(),
115
			'icon'			=> $this->getIcon(),
116
			'isNoUser'		=> true,
117
			'isGuest'		=> true,
118
		];
119
	}
120
}
121