hooks.php 3.4 KB

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