|
2 | 2 |
|
3 | 3 | require_once __DIR__ . "/../../../resources/autoload.php"; |
4 | 4 | use UnityWebPortal\lib\UnityHTTPD; |
| 5 | +use UnityWebPortal\lib\UnityGroup; |
5 | 6 | $CSRFTokenHiddenFormInput = UnityHTTPD::getCSRFTokenHiddenFormInput(); |
| 7 | + |
| 8 | +// cache PI group info in $_SESSION for ajax pi_search.php |
| 9 | +// cache persists only until the user loads this page again |
| 10 | +$owner_uids = $LDAP->getAllPIGroupOwnerUIDs(); |
| 11 | +$owner_attributes = $LDAP->getUsersAttributes( |
| 12 | + $owner_uids, |
| 13 | + ["uid", "gecos", "mail"], |
| 14 | + default_values: ["gecos" => [""], "mail" => [""]] |
| 15 | +); |
| 16 | +$pi_group_gid_to_owner_gecos_and_mail = []; |
| 17 | +foreach ($owner_attributes as $attributes) { |
| 18 | + $gid = UnityGroup::ownerUID2GID($attributes["uid"][0]); |
| 19 | + $pi_group_gid_to_owner_gecos_and_mail[$gid] = [$attributes["gecos"][0], $attributes["mail"][0]]; |
| 20 | +} |
| 21 | +$_SESSION["pi_group_gid_to_owner_gecos_and_mail"] = $pi_group_gid_to_owner_gecos_and_mail; |
6 | 22 | ?> |
7 | 23 |
|
8 | 24 | <form |
|
13 | 29 | <?php echo $CSRFTokenHiddenFormInput; ?> |
14 | 30 | <input type="hidden" name="form_type" value="addPIform"> |
15 | 31 | <div style="position: relative;"> |
16 | | - <input type="text" id="pi_search" name="pi" placeholder="Search PI by NetID" required> |
| 32 | + <input |
| 33 | + type="text" |
| 34 | + id="pi_search" |
| 35 | + name="pi" |
| 36 | + placeholder="Search by GID, Name, or Email" |
| 37 | + required |
| 38 | + > |
17 | 39 | <div class="searchWrapper" style="display: none;"></div> |
18 | 40 | </div> |
19 | 41 | <label> |
|
23 | 45 | Terms of Service |
24 | 46 | </a>. |
25 | 47 | </label> |
26 | | - <input type="submit" value="Send Request"> |
| 48 | + <input type="submit" value="Send Request" disabled> |
27 | 49 | </form> |
28 | 50 |
|
29 | 51 | <script> |
30 | | - $("input[type=text][name=pi]").keyup(function() { |
31 | | - var searchWrapper = $("div.searchWrapper"); |
32 | | - const url = '<?php echo getURL("panel/modal/pi_search.php") ?>'; |
33 | | - $.ajax({ |
34 | | - url: `${url}?search=` + $(this).val(), |
35 | | - success: function(result) { |
36 | | - searchWrapper.html(result); |
37 | | - |
38 | | - if (result == "") { |
39 | | - searchWrapper.hide(); |
40 | | - } else { |
41 | | - searchWrapper.show(); |
| 52 | + (function () { |
| 53 | + const input = $("input[name=pi]"); |
| 54 | + const wrapper = $("div.searchWrapper"); |
| 55 | + const submit = $("#newPIform > input[type=submit]"); |
| 56 | + function updateSearch() { |
| 57 | + const query = input.val(); |
| 58 | + $.ajax({ |
| 59 | + url: '<?php echo getURL("panel/ajax/pi_search.php") ?>', |
| 60 | + data: {"search": query}, |
| 61 | + success: function(data) { |
| 62 | + const results = JSON.parse(data); |
| 63 | + if (results.length === 0) { |
| 64 | + wrapper.html("<span>No Results</span>").show(); |
| 65 | + submit.prop("disabled", true); |
| 66 | + } else if (results.includes(query)) { |
| 67 | + // search query exactly matches a PI group GID |
| 68 | + wrapper.html("").hide(); |
| 69 | + submit.prop("disabled", false); |
| 70 | + } else { |
| 71 | + const html = results.map(gid => `<span>${gid}</span>`).join(''); |
| 72 | + wrapper.html(html).show(); |
| 73 | + submit.prop("disabled", true); |
| 74 | + } |
| 75 | + }, |
| 76 | + error: function(result) { |
| 77 | + const error_msg_div = $("<div></div>"); |
| 78 | + error_msg_div.html(result.responseText); |
| 79 | + submit.after(error_msg_div); |
| 80 | + wrapper.html("").hide(); |
| 81 | + submit.prop("disabled", true); |
42 | 82 | } |
43 | | - }, |
44 | | - error: function (result) { |
45 | | - searchWrapper.html(result.responseText); |
46 | | - searchWrapper.show(); |
47 | | - }, |
| 83 | + }); |
| 84 | + }; |
| 85 | + input.on("keyup", () => updateSearch()); |
| 86 | + wrapper.on("click", "span", function() { |
| 87 | + input.val($(this).text()); |
| 88 | + updateSearch(); |
48 | 89 | }); |
49 | | - }); |
50 | | - |
51 | | - $("div.searchWrapper").on("click", "span", function (event) { |
52 | | - var textBox = $("input[type=text][name=pi]"); |
53 | | - textBox.val($(this).html()); |
54 | | - }); |
55 | | - |
56 | | - /** |
57 | | - * Hides the searchresult box on click anywhere |
58 | | - */ |
59 | | - $(document).click(function() { |
60 | | - $("div.searchWrapper").hide(); |
61 | | - }); |
| 90 | + $(document).on("click", () => wrapper.hide()); |
| 91 | + })(); |
62 | 92 | </script> |
0 commit comments