Elgg  Version master
CreateContentEventHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Notifications;
4 
12 
20  public function __invoke(\Elgg\Event $event): void {
21  $entity = $event->getObject();
22  if (!$entity instanceof \ElggObject && !$entity instanceof \ElggGroup) {
23  return;
24  }
25 
26  $owner = $entity->getOwnerEntity();
27  if (!$owner instanceof \ElggUser) {
28  return;
29  }
30 
31  if ($entity instanceof \ElggObject) {
32  $notification_events = _elgg_services()->notifications->getEvents();
33  if (!isset($notification_events[$entity->getType()]) || !isset($notification_events[$entity->getType()][$entity->getSubtype()])) {
34  // no notification events registered for this entity type/subtype
35  // so there is no need to subscribe
36  // this also prevents the database from flooding with relationships that are never used (e.g. subscriptions to site notifications)
37  return;
38  }
39  }
40 
41  $content_preferences = $owner->getNotificationSettings('content_create');
42  $enabled_methods = array_keys(array_filter($content_preferences));
43 
44  // loop through all notification types
46  foreach ($enabled_methods as $method) {
47  // only enable supported methods
48  if (!in_array($method, $methods)) {
49  continue;
50  }
51 
52  $entity->addSubscription($owner->guid, $method);
53  }
54  }
55 }
$entity
Definition: reset.php:8
$owner
Definition: upload.php:7
Models an event passed to event handlers.
Definition: Event.php:11
Apply subscriptions based on preferences.
__invoke(\Elgg\Event $event)
Subscribe to content you just created in order to receive notifications.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
elgg_get_notification_methods()
Returns registered delivery methods for notifications [ 'email' => 'email', 'sms' => 'sms',...
foreach($entity->getSubscriptions($user->guid) as $subscription) $notification_events
Definition: details.php:44
$methods
Definition: subscribe.php:8