EntityMenu.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace ColdTrick\ContentSubscriptions;
  3. class EntityMenu {
  4. /**
  5. * Add a subscribe/unsubscribe link to the supported entity types
  6. *
  7. * @param string $hook the name of the hook
  8. * @param string $type the type of the hook
  9. * @param \ElggMenuItem[] $return_value the current menu items
  10. * @param array $params supplied params
  11. *
  12. * @return void|\ElggMenuItem[]
  13. */
  14. public static function register($hook, $type, $return_value, $params) {
  15. if (!elgg_is_logged_in()) {
  16. return;
  17. }
  18. if (empty($params) || !is_array($params)) {
  19. return;
  20. }
  21. $entity = elgg_extract('entity', $params);
  22. if (empty($entity) || !content_subscriptions_can_subscribe($entity)) {
  23. return;
  24. }
  25. $subscribed = false;
  26. if (content_subscriptions_check_subscription($entity->getGUID())) {
  27. $subscribed = true;
  28. }
  29. $methods = content_subscriptions_get_notification_settings();
  30. if (!empty($methods)) {
  31. $return_value[] = \ElggMenuItem::factory([
  32. 'name' => 'content_subscription_subscribe',
  33. 'text' => elgg_echo('content_subscriptions:subscribe'),
  34. 'href' => "action/content_subscriptions/subscribe?entity_guid={$entity->getGUID()}",
  35. 'is_action' => true,
  36. 'priority' => 100,
  37. 'item_class' => $subscribed ? 'hidden' : '',
  38. ]);
  39. }
  40. $return_value[] = \ElggMenuItem::factory([
  41. 'name' => 'content_subscription_unsubscribe',
  42. 'text' => elgg_echo('content_subscriptions:unsubscribe'),
  43. 'href' => "action/content_subscriptions/subscribe?entity_guid={$entity->getGUID()}",
  44. 'is_action' => true,
  45. 'priority' => 101,
  46. 'item_class' => $subscribed ? '' : 'hidden',
  47. ]);
  48. return $return_value;
  49. }
  50. }