Bug 31530: Remove HTML tags in TT comments in patron-search.inc
[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                                 [% SET standard = Koha.Preference('DefaultPatronSearchFields') || 'firstname,middle_name,surname,othernames,cardnumber,userid' %]
64                                 [% default_fields = [ standard, 'surname', 'cardnumber', 'email', 'borrowernumber', 'userid', 'phone', 'address', 'dateofbirth', 'sort1', 'sort2' ] %]
65                                 [% search_options = default_fields.merge(standard.split(',')).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 [%# - redirect_if_attribute_equal: Name of the attribute to use for the redirect. Query using this attribute, before the normal search %]
199 [%# filter: Same as patron_search_table %]
200 [%# open_on_row_click: boolean, default off. Will allow to select a patron by clicking on the whole tr element %]
201 [%# 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' %]
202 [%# preview_on_name_click: Open a modal window with patron's info when the name is clicked %]
203 [%# actions: list of buttons to display in the action column. Possible values are: 'select', 'add', 'edit', 'checkout' %]
204 [%# sticky_header and sticky_to: If we need a sticky header %]
205 [%# callback: name of the JS function that will be called when a patron is selected. Only work with action=select %]
206 [%# display_search_description: boolean, default off. Display the description of the search %]
207 [% BLOCK patron_search_js %]
208
209     [% IF redirect_if_one_result && !redirect_url %]
210         <script>console.log("Wrong call of patron_searh_js - missing redirect_url");</script>
211     [% END %]
212     <script>
213         let categories = [% To.json(categories) | $raw %].map(e => {
214             e['_id'] = e.categorycode;
215             e['_str'] = e.description;
216             return e;
217         });
218         let categories_map = categories.reduce((map, e) => {
219             map[e._id] = e;
220             return map;
221         }, {});
222         let libraries  = [% To.json(libraries) | $raw %].map(e => {
223             e['_id'] = e.branchcode;
224             e['_str'] = e.branchname;
225             return e;
226         });
227         let libraries_map = libraries.reduce((map, e) => {
228             map[e._id] = e;
229             return map;
230         }, {});
231
232         [% IF Koha.Preference('ExtendedPatronAttributes') && extended_attribute_types %]
233             let extended_attribute_types = [% To.json(extended_attribute_types || []) | $raw %];
234         [% END %]
235
236     </script>
237
238     [% INCLUDE 'datatables.inc' %]
239     [% INCLUDE 'js-patron-get-age.inc' %]
240     [% INCLUDE 'js-patron-format.inc' %]
241     [% INCLUDE 'js-patron-format-address.inc' %]
242     [% IF sticky_header %]
243         [% Asset.js("lib/hc-sticky.js") | $raw %]
244     [% END %]
245
246     <script>
247         var first_draw = 0;
248         let patrons_table;
249         var Sticky;
250         var singleBranchMode = '[% singleBranchMode | html %]';
251         let logged_in_library_id = "[% Branches.GetLoggedInBranchcode | html %]";
252         [% IF do_not_defer_loading %]
253             let defer_loading = 0;
254         [% ELSE %]
255             let defer_loading = 1;
256         [% END %]
257
258         /* popstate event triggered by forward and back button. Need to refresh search */
259         window.addEventListener('popstate', (event) => {
260             getSearchByLocation( false );
261         });
262
263         [% SWITCH filter %]
264         [% CASE 'suggestions_managers' %]
265             let patron_search_url = '/api/v1/suggestions/managers';
266         [% CASE 'baskets_managers' %]
267             let patron_search_url = '/api/v1/acquisitions/baskets/managers';
268         [% CASE 'funds_owners' %]
269             let patron_search_url = '/api/v1/acquisitions/funds/owners';
270         [% CASE 'funds_users' %]
271             let patron_search_url = '/api/v1/acquisitions/funds/users';
272         [% CASE %]
273             let patron_search_url = '/api/v1/patrons';
274         [% END %]
275         $(document).ready(function(){
276
277             $("#info").hide();
278             $("#error").hide();
279
280             // Build the aLengthMenu
281             var aLengthMenu = [
282                 [% Koha.Preference('PatronsPerPage') | html %], 10, 20, 50, 100, -1
283             ];
284             jQuery.unique(aLengthMenu);
285             aLengthMenu.sort(function( a, b ){
286                 // Put "All" at the end
287                 if ( a == -1 ) {
288                     return 1;
289                 } else if ( b == -1 ) {
290                     return -1;
291                 }
292                 return parseInt(a) < parseInt(b) ? -1 : 1;}
293             );
294             var aLengthMenuLabel = [];
295             $(aLengthMenu).each(function(){
296                 if ( this == -1 ) {
297                     // Label for -1 is "All"
298                     aLengthMenuLabel.push(_("All"));
299                 } else {
300                     aLengthMenuLabel.push(this);
301                 }
302             });
303
304             let additional_filters = {
305                 surname: function(){
306                     let start_with = $("#firstletter_filter").val()
307                     if (!start_with) return "";
308                     return { "like": start_with + "%" }
309                 },
310                 "-and": function(){
311                     let pattern = $("#search_patron_filter").val();
312                     if (!pattern) return "";
313                     let patterns = pattern.split(' ').filter(function(s){ return s.length });
314
315                     let filters = [];
316                     let search_type = $("#searchtype_filter").val() || "contain";
317                     let search_fields = $("#searchfieldstype_filter").val();
318                     if ( !search_fields ) {
319                         search_fields = "[% Koha.Preference('DefaultPatronSearchFields') || 'firstname,middle_name,surname,othernames,cardnumber,userid' | html %]";
320                     }
321
322                     let subquery_and = [];
323                     patterns.forEach(function(pattern,i){
324                         let sub_or = [];
325                         search_fields.split(',').forEach(function(attr,ii){
326                             sub_or.push({["me."+attr]:{"like":(search_type == "contain" ? "%" : "" ) + pattern + "%"}});
327                         });
328                         subquery_and.push(sub_or);
329                     });
330                     filters.push({"-and": subquery_and});
331
332                     [% IF Koha.Preference('ExtendedPatronAttributes') && extended_attribute_types %]
333                         subquery_and = [];
334                         patterns.forEach(function(pattern,i){
335                             let sub_or = [];
336                             sub_or.push({
337                                 "extended_attributes.value": { "like": "%" + pattern + (search_type == "contain" ? "%" : "" )},
338                                 "extended_attributes.code": extended_attribute_types
339                             });
340                             subquery_and.push(sub_or);
341                         });
342                         filters.push({"-and": subquery_and});
343                     [% END %]
344                     return filters;
345                 }
346             };
347
348             [% UNLESS default_sort_column %]
349                 [% default_sort_column = "name" %]
350             [% END %]
351             [% SET order_column_index = 0 %]
352             [% SET embed = ['extended_attributes'] %]
353             patrons_table = $("#[% table_id | html %]").kohaTable({
354                 "ajax": {
355                     "url": patron_search_url,
356                     "dataSrc": function ( json ) {
357                         [% IF redirect_if_one_result %]
358                             // redirect if there is only 1 result.
359                             if ( first_draw && json.recordsFiltered == 1 ) {
360                                 let url = '[% redirect_url | url %]'.indexOf("?") != -1
361                                     ? '[% redirect_url | url %]&borrowernumber=' + json.data[0].patron_id
362                                     : '[% redirect_url | url %]?borrowernumber=' + json.data[0].patron_id;
363                                 document.location.href = url;
364                                 return false;
365                             }
366                             first_draw = 0;
367                         [% END %]
368                         return json.data;
369                     }
370                 },
371                 [% IF open_on_row_click OR preview_on_name_click OR remember_selections %]
372                 "drawCallback": function( settings ) {
373                     var api = this.api();
374                     var data = api.data();
375                     if ( data.length == 0 ) return;
376
377                     [% IF open_on_row_click %]
378                     $.each($(this).find("tbody tr"), function(index, tr) {
379                         let url = "[% on_click_url | url %]&borrowernumber=" + data[index].patron_id;
380                         $(tr).off('click').on('click', function() {
381                             document.location.href = url;
382                         }).addClass('clickable');
383                         $(tr).find("a.patron_name").attr('href', url);
384                     });
385                     [% END %]
386                     [% IF preview_on_name_click %]
387                     $.each($(this).find("tbody tr"), function(index, tr) {
388                         $(tr).find("a.patron_name").addClass("patron_preview");
389                     });
390                     [% END %]
391                     [% IF remember_selections %]
392                         prepSelections();
393                     [% END %]
394                 },
395                 [% END %]
396                 "iDeferLoading": defer_loading,
397                 "columns": [
398                     [% FOR column IN columns %]
399                         [% IF default_sort_column == column %]
400                             [% order_column_index = loop.count - 1%]
401                         [% END %]
402                         [% SWITCH column %]
403                             [% CASE 'checkbox' %]
404                             {
405                                 "data": "patron_id",
406                                 "searchable": false,
407                                 "orderable": false,
408                                 "render": function( data, type, row, meta ) {
409                                     return "<label for='check" + data + "' class='content_hidden'>" + _("Select patron") + "</label><input type='checkbox' id='check" + data + "' class='selection' name='borrowernumber' value='" + data + "' />";
410                                 }
411                             }
412                             [% CASE 'cardnumber' %]
413                             {
414                                 "data": "cardnumber",
415                                 "searchable": true,
416                                 "orderable": true,
417                                 "render": function( data, type, row, meta ) {
418                                     let patron_id = encodeURIComponent(row.patron_id);
419                                     [% IF !open_on_row_click AND CAN_user_circulate_circulate_remaining_permissions %]
420                                         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>";
421                                     [% ELSE %]
422                                         return escape_str(data);
423                                     [% END %]
424                                 }
425
426                             }
427                             [% CASE 'dateofbirth' %]
428                             {
429                                 "data": "date_of_birth",
430                                 "searchable": true,
431                                 "orderable": true,
432                                 "render": function( data, type, row, meta ) {
433                                     return data ? escape_str($date(data) + " (" + _("%s years").format($get_age(data)) + ")") : "";
434                                 }
435                             }
436                             [% CASE 'address' %]
437                             {
438                                 "data": "me.street_number:me.address:me.address2:me.city:me.state:me.postal_code:me.country",
439                                 "searchable": true,
440                                 "orderable": true,
441                                  "render": function( data, type, row, meta ) {
442                                     let r = '<div class="address"><ul>';
443                                     r += $format_address(row, { no_line_break: 1 });
444                                     r += '</div></ul>';
445                                     return r;
446                                 }
447                             }
448                             [% CASE 'address-library' %]
449                             {
450                                 "data": "me.street_number:me.address:me.address2:me.city:me.state:me.postal_code:me.country",
451                                 "searchable": true,
452                                 "orderable": true,
453                                 "render": function( data, type, row, meta ) {
454                                     let r = '<div class="address"><ul>';
455                                     r += $format_address(row, { no_line_break: 1 });
456                                     r += '</div></ul>';
457                                     r += " " + escape_str(libraries_map[row.library_id].branchname);
458                                     return r;
459                                 }
460                             }
461                             [% CASE 'name-address' %]
462                             {
463                                 "data": "me.surname:me.firstname:me.middle_name:me.othernames:me.street_number:me.address:me.address2:me.city:me.state:me.postal_code:me.country",
464                                 "searchable": true,
465                                 "orderable": true,
466                                 "render": function( data, type, row, meta ) {
467                                     let patron_id = encodeURIComponent(row.patron_id);
468                                     let r = '';
469                                     [% IF ! open_on_row_click %]
470                                     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>";
471                                     [% ELSE %]
472                                     r += $patron_to_html(row, { invert_name: 1 });
473                                     [% END %]
474                                     r += '<br/>';
475                                     r += '<div class="address"><ul>';
476                                     r += $format_address(row, { no_line_break: 1 });
477
478                                     if ( row.email ) {
479                                         r += "<li>" + _("Email: ") + "<a href='mailto:" + encodeURIComponent(row.email) + "'>" + escape_str(row.email) + "</a></li>";
480                                     }
481                                     r += '</ul></div>'
482
483                                     return r;
484                                 }
485                             }
486                             [% CASE 'name-address' %]
487                             {
488                                 "data": "me.surname:me.firstname:me.othernames:me.street_number:me.address:me.address2:me.city:me.state:me.postal_code:me.country",
489                                 "searchable": true,
490                                 "orderable": true,
491                                 "render": function( data, type, row, meta ) {
492                                     let patron_id = encodeURIComponent(row.patron_id);
493                                     let r = '';
494                                     [% IF ! open_on_row_click %]
495                                     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>";
496                                     [% ELSE %]
497                                     r += $patron_to_html(row, { invert_name: 1 });
498                                     [% END %]
499                                     r += '<br/>';
500                                     r += '<div class="address"><ul>';
501                                     r += $format_address(row, { no_line_break: 1 });
502
503                                     if ( row.email ) {
504                                         r += "<li>" + _("Email: ") + "<a href='mailto:" + encodeURIComponent(row.email) + "'>" + escape_str(row.email) + "</a></li>";
505                                     }
506                                     r += '</ul></div>'
507
508                                     return r;
509                                 }
510                             }
511                             [% CASE 'name' %]
512                             {
513                                 "data": "me.surname:me.firstname:me.middle_name:me.othernames",
514                                 "searchable": true,
515                                 "orderable": true,
516                                 "render": function( data, type, row, meta ) {
517                                     let patron_id = encodeURIComponent(row.patron_id);
518                                     [% IF ! open_on_row_click %]
519                                     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>";
520                                     [% ELSE %]
521                                     return $patron_to_html(row, { invert_name: 1 });
522                                     [% END %]
523                                 }
524                             }
525                             [% CASE 'branch' %]
526                             {
527                                 "data": "library_id",
528                                 "searchable": true,
529                                 "orderable": true,
530                                 "render": function( data, type, row, meta ) {
531                                     let library_name = libraries_map[data].branchname
532                                     if( !singleBranchMode && data == logged_in_library_id ) {
533                                         return "<span class=\"currentlibrary\">" + escape_str(library_name) + "</span>";
534                                     } else {
535                                         return escape_str(library_name);
536                                     }
537                                 }
538                             }
539                             [% CASE 'category' %]
540                             {
541                                 "data": "category_id",
542                                 "searchable": true,
543                                 "orderable": true,
544                                 "render": function( data, type, row, meta ) {
545                                     return escape_str(categories_map[data].description);
546                                 }
547                             }
548                             [% CASE 'dateexpiry' %]
549                             {
550                                 "data": "expiry_date",
551                                 "searchable": true,
552                                 "orderable": true,
553                                 "render": function( data, type, row, meta ) {
554                                     return data ? escape_str($date(data)) : "";
555                                 }
556                             }
557                             [% CASE 'borrowernotes' %]
558                             {
559                                 "data": "staff_notes",
560                                 "searchable": true,
561                                 "orderable": true,
562                                 [%# We don't escape here, we allow html tag in staff notes %]
563                             }
564                             [% CASE 'phone' %]
565                             {
566                                 "data": "phone",
567                                 "searchable": true,
568                                 "orderable": true,
569                                 "render": function( data, type, row, meta ) {
570                                     return escape_str(data);
571                                 }
572                             }
573                             [% CASE 'checkouts' %][% embed.push('checkouts+count', 'overdues+count') %]
574                             {
575                                 "data": "",
576                                 "searchable": false,
577                                 "orderable": false,
578                                 "render": function( data, type, row, meta ) {
579                                     if ( row.overdues_count ) {
580                                         return "<span class='overdue'><strong>"+row.overdues_count + "</strong></span>";
581                                     } else {
582                                         return "0 / " + row.checkouts_count;
583                                     }
584                                 }
585                             }
586                             [% CASE 'account_balance' %][% embed.push('account_balance') %]
587                             {
588                                 "data": "",
589                                 "searchable": false,
590                                 "orderable": false,
591                                 "render": function( data, type, row, meta ) {
592                                     let r = "<span style='text-align: right; display: block;'><a href=\"/cgi-bin/koha/members/boraccount.pl?borrowernumber="+row.patron_id+"\">";
593                                     let balance_str = row.account_balance || 0;
594                                     balance_str = balance_str.escapeHtml().format_price();
595                                     if ( row.account_balance < 0 ) {
596                                         // FIXME Format price here
597                                         r += "<span class='credit'>" + balance_str + "</span>";
598                                     } else if ( row.account_balance > 0 ) {
599                                         r += "<span class='debit'><strong>" + balance_str  + "</strong></span>"
600                                     } else {
601                                         r += balance_str;
602                                     }
603                                     r += "</a></span>";
604                                     return r;
605                                 }
606                             }
607
608                             [% CASE 'action' %]
609                             {
610                                 "data": function( row, type, val, meta ) {
611
612                                     let patron_id = encodeURIComponent(row.patron_id);
613                                     let action_node = "";
614                                     [% FOR action IN actions %]
615                                     [% SWITCH action %]
616                                     [% CASE 'select' %]
617                                         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)+'\' />';
618                                     [% CASE 'add' %]
619                                         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 + '" />';
620                                     [% CASE 'edit' %]
621                                         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>';
622                                     [% CASE 'checkout' %]
623                                         [% IF CAN_user_circulate_circulate_remaining_permissions %]
624                                             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>';
625                                         [% END %]
626                                     [% END %]
627                                     [% END %]
628                                     return action_node;
629                                 },
630                                 "searchable": false,
631                                 "orderable": false
632                             }
633                         [% END %]
634                         [% UNLESS loop.last %],[% END %]
635                     [% END %]
636                 ],
637                 'embed': [% To.json(embed) | $raw %],
638                 "order": [[ [% order_column_index | html %], "asc" ]],
639                 'bAutoWidth': false,
640                 'lengthMenu': [aLengthMenu, aLengthMenuLabel],
641                 'sPaginationType': 'full_numbers',
642                 "pageLength": [% Koha.Preference('PatronsPerPage') | html %],
643                 [% IF sticky_header %]
644                 "initComplete": function(settings, json) {
645                     $("#[% sticky_header | html %]").show();
646                     Sticky = $("#[% sticky_header | html %]");
647                     Sticky.hcSticky({
648                         stickTo: "#[% sticky_to | html %]",
649                         stickyClass: "floating"
650                     });
651                 },
652                 [% END %]
653             }, typeof table_settings !== 'undefined' ? table_settings : null, 1, additional_filters);
654
655             $("#patron_search_form").on('submit', filter);
656             $(".filterByLetter").on("click",function(e){
657                 e.preventDefault();
658                 filterByFirstLetterSurname($(this).text(), true);
659             });
660             $("body").on("click",".add_user",function(e){
661                 e.preventDefault();
662                 var borrowernumber = $(this).data("borrowernumber");
663                 var firstname = $(this).data("firstname");
664                 var surname = $(this).data("surname");
665                 add_user( borrowernumber, firstname + " " + surname );
666             });
667
668             $("body").on("click",".select_user",function(e){
669                 e.preventDefault();
670                 var borrowernumber = $(this).data("borrowernumber");
671                 var borrower_data = $("#borrower_data"+borrowernumber).val();
672                 select_user( borrowernumber, JSON.parse(borrower_data) );
673             });
674
675             $("body").on("click",".patron_preview", function( e ){
676                 e.preventDefault();
677                 var borrowernumber = $(this).data("borrowernumber");
678                 var page = "/cgi-bin/koha/members/moremember.pl?print=brief&borrowernumber=" + borrowernumber;
679                 $("#patronPreview .modal-body").load( page + " div.container-fluid" );
680                 $('#patronPreview').modal({show:true});
681             });
682
683             $("#patronPreview").on('hidden.bs.modal', function (e) {
684                 $("#patronPreview .modal-body").html("<img src=\"[% interface | html %]/[% theme | html %]/img/spinner-small.gif\" alt=\"\" /> Loading");
685             });
686
687             $("#clear_search").on("click",function(e){
688                 e.preventDefault();
689                 clearFilters();
690                 $("#searchpattern").parent().hide();
691             });
692
693             if ( !defer_loading ) {
694                 $("#patron_search_form").submit();
695             }
696
697             /* Initial page load does not trigger the popstate event, so we explicitly call this */
698             getSearchByLocation( false );
699
700         });
701
702         function getSearchByLocation( setstate ){
703             /* Check to see if the URL contains a search parameter */
704             if( location.search != ""){
705                 var params = new URLSearchParams( location.search );
706                 var firstletter = params.get("firstletter");
707                 /* Check to see if search is a first letter param */
708                 if( firstletter ){
709                     /* Trigger function to return search results by letter */
710                     filterByFirstLetterSurname( firstletter, setstate );
711                 }
712             }
713         }
714
715         function update_search_description(){
716             var searched = $("#searchfieldstype_filter").find("option:selected").text();
717             if ( $("#search_patron_filter").val() ) {
718                 if ( $("#searchtype_filter").val() == 'start_with' ) {
719                     searched += _(" starting with ");
720                 } else {
721                     searched += _(" containing ");
722                 }
723                 searched += "'" + $("#search_patron_filter").val() + "'";
724             }
725             if ( $("#firstletter_filter").val() ) {
726                 searched += _(" begins with ") + "'" + $("#firstletter_filter").val() +"'";
727             }
728             if ( $("#categorycode_filter").val() ) {
729                 searched += _(" with category ") + "'" + $("#categorycode_filter").find("option:selected").text() + "'";
730             }
731             if ( $("#branchcode_filter").val() ) {
732                 searched += _(" in library ") + $("#branchcode_filter").find("option:selected").text();
733             }
734             $("#searchpattern").text(searched);
735             $("#searchpattern").parent().show();
736         }
737
738         function filter() {
739             [% IF redirect_if_attribute_equal %]
740                 let filter = $("#search_patron_filter").val();
741                 if ( filter ) {
742                     $.ajax({
743                         data: { cardnumber: filter, _match: 'exact' },
744                         type: 'GET',
745                         url: patron_search_url,
746                         success: function(data) {
747                             if ( data.length == 1 ) {
748                                 let url = '[% redirect_url | url %]'.indexOf("?") != -1
749                                     ? '[% redirect_url | url %]&borrowernumber=' + data[0].patron_id
750                                     : '[% redirect_url | url %]?borrowernumber=' + data[0].patron_id;
751                                 document.location.href = url;
752                                 return false;
753                             }
754                         },
755                         error: function() {
756                             alert( _("An error occurred. Check the logs") );
757                         }
758                     });
759                 }
760             [% END %]
761             $("#firstletter_filter").val('');
762             $("#[% table_id | html %]_search_results").show();
763
764             let table_dt = patrons_table.DataTable();
765             [% FOR c IN columns %]
766                 [% SWITCH c %]
767                 [% CASE 'branch' %]
768                     library_id = $("#branchcode_filter").val() || "";
769                     patrons_table.find('thead tr:eq(1) th[data-filter="libraries"] select').val(library_id);
770                     table_dt.column([% loop.count - 1 %]).search(library_id ? '^'+library_id+'$' : '');
771                 [% CASE 'category' %]
772                     let category_id = $("#categorycode_filter").val() || "";
773                     patrons_table.find('thead tr:eq(1) th[data-filter="categories"] select').val(category_id);
774                     table_dt.column([% loop.count - 1 %]).search(category_id ? '^'+category_id+'$' : '');
775                 [% END %]
776             [% END %]
777             table_dt.search("");
778             first_draw = 1; // Only redirect if we are coming from here
779             table_dt.draw();
780             [% IF display_search_description %]
781                 update_search_description();
782             [% END %]
783             return false;
784         }
785
786         function clearFilters() {
787             $("#searchfieldstype_filter option:first").prop("selected", true);
788             $("#searchtype_filter option[value='contain']").prop("selected", true);
789             $("#categorycode_filter option:first").prop("selected", true);
790             $("#branchcode_filter option:first").prop("selected", true);
791             $("#firstletter_filter").val('');
792             $("#search_patron_filter").val('');
793             /* remove any search string added by firstletter search */
794             history.pushState( {}, null, window.location.href.split("?" )[0]);
795             $("#[% table_id | html %]_search_results").hide();
796             [% IF display_search_description %]
797                 update_search_description();
798             [% END %]
799         }
800
801         // User has clicked on a letter
802         function filterByFirstLetterSurname(letter, setstate ) {
803             $("#firstletter_filter").val(letter);
804
805             $("#[% table_id | html %]_search_results").show();
806
807             if ( setstate ) {
808                 history.pushState( null, null, "?firstletter=" + letter );
809             }
810
811             patrons_table.DataTable().draw();
812             [% IF display_search_description %]
813                 update_search_description();
814             [% END %]
815         }
816
817         // modify parent window owner element
818         function add_user(borrowernumber, borrowername) {
819             var p = window.opener;
820             // In one place (serials/routing.tt), the page is reload on every add
821             // We have to wait for the page to be there
822             function wait_for_opener () {
823                 if ( ! $(opener.document).find('body').size() ) {
824                     setTimeout(wait_for_opener, 500);
825                 } else {
826                     [%# Note that add_user could sent data instead of borrowername too %]
827                     $("#info").hide();
828                     $("#error").hide();
829                     if ( p.add_user(borrowernumber, borrowername) < 0 ) {
830                         $("#error").html(_("Patron '%s' is already in the list.").format(borrowername));
831                         $("#error").show();
832                     } else {
833                         $("#info").html(_("Patron '%s' added.").format(borrowername));
834                         $("#info").show();
835                     }
836                 }
837             }
838             wait_for_opener();
839         }
840         function select_user(borrowernumber, data) {
841             var p = window.opener;
842             [%  IF callback %]
843                 p.[% callback | html %](borrowernumber, data);
844             [%  ELSE %]
845                 p.select_user(borrowernumber, data);
846             [%  END %]
847             window.close();
848         }
849     </script>
850 [% END %]