|
Lines 567-579
function expandPatronSearchFields(search_fields) {
Link Here
|
| 567 |
* Build patron search query |
567 |
* Build patron search query |
| 568 |
* - term: The full search term input by the user |
568 |
* - term: The full search term input by the user |
| 569 |
* You can then pass a list of options: |
569 |
* You can then pass a list of options: |
| 570 |
* - search_type: String 'contains' or 'starts_with', defaults to DefaultPatronSearchMethod system preference |
570 |
* - search_type: (String) 'contains' or 'starts_with', defaults to defaultPatronSearchMethod (see js_includes.inc) |
| 571 |
* - search_fields: String comma-separated list of specific fields, defaults to DefaultPatronSearchFields system preference |
571 |
* - search_fields: (String) comma-separated list of specific fields, defaults to defaultPatronSearchFields (see js_includes.inc) |
| 572 |
* - extended_attribute_types: JSON object containing the patron attribute types to be searched on |
572 |
* - extended_attribute_types: (JSON object) contains the patron searchable attribute types to be searched on (see patron-search.inc) |
|
|
573 |
* - table_prefix: (String) table name to prefix the fields with, defaults to 'me' |
| 573 |
*/ |
574 |
*/ |
| 574 |
function buildPatronSearchQuery(term, options) { |
575 |
function buildPatronSearchQuery(term, options) { |
| 575 |
|
576 |
|
| 576 |
let q = []; |
577 |
let q = []; |
|
|
578 |
let table_prefix; |
| 577 |
let leading_wildcard; |
579 |
let leading_wildcard; |
| 578 |
let search_fields; |
580 |
let search_fields; |
| 579 |
let patterns = term.split(/[\s,]+/).filter(function (s) { return s.length }); |
581 |
let patterns = term.split(/[\s,]+/).filter(function (s) { return s.length }); |
|
Lines 583-597
function buildPatronSearchQuery(term, options) {
Link Here
|
| 583 |
return q; |
585 |
return q; |
| 584 |
} |
586 |
} |
| 585 |
|
587 |
|
| 586 |
// Leading wildcard: If search_type option exists, we use that |
588 |
// Table prefix: If table_prefix options exists, use that |
|
|
589 |
if (typeof options !== 'undefined' && options.table_prefix) { |
| 590 |
table_prefix = options.table_prefix; |
| 591 |
// If not, default to 'me' |
| 592 |
} else { |
| 593 |
table_prefix = 'me'; |
| 594 |
} |
| 595 |
|
| 596 |
// Leading wildcard: If search_type option exists, use that |
| 587 |
if (typeof options !== 'undefined' && options.search_type) { |
597 |
if (typeof options !== 'undefined' && options.search_type) { |
| 588 |
leading_wildcard = options.search_type === "contains" ? '%' : ''; |
598 |
leading_wildcard = options.search_type === "contains" ? '%' : ''; |
| 589 |
// If not, we use DefaultPatronSearchMethod system preference instead |
599 |
// If not, use DefaultPatronSearchMethod system preference instead |
| 590 |
} else { |
600 |
} else { |
| 591 |
leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : ''; |
601 |
leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : ''; |
| 592 |
} |
602 |
} |
| 593 |
|
603 |
|
| 594 |
// Search fields: If search_fields option exists, we use that |
604 |
// Search fields: If search_fields option exists, use that |
| 595 |
if (typeof options !== 'undefined' && options.search_fields) { |
605 |
if (typeof options !== 'undefined' && options.search_fields) { |
| 596 |
search_fields = expandPatronSearchFields(options.search_fields); |
606 |
search_fields = expandPatronSearchFields(options.search_fields); |
| 597 |
// If not, we use DefaultPatronSearchFields system preference instead |
607 |
// If not, we use DefaultPatronSearchFields system preference instead |
|
Lines 605-616
function buildPatronSearchQuery(term, options) {
Link Here
|
| 605 |
let pattern_subquery_or = []; |
615 |
let pattern_subquery_or = []; |
| 606 |
search_fields.split('\|').forEach(function (field, i) { |
616 |
search_fields.split('\|').forEach(function (field, i) { |
| 607 |
pattern_subquery_or.push( |
617 |
pattern_subquery_or.push( |
| 608 |
{ ["me." + field]: { 'like': leading_wildcard + pattern + '%' } } |
618 |
{ [table_prefix + "." + field]: { 'like': leading_wildcard + pattern + '%' } } |
| 609 |
); |
619 |
); |
| 610 |
if (field == 'dateofbirth') { |
620 |
if (field == 'dateofbirth') { |
| 611 |
try { |
621 |
try { |
| 612 |
let d = $date_to_rfc3339(pattern); |
622 |
let d = $date_to_rfc3339(pattern); |
| 613 |
pattern_subquery_or.push({ ["me." + field]: d }); |
623 |
pattern_subquery_or.push({ [table_prefix + "." + field]: d }); |
| 614 |
} catch { |
624 |
} catch { |
| 615 |
// Hide the warning if the date is not correct |
625 |
// Hide the warning if the date is not correct |
| 616 |
} |
626 |
} |
|
Lines 624-630
function buildPatronSearchQuery(term, options) {
Link Here
|
| 624 |
let term_subquery_or = []; |
634 |
let term_subquery_or = []; |
| 625 |
search_fields.split('\|').forEach(function (field, i) { |
635 |
search_fields.split('\|').forEach(function (field, i) { |
| 626 |
term_subquery_or.push( |
636 |
term_subquery_or.push( |
| 627 |
{ ["me." + field]: { 'like': leading_wildcard + term + '%' } } |
637 |
{ [table_prefix + "." + field]: { 'like': leading_wildcard + term + '%' } } |
| 628 |
); |
638 |
); |
| 629 |
}); |
639 |
}); |
| 630 |
q.push({ "-or": term_subquery_or }); |
640 |
q.push({ "-or": term_subquery_or }); |
| 631 |
- |
|
|