Elgg  Version 4.3
RegisterSubscriptionMenuItemsHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Notifications;
4 
6 
15 
27  public function __invoke(\Elgg\Hook $hook) {
28 
29  $result = $hook->getValue();
30  $entity = $hook->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 ($hook->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($action, array $query=[], $add_csrf_tokens=true)
Generate an action URL.
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
static factory($options)
Create an ElggMenuItem from an associative array.
Models an event passed to hook handlers.
Definition: Hook.php:11
$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