PageMenu.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace ColdTrick\FriendRequest;
  3. class PageMenu {
  4. /**
  5. * Remove menu items from the page menu
  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 return value
  10. * @param array $params supplied params
  11. *
  12. * @return void|\ElggMenuItem[]
  13. */
  14. public static function registerCleanup($hook, $type, $return_value, $params) {
  15. if (empty($return_value) || !is_array($return_value)) {
  16. return;
  17. }
  18. $remove_items = [
  19. 'friends:of',
  20. ];
  21. foreach ($return_value as $index => $menu_item) {
  22. if (!in_array($menu_item->getName(), $remove_items)) {
  23. continue;
  24. }
  25. unset($return_value[$index]);
  26. }
  27. return $return_value;
  28. }
  29. /**
  30. * Add menu items to the page menu
  31. *
  32. * @param string $hook the name of the hook
  33. * @param string $type the type of the hook
  34. * @param \ElggMenuItem[] $return_value the current return value
  35. * @param array $params supplied params
  36. *
  37. * @return void|\ElggMenuItem[]
  38. */
  39. public static function register($hook, $type, $return_value, $params) {
  40. $page_owner = elgg_get_page_owner_entity();
  41. if (!($page_owner instanceof \ElggUser)) {
  42. return;
  43. }
  44. if (!$page_owner->canEdit()) {
  45. return;
  46. }
  47. // count pending requests
  48. $options = [
  49. 'type' => 'user',
  50. 'count' => true,
  51. 'relationship' => 'friendrequest',
  52. 'relationship_guid' => $page_owner->getGUID(),
  53. 'inverse_relationship' => true,
  54. ];
  55. $count = elgg_get_entities_from_relationship($options);
  56. $extra = '';
  57. if (!empty($count)) {
  58. $extra = ' [' . $count . ']';
  59. }
  60. // add menu item
  61. $return_value[] = \ElggMenuItem::factory([
  62. 'name' => 'friend_request',
  63. 'text' => elgg_echo('friend_request:menu') . $extra,
  64. 'href' => "friend_request/{$page_owner->username}",
  65. 'contexts' => ['friends', 'friendsof', 'collections', 'messages'],
  66. ]);
  67. return $return_value;
  68. }
  69. }