Bug 30063: Add kind of POD for the include file
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / includes / patron-search.inc
1 [% USE Koha %]
2 [% USE I18N %]
3 [% USE Branches %]
4 [% USE raw %]
5 [% USE Asset %]
6 [% USE To %]
7
8 [%# Display a simple form %]
9 [% BLOCK patron_search_filters_simple  %]
10     <form id="patron_search_form">
11         <div class="hint">Enter patron card number or partial name:</div>
12         <input type="text" size="40" id="search_patron_filter" class="focus" autocomplete="off" />
13         <input type="submit" value="Search" />
14     </form>
15 [% END %]
16
17 [%# Display a complex patron search form %]
18 [%# - Search: <input> %]
19 [%# You can then pass a list of filters %]
20 [%# - branch: <select library list> %]
21 [%# - category: <select patron category list> %]
22 [%# - search_field: <select patron field list> %]
23 [%# - search_type: <select "contain" or "start with"> %]
24 [% BLOCK patron_search_filters %]
25     <form id="patron_search_form">
26         <fieldset class="brief">
27             <h3>Search for patron</h3>
28             <ol>
29                 <li>
30                     <label for="search_patron_filter">Search:</label>
31                     <input type="text" id="search_patron_filter" value="[% search_filter | html %]" class="focus" />
32                 </li>
33
34                 [% FOR f IN filters %]
35                     [% SWITCH f %]
36                     [% CASE 'branch' %]
37                         <li>
38                             <label for="branchcode_filter">Library:</label>
39                             <select id="branchcode_filter">
40                                 [% SET libraries = Branches.all( only_from_group => 1 ) %]
41                                 [% IF libraries.size != 1 %]
42                                     <option value="">Any</option>
43                                 [% END %]
44                                 [% FOREACH l IN libraries %]
45                                     <option value="[% l.branchcode | html %]">[% l.branchname | html %]</option>
46                                 [% END %]
47                             </select>
48                         </li>
49                     [% CASE 'category' %]
50                         <li>
51                             <label for="categorycode_filter">Category:</label>
52                             <select id="categorycode_filter">
53                                 <option value="">Any</option>
54                                 [% FOREACH category IN categories %]
55                                     <option value="[% category.categorycode | html %]">[% category.description | html %]</option>
56                                 [% END %]
57                             </select>
58                         </li>
59                     [% CASE 'search_field' %]
60                         <li>
61                             <label for="searchfieldstype_filter">Search field:</label>
62                             <select name="searchfieldstype" id="searchfieldstype_filter">
63                                 [% pref_fields = Koha.Preference('DefaultPatronSearchFields').split(',') %]
64                                 [% default_fields = [ 'surname,firstname,othernames,cardnumber,userid', 'surname', 'cardnumber', 'email', 'borrowernumber', 'userid', 'phone', 'address', 'dateofbirth', 'sort1', 'sort2' ] %]
65                                 [% search_options = default_fields.merge(pref_fields).unique %]
66                                 [% FOREACH s_o IN search_options %]
67                                     [% display_name = PROCESS patron_fields name=s_o %]
68                                     [% NEXT IF !display_name %]
69                                     [% IF searchfieldstype == s_o %]
70                                         <option selected="selected" value=[% s_o | html %]>[% display_name | $raw %]</option>
71                                     [% ELSE %]
72                                         <option value=[% s_o | html %]>[% display_name | $raw %]</option>
73                                     [% END %]
74                                 [% END %]
75                             </select>
76                         </li>
77                     [% CASE 'search_type' %]
78                         <li>
79                             <label for="searchtype_filter">Search type:</label>
80                             <select name="searchtype" id="searchtype_filter">
81                               [% IF searchtype == "start_with" %]
82                                 <option value='start_with' selected="selected">Starts with</option>
83                                 <option value="contain">Contains</option>
84                               [% ELSE %]
85                                 <option value='start_with'>Starts with</option>
86                                 <option value="contain" selected="selected">Contains</option>
87                               [% END %]
88                             </select>
89                         </li>
90                     [% END %]
91                 [% END %]
92             </ol>
93             <fieldset class="action">
94                 <input type="submit" value="Search" />
95                 <input type="button" value="Clear" id="clear_search" />
96             </fieldset>
97         </fieldset>
98     </form>
99 [% END %]
100
101 [%# Display the table with: %]
102 [%# - At the top a hint about a possible filter %]
103 [%# - Browse by last name %]
104 [%# - The table %]
105 [%# Get the following parameters: %]
106 [%# - filter: can be 'suggestions_managers', 'orders_managers', 'funds_owners' or 'funds_users' to filter patrons on their permissions %]
107 [%# - table_id: the ID of the table %]
108 [%# open_on_row_click: See patron_search_js %]
109 [%# columns: See patron_search_js %]
110 [% BLOCK patron_search_table %]
111
112     [% IF filter == 'suggestions_managers' %]
113         <div class="hint">Only staff with superlibrarian or suggestions_manage permissions are returned in the search results</div>
114     [% ELSIF filter == 'orders_managers' %]
115         <div class="hint">Only staff with superlibrarian or acquisitions permissions (or order_manage permission if granular permissions are enabled) are returned in the search results</div>
116     [% ELSIF filter == 'funds_owners' OR filter == 'funds_users' %]
117         <div class="hint">Only staff with superlibrarian or acquisitions permissions (or budget_modify permission if granular permissions are enabled) are returned in the search results</div>
118     [% END %]
119
120     <div class="browse">
121         Browse by last name:
122         [% SET alphabet = Koha.Preference('alphabet').split(' ') %]
123         [% UNLESS alphabet.size %]
124             [% alphabet = ['A' .. 'Z'] %]
125         [% END %]
126         [% FOREACH letter IN alphabet %]
127             <a href="#" class="filterByLetter">[% letter | html %]</a>
128         [% END %]
129     </div>
130
131
132     <h3 style="display: none;">Patrons found for: <span id="searchpattern"></span></h3>
133
134     <div id="[% table_id | html %]_search_results" style="display:none;">
135
136         <div id="info" class="dialog message" style="display: none;"></div>
137         <div id="error" class="dialog alert" style="display: none;"></div>
138
139         <input type="hidden" id="firstletter_filter" value="" />
140         [% IF open_on_row_click %]
141         <table id="[% table_id | html %]" class="selections-table">
142         [% ELSE %]
143         <table id="[% table_id | html %]">
144         [% END %]
145             <thead>
146                 <tr>
147                     [% FOR column IN columns %]
148                         [% SWITCH column %]
149                             [% CASE 'checkbox' %]<th class="noExport"></th>
150                             [% CASE 'cardnumber' %]<th>Card</th>
151                             [% CASE 'dateofbirth' %]<th>Date of birth</th>
152                             [% CASE 'name' %]<th>Name</th>
153                             [% CASE 'name-address' %]<th>Name</th>
154                             [% CASE 'address' %]<th>Address</th>
155                             [% CASE 'address-library' %]<th>Address</th>
156                             [% CASE 'branch' %]<th data-filter="libraries">Library</th>
157                             [% CASE 'category' %]<th data-filter="categories">Category</th>
158                             [% CASE 'dateexpiry' %]<th>Expires on</td>
159                             [% CASE 'borrowernotes' %]<th>Notes</th>
160                             [% CASE 'phone' %]<th>Phone</th>
161                             [% CASE 'checkouts' %]<th>Checkouts</th>
162                             [% CASE 'account_balance' %]<th>Fines</th>
163                             [% CASE 'action' %]<th>&nbsp;</th>
164                         [% END %]
165                     [% END %]
166                 </tr>
167               </thead>
168             <tbody></tbody>
169         </table>
170     </div>
171
172 <!-- Patron preview modal -->
173 <div class="modal" id="patronPreview" tabindex="-1" role="dialog" aria-labelledby="patronPreviewLabel">
174     <div class="modal-dialog" role="document">
175         <div class="modal-content">
176             <div class="modal-header">
177                 <button type="button" class="closebtn" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
178                 <h4 class="modal-title" id="patronPreviewLabel"></h4>
179             </div>
180             <div class="modal-body">
181                 <div id="loading">
182                     <img src="[% interface | html %]/[% theme | html %]/img/spinner-small.gif" alt="" /> Loading
183                 </div>
184             </div>
185             <div class="modal-footer">
186                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
187             </div>
188         </div>
189     </div>
190 </div>
191
192 [% END %]
193
194 [%# Integrate all the JS code, outside of a script tag %]
195 [%# Get the following parameters: %]
196 [%# - redirect_if_one_result: Redirect to the patron if the search returns only one result, note that it will not redirect if filters of the DT are used (this is a feature) %]
197 [%# - redirect_url: The URL to use, the borrowernumber parameter will be added %]
198 [%# filter: Same as patron_search_table %]
199 [%# open_on_row_click: boolean, default off. Will allow to select a patron by clicking on the whole tr element %]
200 [%# columns: list of columns that will be displayed. Possible values are: 'checkbox', 'cardnumber', 'dateofbirth', 'address', 'name', 'name-address', 'branch', 'category', 'dateexpiry', 'borrowernotes, 'phone', 'checkouts', 'account_balance', 'action' %]
201 [%# preview_on_name_click: Open a modal window with patron's info when the name is clicked %]
202 [%# actions: list of buttons to display in the action column. Possible values are: 'select', 'add', 'edit', 'checkout' %]
203 [%# sticky_header and sticky_to: If we need a sticky header %]
204 [%# callback: name of the JS function that will be called when a patron is selected. Only work with action=select %]
205 [% BLOCK patron_search_js %]
206
207     [% IF redirect_if_one_result && !redirect_url %]
208         <script>console.log("Wrong call of patron_searh_js - missing redirect_url");</script>
209     [% END %]
210     <script>
211         let categories = [% To.json(categories) | $raw %];
212         let categories_map = categories.reduce((map, c) => {
213             map[c.categorycode] = c;
214             return map;
215         }, {});
216         let libraries  = [% To.json(libraries) | $raw %];
217         let libraries_map = libraries.reduce((map, l) => {
218             map[l.branchcode] = l;
219             return map;
220         }, {});
221
222         [% IF Koha.Preference('ExtendedPatronAttributes') && extended_attribute_types %]
223             let extended_attribute_types = [% To.json(extended_attribute_types || []) | $raw %];
224         [% END %]
225
226     </script>
227
228     [% INCLUDE 'datatables.inc' %]
229     [% INCLUDE 'js-date-format.inc' %]
230     [% INCLUDE 'js-patron-get-age.inc' %]
231     [% INCLUDE 'js-patron-format.inc' %]
232     [% INCLUDE 'js-patron-format-address.inc' %]
233     [% IF sticky_header %]
234         [% Asset.js("lib/hc-sticky.js") | $raw %]
235     [% END %]
236
237     <script>
238         var first_draw = 0;
239         let patrons_table;
240         var Sticky;
241         var singleBranchMode = '[% singleBranchMode | html %]';
242         let logged_in_library_id = "[% Branches.GetLoggedInBranchcode | html %]";
243
244         /* popstate event triggered by forward and back button. Need to refresh search */
245         window.addEventListener('popstate', (event) => {
246             getSearchByLocation( false );
247         });
248
249         $(document).ready(function(){
250
251             $("#info").hide();
252             $("#error").hide();
253
254             // Build the aLengthMenu
255             var aLengthMenu = [
256                 [% Koha.Preference('PatronsPerPage') | html %], 10, 20, 50, 100, -1
257             ];
258             jQuery.unique(aLengthMenu);
259             aLengthMenu.sort(function( a, b ){
260                 // Put "All" at the end
261                 if ( a == -1 ) {
262                     return 1;
263                 } else if ( b == -1 ) {
264                     return -1;
265                 }
266                 return parseInt(a) < parseInt(b) ? -1 : 1;}
267             );
268             var aLengthMenuLabel = [];
269             $(aLengthMenu).each(function(){
270                 if ( this == -1 ) {
271                     // Label for -1 is "All"
272                     aLengthMenuLabel.push(_("All"));
273                 } else {
274                     aLengthMenuLabel.push(this);
275                 }
276             });
277
278             let additional_filters = {
279                 surname: function(){
280                     let start_with = $("#firstletter_filter").val()
281                     if (!start_with) return "";
282                     return { "like": start_with + "%" }
283                 },
284                 "-and": function(){
285                     let filter = $("#search_patron_filter").val();
286                     if (!filter) return "";
287
288                     let filters = [];
289                     let search_type = $("#searchtype_filter").val() || "contain";
290                     let search_fields = $("#searchfieldstype_filter").val();
291                     if ( !search_fields ) {
292                         search_fields = "[% Koha.Preference('DefaultPatronSearchFields') || 'surname,firstname,othernames,cardnumber,userid' | html %]";
293                     }
294                     search_fields.split(',').forEach(function(e,i){
295                         filters.push({["me."+e]:{"like":"%"+filter+(search_type == "contain" ? "%" : "" )}});
296                     });
297                     [% IF Koha.Preference('ExtendedPatronAttributes') && extended_attribute_types %]
298                         filters.push({
299                             "extended_attributes.value": { "like": "%" + filter + (search_type == "contain" ? "%" : "" )},
300                             "extended_attributes.code": extended_attribute_types
301                         });
302                     [% END %]
303                     return filters;
304                 }
305             };
306             [% UNLESS default_sort_column %]
307                 [% default_sort_column = "name" %]
308             [% END %]
309             [% SET order_column_index = 0 %]
310             [% SET embed = ['extended_attributes'] %]
311             patrons_table = $("#[% table_id | html %]").kohaTable({
312                 "ajax": {
313                     [% SWITCH filter %]
314                     [% CASE 'suggestions_managers' %]
315                         "url": '/api/v1/suggestions/managers',
316                     [% CASE 'baskets_managers' %]
317                         "url": '/api/v1/acquisitions/baskets/managers',
318                     [% CASE 'funds_owners' %]
319                         "url": '/api/v1/acquisitions/funds/owners',
320                     [% CASE 'funds_users' %]
321                         "url": '/api/v1/acquisitions/funds/users',
322                     [% CASE %]
323                         "url": '/api/v1/patrons',
324                     [% END %]
325                     "dataSrc": function ( json ) {
326                         [% IF redirect_if_one_result %]
327                             // redirect if there is only 1 result.
328                             if ( first_draw && json.recordsFiltered == 1 ) {
329                                 let url = '[% redirect_url | url %]'.indexOf("?") != -1
330                                     ? '[% redirect_url | url %]&borrowernumber=' + json.data[0].patron_id
331                                     : '[% redirect_url | url %]?borrowernumber=' + json.data[0].patron_id;
332                                 document.location.href = url;
333                                 return false;
334                             }
335                             first_draw = 0;
336                         [% END %]
337                         return json.data;
338                     }
339                 },
340                 [% IF open_on_row_click OR preview_on_name_click %]
341                 "drawCallback": function( settings ) {
342                     var api = this.api();
343                     var data = api.data();
344                     if ( data.length == 0 ) return;
345
346                     [% IF open_on_row_click %]
347                     $.each($(this).find("tbody tr"), function(index, tr) {
348                         let url = "[% on_click_url | url %]&borrowernumber=" + data[index].patron_id;
349                         $(tr).off('click').on('click', function() {
350                             document.location.href = url;
351                         }).addClass('clickable');
352                         $(tr).find("a.patron_name").attr('href', url);
353                     });
354                     [% END %]
355                     [% IF preview_on_name_click %]
356                     $.each($(this).find("tbody tr"), function(index, tr) {
357                         $(tr).find("a.patron_name").addClass("patron_preview");
358                     });
359                     [% END %]
360                 },
361                 [% END %]
362                 "iDeferLoading": 0,
363                 "columns": [
364                     [% FOR column IN columns %]
365                         [% IF default_sort_column == column %]
366                             [% order_column_index = loop.count - 1%]
367                         [% END %]
368                         [% SWITCH column %]
369                             [% CASE 'checkbox' %]
370                             {
371                                 "data": "patron_id",
372                                 "searchable": false,
373                                 "orderable": false,
374                                 "render": function( data, type, row, meta ) {
375                                     return "<label for='check" + data + "' class='content_hidden'>" + _("Select patron") + "</label><input type='checkbox' id='check" + data + "' class='selection' name='borrowernumber' value='" + data + "' />";
376                                 }
377                             }
378                             [% CASE 'cardnumber' %]
379                             {
380                                 "data": "cardnumber",
381                                 "searchable": true,
382                                 "orderable": true,
383                                 "render": function( data, type, row, meta ) {
384                                     let patron_id = encodeURIComponent(row.patron_id);
385                                     [% IF !open_on_row_click AND CAN_user_circulate_circulate_remaining_permissions %]
386                                         return "<a href=\"/cgi-bin/koha/circ/circulation.pl?borrowernumber=" + patron_id + "\" title=\"[% I18N.t("Check out") | html %]\" class=\"patron_name\" data-borrowernumber=\"" + patron_id + "\" style=\"white-space:nowrap\">" + escape_str(data) + "</a>";
387                                     [% ELSE %]
388                                         return escape_str(data);
389                                     [% END %]
390                                 }
391
392                             }
393                             [% CASE 'dateofbirth' %]
394                             {
395                                 "data": "date_of_birth",
396                                 "searchable": true,
397                                 "orderable": true,
398                                 "render": function( data, type, row, meta ) {
399                                     return data ? escape_str($date(data) + " (" + _("%s years").format($get_age(data)) + ")") : "";
400                                 }
401                             }
402                             [% CASE 'address' %]
403                             {
404                                 "data": "me.street_number:me.address:me.address2:me.city:me.state:me.postal_code:me.country",
405                                 "searchable": true,
406                                 "orderable": true,
407                                  "render": function( data, type, row, meta ) {
408                                     let r = '<div class="address"><ul>';
409                                     r += $format_address(row, { no_line_break: 1 });
410                                     r += '</div></ul>';
411                                     return r;
412                                 }
413                             }
414                             [% CASE 'address-library' %]
415                             {
416                                 "data": "me.street_number:me.address:me.address2:me.city:me.state:me.postal_code:me.country",
417                                 "searchable": true,
418                                 "orderable": true,
419                                 "render": function( data, type, row, meta ) {
420                                     let r = '<div class="address"><ul>';
421                                     r += $format_address(row, { no_line_break: 1 });
422                                     r += '</div></ul>';
423                                     r += " " + escape_str(libraries_map[row.library_id].branchname);
424                                     return r;
425                                 }
426                             }
427                             [% CASE 'name-address' %]
428                             {
429                                 "data": "me.firstname:me.surname:me.othernames:me.street_number:me.address:me.address2:me.city:me.state:me.postal_code:me.country",
430                                 "searchable": true,
431                                 "orderable": true,
432                                 "render": function( data, type, row, meta ) {
433                                     let patron_id = encodeURIComponent(row.patron_id);
434                                     let r = '';
435                                     [% IF ! open_on_row_click %]
436                                     r += "<a href=\"/cgi-bin/koha/members/moremember.pl?borrowernumber=" + patron_id + "\" class=\"patron_name\" data-borrowernumber=\"" + patron_id + "\" style=\"white-space:nowrap\">" + $patron_to_html(row, { invert_name: 1 }) + "</a>";
437                                     [% ELSE %]
438                                     r += $patron_to_html(row, { invert_name: 1 });
439                                     [% END %]
440                                     r += '<br/>';
441                                     r += '<div class="address"><ul>';
442                                     r += $format_address(row, { no_line_break: 1 });
443
444                                     if ( row.email ) {
445                                         r += "<li>" + _("Email: ") + "<a href='mailto:" + encodeURIComponent(row.email) + "'>" + escape_str(row.email) + "</a></li>";
446                                     }
447                                     r += '</ul></div>'
448
449                                     return r;
450                                 }
451                             }
452                             [% CASE 'name-address' %]
453                             {
454                                 "data": "me.firstname:me.surname:me.othernames:me.street_number:me.address:me.address2:me.city:me.state:me.postal_code:me.country",
455                                 "searchable": true,
456                                 "orderable": true,
457                                 "render": function( data, type, row, meta ) {
458                                     let patron_id = encodeURIComponent(row.patron_id);
459                                     let r = '';
460                                     [% IF ! open_on_row_click %]
461                                     r += "<a href=\"/cgi-bin/koha/members/moremember.pl?borrowernumber=" + patron_id + "\" class=\"patron_name\" data-borrowernumber=\"" + patron_id + "\" style=\"white-space:nowrap\">" + $patron_to_html(row, { invert_name: 1 }) + "</a>";
462                                     [% ELSE %]
463                                     r += $patron_to_html(row, { invert_name: 1 });
464                                     [% END %]
465                                     r += '<br/>';
466                                     r += '<div class="address"><ul>';
467                                     r += $format_address(row, { no_line_break: 1 });
468
469                                     if ( row.email ) {
470                                         r += "<li>" + _("Email: ") + "<a href='mailto:" + encodeURIComponent(row.email) + "'>" + escape_str(row.email) + "</a></li>";
471                                     }
472                                     r += '</ul></div>'
473
474                                     return r;
475                                 }
476                             }
477                             [% CASE 'name' %]
478                             {
479                                 "data": "me.firstname:me.surname:me.othernames",
480                                 "searchable": true,
481                                 "orderable": true,
482                                 "render": function( data, type, row, meta ) {
483                                     let patron_id = encodeURIComponent(row.patron_id);
484                                     [% IF ! open_on_row_click %]
485                                     return "<a href=\"/cgi-bin/koha/members/moremember.pl?borrowernumber=" + patron_id + "\" class=\"patron_name\" data-borrowernumber=\"" + patron_id + "\" style=\"white-space:nowrap\">" + $patron_to_html(row, { invert_name: 1 }) + "</a>";
486                                     [% ELSE %]
487                                     return $patron_to_html(row, { invert_name: 1 });
488                                     [% END %]
489                                 }
490                             }
491                             [% CASE 'branch' %]
492                             {
493                                 "data": "library_id",
494                                 "searchable": true,
495                                 "orderable": true,
496                                 "render": function( data, type, row, meta ) {
497                                     let library_name = libraries_map[data].branchname
498                                     if( !singleBranchMode && data == logged_in_library_id ) {
499                                         return "<span class=\"currentlibrary\">" + escape_str(library_name) + "</span>";
500                                     } else {
501                                         return escape_str(library_name);
502                                     }
503                                 }
504                             }
505                             [% CASE 'category' %]
506                             {
507                                 "data": "category_id",
508                                 "searchable": true,
509                                 "orderable": true,
510                                 "render": function( data, type, row, meta ) {
511                                     return escape_str(categories_map[data].description);
512                                 }
513                             }
514                             [% CASE 'dateexpiry' %]
515                             {
516                                 "data": "expiry_date",
517                                 "searchable": true,
518                                 "orderable": true,
519                                 "render": function( data, type, row, meta ) {
520                                     return data ? escape_str($date(data)) : "";
521                                 }
522                             }
523                             [% CASE 'borrowernotes' %]
524                             {
525                                 "data": "staff_notes",
526                                 "searchable": true,
527                                 "orderable": true,
528                                 [%# We don't escape here, we allow html tag in staff notes %]
529                             }
530                             [% CASE 'phone' %]
531                             {
532                                 "data": "phone",
533                                 "searchable": true,
534                                 "orderable": true,
535                                 "render": function( data, type, row, meta ) {
536                                     return escape_str(data);
537                                 }
538                             }
539                             [% CASE 'checkouts' %][% embed.push('checkouts+count', 'overdues+count') %]
540                             {
541                                 "data": "",
542                                 "searchable": false,
543                                 "orderable": false,
544                                 "render": function( data, type, row, meta ) {
545                                     if ( row.overdues_count ) {
546                                         return "<span class='overdue'><strong>"+row.overdues_count + "</strong></span>";
547                                     } else {
548                                         return "0 / " + row.checkouts_count;
549                                     }
550                                 }
551                             }
552                             [% CASE 'account_balance' %][% embed.push('account_balance') %]
553                             {
554                                 "data": "",
555                                 "searchable": false,
556                                 "orderable": false,
557                                 "render": function( data, type, row, meta ) {
558                                     let r = "<span style='text-align: right; display: block;'><a href=\"/cgi-bin/koha/members/boraccount.pl?borrowernumber="+row.patron_id+"\">";
559                                     let balance_str = row.account_balance || 0;
560                                     balance_str = balance_str.escapeHtml().format_price();
561                                     if ( row.account_balance < 0 ) {
562                                         // FIXME Format price here
563                                         r += "<span class='credit'>" + balance_str + "</span>";
564                                     } else if ( row.account_balance > 0 ) {
565                                         r += "<span class='debit'><strong>" + balance_str  + "</strong></span>"
566                                     } else {
567                                         r += balance_str;
568                                     }
569                                     r += "</a></span>";
570                                     return r;
571                                 }
572                             }
573
574                             [% CASE 'action' %]
575                             {
576                                 "data": function( row, type, val, meta ) {
577
578                                     let patron_id = encodeURIComponent(row.patron_id);
579                                     let action_node = "";
580                                     [% FOR action IN actions %]
581                                     [% SWITCH action %]
582                                     [% CASE 'select' %]
583                                         action_node += '<a href="#" class="btn btn-default btn-xs select_user" data-borrowernumber="' + patron_id + '">Select</a><input type="hidden" id="borrower_data' + patron_id + '" name="borrower_data'+ patron_id + '" value=\''+JSON.stringify(row)+'\' />';
584                                     [% CASE 'add' %]
585                                         action_node += '<a href="#" class="btn btn-default btn-xs add_user" data-borrowernumber="' + patron_id + '" data-firstname="' + encodeURIComponent(row.firstname) + '" data-surname="' + encodeURIComponent(row.surname) + '">Add</a><input type="hidden" id="borrower_data' + patron_id + '" name="borrower_data'+ patron_id + '" />';
586                                     [% CASE 'edit' %]
587                                         action_node += '<a href="/cgi-bin/koha/members/memberentry.pl?op=modify&amp;destination=circ&amp;borrowernumber=' + patron_id + '" class="btn btn-default btn-xs"><i class="fa fa-pencil"></i> Edit</a>';
588                                     [% CASE 'checkout' %]
589                                         [% IF CAN_user_circulate_circulate_remaining_permissions %]
590                                             action_node += '<a class="btn btn-default btn-xs" href="/cgi-bin/koha/circ/circulation.pl?borrowernumber=' + patron_id + '"><i class="fa fa-barcode"></i> ' + _("Check out") + '</a>';
591                                         [% END %]
592                                     [% END %]
593                                     [% END %]
594                                     return action_node;
595                                 },
596                                 "searchable": false,
597                                 "orderable": false
598                             }
599                         [% END %]
600                         [% UNLESS loop.last %],[% END %]
601                     [% END %]
602                 ],
603                 'embed': [% To.json(embed) | $raw %],
604                 "order": [[ [% order_column_index | html %], "asc" ]],
605                 'bAutoWidth': false,
606                 'lengthMenu': [aLengthMenu, aLengthMenuLabel],
607                 'sPaginationType': 'full_numbers',
608                 "pageLength": [% Koha.Preference('PatronsPerPage') | html %],
609                 [% IF sticky_header %]
610                 "initComplete": function(settings, json) {
611                     Sticky = $("#[% sticky_header | html %]");
612                     Sticky.hcSticky({
613                         stickTo: "#[% sticky_to | html %]",
614                         stickyClass: "floating"
615                     });
616                 },
617                 [% END %]
618             }, typeof table_settings !== 'undefined' ? table_settings : null, 1, additional_filters);
619
620             $("#patron_search_form").on('submit', filter);
621             $(".filterByLetter").on("click",function(e){
622                 e.preventDefault();
623                 filterByFirstLetterSurname($(this).text(), true);
624             });
625             $("body").on("click",".add_user",function(e){
626                 e.preventDefault();
627                 var borrowernumber = $(this).data("borrowernumber");
628                 var firstname = $(this).data("firstname");
629                 var surname = $(this).data("surname");
630                 add_user( borrowernumber, firstname + " " + surname );
631             });
632
633             $("body").on("click",".select_user",function(e){
634                 e.preventDefault();
635                 var borrowernumber = $(this).data("borrowernumber");
636                 var borrower_data = $("#borrower_data"+borrowernumber).val();
637                 select_user( borrowernumber, JSON.parse(borrower_data) );
638             });
639
640             $("body").on("click",".patron_preview", function( e ){
641                 e.preventDefault();
642                 var borrowernumber = $(this).data("borrowernumber");
643                 var page = "/cgi-bin/koha/members/moremember.pl?print=brief&borrowernumber=" + borrowernumber;
644                 $("#patronPreview .modal-body").load( page + " div.container-fluid" );
645                 $('#patronPreview').modal({show:true});
646             });
647
648             $("#patronPreview").on('hidden.bs.modal', function (e) {
649                 $("#patronPreview .modal-body").html("<img src=\"[% interface | html %]/[% theme | html %]/img/spinner-small.gif\" alt=\"\" /> Loading");
650             });
651
652             $("#clear_search").on("click",function(e){
653                 e.preventDefault();
654                 clearFilters();
655                 $("#searchpattern").parent().hide();
656             });
657
658             if ( $("#search_patron_filter").val().length > 0 ) {
659                 $("#patron_search_form").submit();
660             }
661
662             /* Initial page load does not trigger the popstate event, so we explicitly call this */
663             getSearchByLocation( false );
664
665         });
666
667         function getSearchByLocation( setstate ){
668             /* Check to see if the URL contains a search parameter */
669             if( location.search != ""){
670                 var params = new URLSearchParams( location.search );
671                 var firstletter = params.get("firstletter");
672                 /* Check to see if search is a first letter param */
673                 if( firstletter ){
674                     /* Trigger function to return search results by letter */
675                     filterByFirstLetterSurname( firstletter, setstate );
676                 }
677             }
678         }
679
680         function update_search_description(){
681             var searched = $("#searchfieldstype_filter").find("option:selected").text();
682             if ( $("#search_patron_filter").val() ) {
683                 if ( $("#searchtype_filter").val() == 'start_with' ) {
684                     searched += _(" starting with ");
685                 } else {
686                     searched += _(" containing ");
687                 }
688                 searched += "'" + $("#search_patron_filter").val() + "'";
689             }
690             if ( $("#firstletter_filter").val() ) {
691                 searched += _(" begins with ") + "'" + $("#firstletter_filter").val() +"'";
692             }
693             if ( $("#categorycode_filter").val() ) {
694                 searched += _(" with category ") + "'" + $("#categorycode_filter").find("option:selected").text() + "'";
695             }
696             if ( $("#branchcode_filter").val() ) {
697                 searched += _(" in library ") + $("#branchcode_filter").find("option:selected").text();
698             }
699             $("#searchpattern").text(searched);
700             $("#searchpattern").parent().show();
701         }
702
703         function filter() {
704             $("#firstletter_filter").val('');
705             $("#[% table_id | html %]_search_results").show();
706
707             let table_dt = patrons_table.DataTable();
708             [% FOR c IN columns %]
709                 [% SWITCH c %]
710                 [% CASE 'branch' %]
711                     library_id = $("#branchcode_filter").val() || "";
712                     patrons_table.find('thead tr:eq(1) th[data-filter="libraries"] select').val(library_id);
713                     table_dt.column([% loop.count - 1 %]).search(library_id ? '^'+library_id+'$' : '');
714                 [% CASE 'category' %]
715                     let category_id = $("#categorycode_filter").val() || "";
716                     patrons_table.find('thead tr:eq(1) th[data-filter="categories"] select').val(category_id);
717                     table_dt.column([% loop.count - 1 %]).search(category_id ? '^'+category_id+'$' : '');
718                 [% END %]
719             [% END %]
720             table_dt.search("");
721             first_draw = 1; // Only redirect if we are coming from here
722             table_dt.draw();
723             update_search_description();
724             return false;
725         }
726
727         function clearFilters() {
728             $("#searchfieldstype_filter option:first").prop("selected", true);
729             $("#searchtype_filter option[value='contain']").prop("selected", true);
730             $("#categorycode_filter option:first").prop("selected", true);
731             $("#branchcode_filter option:first").prop("selected", true);
732             $("#firstletter_filter").val('');
733             $("#search_patron_filter").val('');
734             /* remove any search string added by firstletter search */
735             history.pushState( {}, null, window.location.href.split("?" )[0]);
736             $("#[% table_id | html %]_search_results").hide();
737             update_search_description();
738         }
739
740         // User has clicked on a letter
741         function filterByFirstLetterSurname(letter, setstate ) {
742             $("#firstletter_filter").val(letter);
743
744             $("#[% table_id | html %]_search_results").show();
745
746             if ( setstate ) {
747                 history.pushState( null, null, "?firstletter=" + letter );
748             }
749
750             patrons_table.DataTable().draw();
751             update_search_description();
752         }
753
754         // modify parent window owner element
755         function add_user(borrowernumber, borrowername) {
756             var p = window.opener;
757             // In one place (serials/routing.tt), the page is reload on every add
758             // We have to wait for the page to be there
759             function wait_for_opener () {
760                 if ( ! $(opener.document).find('body').size() ) {
761                     setTimeout(wait_for_opener, 500);
762                 } else {
763                     [%# Note that add_user could sent data instead of borrowername too %]
764                     $("#info").hide();
765                     $("#error").hide();
766                     if ( p.add_user(borrowernumber, borrowername) < 0 ) {
767                         $("#error").html(_("Patron '%s' is already in the list.").format(borrowername));
768                         $("#error").show();
769                     } else {
770                         $("#info").html(_("Patron '%s' added.").format(borrowername));
771                         $("#info").show();
772                     }
773                 }
774             }
775             wait_for_opener();
776         }
777         function select_user(borrowernumber, data) {
778             var p = window.opener;
779             [%  IF callback %]
780                 p.[% callback | html %](borrowernumber, data);
781             [%  ELSE %]
782                 p.select_user(borrowernumber, data);
783             [%  END %]
784             window.close();
785         }
786     </script>
787 [% END %]