Bug 30055: Put extended attributes search back
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / modules / members / search.tt
1 [% USE raw %]
2 [% USE To %]
3 [% USE Asset %]
4 [% USE Koha %]
5 [% USE Branches %]
6 [% USE Categories %]
7 [% SET footerjs = 1 %]
8 [% INCLUDE 'doc-head-open.inc' %]
9 [% SET libraries = Branches.all %]
10 [% SET categories = Categories.all.unblessed %]
11 <title>Patron search &rsaquo; Koha</title>
12 [% INCLUDE 'doc-head-close.inc' %]
13 <style> .modal-body .close { display: none; } </style>
14 </head>
15
16 <body id="common_patron_search" class="common">
17 <div id="patron_search" class="yui-t7">
18     <div class="container-fluid">
19
20         <form id="searchform">
21             <fieldset class="brief">
22                 <h3>Search for patron</h3>
23                 <ol>
24                     <li>
25                         <label for="searchmember_filter">Search:</label>
26                         <input type="text" id="searchmember_filter" value="[% searchmember | html %]" class="focus" />
27                     </li>
28
29                     [% FOR column IN columns %]
30                         [% SWITCH column %]
31                         [% CASE 'branch' %]
32                             <li>
33                                 <label for="branchcode_filter">Library:</label>
34                                 <select id="branchcode_filter">
35                                     [% SET libraries = Branches.all( only_from_group => 1 ) %]
36                                     [% IF libraries.size != 1 %]
37                                         <option value="">Any</option>
38                                     [% END %]
39                                     [% FOREACH l IN libraries %]
40                                         <option value="[% l.branchcode | html %]">[% l.branchname | html %]</option>
41                                     [% END %]
42                                 </select>
43                             </li>
44                         [% CASE 'category' %]
45                             <li>
46                                 <label for="categorycode_filter">Category:</label>
47                                 <select id="categorycode_filter">
48                                     <option value="">Any</option>
49                                     [% FOREACH category IN categories %]
50                                         <option value="[% category.categorycode | html %]">[% category.description | html %]</option>
51                                     [% END %]
52                                 </select>
53                             </li>
54                         [% END %]
55                     [% END %]
56                 </ol>
57                 <fieldset class="action">
58                     <input type="submit" value="Search" />
59                 </fieldset>
60             </fieldset>
61         </form>
62
63         [% IF filter == 'suggestions_managers' %]
64             <div class="hint">Only staff with superlibrarian or suggestions_manage permissions are returned in the search results</div>
65         [% ELSIF filter == 'orders_managers' %]
66             <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>
67         [% ELSIF filter == 'funds_owners' OR filter == 'funds_users' %]
68             <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>
69         [% END %]
70
71         <div class="browse">
72             Browse by last name:
73             [% FOREACH letter IN alphabet.split(' ') %]
74                 <a href="#" class="filterByLetter">[% letter | html %]</a>
75             [% END %]
76         </div>
77
78         <div id="info" class="dialog message" style="display: none;"></div>
79         <div id="error" class="dialog alert" style="dispay: none;"></div>
80
81         <input type="hidden" id="firstletter_filter" value="" />
82         <div id="searchresults" style="display:none;">
83             <table id="memberresultst">
84                 <thead>
85                     <tr>
86                         [% FOR column IN columns %]
87                             [% SWITCH column %]
88                                 [% CASE 'cardnumber' %]<th>Card</th>
89                                 [% CASE 'dateofbirth' %]<th>Date of birth</th>
90                                 [% CASE 'address' %]<th>Address</th>
91                                 [% CASE 'name' %]<th>Name</th>
92                                 [% CASE 'branch' %]<th data-filter="libraries">Library</th>
93                                 [% CASE 'category' %]<th data-filter="categories">Category</th>
94                                 [% CASE 'dateexpiry' %]<th>Expires on</td>
95                                 [% CASE 'borrowernotes' %]<th>Notes</th>
96                                 [% CASE 'action' %]<th>&nbsp;</th>
97                             [% END %]
98                         [% END %]
99                     </tr>
100                   </thead>
101                 <tbody></tbody>
102             </table>
103         </div>
104
105 <div id="closewindow"><a href="#" class="btn btn-default btn-default close">Close</a></div>
106
107 <!-- Patron preview modal -->
108 <div class="modal" id="patronPreview" tabindex="-1" role="dialog" aria-labelledby="patronPreviewLabel">
109     <div class="modal-dialog" role="document">
110         <div class="modal-content">
111             <div class="modal-header">
112                 <button type="button" class="closebtn" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
113                 <h4 class="modal-title" id="patronPreviewLabel"></h4>
114             </div>
115             <div class="modal-body">
116                 <div id="loading">
117                     <img src="[% interface | html %]/[% theme | html %]/img/spinner-small.gif" alt="" /> Loading
118                 </div>
119             </div>
120             <div class="modal-footer">
121                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
122             </div>
123         </div>
124     </div>
125 </div>
126
127 </div>
128 </div>
129
130 [% MACRO jsinclude BLOCK %]
131     <script>
132         let categories = [% To.json(categories) | $raw %];
133         let categories_map = categories.reduce((map, c) => {
134             map[c.categorycode] = c;
135             return map;
136         }, {});
137         let libraries  = [% To.json(libraries) | $raw %];
138         let libraries_map = libraries.reduce((map, l) => {
139             map[l.branchcode] = l;
140             return map;
141         }, {});
142
143         [% IF Koha.Preference('ExtendedPatronAttributes') %]
144             let extended_attribute_types = [% To.json(attribute_type_codes) | $raw %];
145         [% END %]
146     </script>
147     [% INCLUDE 'datatables.inc' %]
148     [% INCLUDE 'js-date-format.inc' %]
149     [% INCLUDE 'js-patron-get-age.inc' %]
150     [% INCLUDE 'js-patron-format.inc' %]
151     [% INCLUDE 'js-patron-format-address.inc' %]
152
153     <script>
154         var search = 0;
155         let patrons_table;
156         $(document).ready(function(){
157             $("#info").hide();
158             $("#error").hide();
159
160             $("#searchresults").hide();
161
162             let additional_filters = {
163                 surname: function(){
164                     let start_with = $("#firstletter_filter").val()
165                     if (!start_with) return "";
166                     return { "like": start_with + "%" }
167                 },
168                 [% IF Koha.Preference('ExtendedPatronAttributes') %]
169                 "-or": function(){
170                     let filter = $("#searchmember_filter").val();
171                     if (!filter) return "";
172                     return [
173                         {
174                             "extended_attributes.value": { "like": "%" + filter + "%" }
175                         },
176                         {
177                             "extended_attributes.code": extended_attribute_types
178                         }
179                     ];
180                 },
181                 [% END %]
182             };
183             patrons_table = $("#memberresultst").kohaTable({
184                 "ajax": {
185                     [% SWITCH filter %]
186                     [% CASE 'suggestions_managers' %]
187                         "url": '/api/v1/suggestions/managers'
188                     [% CASE 'baskets_managers' %]
189                         "url": '/api/v1/acquisitions/baskets/managers'
190                     [% CASE 'funds_owners' %]
191                         "url": '/api/v1/acquisitions/funds/owners'
192                     [% CASE 'funds_users' %]
193                         "url": '/api/v1/acquisitions/funds/users'
194                     [% CASE %]
195                         "url": '/api/v1/patrons'
196                     [% END %]
197                 },
198                 embed: ['extended_attributes'],
199                 "order": [[ 1, "asc" ]],
200                 "iDeferLoading": 0,
201                 "columns": [
202                     [% FOR column IN columns %]
203                         [% SWITCH column %]
204                             [% CASE 'cardnumber' %]
205                             {
206                                 "data": "cardnumber",
207                                 "searchable": true,
208                                 "orderable": true
209                             }
210                             [% CASE 'dateofbirth' %]
211                             {
212                                 "data": "date_of_birth",
213                                 "searchable": true,
214                                 "orderable": true,
215                                 "render": function( data, type, row, meta ) {
216                                     return data ? escape_str($date(data) + " (" + _("%s years").format($get_age(data)) + ")") : "";
217                                 }
218                             }
219                             [% CASE 'address' %]
220                             {
221                                 "data": "me.street_number:me.address:me.address2:me.city:me.state:me.postal_code:me.country",
222                                 "searchable": true,
223                                 "orderable": true,
224                                 "render": function( data, type, row, meta ) {
225                                     return escape_str($format_address(row) + " ") + escape_str(libraries_map[row.library_id].branchname);
226                                 }
227                             }
228                             [% CASE 'name' %]
229                             {
230                                 "data": "me.firstname:me.surname",
231                                 "searchable": true,
232                                 "orderable": true,
233                                 "render": function( data, type, row, meta ) {
234                                     let patron_id = encodeURIComponent(row.patron_id);
235                                     return "<a href=\"/cgi-bin/koha/members/moremember.pl?borrowernumber=" + patron_id + "\" class=\"patron_preview\" data-borrowernumber=\"" + patron_id + "\" style=\"white-space:nowrap\">" + $patron_to_html(row, { invert_name: 1 }) + "</a>";
236                                 }
237                             }
238                             [% CASE 'branch' %]
239                             {
240                                 "data": "library_id",
241                                 "searchable": true,
242                                 "orderable": true,
243                                 "render": function( data, type, row, meta ) {
244                                     return escape_str(libraries_map[data].branchname);
245                                 }
246                             }
247                             [% CASE 'category' %]
248                             {
249                                 "data": "category_id",
250                                 "searchable": true,
251                                 "orderable": true,
252                                 "render": function( data, type, row, meta ) {
253                                     return escape_str(categories_map[data].description);
254                                 }
255                             }
256                             [% CASE 'dateexpiry' %]
257                             {
258                                 "data": "expiry_date",
259                                 "searchable": true,
260                                 "orderable": true,
261                                 "render": function( data, type, row, meta ) {
262                                     return data ? escape_str($date(data)) : "";
263                                 }
264                             }
265                             [% CASE 'borrowernotes' %]
266                             {
267                                 "data": "staff_notes",
268                                 "searchable": true,
269                                 "orderable": true,
270                                 [%# We don't escape here, we allow html tag in staff notes %]
271                             }
272                             [% CASE 'action' %]
273                             {
274                                 "data": function( row, type, val, meta ) {
275
276                                     let patron_id = encodeURIComponent(row.patron_id);
277                                     [% IF selection_type == 'select' %]
278                                         return '<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)+'\' />';
279                                     [% ELSE %]
280                                         return '<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 + '" />';
281                                     [% END %]
282                                 },
283                                 "searchable": false,
284                                 "orderable": false
285                             }
286                         [% END %]
287                         [% UNLESS loop.last %],[% END %]
288                     [% END %]
289                 ],
290                 'bAutoWidth': false,
291                 'sPaginationType': 'full_numbers',
292                 "iDisplayLength": [% Koha.Preference('PatronsPerPage') | html %],
293             }, null, 1, additional_filters);
294
295             $("#searchform").on('submit', filter);
296             $(".filterByLetter").on("click",function(e){
297                 e.preventDefault();
298                 filterByFirstLetterSurname($(this).text());
299             });
300             $("body").on("click",".add_user",function(e){
301                 e.preventDefault();
302                 var borrowernumber = $(this).data("borrowernumber");
303                 var firstname = $(this).data("firstname");
304                 var surname = $(this).data("surname");
305                 add_user( borrowernumber, firstname + " " + surname );
306             });
307
308             $("body").on("click",".select_user",function(e){
309                 e.preventDefault();
310                 var borrowernumber = $(this).data("borrowernumber");
311                 var borrower_data = $("#borrower_data"+borrowernumber).val();
312                 select_user( borrowernumber, JSON.parse(borrower_data) );
313             });
314
315             $("body").on("click",".patron_preview", function( e ){
316                 e.preventDefault();
317                 var borrowernumber = $(this).data("borrowernumber");
318                 var page = "/cgi-bin/koha/members/moremember.pl?print=brief&borrowernumber=" + borrowernumber;
319                 $("#patronPreview .modal-body").load( page + " div.container-fluid" );
320                 $('#patronPreview').modal({show:true});
321             });
322
323             $("#patronPreview").on('hidden.bs.modal', function (e) {
324                 $("#patronPreview .modal-body").html("<img src=\"[% interface | html %]/[% theme | html %]/img/spinner-small.gif\" alt=\"\" /> Loading");
325             });
326
327         });
328
329         function filter() {
330             search = 1;
331             $("#firstletter_filter").val('');
332             $("#searchresults").show();
333
334             let table_dt = patrons_table.DataTable();
335             [% FOR c IN columns %]
336                 [% SWITCH c %]
337                 [% CASE 'branch' %]
338                     let library_id = $("#branchcode_filter").val();
339                     patrons_table.find('thead tr:eq(1) th[data-filter="libraries"] select').val(library_id);
340                     table_dt.column([% loop.count - 1 %]).search(library_id);
341                 [% CASE 'category' %]
342                     let category_id = $("#categorycode_filter").val();
343                     patrons_table.find('thead tr:eq(1) th[data-filter="categories"] select').val(category_id);
344                     table_dt.column([% loop.count - 1 %]).search(category_id);
345                 [% END %]
346             [% END %]
347             table_dt.search($("#searchmember_filter").val());
348             table_dt.draw();
349             return false;
350         }
351
352         // User has clicked on a letter
353         function filterByFirstLetterSurname(letter) {
354             $("#firstletter_filter").val(letter);
355             search = 1;
356             $("#searchresults").show();
357             patrons_table.fnDraw();
358         }
359
360         // modify parent window owner element
361         [% IF selection_type == 'add' %]
362             function add_user(borrowernumber, borrowername) {
363                 var p = window.opener;
364                 // In one place (serials/routing.tt), the page is reload on every add
365                 // We have to wait for the page to be there
366                 function wait_for_opener () {
367                     if ( ! $(opener.document).find('body').size() ) {
368                         setTimeout(wait_for_opener, 500);
369                     } else {
370                         [%# Note that add_user could sent data instead of borrowername too %]
371                         $("#info").hide();
372                         $("#error").hide();
373                         if ( p.add_user(borrowernumber, borrowername) < 0 ) {
374                             $("#error").html(_("Patron '%s' is already in the list.").format(borrowername));
375                             $("#error").show();
376                         } else {
377                             $("#info").html(_("Patron '%s' added.").format(borrowername));
378                             $("#info").show();
379                         }
380                     }
381                 }
382                 wait_for_opener();
383             }
384         [% ELSIF selection_type == 'select' %]
385             function select_user(borrowernumber, data) {
386                 var p = window.opener;
387                 [%  IF callback %]
388                     p.[% callback | html %](borrowernumber, data);
389                 [%  ELSE %]
390                     p.select_user(borrowernumber, data);
391                 [%  END %]
392                 window.close();
393             }
394         [% END %]
395     </script>
396 [% END %]
397
398 [% SET popup_window = 1 %]
399 [% INCLUDE 'intranet-bottom.inc' %]