Issues (946)

engine/classes/ElggAccessCollection.php (1 issue)

1
<?php
2
3
/**
4
 * Access collection class
5
 *
6
 * @package    Elgg.Core
7
 * @subpackage Core
8
 *
9
 * @property-read int    $id         The unique identifier (read-only)
10
 * @property      int    $owner_guid GUID of the owner
11
 * @property      string $name       Name of the collection
12
 */
13
class ElggAccessCollection extends ElggData {
14
15
	/**
16
	 * Create an access collection object
17
	 *
18
	 * @param stdClass $row Database row
19
	 * @throws InvalidArgumentException
20
	 */
21 70
	public function __construct(stdClass $row = null) {
22 70
		$this->initializeAttributes();
23
24 70
		foreach ((array) $row as $key => $value) {
25 70
			$this->attributes[$key] = $value;
26
		}
27 70
	}
28
29
	/**
30
	 * Initialize the attributes array
31
	 *
32
	 * @see ElggData::initializeAttributes()
33
	 * @return void
34
	 */
35 70
	protected function initializeAttributes() {
36 70
		parent::initializeAttributes();
37
38 70
		$this->attributes['id'] = null;
39 70
		$this->attributes['owner_guid'] = null;
40 70
		$this->attributes['name'] = null;
41 70
		$this->attributes['subtype'] = null;
42 70
	}
43
44
	/**
45
	 * Set an attribute
46
	 *
47
	 * @param string $name  Name
48
	 * @param mixed  $value Value
49
	 * @return void
50
	 * @throws RuntimeException
51
	 */
52 1
	public function __set($name, $value) {
53 1
		if (in_array($name, ['id', 'owner_guid', 'subtype'])) {
54
			throw new RuntimeException("$name can not be set at runtime");
55
		}
56 1
		$this->attributes[$name] = $value;
57 1
	}
58
59
	/**
60
	 * Get an attribute
61
	 *
62
	 * @param string $name Name
63
	 * @return mixed
64
	 */
65 70
	public function __get($name) {
66 70
		if (array_key_exists($name, $this->attributes)) {
67 70
			return $this->attributes[$name];
68
		}
69
70
		return null;
71
	}
72
73
	/**
74
	 * Returns owner entity of the collection
75
	 * @return \ElggEntity|false
76
	 */
77 5
	public function getOwnerEntity() {
78 5
		return _elgg_services()->entityTable->get($this->owner_guid);
79
	}
80
81
	/**
82
	 * Get readable access level name for this collection
83
	 * @return string
84
	 */
85
	public function getDisplayName() {
86
87 5
		$filter = function($name = null) {
88 5
			if (!isset($name)) {
89 2
				$name = _elgg_services()->translator->translate('access:limited:label');
90
			}
91
			$params = [
92 5
				'access_collection' => $this,
93
			];
94 5
			return _elgg_services()->hooks->trigger('access_collection:name', $this->getType(), $params, $name);
95 5
		};
96
97 5
		$user = _elgg_services()->session->getLoggedInUser();
98 5
		$owner = $this->getOwnerEntity();
99 5
		if (!$user || !$owner) {
100
			// User is not logged in or does not access to the owner entity:
101
			// return default 'Limited' label
102 1
			return $filter();
103
		}
104
		
105 5
		if ($user->isAdmin() || $owner->guid == $user->guid) {
106 4
			return $filter($this->name);
107
		}
108
109 1
		return $filter();
110
	}
111
112
	/**
113
	 * {@inheritdoc}
114
	 */
115 1
	public function save() {
116 1
		if ($this->id > 0) {
117 1
			return _elgg_services()->accessCollections->rename($this->id, $this->name);
118
		} else {
119
			return _elgg_services()->accessCollections->create($this->name, $this->owner_guid);
0 ignored issues
show
Bug Best Practice introduced by Ismayil Khayredinov
The expression return _elgg_services()-...ame, $this->owner_guid) also could return the type integer which is incompatible with the return type mandated by ElggData::save() of boolean.
Loading history...
120
		}
121
	}
122
123
	/**
124
	 * {@inheritdoc}
125
	 */
126 51
	public function delete() {
127 51
		return _elgg_services()->accessCollections->delete($this->id);
128
	}
129
130
	/**
131
	 * Check if user can this collection
132
	 *
133
	 * @param int $user_guid GUID of the user
134
	 * @return bool
135
	 */
136 1
	public function canEdit($user_guid = null) {
137 1
		return _elgg_services()->accessCollections->canEdit($this->id, $user_guid);
138
	}
139
140
	/**
141
	 * Returns members of the access collection
142
	 *
143
	 * @param array $options ege options
144
	 * @return ElggEntity[]|int|false
145
	 */
146
	public function getMembers(array $options = []) {
147
		return _elgg_services()->accessCollections->getMembers($this->id, $options);
148
	}
149
150
	/**
151
	 * Checks if user is already in access collection
152
	 *
153
	 * @param int $member_guid GUID of the user
154
	 * @return bool
155
	 */
156 2
	public function hasMember($member_guid = 0) {
157 2
		return _elgg_services()->accessCollections->hasUser($member_guid, $this->id);
158
	}
159
160
	/**
161
	 * Adds a new member to access collection
162
	 *
163
	 * @param int $member_guid GUID of the user
164
	 * @return bool
165
	 */
166 13
	public function addMember($member_guid = 0) {
167 13
		return _elgg_services()->accessCollections->addUser($member_guid, $this->id);
168
	}
169
170
	/**
171
	 * Removes a user from access collection
172
	 *
173
	 * @param int $member_guid GUID of the user
174
	 * @return bool
175
	 */
176 5
	public function removeMember($member_guid = 0) {
177 5
		return _elgg_services()->accessCollections->removeUser($member_guid, $this->id);
178
	}
179
180
	/**
181
	 * {@inheritdoc}
182
	 */
183 1
	public function getURL() {
184 1
		$type = $this->getType();
185
		$params = [
186 1
			'access_collection' => $this,
187
		];
188 1
		$url = _elgg_services()->hooks->trigger('access_collection:url', $type, $params);
189 1
		return elgg_normalize_url($url);
190
	}
191
192
	/**
193
	 * {@inheritdoc}
194
	 */
195 4
	public function toObject(array $params = []) {
196 4
		$object = new \Elgg\Export\AccessCollection();
197 4
		$object->type = $this->getType();
198 4
		$object->subtype = $this->getSubtype();
199 4
		$object->id = $this->id;
200 4
		$object->owner_guid = $this->owner_guid;
201 4
		$object->name = $this->name;
202
203 4
		$params['access_collection'] = $this;
204
205 4
		return _elgg_services()->hooks->trigger('to:object', 'access_collection', $params, $object);
206
	}
207
208
	/**
209
	 * {@inheritdoc}
210
	 */
211 1
	public function getSystemLogID() {
212 1
		return $this->id;
213
	}
214
215
	/**
216
	 * {@inheritdoc}
217
	 */
218 1
	public function getObjectFromID($id) {
219 1
		return _elgg_services()->accessCollections->get($id);
220
	}
221
222
	/**
223
	 * {@inheritdoc}
224
	 */
225 9
	public function getType() {
226 9
		return 'access_collection';
227
	}
228
229
	/**
230
	 * {@inheritdoc}
231
	 */
232 8
	public function getSubtype() {
233 8
		if (isset($this->subtype)) {
234 5
			return $this->subtype;
235
		}
236
		
237 3
		return $this->name;
238
	}
239
240
}
241