Lines 543-555
function expandPatronSearchFields(search_fields) {
Link Here
|
543 |
* Build patron search query |
543 |
* Build patron search query |
544 |
* - term: The full search term input by the user |
544 |
* - term: The full search term input by the user |
545 |
* You can then pass a list of options: |
545 |
* You can then pass a list of options: |
546 |
* - search_type: String 'contains' or 'starts_with', defaults to DefaultPatronSearchMethod system preference |
546 |
* - search_type: (String) 'contains' or 'starts_with', defaults to defaultPatronSearchMethod (see js_includes.inc) |
547 |
* - search_fields: String comma-separated list of specific fields, defaults to DefaultPatronSearchFields system preference |
547 |
* - search_fields: (String) comma-separated list of specific fields, defaults to defaultPatronSearchFields (see js_includes.inc) |
548 |
* - extended_attribute_types: JSON object containing the patron attribute types to be searched on |
548 |
* - extended_attribute_types: (JSON object) contains the patron searchable attribute types to be searched on (see patron-search.inc) |
|
|
549 |
* - table_prefix: (String) table name to prefix the fields with, defaults to 'me' |
549 |
*/ |
550 |
*/ |
550 |
function buildPatronSearchQuery(term, options) { |
551 |
function buildPatronSearchQuery(term, options) { |
551 |
|
552 |
|
552 |
let q = []; |
553 |
let q = []; |
|
|
554 |
let table_prefix; |
553 |
let leading_wildcard; |
555 |
let leading_wildcard; |
554 |
let search_fields; |
556 |
let search_fields; |
555 |
let patterns = term.split(/[\s,]+/).filter(function (s) { return s.length }); |
557 |
let patterns = term.split(/[\s,]+/).filter(function (s) { return s.length }); |
Lines 559-573
function buildPatronSearchQuery(term, options) {
Link Here
|
559 |
return q; |
561 |
return q; |
560 |
} |
562 |
} |
561 |
|
563 |
|
562 |
// Leading wildcard: If search_type option exists, we use that |
564 |
// Table prefix: If table_prefix options exists, use that |
|
|
565 |
if (typeof options !== 'undefined' && options.table_prefix) { |
566 |
table_prefix = options.table_prefix; |
567 |
// If not, default to 'me' |
568 |
} else { |
569 |
table_prefix = 'me'; |
570 |
} |
571 |
|
572 |
// Leading wildcard: If search_type option exists, use that |
563 |
if (typeof options !== 'undefined' && options.search_type) { |
573 |
if (typeof options !== 'undefined' && options.search_type) { |
564 |
leading_wildcard = options.search_type === "contains" ? '%' : ''; |
574 |
leading_wildcard = options.search_type === "contains" ? '%' : ''; |
565 |
// If not, we use DefaultPatronSearchMethod system preference instead |
575 |
// If not, use DefaultPatronSearchMethod system preference instead |
566 |
} else { |
576 |
} else { |
567 |
leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : ''; |
577 |
leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : ''; |
568 |
} |
578 |
} |
569 |
|
579 |
|
570 |
// Search fields: If search_fields option exists, we use that |
580 |
// Search fields: If search_fields option exists, use that |
571 |
if (typeof options !== 'undefined' && options.search_fields) { |
581 |
if (typeof options !== 'undefined' && options.search_fields) { |
572 |
search_fields = expandPatronSearchFields(options.search_fields); |
582 |
search_fields = expandPatronSearchFields(options.search_fields); |
573 |
// If not, we use DefaultPatronSearchFields system preference instead |
583 |
// If not, we use DefaultPatronSearchFields system preference instead |
Lines 581-592
function buildPatronSearchQuery(term, options) {
Link Here
|
581 |
let pattern_subquery_or = []; |
591 |
let pattern_subquery_or = []; |
582 |
search_fields.split('\|').forEach(function (field, i) { |
592 |
search_fields.split('\|').forEach(function (field, i) { |
583 |
pattern_subquery_or.push( |
593 |
pattern_subquery_or.push( |
584 |
{ ["me." + field]: { 'like': leading_wildcard + pattern + '%' } } |
594 |
{ [table_prefix + "." + field]: { 'like': leading_wildcard + pattern + '%' } } |
585 |
); |
595 |
); |
586 |
if (field == 'dateofbirth') { |
596 |
if (field == 'dateofbirth') { |
587 |
try { |
597 |
try { |
588 |
let d = $date_to_rfc3339(pattern); |
598 |
let d = $date_to_rfc3339(pattern); |
589 |
pattern_subquery_or.push({ ["me." + field]: d }); |
599 |
pattern_subquery_or.push({ [table_prefix + "." + field]: d }); |
590 |
} catch { |
600 |
} catch { |
591 |
// Hide the warning if the date is not correct |
601 |
// Hide the warning if the date is not correct |
592 |
} |
602 |
} |
Lines 600-606
function buildPatronSearchQuery(term, options) {
Link Here
|
600 |
let term_subquery_or = []; |
610 |
let term_subquery_or = []; |
601 |
search_fields.split('\|').forEach(function (field, i) { |
611 |
search_fields.split('\|').forEach(function (field, i) { |
602 |
term_subquery_or.push( |
612 |
term_subquery_or.push( |
603 |
{ ["me." + field]: { 'like': leading_wildcard + term + '%' } } |
613 |
{ [table_prefix + "." + field]: { 'like': leading_wildcard + term + '%' } } |
604 |
); |
614 |
); |
605 |
}); |
615 |
}); |
606 |
q.push({ "-or": term_subquery_or }); |
616 |
q.push({ "-or": term_subquery_or }); |
607 |
- |
|
|