Elgg  Version master
RegisterSubscriptionMenuItemsHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Notifications;
4 
6 
15 
27  public function __invoke(\Elgg\Event $event) {
28 
29  $result = $event->getValue();
30  $entity = $event->getEntityParam();
31  if (!$result instanceof MenuItems || !$entity instanceof \ElggEntity) {
32  return;
33  }
34 
35  if (!elgg_is_logged_in()) {
36  return;
37  }
38 
39  $link_classes = [];
40  if ($event->getParam('name') === 'title') {
41  $link_classes = [
42  'elgg-button',
43  'elgg-button-action',
44  ];
45  }
46 
47  $has_subscriptions = $entity->hasSubscriptions();
48 
49  // subscribe
50  $subscribe_options = [
51  'name' => 'entity_subscribe',
52  'icon' => 'bell',
53  'text' => elgg_echo('entity:subscribe'),
54  'href' => false,
55  'item_class' => $has_subscriptions ? 'hidden' : '',
56  'link_class' => $link_classes,
57  ];
58 
59  // check if it makes sense to enable the subscribe button
60  $has_preferences = !empty(array_keys(array_filter(elgg_get_logged_in_user_entity()->getNotificationSettings())));
61  if ($has_preferences) {
62  $subscribe_options['href'] = elgg_generate_action_url('entity/subscribe', [
63  'guid' => $entity->guid,
64  ]);
65  $subscribe_options['data-toggle'] = 'entity_mute';
66  } else {
67  $subscribe_options['link_class'][] = 'elgg-state-disabled';
68  $subscribe_options['title'] = elgg_echo('entity:subscribe:disabled');
69  }
70 
71  $result[] = \ElggMenuItem::factory($subscribe_options);
72 
73  // mute
75  'name' => 'entity_mute',
76  'icon' => 'bell-slash',
77  'text' => elgg_echo('entity:mute'),
78  'href' => elgg_generate_action_url('entity/mute', [
79  'guid' => $entity->guid,
80  ]),
81  'item_class' => $has_subscriptions ? '' : 'hidden',
82  'link_class' => $link_classes,
83  'data-toggle' => 'entity_subscribe',
84  ]);
85 
86  return $result;
87  }
88 }
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:43
elgg_generate_action_url(string $action, array $query=[], bool $add_csrf_tokens=true)
Generate an action URL.
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
static factory(array $options)
Create an ElggMenuItem from an associative array.
$entity
Definition: reset.php:8
Register menu items to any menu to handle entity subscriptions.
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:24
A collection of menu items.
Definition: MenuItems.php:10
Models an event passed to event handlers.
Definition: Event.php:11