Bug 34092: Move search-patron.inc logic into buildPatronSearchQuery
Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
(cherry picked from commit a9f8db17fe
)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
parent
e650fe865f
commit
0c3b4bb30f
3 changed files with 72 additions and 54 deletions
|
@ -71,6 +71,7 @@
|
|||
<script>
|
||||
var defaultPatronSearchFields = "[% Koha.Preference('DefaultPatronSearchFields') || 'firstname,middle_name,surname,othernames,cardnumber,userid' | html %]";
|
||||
var defaultPatronSearchMethod = "[% Koha.Preference('DefaultPatronSearchMethod') || 'contains' | html %]";
|
||||
var extendedPatronAttributes = "[% Koha.Preference('ExtendedPatronAttributes') | html %]";
|
||||
var loggedInLibrary = '[% Branches.GetLoggedInBranchcode | html %]';
|
||||
var singleBranchMode = '[% singleBranchMode | html %]';
|
||||
</script>
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
[%# You can then pass a list of filters %]
|
||||
[%# - branch: select library list %]
|
||||
[%# - category: select patron category list %]
|
||||
[%# - sort1: select patron sort1 field %]
|
||||
[%# - sort2: select patron sort2 field %]
|
||||
[%# - search_field: select patron field list %]
|
||||
[%# - search_type: select 'contains' or 'starts with' %]
|
||||
[%- SET searchtype = Koha.Preference('DefaultPatronSearchMethod') -%]
|
||||
|
@ -316,6 +318,20 @@
|
|||
},
|
||||
"-and": function(){
|
||||
let filters = [];
|
||||
|
||||
let search_type = $("#searchtype_filter").val() || "contains";
|
||||
let search_fields = $("#searchfieldstype_filter").val();
|
||||
let pattern = $("#search_patron_filter").val();
|
||||
|
||||
filters = buildPatronSearchQuery(
|
||||
pattern,
|
||||
{
|
||||
search_type: search_type,
|
||||
search_fields: search_fields,
|
||||
...(typeof extended_attribute_types != 'undefined' && {extended_attribute_types: extended_attribute_types})
|
||||
}
|
||||
);
|
||||
|
||||
let f_sort1 = $("#sort1_filter").val();
|
||||
if ( f_sort1 ) {
|
||||
filters.push({
|
||||
|
@ -329,53 +345,6 @@
|
|||
});
|
||||
}
|
||||
|
||||
let pattern = $("#search_patron_filter").val();
|
||||
if (!pattern) {
|
||||
if ( filters.length == 0 ) {
|
||||
return "";
|
||||
}
|
||||
else {
|
||||
return filters;
|
||||
}
|
||||
}
|
||||
let patterns = pattern.split(/[\s,]+/).filter(function(s){ return s.length });
|
||||
|
||||
let search_type = $("#searchtype_filter").val() || "contains";
|
||||
let search_fields = $("#searchfieldstype_filter").val();
|
||||
if ( !search_fields ) {
|
||||
search_fields = "[% Koha.Preference('DefaultPatronSearchFields') || 'firstname,middle_name,surname,othernames,cardnumber,userid' | html %]";
|
||||
}
|
||||
|
||||
let subquery_and = [];
|
||||
patterns.forEach(function(pattern,i){
|
||||
let sub_or = [];
|
||||
search_fields.split(',').forEach(function(attr,ii){
|
||||
sub_or.push({["me."+attr]:{"like":(search_type == "contains" ? "%" : "" ) + pattern + "%"}});
|
||||
if ( attr == 'dateofbirth' ) {
|
||||
try {
|
||||
let d = $date_to_rfc3339(pattern);
|
||||
sub_or.push({["me."+attr]:d});
|
||||
} catch {
|
||||
// Hide the warning if the date is not correct
|
||||
}
|
||||
}
|
||||
});
|
||||
subquery_and.push(sub_or);
|
||||
});
|
||||
filters.push({"-and": subquery_and});
|
||||
|
||||
[% IF Koha.Preference('ExtendedPatronAttributes') && extended_attribute_types %]
|
||||
subquery_and = [];
|
||||
patterns.forEach(function(pattern,i){
|
||||
let sub_or = [];
|
||||
sub_or.push({
|
||||
"extended_attributes.value": { "like": "%" + pattern + (search_type == "contains" ? "%" : "" )},
|
||||
"extended_attributes.code": extended_attribute_types
|
||||
});
|
||||
subquery_and.push(sub_or);
|
||||
});
|
||||
filters.push({"-and": subquery_and});
|
||||
[% END %]
|
||||
return filters;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -510,22 +510,58 @@ function patron_autocomplete(node, options) {
|
|||
};
|
||||
}
|
||||
|
||||
|
||||
function buildPatronSearchQuery(term) {
|
||||
/**
|
||||
* Build patron search query
|
||||
* - term: The full search term input by the user
|
||||
* You can then pass a list of options:
|
||||
* - search_type: String 'contains' or 'starts_with', defaults to DefaultPatronSearchMethod system preference
|
||||
* - search_fields: String comma-separated list of specific fields, defaults to DefaultPatronSearchFields system preference
|
||||
* - extended_attribute_types: JSON object containing the patron attribute types to be searched on
|
||||
*/
|
||||
function buildPatronSearchQuery(term, options) {
|
||||
|
||||
let q = [];
|
||||
let leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : '';
|
||||
let leading_wildcard;
|
||||
let search_fields;
|
||||
let patterns = term.split(/[\s,]+/).filter(function (s) { return s.length });
|
||||
|
||||
// Bail if no patterns
|
||||
if (patterns.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Leading wildcard: If search_type option exists, we use that
|
||||
if (typeof options !== 'undefined' && options.search_type) {
|
||||
leading_wildcard = options.search_type === "contains" ? '%' : '';
|
||||
// If not, we use DefaultPatronSearchMethod system preference instead
|
||||
} else {
|
||||
leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : '';
|
||||
}
|
||||
|
||||
// Search fields: If search_fields option exists, we use that
|
||||
if (typeof options !== 'undefined' && options.search_fields) {
|
||||
search_fields = options.search_fields;
|
||||
// If not, we use DefaultPatronSearchFields system preference instead
|
||||
} else {
|
||||
search_fields = defaultPatronSearchFields;
|
||||
}
|
||||
|
||||
// Add each pattern for each search field
|
||||
let pattern_subquery_and = [];
|
||||
term.split(/[\s,]+/)
|
||||
.filter(function (s) { return s.length })
|
||||
.forEach(function (pattern, i) {
|
||||
patterns.forEach(function (pattern, i) {
|
||||
let pattern_subquery_or = [];
|
||||
defaultPatronSearchFields.split(',').forEach(function (field, i) {
|
||||
pattern_subquery_or.push(
|
||||
{ ["me." + field]: { 'like': leading_wildcard + pattern + '%' } }
|
||||
);
|
||||
if (field == 'dateofbirth') {
|
||||
try {
|
||||
let d = $date_to_rfc3339(pattern);
|
||||
pattern_subquery_or.push({ ["me." + field]: d });
|
||||
} catch {
|
||||
// Hide the warning if the date is not correct
|
||||
}
|
||||
}
|
||||
});
|
||||
pattern_subquery_and.push(pattern_subquery_or);
|
||||
});
|
||||
|
@ -540,6 +576,18 @@ function buildPatronSearchQuery(term) {
|
|||
});
|
||||
q.push({ "-or": term_subquery_or });
|
||||
|
||||
|
||||
// Add each pattern for each extended patron attributes
|
||||
if (typeof options !== 'undefined' && options.extended_attribute_types && extendedPatronAttributes) {
|
||||
extended_attribute_subquery_and = [];
|
||||
patterns.forEach(function (pattern, i) {
|
||||
let extended_attribute_sub_or = [];
|
||||
extended_attribute_sub_or.push({
|
||||
"extended_attributes.value": { "like": leading_wildcard + pattern + '%' },
|
||||
"extended_attributes.code": options.extended_attribute_types
|
||||
});
|
||||
extended_attribute_subquery_and.push(extended_attribute_sub_or);
|
||||
});
|
||||
q.push({ "-and": extended_attribute_subquery_and });
|
||||
}
|
||||
return q;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue