diff options
author | nickw <nickw@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2010-06-09 22:26:41 +0000 |
---|---|---|
committer | nickw <nickw@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2010-06-09 22:26:41 +0000 |
commit | 7c7fac52ebcf4449e7103c41bccf8babbc213b95 (patch) | |
tree | fc76ffe59368e7cc22b99da439ba37acbe409087 /views/default/input | |
parent | d4c577a6026c0f744af56cef2850d167abbad673 (diff) | |
download | elgg-7c7fac52ebcf4449e7103c41bccf8babbc213b95.tar.gz elgg-7c7fac52ebcf4449e7103c41bccf8babbc213b95.tar.bz2 |
Pre-populating userpicker list when values are passed.
git-svn-id: http://code.elgg.org/elgg/trunk@6430 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'views/default/input')
-rw-r--r-- | views/default/input/userpicker.php | 66 |
1 files changed, 53 insertions, 13 deletions
diff --git a/views/default/input/userpicker.php b/views/default/input/userpicker.php index a9351a36f..001090729 100644 --- a/views/default/input/userpicker.php +++ b/views/default/input/userpicker.php @@ -22,10 +22,37 @@ global $user_picker_js_sent; +function user_picker_add_user($user_id) { + $user = get_entity($user_id); + $icon = $user->getIcon('tiny'); + + $code = '<li class="user_picker_entry">'; + $code .= "<img class=\"livesearch_icon\" src=\"$icon\" />"; + $code .= "$user->name - $user->username"; + $code .= '<div class="delete_button">'; + $code .= "<a onclick=\"userPickerRemoveUser(this, $user_id)\"><strong>X</strong></a>"; + $code .= '</div>'; + $code .= "<input type=\"hidden\" name=\"members[]\" value=\"$user_id\">"; + $code .= '</li>'; + + return $code; +} + if (!isset($vars['value']) || $vars['value'] === FALSE) { $vars['value'] = elgg_get_sticky_value($vars['internalname']); } +// loop over all values and prepare them so that "in" will work in javascript +if (!is_array($vars['value'])) { + $vars['value'] = array($vars['value']); +} +foreach ($vars['value'] as $value) { + $values[$value] = TRUE; +} + +// convert the values to a json-encoded list +$json_values = json_encode($values); + if (!$user_picker_js_sent) { ?> <!-- User picker JS --> @@ -33,6 +60,7 @@ if (!$user_picker_js_sent) { <script type="text/javascript"> // set up a few required variables userPickerURL = '<?php echo $vars['url'] ?>pg/livesearch'; +userList = <?php echo $json_values ?>; function userPickerBindEvents() { // binding autocomplete. @@ -102,18 +130,24 @@ function userPickerFormatItem(row, i, max, term) { function userPickerAddUser(event, data, formatted) { eval("var info = " + data + ";"); - var picker = $(this).parent('.user_picker'); - var users = picker.find('.users'); - var internalName = picker.find('input.internalname').val(); - // not sure why formatted isn't. - var formatted = userPickerFormatItem(data); - - // add guid as hidden input and to list. - var li = formatted + ' <div class="delete_button"><a onclick="userPickerRemoveUser(this, ' + info.guid + ')"><strong>X</strong></a></div>' - + '<input type="hidden" name="' + internalName + '[]" value="' + info.guid + '" />'; - $('<li class="user_picker_entry">').html(li).appendTo(users); - - $(this).val(''); + + // do not allow users to be added multiple times + if (!(info.guid in userList)) { + userList.push(info.guid); + + var picker = $(this).parent('.user_picker'); + var users = picker.find('.users'); + var internalName = picker.find('input.internalname').val(); + // not sure why formatted isn't. + var formatted = userPickerFormatItem(data); + + // add guid as hidden input and to list. + var li = formatted + ' <div class="delete_button"><a onclick="userPickerRemoveUser(this, ' + info.guid + ')"><strong>X</strong></a></div>' + + '<input type="hidden" name="' + internalName + '[]" value="' + info.guid + '" />'; + $('<li class="user_picker_entry">').html(li).appendTo(users); + + $(this).val(''); + } } function userPickerRemoveUser(link, guid) { @@ -136,6 +170,12 @@ $(document).ready(function() { $user_picker_js_sent = true; } +// create an HTML list of users +$user_list = ''; +foreach ($vars['value'] as $user_id) { + $user_list .= user_picker_add_user($user_id); +} + ?> <div class="user_picker"> <input class="internalname" type="hidden" name="internalname" value="<?php echo $vars['internalname']; ?>" /> @@ -146,5 +186,5 @@ $(document).ready(function() { <div class="results"> <!-- This space will be filled with users, checkboxes and magic. --> </div> - <ul class="users"></ul> + <ul class="users"><?php echo $user_list; ?></ul> </div> |