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 '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 (-8 / +54 lines)
Lines 510-532 function patron_autocomplete(node, options) { Link Here
510
        };
510
        };
511
}
511
}
512
512
513
513
/**
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 'contain' or 'start_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
 */
514
function buildPatronSearchQuery(term, options) {
521
function buildPatronSearchQuery(term, options) {
515
522
516
    let q = [];
523
    let q = [];
517
    let leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : '';
524
    let leading_wildcard;
518
    let search_fields = defaultPatronSearchFields;
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 === "contain" ? '%' : '';
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
    }
519
548
520
    // Add each pattern for each search field
549
    // Add each pattern for each search field
521
    let pattern_subquery_and = [];
550
    let pattern_subquery_and = [];
522
    term.split(/[\s,]+/)
551
    patterns.forEach(function (pattern, i) {
523
        .filter(function (s) { return s.length })
524
        .forEach(function (pattern, i) {
525
            let pattern_subquery_or = [];
552
            let pattern_subquery_or = [];
526
            search_fields.split(',').forEach(function (field, i) {
553
            search_fields.split(',').forEach(function (field, i) {
527
                pattern_subquery_or.push(
554
                pattern_subquery_or.push(
528
                    { ["me." + field]: { 'like': leading_wildcard + pattern + '%' } }
555
                    { ["me." + field]: { 'like': leading_wildcard + pattern + '%' } }
529
                );
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
                }
530
            });
565
            });
531
            pattern_subquery_and.push(pattern_subquery_or);
566
            pattern_subquery_and.push(pattern_subquery_or);
532
        });
567
        });
Lines 541-546 function buildPatronSearchQuery(term, options) { Link Here
541
    });
576
    });
542
    q.push({ "-or": term_subquery_or });
577
    q.push({ "-or": term_subquery_or });
543
578
544
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
    }
545
    return q;
592
    return q;
546
}
593
}
547
- 

Return to bug 34092