Lines 398-400
function saveOrClearSimpleSearchParams() {
Link Here
|
398 |
localStorage.setItem('cat_search_pulldown_selection', pulldown_selection ); |
398 |
localStorage.setItem('cat_search_pulldown_selection', pulldown_selection ); |
399 |
localStorage.setItem('searchbox_value', searchbox_value ); |
399 |
localStorage.setItem('searchbox_value', searchbox_value ); |
400 |
} |
400 |
} |
401 |
- |
401 |
|
|
|
402 |
function patron_autocomplete(node, options) { |
403 |
let link_to; |
404 |
let url_params; |
405 |
let on_select_callback; |
406 |
let leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : ''; |
407 |
if (options) { |
408 |
if (options['link-to']) { |
409 |
link_to = options['link-to']; |
410 |
} |
411 |
if (options['url-params']) { |
412 |
url_params = options['url-params']; |
413 |
} |
414 |
if (options['on-select-callback']) { |
415 |
on_select_callback = options['on-select-callback']; |
416 |
} |
417 |
} |
418 |
return node.autocomplete({ |
419 |
source: function (request, response) { |
420 |
let q = [] |
421 |
|
422 |
// Add each pattern in search term for each search field |
423 |
let pattern_subquery_and = []; |
424 |
request.term.split(/[\s,]+/) |
425 |
.filter(function (s) { return s.length }) |
426 |
.forEach(function (pattern, i) { |
427 |
let pattern_subquery_or = []; |
428 |
defaultPatronSearchFields.split(',').forEach(function (field, i) { |
429 |
pattern_subquery_or.push( |
430 |
{ ["me." + field]: { 'like': leading_wildcard + pattern + '%' } } |
431 |
); |
432 |
}); |
433 |
pattern_subquery_and.push(pattern_subquery_or); |
434 |
}); |
435 |
q.push({ "-and": pattern_subquery_and }); |
436 |
|
437 |
// Add full search term for each search field |
438 |
let term_subquery_or = []; |
439 |
defaultPatronSearchFields.split(',').forEach(function (field, i) { |
440 |
term_subquery_or.push( |
441 |
{ ["me." + field]: { 'like': leading_wildcard + request.term + '%' } } |
442 |
); |
443 |
}); |
444 |
q.push({ "-or": term_subquery_or }); |
445 |
|
446 |
let params = { |
447 |
'_page': 1, |
448 |
'_per_page': 10, |
449 |
'q': JSON.stringify(q), |
450 |
'_order_by': '+me.surname,+me.firstname', |
451 |
}; |
452 |
$.ajax({ |
453 |
data: params, |
454 |
type: 'GET', |
455 |
url: '/api/v1/patrons', |
456 |
headers: { |
457 |
"x-koha-embed": "library" |
458 |
}, |
459 |
success: function (data) { |
460 |
return response(data); |
461 |
}, |
462 |
error: function (e) { |
463 |
if (e.state() != 'rejected') { |
464 |
alert(__("An error occurred. Check the logs")); |
465 |
} |
466 |
return response(); |
467 |
} |
468 |
}); |
469 |
}, |
470 |
minLength: 3, |
471 |
select: function (event, ui) { |
472 |
if (ui.item.link) { |
473 |
window.location.href = ui.item.link; |
474 |
} else if (on_select_callback) { |
475 |
return on_select_callback(event, ui); |
476 |
} |
477 |
}, |
478 |
focus: function (event, ui) { |
479 |
event.preventDefault(); // Don't replace the text field |
480 |
}, |
481 |
}) |
482 |
.data("ui-autocomplete") |
483 |
._renderItem = function (ul, item) { |
484 |
if (link_to) { |
485 |
item.link = link_to == 'circ' |
486 |
? "/cgi-bin/koha/circ/circulation.pl" |
487 |
: link_to == 'reserve' |
488 |
? "/cgi-bin/koha/reserve/request.pl" |
489 |
: "/cgi-bin/koha/members/moremember.pl"; |
490 |
item.link += (url_params ? '?' + url_params + '&' : "?") + 'borrowernumber=' + item.patron_id; |
491 |
} else { |
492 |
item.link = null; |
493 |
} |
494 |
|
495 |
var cardnumber = ""; |
496 |
if (item.cardnumber != "") { |
497 |
// Display card number in parentheses if it exists |
498 |
cardnumber = " (" + item.cardnumber + ") "; |
499 |
} |
500 |
if (item.library_id == loggedInLibrary) { |
501 |
loggedInClass = "ac-currentlibrary"; |
502 |
} else { |
503 |
loggedInClass = ""; |
504 |
} |
505 |
return $("<li></li>") |
506 |
.addClass(loggedInClass) |
507 |
.data("ui-autocomplete-item", item) |
508 |
.append( |
509 |
"" |
510 |
+ (item.link ? "<a href=\"" + item.link + "\">" : "<a>") |
511 |
+ (item.surname ? item.surname.escapeHtml() : "") + ", " |
512 |
+ (item.firstname ? item.firstname.escapeHtml() : "") |
513 |
+ cardnumber.escapeHtml() |
514 |
+ " <small>" |
515 |
+ (item.date_of_birth |
516 |
? $date(item.date_of_birth) |
517 |
+ "<span class=\"age_years\"> (" |
518 |
+ $get_age(item.date_of_birth) |
519 |
+ " " |
520 |
+ __("years") |
521 |
+ ")</span>," |
522 |
: "" |
523 |
) + " " |
524 |
+ $format_address(item, { no_line_break: true, include_li: false }) + " " |
525 |
+ (!singleBranchMode |
526 |
? |
527 |
"<span class=\"ac-library\">" |
528 |
+ item.library.name.escapeHtml() |
529 |
+ "</span>" |
530 |
: "") |
531 |
+ "</small>" |
532 |
+ "</a>") |
533 |
.appendTo(ul); |
534 |
}; |
535 |
} |