Elgg  Version 6.3
NotificationsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Notifications;
4 
7 use Elgg\Traits\Loggable;
9 
17 
18  use Loggable;
19 
21  protected $queue;
22 
24  protected $elgg_events;
25 
27  protected $session;
28 
30  protected $events = [];
31 
33  protected $methods = [];
34 
42  public function __construct(
43  Queue $queue,
46  ) {
47 
48  $this->queue = $queue;
49  $this->session = $session;
50  $this->elgg_events = $elgg_events;
51  }
52 
68  public function registerEvent(string $type, string $subtype, array $actions = [], string $handler = NotificationEventHandler::class) {
69  if (!is_a($handler, NotificationEventHandler::class, true)) {
70  throw new InvalidArgumentException('$handler needs to be a ' . NotificationEventHandler::class . ' classname');
71  }
72 
73  if (!isset($this->events[$type])) {
74  $this->events[$type] = [];
75  }
76 
77  if (!isset($this->events[$type][$subtype])) {
78  $this->events[$type][$subtype] = [];
79  }
80 
81  if (empty($actions) && !array_key_exists('create', $this->events[$type][$subtype])) {
82  $actions[] = 'create';
83  }
84 
85  foreach ($actions as $action) {
86  $this->events[$type][$subtype][$action] = $handler;
87  }
88  }
89 
100  public function unregisterEvent(string $type, string $subtype, array $actions = []): void {
101 
102  if (empty($actions)) {
103  unset($this->events[$type][$subtype]);
104  }
105 
106  foreach ($actions as $action) {
107  unset($this->events[$type][$subtype][$action]);
108  }
109 
110  if (empty($this->events[$type][$subtype])) {
111  unset($this->events[$type][$subtype]);
112  }
113 
114  if (empty($this->events[$type])) {
115  unset($this->events[$type]);
116  }
117  }
118 
129  public function isRegisteredEvent(string $type, string $subtype, string $action): bool {
130  return isset($this->events[$type][$subtype][$action]);
131  }
132 
138  public function getEvents(): array {
139  return $this->events;
140  }
141 
150  public function registerMethod(string $name): void {
151  $this->methods[$name] = $name;
152  }
153 
162  public function unregisterMethod(string $name): void {
163  if (!$this->isRegisteredMethod($name)) {
164  return;
165  }
166 
167  unset($this->methods[$name]);
168  }
169 
176  public function getMethods(): array {
177  return $this->methods;
178  }
179 
187  public function isRegisteredMethod(string $method): bool {
188  return in_array($method, $this->methods);
189  }
190 
200  public function enqueueEvent(string $action, \ElggData $object, ?\ElggEntity $actor = null): void {
201  $object_type = $object->getType();
202  $object_subtype = $object->getSubtype();
203  $actor = $actor ?? elgg_get_logged_in_user_entity(); // default to logged in user
204  if (!isset($actor) && ($object instanceof \ElggEntity || $object instanceof \ElggExtender)) {
205  // still not set, default to the owner of $object
206  $actor = $object->getOwnerEntity() ?: null;
207  }
208 
209  $registered = $this->isRegisteredEvent($object_type, $object_subtype, $action);
210  if (!$registered) {
211  return;
212  }
213 
214  $event = new SubscriptionNotificationEvent($object, $action, $actor);
215  $handler = $this->getNotificationHandler($event);
216  if ($handler instanceof InstantNotificationEventHandler) {
217  // don't enqueue the instant handlers
218  return;
219  }
220 
221  $params = [
222  'action' => $action,
223  'object' => $object,
224  'actor' => $actor,
225  ];
226  $registered = (bool) $this->elgg_events->triggerResults('enqueue', 'notification', $params, $registered);
227  if (!$registered) {
228  return;
229  }
230 
231  $this->elgg_events->trigger('enqueue', 'notifications', $object);
232  $this->queue->enqueue($event);
233  }
234 
244  $object = $event->getObject();
245  $handler = NotificationEventHandler::class;
246 
247  if (isset($this->events[$object->getType()][$object->getSubtype()][$event->getAction()])) {
248  $handler = $this->events[$object->getType()][$object->getSubtype()][$event->getAction()];
249  }
250 
251  return new $handler($event, $this, $params);
252  }
253 
262  public function processQueue(int $stopTime, bool $matrix = false) {
263  return elgg_call(ELGG_IGNORE_ACCESS, function() use ($stopTime, $matrix) {
264  $delivery_matrix = [];
265 
266  $count = 0;
267 
268  while (time() < $stopTime) {
269  // dequeue notification event
270  $event = elgg_call(ELGG_SHOW_DISABLED_ENTITIES, function () {
271  // showing disabled entities for deserialization
272  return $this->queue->dequeue();
273  });
274 
275  if (!$event instanceof NotificationEvent) {
276  // queue is empty
277  break;
278  }
279 
280  if (!$event->getObject() || !$event->getActor()) {
281  // event object or actor have been deleted since the event was enqueued
282  continue;
283  }
284 
285  $this->elgg_events->trigger('dequeue', 'notifications', $event->getObject());
286 
287  $handler = $this->getNotificationHandler($event);
288  if ($handler instanceof InstantNotificationEventHandler) {
289  // this shouldn't happen
290  continue;
291  }
292 
293  try {
294  $delivery_matrix[$event->getDescription()] = $handler->send();
295  $count++;
296  } catch (\Throwable $t) {
297  $this->getLogger()->error($t);
298  }
299  }
300 
301  return $matrix ? $delivery_matrix : $count;
302  });
303  }
304 
341  public function sendInstantNotifications(\ElggEntity $sender, array $recipients = [], array $params = []): array {
342  if (empty($this->methods)) {
343  return [];
344  }
345 
346  $object = elgg_extract('object', $params);
347  $action = elgg_extract('action', $params);
348 
349  $event = new InstantNotificationEvent($object, $action, $sender);
350 
351  $handler = new InstantNotificationEventHandler($event, $this, $params);
352  $handler->setRecipients($recipients);
353 
354  return $handler->send();
355  }
356 
370  public function sendInstantNotification(\ElggUser $recipient, string $action, \ElggData $subject, array $params = [], ?\ElggEntity $from = null): array {
371  if (empty($this->methods)) {
372  return [];
373  }
374 
375  $from = $from ?? elgg_get_site_entity();
376 
377  $event = new InstantNotificationEvent($subject, $action, $from);
378 
379  $handler = $this->getNotificationHandler($event, $params);
380  if (!$handler instanceof InstantNotificationEventHandler) {
381  return [];
382  }
383 
384  $handler->setRecipients([$recipient]);
385 
386  return $handler->send();
387  }
388 }
if(! $user||! $user->canDelete()) $name
Definition: delete.php:22
$subtype
Definition: delete.php:22
$type
Definition: delete.php:21
$recipient
Definition: mute.php:8
$params
Saves global plugin settings.
Definition: save.php:13
$handler
Definition: add.php:7
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
$count
Definition: ban.php:24
A generic class that contains shared code among \ElggExtender, \ElggEntity, and \ElggRelationship.
Definition: ElggData.php:12
Elgg Session Management.
Definition: ElggSession.php:19
Events service.
Exception thrown if an argument is not of the expected type.
Notification Event Handler for instant notifications.
Notification Event Handler handles preparation of a notification.
enqueueEvent(string $action, \ElggData $object, ?\ElggEntity $actor=null)
Add a notification event to the queue.
sendInstantNotifications(\ElggEntity $sender, array $recipients=[], array $params=[])
Notify a user via their preferences.
isRegisteredEvent(string $type, string $subtype, string $action)
Check if a notification event is registered.
unregisterMethod(string $name)
Unregister a delivery method for notifications.
getEvents()
Return the notification events.
sendInstantNotification(\ElggUser $recipient, string $action, \ElggData $subject, array $params=[], ?\ElggEntity $from=null)
Send an instant notification to a user.
registerEvent(string $type, string $subtype, array $actions=[], string $handler=NotificationEventHandler::class)
Register a notification event.
__construct(Queue $queue, \ElggSession $session, EventsService $elgg_events)
Constructor.
isRegisteredMethod(string $method)
Check if a notification method is registed.
registerMethod(string $name)
Register a delivery method for notifications.
getNotificationHandler(NotificationEvent $event, array $params=[])
Returns notification event handler based on event.
unregisterEvent(string $type, string $subtype, array $actions=[])
Unregister a notification event.
processQueue(int $stopTime, bool $matrix=false)
Pull notification events from queue until stop time is reached.
getMethods()
Returns registered delivery methods for notifications.
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:123
if($email instanceof \Elgg\Email) $object
Definition: body.php:24
$subject
HTML body of an email.
Definition: body.php:11
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags.
Definition: elgglib.php:290
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:240
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:99
Notification event interface.
getDescription()
Get a description of the event.
getActor()
Get the actor of the event.
getAction()
Get the name of the action.
getObject()
Get the object of the event.
Queue interface.
Definition: Queue.php:11
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
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:24
$action
Definition: subscribe.php:11
if(! $menu instanceof \Elgg\Menu\PreparedMenu) $actions
Definition: user_hover.php:21
if(($owner instanceof \ElggGroup|| $owner instanceof \ElggUser) &&!in_array($owner->guid, $mute_guids)) $actor
Definition: mute.php:78