Koha/koha-tmpl/intranet-tmpl/prog/js/patron-autocomplete.js
Martin Renvoize 6898f1f132
Bug 32520: Use DefaultPatronSearchFields in patron_autocomplete
This patch updates js_includes.inc to set a new global js variable
`defaultPatronSearchFields` with the content of the corresponding system
preference.

We then update the patron_autocomplete function to use this new global
variable and iterate in the same way as
koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc.

NOTE: This changes the behaviour of the autocomplete from always
searching using contains on surname or firstname or starts with on
cardnumber to searching using 'contains' on any of the fields listed in
the DefaultPatronSearchFields system preference of defaulting to
'firstname,middle_name,surname,othernames,cardnumber,userid'.

Test plan
1. Ensure autocomplete still works everywhere
2. Confirm the system preference fields are being used.

Signed-off-by: Barbara Johnson <barbara.johnson@bedfordtx.gov>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
2023-01-27 16:20:54 -03:00

123 lines
4.6 KiB
JavaScript

function patron_autocomplete(node, options) {
let link_to;
let url_params;
let on_select_callback;
if ( options ) {
if ( options['link-to'] ) {
link_to = options['link-to'];
}
if ( options['url-params'] ) {
url_params = options['url-params'];
}
if ( options['on-select-callback'] ) {
on_select_callback = options['on-select-callback'];
}
}
return node.autocomplete({
source: function( request, response ) {
let subquery_and = [];
request.term.split(/[\s,]+/)
.filter(function(s){ return s.length })
.forEach(function(pattern,i){
let subquery_or = [];
defaultPatronSearchFields.split(',').forEach(function(field,i){
subquery_or.push(
{["me."+field]: {'like': '%' + pattern + '%'}}
);
});
subquery_and.push(subquery_or);
});
let q = {"-and": subquery_and};
let params = {
'_page': 1,
'_per_page': 10,
'q': JSON.stringify(q),
'_order_by': '+me.surname,+me.firstname',
};
$.ajax({
data: params,
type: 'GET',
url: '/api/v1/patrons',
headers: {
"x-koha-embed": "library"
},
success: function(data) {
return response(data);
},
error: function(e) {
if ( e.state() != 'rejected' ) {
alert( _("An error occurred. Check the logs") );
}
return response();
}
});
},
minLength: 3,
select: function( event, ui ) {
if ( ui.item.link ) {
window.location.href = ui.item.link;
} else if ( on_select_callback ) {
return on_select_callback(event, ui);
}
},
focus: function( event, ui ) {
event.preventDefault(); // Don't replace the text field
},
})
.data( "ui-autocomplete" )
._renderItem = function( ul, item ) {
if ( link_to ) {
item.link = link_to == 'circ'
? "/cgi-bin/koha/circ/circulation.pl"
: link_to == 'reserve'
? "/cgi-bin/koha/reserve/request.pl"
: "/cgi-bin/koha/members/moremember.pl";
item.link += ( url_params ? '?' + url_params + '&' : "?" ) + 'borrowernumber=' + item.patron_id;
} else {
item.link = null;
}
var cardnumber = "";
if( item.cardnumber != "" ){
// Display card number in parentheses if it exists
cardnumber = " (" + item.cardnumber + ") ";
}
if( item.library_id == loggedInLibrary ){
loggedInClass = "ac-currentlibrary";
} else {
loggedInClass = "";
}
return $( "<li></li>" )
.addClass( loggedInClass )
.data( "ui-autocomplete-item", item )
.append(
""
+ ( item.link ? "<a href=\"" + item.link + "\">" : "<a>" )
+ ( item.surname ? item.surname.escapeHtml() : "" ) + ", "
+ ( item.firstname ? item.firstname.escapeHtml() : "" )
+ cardnumber.escapeHtml()
+ " <small>"
+ ( item.date_of_birth
? $date(item.date_of_birth)
+ "<span class=\"age_years\"> ("
+ $get_age(item.date_of_birth)
+ " "
+ __("years")
+ ")</span>,"
: ""
) + " "
+ ( item.address ? item.address.escapeHtml() : "" ) + " "
+ ( item.city ? item.city.escapeHtml() : "" ) + " "
+ ( item.zipcode ? item.zipcode.escapeHtml() : "" ) + " "
+ ( item.country ? item.country.escapeHtml() : "" ) + " "
+ ( !singleBranchMode
?
"<span class=\"ac-library\">"
+ item.library.name.escapeHtml()
+ "</span>"
: "" )
+ "</small>"
+ "</a>" )
.appendTo( ul );
};
}