Elgg  Version 4.3
Subscriptions.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Traits\Entity;
4 
6 
12 trait Subscriptions {
13 
26  public function addSubscription(int $user_guid = 0, $methods = [], string $type = null, string $subtype = null, string $action = null): bool {
27  if ($user_guid === 0) {
28  $user_guid = _elgg_services()->session->getLoggedInUserGuid();
29  }
30 
31  if (!empty($user_guid) && is_array($methods) && empty($methods)) {
33  if ($user instanceof \ElggUser) {
34  $prefered = $user->getNotificationSettings();
35  $methods = array_keys(array_filter($prefered));
36 
37  if (empty($methods)) {
38  return true;
39  }
40  }
41  }
42 
43  $methods = $this->normalizeSubscriptionMethods($methods);
44 
45  $result = true;
46  foreach ($methods as $method) {
47  $result &= _elgg_services()->subscriptions->addSubscription($user_guid, $method, $this->guid, $type, $subtype, $action);
48  }
49 
50  return $result;
51  }
52 
65  public function hasSubscription(int $user_guid = 0, $methods = [], string $type = null, string $subtype = null, string $action = null): bool {
66  if ($user_guid === 0) {
67  $user_guid = _elgg_services()->session->getLoggedInUserGuid();
68  }
69 
70  $methods = $this->normalizeSubscriptionMethods($methods);
71 
72  $result = true;
73  foreach ($methods as $method) {
74  $result &= _elgg_services()->subscriptions->hasSubscription($user_guid, $method, $this->guid, $type, $subtype, $action);
75  }
76 
77  return $result;
78  }
79 
89  public function hasSubscriptions(int $user_guid = 0, $methods = []): bool {
90  if ($user_guid === 0) {
91  $user_guid = _elgg_services()->session->getLoggedInUserGuid();
92  }
93 
94  $methods = $this->normalizeSubscriptionMethods($methods);
95 
96  return _elgg_services()->subscriptions->hasSubscriptions($user_guid, $this->guid, $methods);
97  }
98 
111  public function removeSubscription(int $user_guid = 0, $methods = [], string $type = null, string $subtype = null, string $action = null): bool {
112  if ($user_guid === 0) {
113  $user_guid = _elgg_services()->session->getLoggedInUserGuid();
114  }
115 
116  $methods = $this->normalizeSubscriptionMethods($methods);
117 
118  $result = true;
119  foreach ($methods as $method) {
120  $result &= _elgg_services()->subscriptions->removeSubscription($user_guid, $method, $this->guid, $type, $subtype, $action);
121  }
122 
123  return $result;
124  }
125 
134  public function removeSubscriptions(int $user_guid = 0, $methods = []): bool {
135  if ($user_guid === 0) {
136  $user_guid = _elgg_services()->session->getLoggedInUserGuid();
137  }
138 
139  $methods = (array) $methods;
140 
141  return _elgg_services()->subscriptions->removeSubscriptions($user_guid, $this->guid, $methods);
142  }
143 
155  public function getSubscriptions(int $user_guid = 0, $methods = [], string $type = null, string $subtype = null, string $action = null): array {
156  if ($user_guid === 0) {
157  $user_guid = _elgg_services()->session->getLoggedInUserGuid();
158  }
159 
160  $methods = (array) $methods;
161 
162  return _elgg_services()->subscriptions->getEntitySubscriptions($this->guid, $user_guid, $methods, $type, $subtype, $action);
163  }
164 
172  public function getSubscribers($methods = []): array {
173  $methods = (array) $methods;
174 
175  return _elgg_services()->subscriptions->getSubscribers($this->guid, $methods);
176  }
177 
185  public function muteNotifications(int $user_guid = 0): bool {
186  if ($user_guid === 0) {
187  $user_guid = _elgg_services()->session->getLoggedInUserGuid();
188  }
189 
190  return _elgg_services()->subscriptions->muteNotifications($user_guid, $this->guid);
191  }
192 
200  public function hasMutedNotifications(int $user_guid = 0): bool {
201  if ($user_guid === 0) {
202  $user_guid = _elgg_services()->session->getLoggedInUserGuid();
203  }
204 
205  return _elgg_services()->subscriptions->hasMutedNotifications($user_guid, $this->guid);
206  }
207 
215  public function unmuteNotifications(int $user_guid = 0): bool {
216  if ($user_guid === 0) {
217  $user_guid = _elgg_services()->session->getLoggedInUserGuid();
218  }
219 
220  return _elgg_services()->subscriptions->unmuteNotifications($user_guid, $this->guid);
221  }
222 
231  protected function normalizeSubscriptionMethods($methods = []): array {
232  if (!is_string($methods) && !is_array($methods)) {
233  $dbt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
234  $caller = $dbt[1]['function'] ?? 'unknown';
235 
236  throw new InvalidArgumentException(elgg_echo('Entity:Subscriptions:InvalidMethodsException', [$caller]));
237  }
238 
239  $methods = (array) $methods;
240 
241  foreach ($methods as $method) {
242  if (!is_string($method) || $method === '') {
243  $dbt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
244  $caller = $dbt[1]['function'] ?? 'unknown';
245 
246  throw new InvalidArgumentException(elgg_echo('Entity:Subscriptions:InvalidMethodsException', [$caller]));
247  }
248  }
249 
250  return $methods ?: _elgg_services()->notifications->getMethods();
251  }
252 }
$user_guid
Definition: login_as.php:10
get_user($guid)
Get a user object from a GUID.
Definition: users.php:20
$type
Definition: delete.php:21
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
$user
Definition: ban.php:7
$action
Definition: subscribe.php:11
$subtype
Definition: delete.php:22
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
$methods
Definition: subscribe.php:8