Skip to content

Commit

Permalink
Autocomplete for recipient field
Browse files Browse the repository at this point in the history
  • Loading branch information
annda committed Nov 23, 2022
1 parent 369955a commit 9121cb0
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 7 deletions.
59 changes: 59 additions & 0 deletions action.php
Expand Up @@ -8,8 +8,15 @@ public function register(Doku_Event_Handler $controller) {
$controller->register_hook($event, 'BEFORE', $this, 'handle');
}
$controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'handleMenu');
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'autocomplete');
}

/**
* Main processing
*
* @param Doku_Event $event
* @return void
*/
public function handle(Doku_Event $event) {
if ($event->data !=='recommend') {
return;
Expand Down Expand Up @@ -58,6 +65,58 @@ public function handleMenu(Doku_Event $event)
array_splice($event->data['items'], -1, 0, [new \dokuwiki\plugin\recommend\MenuItem()]);
}

/**
* Autocomplete
* @param Doku_Event $event
* @throws Exception
* @author Andreas Gohr
*
*/
public function autocomplete(Doku_Event $event)
{

if ($event->data !=='plugin_recommend_ac') {
return;
}

$event->preventDefault();
$event->stopPropagation();

/** @var \DokuWiki_Auth_Plugin $auth */
global $auth;
global $INPUT;

if (!$auth->canDo('getUsers')) {
throw new Exception('The user backend can not search for users');
}

header('Content-Type: application/json');

// check minimum length
$lookup = trim($INPUT->str('search'));
if (utf8_strlen($lookup) < 3) {
echo json_encode([]);
return;
}

// find users by login and name
$logins = $auth->retrieveUsers(0, 10, ['user' => $lookup]);
if (count($logins) < 10) {
$logins = array_merge($logins, $auth->retrieveUsers(0, 10, ['name' => $lookup]));
}

// reformat result for jQuery UI Autocomplete
$users = [];
foreach ($logins as $login => $info) {
$users[] = [
'label' => $info['name'] . ' [' . $login . ']',
'value' => $login
];
}

echo json_encode($users);
}

/**
* Returns rendered form
*
Expand Down
43 changes: 36 additions & 7 deletions helper/mail.php
Expand Up @@ -41,17 +41,13 @@ public function resolveRecipients($recipients)

$recipients = explode(',', $recipients);

/** @var AuthPlugin $auth */
global $auth;

foreach ($recipients as $recipient) {
$recipient = trim($recipient);

if ($recipient[0] === '@') {
$users = $auth->retrieveUsers(0, 0, ['grps' => substr($recipient, 1)]);
foreach ($users as $user) {
$resolved[] = $user['mail'];
}
$this->resolveGroup($resolved, $recipient);
} elseif (strpos($recipient, '@') === false) {
$this->resolveUser($resolved, $recipient);
} else {
if (!mail_isvalid($recipient)) {
throw new \Exception($this->getLang('err_recipient'));
Expand All @@ -61,4 +57,37 @@ public function resolveRecipients($recipients)
}
return $resolved;
}

/**
* @param array $resolved
* @param string $recipient
* @return void
* @throws Exception
*/
protected function resolveGroup(&$resolved, $recipient)
{
/** @var AuthPlugin $auth */
global $auth;
if (!$auth->canDo('getUsers')) {
throw new \Exception('');
}

$users = $auth->retrieveUsers(0, 0, ['grps' => substr($recipient, 1)]);
foreach ($users as $user) {
$resolved[] = $user['mail'];
}
}

/**
* @param array $resolved
* @param string $recipient
* @return void
*/
protected function resolveUser(&$resolved, $recipient)
{
/** @var AuthPlugin $auth */
global $auth;
$user = $auth->getUserData($recipient);
if ($user) $resolved[] = $user['mail'];
}
}
43 changes: 43 additions & 0 deletions script.js
Expand Up @@ -65,9 +65,52 @@ const recommend = {
* @param {string} data The HTML
*/
handleResult: function (data) {

function commasplit( val ) {
return val.split( /,\s*/ );
}

recommend.$dialog.html(data);
recommend.$dialog.find('button[type=reset]').click(recommend.cancel);
recommend.$dialog.find('button[type=submit]').click(recommend.send);
recommend.$dialog.find('input[name=r_email]').autocomplete({
source: function (request, cb) {
let term = request.term;
term = commasplit(term).pop();

const payload = {};
payload['call'] = 'plugin_recommend_ac';
payload['search'] = term;

jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', payload, cb, 'json')
.fail(function (result) {
if (result.responseJSON) {
if (result.responseJSON.stacktrace) {
console.error(result.responseJSON.error + "\n" + result.responseJSON.stacktrace);
}
alert(result.responseJSON.error);
} else {
// some fatal error occurred, get a text only version of the response
alert(jQuery(result.responseText).text());
}
});
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
let terms = commasplit( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
},

/**
Expand Down

0 comments on commit 9121cb0

Please sign in to comment.