Lines 510-531
function patron_autocomplete(node, options) {
Link Here
|
510 |
}; |
510 |
}; |
511 |
} |
511 |
} |
512 |
|
512 |
|
513 |
|
513 |
/** |
514 |
function buildPatronSearchQuery(term) { |
514 |
* Build patron search query |
|
|
515 |
* - term: The full search term input by the user |
516 |
* You can then pass a list of options: |
517 |
* - search_type: String 'contains' or 'starts_with', defaults to DefaultPatronSearchMethod system preference |
518 |
* - search_fields: String comma-separated list of specific fields, defaults to DefaultPatronSearchFields system preference |
519 |
* - extended_attribute_types: JSON object containing the patron attribute types to be searched on |
520 |
*/ |
521 |
function buildPatronSearchQuery(term, options) { |
515 |
|
522 |
|
516 |
let q = []; |
523 |
let q = []; |
517 |
let leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : ''; |
524 |
let leading_wildcard; |
|
|
525 |
let search_fields; |
526 |
let patterns = term.split(/[\s,]+/).filter(function (s) { return s.length }); |
527 |
|
528 |
// Bail if no patterns |
529 |
if (patterns.length == 0) { |
530 |
return; |
531 |
} |
532 |
|
533 |
// Leading wildcard: If search_type option exists, we use that |
534 |
if (typeof options !== 'undefined' && options.search_type) { |
535 |
leading_wildcard = options.search_type === "contains" ? '%' : ''; |
536 |
// If not, we use DefaultPatronSearchMethod system preference instead |
537 |
} else { |
538 |
leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : ''; |
539 |
} |
540 |
|
541 |
// Search fields: If search_fields option exists, we use that |
542 |
if (typeof options !== 'undefined' && options.search_fields) { |
543 |
search_fields = options.search_fields; |
544 |
// If not, we use DefaultPatronSearchFields system preference instead |
545 |
} else { |
546 |
search_fields = defaultPatronSearchFields; |
547 |
} |
518 |
|
548 |
|
519 |
// Add each pattern for each search field |
549 |
// Add each pattern for each search field |
520 |
let pattern_subquery_and = []; |
550 |
let pattern_subquery_and = []; |
521 |
term.split(/[\s,]+/) |
551 |
patterns.forEach(function (pattern, i) { |
522 |
.filter(function (s) { return s.length }) |
|
|
523 |
.forEach(function (pattern, i) { |
524 |
let pattern_subquery_or = []; |
552 |
let pattern_subquery_or = []; |
525 |
defaultPatronSearchFields.split(',').forEach(function (field, i) { |
553 |
defaultPatronSearchFields.split(',').forEach(function (field, i) { |
526 |
pattern_subquery_or.push( |
554 |
pattern_subquery_or.push( |
527 |
{ ["me." + field]: { 'like': leading_wildcard + pattern + '%' } } |
555 |
{ ["me." + field]: { 'like': leading_wildcard + pattern + '%' } } |
528 |
); |
556 |
); |
|
|
557 |
if (field == 'dateofbirth') { |
558 |
try { |
559 |
let d = $date_to_rfc3339(pattern); |
560 |
pattern_subquery_or.push({ ["me." + field]: d }); |
561 |
} catch { |
562 |
// Hide the warning if the date is not correct |
563 |
} |
564 |
} |
529 |
}); |
565 |
}); |
530 |
pattern_subquery_and.push(pattern_subquery_or); |
566 |
pattern_subquery_and.push(pattern_subquery_or); |
531 |
}); |
567 |
}); |
Lines 540-545
function buildPatronSearchQuery(term) {
Link Here
|
540 |
}); |
576 |
}); |
541 |
q.push({ "-or": term_subquery_or }); |
577 |
q.push({ "-or": term_subquery_or }); |
542 |
|
578 |
|
543 |
|
579 |
// Add each pattern for each extended patron attributes |
|
|
580 |
if (typeof options !== 'undefined' && options.extended_attribute_types && extendedPatronAttributes) { |
581 |
extended_attribute_subquery_and = []; |
582 |
patterns.forEach(function (pattern, i) { |
583 |
let extended_attribute_sub_or = []; |
584 |
extended_attribute_sub_or.push({ |
585 |
"extended_attributes.value": { "like": leading_wildcard + pattern + '%' }, |
586 |
"extended_attributes.code": options.extended_attribute_types |
587 |
}); |
588 |
extended_attribute_subquery_and.push(extended_attribute_sub_or); |
589 |
}); |
590 |
q.push({ "-and": extended_attribute_subquery_and }); |
591 |
} |
544 |
return q; |
592 |
return q; |
545 |
} |
593 |
} |
546 |
- |
|
|