Elgg  Version 6.1
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, string|array $methods = [], string $type = null, string $subtype = null, string $action = null): bool {
27  if ($user_guid === 0) {
28  $user_guid = _elgg_services()->session_manager->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, string|array $methods = [], string $type = null, string $subtype = null, string $action = null): bool {
66  if ($user_guid === 0) {
67  $user_guid = _elgg_services()->session_manager->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, string|array $methods = []): bool {
90  if ($user_guid === 0) {
91  $user_guid = _elgg_services()->session_manager->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, string|array $methods = [], string $type = null, string $subtype = null, string $action = null): bool {
112  if ($user_guid === 0) {
113  $user_guid = _elgg_services()->session_manager->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, string|array $methods = []): bool {
135  if ($user_guid === 0) {
136  $user_guid = _elgg_services()->session_manager->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, string|array $methods = [], string $type = null, string $subtype = null, string $action = null): array {
156  if ($user_guid === 0) {
157  $user_guid = _elgg_services()->session_manager->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(string|array $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_manager->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_manager->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_manager->getLoggedInUserGuid();
218  }
219 
220  return _elgg_services()->subscriptions->unmuteNotifications($user_guid, $this->guid);
221  }
222 
231  protected function normalizeSubscriptionMethods(string|array $methods = []): array {
232  $methods = (array) $methods;
233 
234  foreach ($methods as $method) {
235  if (!is_string($method) || $method === '') {
236  $dbt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
237  $caller = $dbt[1]['function'] ?? 'unknown';
238 
239  throw new InvalidArgumentException(elgg_echo('Entity:Subscriptions:InvalidMethodsException', [$caller]));
240  }
241  }
242 
243  return $methods ?: _elgg_services()->notifications->getMethods();
244  }
245 }
$user_guid
Definition: login_as.php:10
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
$type
Definition: delete.php:21
$user
Definition: ban.php:7
get_user(int $guid)
Elgg users Functions to manage multiple or single users in an Elgg install.
Definition: users.php:16
$action
Definition: subscribe.php:11
$subtype
Definition: delete.php:22
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
$methods
Definition: subscribe.php:8