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 29-35 Link Here
29
[% INCLUDE 'js-date-format.inc' %]
29
[% INCLUDE 'js-date-format.inc' %]
30
[% INCLUDE 'js-patron-get-age.inc' %]
30
[% INCLUDE 'js-patron-get-age.inc' %]
31
[% INCLUDE 'js-patron-format-address.inc' %]
31
[% INCLUDE 'js-patron-format-address.inc' %]
32
[% Asset.js("js/patron-autocomplete.js") | $raw %]
33
32
34
[% INCLUDE 'validator-strings.inc' %]
33
[% INCLUDE 'validator-strings.inc' %]
35
[% IF ( IntranetUserJS ) %]
34
[% IF ( IntranetUserJS ) %]
(-)a/koha-tmpl/intranet-tmpl/prog/js/patron-autocomplete.js (-134 lines)
Lines 1-134 Link Here
1
function patron_autocomplete(node, options) {
2
    let link_to;
3
    let url_params;
4
    let on_select_callback;
5
    let leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : '';
6
    if ( options ) {
7
        if ( options['link-to'] ) {
8
            link_to = options['link-to'];
9
        }
10
        if ( options['url-params'] ) {
11
            url_params = options['url-params'];
12
        }
13
        if ( options['on-select-callback'] ) {
14
            on_select_callback = options['on-select-callback'];
15
        }
16
    }
17
    return node.autocomplete({
18
        source: function( request, response ) {
19
            let q = []
20
21
            // Add each pattern in search term for each search field
22
            let pattern_subquery_and = [];
23
            request.term.split(/[\s,]+/)
24
                .filter(function(s){ return s.length })
25
                .forEach(function(pattern,i){
26
                    let pattern_subquery_or = [];
27
                    defaultPatronSearchFields.split(',').forEach(function(field,i){
28
                        pattern_subquery_or.push(
29
                            { ["me." + field]: { 'like': leading_wildcard + pattern + '%' } }
30
                        );
31
                    });
32
                    pattern_subquery_and.push(pattern_subquery_or);
33
                });
34
            q.push({ "-and": pattern_subquery_and });
35
36
            // Add full search term for each search field
37
            let term_subquery_or = [];
38
            defaultPatronSearchFields.split(',').forEach(function (field, i) {
39
                term_subquery_or.push(
40
                    { ["me." + field]: { 'like': leading_wildcard + request.term + '%' } }
41
                );
42
            });
43
            q.push({ "-or": term_subquery_or });
44
45
            let params = {
46
                '_page': 1,
47
                '_per_page': 10,
48
                'q': JSON.stringify(q),
49
                '_order_by': '+me.surname,+me.firstname',
50
            };
51
            $.ajax({
52
                data: params,
53
                type: 'GET',
54
                url: '/api/v1/patrons',
55
                headers: {
56
                    "x-koha-embed": "library"
57
                },
58
                success: function(data) {
59
                    return response(data);
60
                },
61
                error: function(e) {
62
                    if ( e.state() != 'rejected' ) {
63
                        alert( __("An error occurred. Check the logs") );
64
                    }
65
                    return response();
66
                }
67
            });
68
        },
69
        minLength: 3,
70
        select: function( event, ui ) {
71
            if ( ui.item.link ) {
72
                window.location.href = ui.item.link;
73
            } else if ( on_select_callback ) {
74
                return on_select_callback(event, ui);
75
            }
76
        },
77
        focus: function( event, ui ) {
78
            event.preventDefault(); // Don't replace the text field
79
        },
80
    })
81
    .data( "ui-autocomplete" )
82
    ._renderItem = function( ul, item ) {
83
        if ( link_to ) {
84
            item.link = link_to == 'circ'
85
                ? "/cgi-bin/koha/circ/circulation.pl"
86
                : link_to == 'reserve'
87
                    ? "/cgi-bin/koha/reserve/request.pl"
88
                    : "/cgi-bin/koha/members/moremember.pl";
89
            item.link += ( url_params ? '?' + url_params + '&' : "?" ) + 'borrowernumber=' + item.patron_id;
90
        } else {
91
            item.link = null;
92
        }
93
94
        var cardnumber = "";
95
        if( item.cardnumber != "" ){
96
            // Display card number in parentheses if it exists
97
            cardnumber = " (" + item.cardnumber + ") ";
98
        }
99
        if( item.library_id == loggedInLibrary ){
100
            loggedInClass = "ac-currentlibrary";
101
        } else {
102
            loggedInClass = "";
103
        }
104
        return $( "<li></li>" )
105
        .addClass( loggedInClass )
106
        .data( "ui-autocomplete-item", item )
107
        .append(
108
            ""
109
            + ( item.link ? "<a href=\"" + item.link + "\">" : "<a>" )
110
                + ( item.surname ? item.surname.escapeHtml() : "" ) + ", "
111
                + ( item.firstname ? item.firstname.escapeHtml() : "" )
112
                + cardnumber.escapeHtml()
113
                + " <small>"
114
                    + ( item.date_of_birth
115
                        ?   $date(item.date_of_birth)
116
                          + "<span class=\"age_years\"> ("
117
                          + $get_age(item.date_of_birth)
118
                          + " "
119
                          + __("years")
120
                          + ")</span>,"
121
                        : ""
122
                    ) + " "
123
                    + $format_address(item, { no_line_break: true, include_li: false }) + " "
124
                    + ( !singleBranchMode
125
                        ?
126
                              "<span class=\"ac-library\">"
127
                            + item.library.name.escapeHtml()
128
                            + "</span>"
129
                        : "" )
130
                + "</small>"
131
            + "</a>" )
132
        .appendTo( ul );
133
    };
134
}
(-)a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js (-1 / +135 lines)
Lines 398-400 function saveOrClearSimpleSearchParams() { Link Here
398
    localStorage.setItem('cat_search_pulldown_selection', pulldown_selection );
398
    localStorage.setItem('cat_search_pulldown_selection', pulldown_selection );
399
    localStorage.setItem('searchbox_value', searchbox_value );
399
    localStorage.setItem('searchbox_value', searchbox_value );
400
}
400
}
401
- 
401
402
function patron_autocomplete(node, options) {
403
    let link_to;
404
    let url_params;
405
    let on_select_callback;
406
    let leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : '';
407
    if (options) {
408
        if (options['link-to']) {
409
            link_to = options['link-to'];
410
        }
411
        if (options['url-params']) {
412
            url_params = options['url-params'];
413
        }
414
        if (options['on-select-callback']) {
415
            on_select_callback = options['on-select-callback'];
416
        }
417
    }
418
    return node.autocomplete({
419
        source: function (request, response) {
420
            let q = []
421
422
            // Add each pattern in search term for each search field
423
            let pattern_subquery_and = [];
424
            request.term.split(/[\s,]+/)
425
                .filter(function (s) { return s.length })
426
                .forEach(function (pattern, i) {
427
                    let pattern_subquery_or = [];
428
                    defaultPatronSearchFields.split(',').forEach(function (field, i) {
429
                        pattern_subquery_or.push(
430
                            { ["me." + field]: { 'like': leading_wildcard + pattern + '%' } }
431
                        );
432
                    });
433
                    pattern_subquery_and.push(pattern_subquery_or);
434
                });
435
            q.push({ "-and": pattern_subquery_and });
436
437
            // Add full search term for each search field
438
            let term_subquery_or = [];
439
            defaultPatronSearchFields.split(',').forEach(function (field, i) {
440
                term_subquery_or.push(
441
                    { ["me." + field]: { 'like': leading_wildcard + request.term + '%' } }
442
                );
443
            });
444
            q.push({ "-or": term_subquery_or });
445
446
            let params = {
447
                '_page': 1,
448
                '_per_page': 10,
449
                'q': JSON.stringify(q),
450
                '_order_by': '+me.surname,+me.firstname',
451
            };
452
            $.ajax({
453
                data: params,
454
                type: 'GET',
455
                url: '/api/v1/patrons',
456
                headers: {
457
                    "x-koha-embed": "library"
458
                },
459
                success: function (data) {
460
                    return response(data);
461
                },
462
                error: function (e) {
463
                    if (e.state() != 'rejected') {
464
                        alert(__("An error occurred. Check the logs"));
465
                    }
466
                    return response();
467
                }
468
            });
469
        },
470
        minLength: 3,
471
        select: function (event, ui) {
472
            if (ui.item.link) {
473
                window.location.href = ui.item.link;
474
            } else if (on_select_callback) {
475
                return on_select_callback(event, ui);
476
            }
477
        },
478
        focus: function (event, ui) {
479
            event.preventDefault(); // Don't replace the text field
480
        },
481
    })
482
        .data("ui-autocomplete")
483
        ._renderItem = function (ul, item) {
484
            if (link_to) {
485
                item.link = link_to == 'circ'
486
                    ? "/cgi-bin/koha/circ/circulation.pl"
487
                    : link_to == 'reserve'
488
                        ? "/cgi-bin/koha/reserve/request.pl"
489
                        : "/cgi-bin/koha/members/moremember.pl";
490
                item.link += (url_params ? '?' + url_params + '&' : "?") + 'borrowernumber=' + item.patron_id;
491
            } else {
492
                item.link = null;
493
            }
494
495
            var cardnumber = "";
496
            if (item.cardnumber != "") {
497
                // Display card number in parentheses if it exists
498
                cardnumber = " (" + item.cardnumber + ") ";
499
            }
500
            if (item.library_id == loggedInLibrary) {
501
                loggedInClass = "ac-currentlibrary";
502
            } else {
503
                loggedInClass = "";
504
            }
505
            return $("<li></li>")
506
                .addClass(loggedInClass)
507
                .data("ui-autocomplete-item", item)
508
                .append(
509
                    ""
510
                    + (item.link ? "<a href=\"" + item.link + "\">" : "<a>")
511
                    + (item.surname ? item.surname.escapeHtml() : "") + ", "
512
                    + (item.firstname ? item.firstname.escapeHtml() : "")
513
                    + cardnumber.escapeHtml()
514
                    + " <small>"
515
                    + (item.date_of_birth
516
                        ? $date(item.date_of_birth)
517
                        + "<span class=\"age_years\"> ("
518
                        + $get_age(item.date_of_birth)
519
                        + " "
520
                        + __("years")
521
                        + ")</span>,"
522
                        : ""
523
                    ) + " "
524
                    + $format_address(item, { no_line_break: true, include_li: false }) + " "
525
                    + (!singleBranchMode
526
                        ?
527
                        "<span class=\"ac-library\">"
528
                        + item.library.name.escapeHtml()
529
                        + "</span>"
530
                        : "")
531
                    + "</small>"
532
                    + "</a>")
533
                .appendTo(ul);
534
        };
535
}

Return to bug 34092