Users.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace ColdTrick\FriendRequest;
  3. class Users {
  4. /**
  5. * Add menu items to the entity 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 registerEntityMenu($hook, $type, $return_value, $params) {
  15. if (empty($params) || !is_array($params)) {
  16. return;
  17. }
  18. $user = elgg_get_logged_in_user_entity();
  19. if (empty($user)) {
  20. return;
  21. }
  22. $entity = elgg_extract('entity', $params);
  23. if (!($entity instanceof \ElggUser)) {
  24. return;
  25. }
  26. if ($entity->getGUID() === $user->getGUID()) {
  27. // looking at yourself
  28. return;
  29. }
  30. // are we friends
  31. if (!$entity->isFriendOf($user->getGUID())) {
  32. // no, check if pending request
  33. if (check_entity_relationship($user->getGUID(), 'friendrequest', $entity->getGUID())) {
  34. // pending request
  35. $return_value[] = \ElggMenuItem::factory([
  36. 'name' => 'friend_request',
  37. 'text' => elgg_echo('friend_request:friend:add:pending'),
  38. 'href' => "friend_request/{$user->username}#friend_request_sent_listing",
  39. 'priority' => 500,
  40. ]);
  41. } else {
  42. // add as friend
  43. $return_value[] = \ElggMenuItem::factory([
  44. 'name' => 'add_friend',
  45. 'text' => elgg_echo('friend:add'),
  46. 'href' => "action/friends/add?friend={$entity->getGUID()}",
  47. 'is_action' => true,
  48. 'priority' => 500,
  49. ]);
  50. }
  51. } else {
  52. // is friend, se remove friend link
  53. $return_value[] = \ElggMenuItem::factory([
  54. 'name' => 'remove_friend',
  55. 'text' => elgg_echo('friend:remove'),
  56. 'href' => "action/friends/remove?friend={$entity->getGUID()}",
  57. 'is_action' => true,
  58. 'priority' => 500,
  59. ]);
  60. }
  61. return $return_value;
  62. }
  63. /**
  64. * Add menu items to the hover menu
  65. *
  66. * @param string $hook the name of the hook
  67. * @param string $type the type of the hook
  68. * @param \ElggMenuItem[] $return_value the current return value
  69. * @param array $params supplied params
  70. *
  71. * @return void|\ElggMenuItem[]
  72. */
  73. public static function registerUserHoverMenu($hook, $type, $return_value, $params) {
  74. if (empty($params) || !is_array($params)) {
  75. return;
  76. }
  77. $user = elgg_get_logged_in_user_entity();
  78. if (empty($user)) {
  79. return;
  80. }
  81. $entity = elgg_extract('entity', $params);
  82. if (!($entity instanceof \ElggUser)) {
  83. return;
  84. }
  85. if ($entity->getGUID() === $user->getGUID()) {
  86. // looking at yourself
  87. return;
  88. }
  89. $requested = check_entity_relationship($user->getGUID(), 'friendrequest', $entity->getGUID());
  90. $is_friend = $entity->isFriend($user->getGUID());
  91. foreach ($return_value as $index => $item) {
  92. // change the text of the button to tell you already requested a friendship
  93. switch ($item->getName()) {
  94. case 'add_friend':
  95. if ($requested) {
  96. $item->setItemClass('hidden');
  97. }
  98. break;
  99. case 'remove_friend':
  100. if (!$requested && !$is_friend) {
  101. unset($return_value[$index]);
  102. }
  103. break;
  104. }
  105. }
  106. if ($requested) {
  107. $return_value[] = \ElggMenuItem::factory([
  108. 'name' => 'friend_request',
  109. 'text' => elgg_echo('friend_request:friend:add:pending'),
  110. 'href' => "friend_request/{$user->username}#friend_request_sent_listing",
  111. 'section' => 'action',
  112. ]);
  113. }
  114. return $return_value;
  115. }
  116. }