group_invite_autocomplete.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * jQuery procedure to fill an autocomplete dropdown
  4. */
  5. global $CONFIG;
  6. $q = sanitize_string(get_input("q"));
  7. $current_users = sanitize_string(get_input("user_guids"));
  8. $limit = (int) get_input("limit", 50);
  9. $group_guid = (int) get_input("group_guid", 0);
  10. $relationship = sanitize_string(get_input("relationship", "none"));
  11. $include_self = get_input("include_self", false);
  12. if (!empty($include_self)) {
  13. $include_self = true;
  14. }
  15. $user = elgg_get_logged_in_user_entity();
  16. $result = [];
  17. header("Content-Type: application/json");
  18. if (empty($user) || empty($q) || empty($group_guid)) {
  19. echo json_encode(array_values($result));
  20. exit();
  21. }
  22. $users_found = [];
  23. // show hidden (unvalidated) users
  24. $hidden = access_show_hidden_entities(true);
  25. if ($relationship != "email") {
  26. $dbprefix = elgg_get_config("dbprefix");
  27. // find existing users
  28. $query_options = [
  29. "type" => "user",
  30. "limit" => $limit,
  31. "joins" => [
  32. "JOIN {$dbprefix}users_entity u ON e.guid = u.guid"
  33. ],
  34. "wheres" => [
  35. "(u.name LIKE '%{$q}%' OR u.username LIKE '%{$q}%')", "u.banned = 'no'"
  36. ],
  37. "order_by" => "u.name asc"
  38. ];
  39. if (!$include_self) {
  40. if (empty($current_users)) {
  41. $current_users = $user->getGUID();
  42. } else {
  43. $current_users .= "," . $user->getGUID();
  44. }
  45. }
  46. if (!empty($current_users)) {
  47. $query_options["wheres"][] = "e.guid NOT IN (" . $current_users . ")";
  48. }
  49. if ($relationship == "friends") {
  50. $query_options["relationship"] = "friend";
  51. $query_options["relationship_guid"] = $user->getGUID();
  52. } elseif ($relationship == "site") {
  53. $query_options["relationship"] = "member_of_site";
  54. $query_options["relationship_guid"] = elgg_get_site_entity()->getGUID();
  55. $query_options["inverse_relationship"] = true;
  56. }
  57. $users_found = elgg_get_entities_from_relationship($query_options);
  58. } else {
  59. // invite by email
  60. $regexpr = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/";
  61. if (preg_match($regexpr, $q)) {
  62. // only start matching if $q is an emailaddress
  63. $users_found = get_user_by_email($q);
  64. if (empty($users_found)) {
  65. $result[] = [
  66. "type" => "email",
  67. "value" => $q,
  68. "label" => $q,
  69. "content" => $q
  70. ];
  71. }
  72. }
  73. }
  74. foreach ($users_found as $user) {
  75. $content = "<img src='" . $user->getIconURL("tiny") . "' /> " . $user->name;
  76. $is_member = false;
  77. if (check_entity_relationship($user->getGUID(), "member", $group_guid)) {
  78. $is_member = true;
  79. $content .= ' - ' . elgg_format_element('span', ['class' => 'elgg-subtext'], elgg_echo('group_tools:groups:invite:user_already_member'));
  80. }
  81. $result[] = [
  82. "type" => 'user',
  83. 'member' => $is_member,
  84. "value" => $user->getGUID(),
  85. "label" => $user->name,
  86. "content" => $content,
  87. "name" => $user->name
  88. ];
  89. }
  90. // restore hidden users
  91. access_show_hidden_entities($hidden);
  92. echo json_encode(array_values($result));
  93. exit();