Elgg  Version master
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  $methods = $user->getNotificationSettings('default', true);
35  if (empty($methods)) {
36  return true;
37  }
38  }
39  }
40 
41  $methods = $this->normalizeSubscriptionMethods($methods);
42 
43  $result = true;
44  foreach ($methods as $method) {
45  $result &= _elgg_services()->subscriptions->addSubscription($user_guid, $method, $this->guid, $type, $subtype, $action);
46  }
47 
48  return $result;
49  }
50 
63  public function hasSubscription(int $user_guid = 0, string|array $methods = [], ?string $type = null, ?string $subtype = null, ?string $action = null): bool {
64  if ($user_guid === 0) {
65  $user_guid = _elgg_services()->session_manager->getLoggedInUserGuid();
66  }
67 
68  $methods = $this->normalizeSubscriptionMethods($methods);
69 
70  $result = true;
71  foreach ($methods as $method) {
72  $result &= _elgg_services()->subscriptions->hasSubscription($user_guid, $method, $this->guid, $type, $subtype, $action);
73  }
74 
75  return $result;
76  }
77 
87  public function hasSubscriptions(int $user_guid = 0, string|array $methods = []): bool {
88  if ($user_guid === 0) {
89  $user_guid = _elgg_services()->session_manager->getLoggedInUserGuid();
90  }
91 
92  $methods = $this->normalizeSubscriptionMethods($methods);
93 
94  return _elgg_services()->subscriptions->hasSubscriptions($user_guid, $this->guid, $methods);
95  }
96 
109  public function removeSubscription(int $user_guid = 0, string|array $methods = [], ?string $type = null, ?string $subtype = null, ?string $action = null): bool {
110  if ($user_guid === 0) {
111  $user_guid = _elgg_services()->session_manager->getLoggedInUserGuid();
112  }
113 
114  $methods = $this->normalizeSubscriptionMethods($methods);
115 
116  $result = true;
117  foreach ($methods as $method) {
118  $result &= _elgg_services()->subscriptions->removeSubscription($user_guid, $method, $this->guid, $type, $subtype, $action);
119  }
120 
121  return $result;
122  }
123 
132  public function removeSubscriptions(int $user_guid = 0, string|array $methods = []): bool {
133  if ($user_guid === 0) {
134  $user_guid = _elgg_services()->session_manager->getLoggedInUserGuid();
135  }
136 
137  $methods = (array) $methods;
138 
139  return _elgg_services()->subscriptions->removeSubscriptions($user_guid, $this->guid, $methods);
140  }
141 
153  public function getSubscriptions(int $user_guid = 0, string|array $methods = [], ?string $type = null, ?string $subtype = null, ?string $action = null): array {
154  if ($user_guid === 0) {
155  $user_guid = _elgg_services()->session_manager->getLoggedInUserGuid();
156  }
157 
158  $methods = (array) $methods;
159 
160  return _elgg_services()->subscriptions->getEntitySubscriptions($this->guid, $user_guid, $methods, $type, $subtype, $action);
161  }
162 
170  public function getSubscribers(string|array $methods = []): array {
171  $methods = (array) $methods;
172 
173  return _elgg_services()->subscriptions->getSubscribers($this->guid, $methods);
174  }
175 
183  public function muteNotifications(int $user_guid = 0): bool {
184  if ($user_guid === 0) {
185  $user_guid = _elgg_services()->session_manager->getLoggedInUserGuid();
186  }
187 
188  return _elgg_services()->subscriptions->muteNotifications($user_guid, $this->guid);
189  }
190 
198  public function hasMutedNotifications(int $user_guid = 0): bool {
199  if ($user_guid === 0) {
200  $user_guid = _elgg_services()->session_manager->getLoggedInUserGuid();
201  }
202 
203  return _elgg_services()->subscriptions->hasMutedNotifications($user_guid, $this->guid);
204  }
205 
213  public function unmuteNotifications(int $user_guid = 0): bool {
214  if ($user_guid === 0) {
215  $user_guid = _elgg_services()->session_manager->getLoggedInUserGuid();
216  }
217 
218  return _elgg_services()->subscriptions->unmuteNotifications($user_guid, $this->guid);
219  }
220 
229  protected function normalizeSubscriptionMethods(string|array $methods = []): array {
230  $methods = (array) $methods;
231 
232  foreach ($methods as $method) {
233  if (!is_string($method) || $method === '') {
234  $dbt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
235  $caller = $dbt[1]['function'] ?? 'unknown';
236 
237  throw new InvalidArgumentException(elgg_echo('Entity:Subscriptions:InvalidMethodsException', [$caller]));
238  }
239  }
240 
241  return $methods ?: _elgg_services()->notifications->getMethods();
242  }
243 }
$subtype
Definition: delete.php:22
$type
Definition: delete.php:21
$user
Definition: ban.php:7
Exception thrown if an argument is not of the expected type.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:337
get_user(int $guid)
Elgg users Functions to manage multiple or single users in an Elgg install.
Definition: users.php:16
elgg_echo(string $message_key, array $args=[], string $language='')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
$user_guid
Definition: login_as.php:10
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
$methods
Definition: subscribe.php:8
$action
Definition: subscribe.php:11