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