View | Details | Raw Unified | Return to bug 34092
Collapse All | Expand All

(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/js_includes.inc (+1 lines)
Lines 71-76 Link Here
71
<script>
71
<script>
72
    var defaultPatronSearchFields = "[% Koha.Preference('DefaultPatronSearchFields') || 'firstname,middle_name,surname,othernames,cardnumber,userid' | html %]";
72
    var defaultPatronSearchFields = "[% Koha.Preference('DefaultPatronSearchFields') || 'firstname,middle_name,surname,othernames,cardnumber,userid' | html %]";
73
    var defaultPatronSearchMethod = "[% Koha.Preference('DefaultPatronSearchMethod') || 'contains' | html %]";
73
    var defaultPatronSearchMethod = "[% Koha.Preference('DefaultPatronSearchMethod') || 'contains' | html %]";
74
    var extendedPatronAttributes = "[% Koha.Preference('ExtendedPatronAttributes') | html %]";
74
    var loggedInLibrary = '[% Branches.GetLoggedInBranchcode | html %]';
75
    var loggedInLibrary = '[% Branches.GetLoggedInBranchcode | html %]';
75
    var singleBranchMode = '[% singleBranchMode | html %]';
76
    var singleBranchMode = '[% singleBranchMode | html %]';
76
</script>
77
</script>
(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc (-47 / +16 lines)
Lines 19-24 Link Here
19
[%# You can then pass a list of filters %]
19
[%# You can then pass a list of filters %]
20
[%# - branch: select library list %]
20
[%# - branch: select library list %]
21
[%# - category: select patron category list %]
21
[%# - category: select patron category list %]
22
[%# - sort1: select patron sort1 field %]
23
[%# - sort2: select patron sort2 field %]
22
[%# - search_field: select patron field list %]
24
[%# - search_field: select patron field list %]
23
[%# - search_type: select 'contains' or 'starts with' %]
25
[%# - search_type: select 'contains' or 'starts with' %]
24
[%- SET searchtype = Koha.Preference('DefaultPatronSearchMethod') -%]
26
[%- SET searchtype = Koha.Preference('DefaultPatronSearchMethod') -%]
Lines 316-321 Link Here
316
                },
318
                },
317
                "-and": function(){
319
                "-and": function(){
318
                    let filters = [];
320
                    let filters = [];
321
322
                    let search_type = $("#searchtype_filter").val() || "contains";
323
                    let search_fields = $("#searchfieldstype_filter").val();
324
                    let pattern = $("#search_patron_filter").val();
325
326
                    filters = buildPatronSearchQuery(
327
                        pattern,
328
                        {
329
                            search_type: search_type,
330
                            search_fields: search_fields,
331
                            ...(typeof extended_attribute_types != 'undefined' && {extended_attribute_types: extended_attribute_types})
332
                        }
333
                    );
334
319
                    let f_sort1 = $("#sort1_filter").val();
335
                    let f_sort1 = $("#sort1_filter").val();
320
                    if ( f_sort1 ) {
336
                    if ( f_sort1 ) {
321
                        filters.push({
337
                        filters.push({
Lines 329-381 Link Here
329
                        });
345
                        });
330
                    }
346
                    }
331
347
332
                    let pattern = $("#search_patron_filter").val();
333
                    if (!pattern) {
334
                        if ( filters.length == 0 ) {
335
                            return "";
336
                        }
337
                        else {
338
                            return filters;
339
                        }
340
                    }
341
                    let patterns = pattern.split(/[\s,]+/).filter(function(s){ return s.length });
342
343
                    let search_type = $("#searchtype_filter").val() || "contains";
344
                    let search_fields = $("#searchfieldstype_filter").val();
345
                    if ( !search_fields ) {
346
                        search_fields = "[% Koha.Preference('DefaultPatronSearchFields') || 'firstname,middle_name,surname,othernames,cardnumber,userid' | html %]";
347
                    }
348
349
                    let subquery_and = [];
350
                    patterns.forEach(function(pattern,i){
351
                        let sub_or = [];
352
                        search_fields.split(',').forEach(function(attr,ii){
353
                            sub_or.push({["me."+attr]:{"like":(search_type == "contains" ? "%" : "" ) + pattern + "%"}});
354
                            if ( attr == 'dateofbirth' ) {
355
                                try {
356
                                    let d = $date_to_rfc3339(pattern);
357
                                    sub_or.push({["me."+attr]:d});
358
                                } catch {
359
                                    // Hide the warning if the date is not correct
360
                                }
361
                            }
362
                        });
363
                        subquery_and.push(sub_or);
364
                    });
365
                    filters.push({"-and": subquery_and});
366
367
                    [% IF Koha.Preference('ExtendedPatronAttributes') && extended_attribute_types %]
368
                        subquery_and = [];
369
                        patterns.forEach(function(pattern,i){
370
                            let sub_or = [];
371
                            sub_or.push({
372
                                "extended_attributes.value": { "like": "%" + pattern + (search_type == "contains" ? "%" : "" )},
373
                                "extended_attributes.code": extended_attribute_types
374
                            });
375
                            subquery_and.push(sub_or);
376
                        });
377
                        filters.push({"-and": subquery_and});
378
                    [% END %]
379
                    return filters;
348
                    return filters;
380
                }
349
                }
381
            };
350
            };
(-)a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js (-8 / +55 lines)
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
- 

Return to bug 34092