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('DefaultPatronSearchMethod') | 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 'contain' or 'start with' %]
25
[%# - search_type: select 'contain' or 'start with' %]
24
[% BLOCK patron_search_filters %]
26
[% BLOCK patron_search_filters %]
Lines 315-320 Link Here
315
                },
317
                },
316
                "-and": function(){
318
                "-and": function(){
317
                    let filters = [];
319
                    let filters = [];
320
321
                    let search_type = $("#searchtype_filter").val() || "contain";
322
                    let search_fields = $("#searchfieldstype_filter").val();
323
                    let pattern = $("#search_patron_filter").val();
324
325
                    filters = buildPatronSearchQuery(
326
                        pattern,
327
                        {
328
                            search_type: search_type,
329
                            search_fields: search_fields,
330
                            extended_attribute_types: extended_attribute_types
331
                        }
332
                    );
333
318
                    let f_sort1 = $("#sort1_filter").val();
334
                    let f_sort1 = $("#sort1_filter").val();
319
                    if ( f_sort1 ) {
335
                    if ( f_sort1 ) {
320
                        filters.push({
336
                        filters.push({
Lines 328-380 Link Here
328
                        });
344
                        });
329
                    }
345
                    }
330
346
331
                    let pattern = $("#search_patron_filter").val();
332
                    if (!pattern) {
333
                        if ( filters.length == 0 ) {
334
                            return "";
335
                        }
336
                        else {
337
                            return filters;
338
                        }
339
                    }
340
                    let patterns = pattern.split(/[\s,]+/).filter(function(s){ return s.length });
341
342
                    let search_type = $("#searchtype_filter").val() || "contain";
343
                    let search_fields = $("#searchfieldstype_filter").val();
344
                    if ( !search_fields ) {
345
                        search_fields = "[% Koha.Preference('DefaultPatronSearchFields') || 'firstname,middle_name,surname,othernames,cardnumber,userid' | html %]";
346
                    }
347
348
                    let subquery_and = [];
349
                    patterns.forEach(function(pattern,i){
350
                        let sub_or = [];
351
                        search_fields.split(',').forEach(function(attr,ii){
352
                            sub_or.push({["me."+attr]:{"like":(search_type == "contain" ? "%" : "" ) + pattern + "%"}});
353
                            if ( attr == 'dateofbirth' ) {
354
                                try {
355
                                    let d = $date_to_rfc3339(pattern);
356
                                    sub_or.push({["me."+attr]:d});
357
                                } catch {
358
                                    // Hide the warning if the date is not correct
359
                                }
360
                            }
361
                        });
362
                        subquery_and.push(sub_or);
363
                    });
364
                    filters.push({"-and": subquery_and});
365
366
                    [% IF Koha.Preference('ExtendedPatronAttributes') && extended_attribute_types %]
367
                        subquery_and = [];
368
                        patterns.forEach(function(pattern,i){
369
                            let sub_or = [];
370
                            sub_or.push({
371
                                "extended_attributes.value": { "like": "%" + pattern + (search_type == "contain" ? "%" : "" )},
372
                                "extended_attributes.code": extended_attribute_types
373
                            });
374
                            subquery_and.push(sub_or);
375
                        });
376
                        filters.push({"-and": subquery_and});
377
                    [% END %]
378
                    return filters;
347
                    return filters;
379
                }
348
                }
380
            };
349
            };
(-)a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js (-7 / +46 lines)
Lines 514-532 function patron_autocomplete(node, options) { Link Here
514
function buildPatronSearchQuery(term, options) {
514
function buildPatronSearchQuery(term, options) {
515
515
516
    let q = [];
516
    let q = [];
517
    let leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : '';
517
    let leading_wildcard;
518
    let search_fields = defaultPatronSearchFields;
518
    let search_fields;
519
    let patterns = term.split(/[\s,]+/).filter(function (s) { return s.length });
520
521
    // Bail if no patterns
522
    if (patterns.length == 0) {
523
        return;
524
    }
525
526
    // Leading wildcard: If search_type option exists, we use that
527
    if (typeof options !== 'undefined' && options.search_type) {
528
        leading_wildcard = options.search_type === "contain" ? '%' : '';
529
    // If not, we use DefaultPatronSearchMethod system preference instead
530
    } else {
531
        leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : '';
532
    }
533
534
    // Search fields: If search_fields option exists, we use that
535
    if (typeof options !== 'undefined' && options.search_fields) {
536
        search_fields = options.search_fields;
537
    // If not, we use DefaultPatronSearchFields system preference instead
538
    } else {
539
        search_fields = defaultPatronSearchFields;
540
    }
519
541
520
    // Add each pattern for each search field
542
    // Add each pattern for each search field
521
    let pattern_subquery_and = [];
543
    let pattern_subquery_and = [];
522
    term.split(/[\s,]+/)
544
    patterns.forEach(function (pattern, i) {
523
        .filter(function (s) { return s.length })
524
        .forEach(function (pattern, i) {
525
            let pattern_subquery_or = [];
545
            let pattern_subquery_or = [];
526
            search_fields.split(',').forEach(function (field, i) {
546
            search_fields.split(',').forEach(function (field, i) {
527
                pattern_subquery_or.push(
547
                pattern_subquery_or.push(
528
                    { ["me." + field]: { 'like': leading_wildcard + pattern + '%' } }
548
                    { ["me." + field]: { 'like': leading_wildcard + pattern + '%' } }
529
                );
549
                );
550
                if (field == 'dateofbirth') {
551
                    try {
552
                        let d = $date_to_rfc3339(pattern);
553
                        pattern_subquery_or.push({ ["me." + field]: d });
554
                    } catch {
555
                        // Hide the warning if the date is not correct
556
                    }
557
                }
530
            });
558
            });
531
            pattern_subquery_and.push(pattern_subquery_or);
559
            pattern_subquery_and.push(pattern_subquery_or);
532
        });
560
        });
Lines 541-546 function buildPatronSearchQuery(term, options) { Link Here
541
    });
569
    });
542
    q.push({ "-or": term_subquery_or });
570
    q.push({ "-or": term_subquery_or });
543
571
544
572
    // Add each pattern for each extended patron attributes
573
    if (typeof options !== 'undefined' && options.extended_attribute_types && extendedPatronAttributes) {
574
        extended_attribute_subquery_and = [];
575
        patterns.forEach(function (pattern, i) {
576
            let extended_attribute_sub_or = [];
577
            extended_attribute_sub_or.push({
578
                "extended_attributes.value": { "like": leading_wildcard + pattern + '%' },
579
                "extended_attributes.code": extended_attribute_types
580
            });
581
            extended_attribute_subquery_and.push(extended_attribute_sub_or);
582
        });
583
        q.push({ "-and": extended_attribute_subquery_and });
584
    }
545
    return q;
585
    return q;
546
}
586
}
547
- 

Return to bug 34092