Bug 30055: /api/v1/acquisitions/funds/owners and users for funds
[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 patrons_with_acq_perm_only %]
64             <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>
65         [% END %]
66
67         [% IF patrons_with_suggestion_perm_only %]
68             <div class="hint">Only staff with superlibrarian or suggestions_manage permissions 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"></div>
79         <div id="error" class="dialog alert"></div>
80
81         <input type="hidden" id="firstletter_filter" value="" />
82         <div id="searchresults">
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     </script>
143     [% INCLUDE 'datatables.inc' %]
144     [% INCLUDE 'js-date-format.inc' %]
145     [% INCLUDE 'js-patron-get-age.inc' %]
146     [% INCLUDE 'js-patron-format.inc' %]
147     [% INCLUDE 'js-patron-format-address.inc' %]
148
149     <script>
150         var search = 0;
151         let patrons_table;
152         $(document).ready(function(){
153             $("#info").hide();
154             $("#error").hide();
155
156             $("#searchresults").hide();
157
158             let additional_filters = {
159                 surname: function(){
160                     let start_with = $("#firstletter_filter").val()
161                     if (!start_with) return "";
162                     return { "like": start_with + "%" }
163                 }
164             };
165             patrons_table = $("#memberresultst").kohaTable({
166                 "ajax": {
167                     [% SWITCH filter %]
168                     [% CASE 'suggestions_managers' %]
169                         "url": '/api/v1/suggestions/managers'
170                     [% CASE 'baskets_managers' %]
171                         "url": '/api/v1/acquisitions/baskets/managers'
172                     [% CASE 'funds_owners' %]
173                         "url": '/api/v1/acquisitions/funds/owners'
174                     [% CASE 'funds_users' %]
175                         "url": '/api/v1/acquisitions/funds/users'
176                     [% CASE %]
177                         "url": '/api/v1/patrons'
178                     [% END %]
179                 },
180                 "order": [[ 1, "asc" ]],
181                 "iDeferLoading": 0,
182                 "columns": [
183                     [% FOR column IN columns %]
184                         [% SWITCH column %]
185                             [% CASE 'cardnumber' %]
186                             {
187                                 "data": "cardnumber",
188                                 "searchable": true,
189                                 "orderable": true
190                             }
191                             [% CASE 'dateofbirth' %]
192                             {
193                                 "data": "date_of_birth",
194                                 "searchable": true,
195                                 "orderable": true,
196                                 "render": function( data, type, row, meta ) {
197                                     return data ? escape_str($date(data) + " (" + _("%s years").format($get_age(data)) + ")") : "";
198                                 }
199                             }
200                             [% CASE 'address' %]
201                             {
202                                 "data": "me.street_number:me.address:me.address2:me.city:me.state:me.postal_code:me.country",
203                                 "searchable": true,
204                                 "orderable": true,
205                                 "render": function( data, type, row, meta ) {
206                                     return escape_str($format_address(row) + " ") + escape_str(libraries_map[row.library_id].branchname);
207                                 }
208                             }
209                             [% CASE 'name' %]
210                             {
211                                 "data": "me.firstname:me.surname",
212                                 "searchable": true,
213                                 "orderable": true,
214                                 "render": function( data, type, row, meta ) {
215                                     return $patron_to_html(row, { invert_name: 1 });
216                                 }
217                             }
218                             [% CASE 'branch' %]
219                             {
220                                 "data": "library_id",
221                                 "searchable": true,
222                                 "orderable": true,
223                                 "render": function( data, type, row, meta ) {
224                                     return escape_str(libraries_map[data].branchname);
225                                 }
226                             }
227                             [% CASE 'category' %]
228                             {
229                                 "data": "category_id",
230                                 "searchable": true,
231                                 "orderable": true,
232                                 "render": function( data, type, row, meta ) {
233                                     return escape_str(categories_map[data].description);
234                                 }
235                             }
236                             [% CASE 'dateexpiry' %]
237                             {
238                                 "data": "expiry_date",
239                                 "searchable": true,
240                                 "orderable": true,
241                                 "render": function( data, type, row, meta ) {
242                                     return data ? escape_str($date(data)) : "";
243                                 }
244                             }
245                             [% CASE 'borrowernotes' %]
246                             {
247                                 "data": "staff_notes",
248                                 "searchable": true,
249                                 "orderable": true,
250                                 [%# We don't escape here, we allow html tag in staff notes %]
251                             }
252                             [% CASE 'action' %]
253                             {
254                                 "data": function( row, type, val, meta ) {
255
256                                     let patron_id = encodeURIComponent(row.patron_id);
257                                     [% IF selection_type == 'select' %]
258                                         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)+'\' />';
259                                     [% ELSE %]
260                                         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 + '" />';
261                                     [% END %]
262                                 },
263                                 "searchable": false,
264                                 "orderable": false
265                             }
266                         [% END %]
267                         [% UNLESS loop.last %],[% END %]
268                     [% END %]
269                 ],
270                 'bAutoWidth': false,
271                 'sPaginationType': 'full_numbers',
272                 "iDisplayLength": [% Koha.Preference('PatronsPerPage') | html %],
273             }, null, 1, additional_filters);
274
275             $("#searchform").on('submit', filter);
276             $(".filterByLetter").on("click",function(e){
277                 e.preventDefault();
278                 filterByFirstLetterSurname($(this).text());
279             });
280             $("body").on("click",".add_user",function(e){
281                 e.preventDefault();
282                 var borrowernumber = $(this).data("borrowernumber");
283                 var firstname = $(this).data("firstname");
284                 var surname = $(this).data("surname");
285                 add_user( borrowernumber, firstname + " " + surname );
286             });
287
288             $("body").on("click",".select_user",function(e){
289                 e.preventDefault();
290                 var borrowernumber = $(this).data("borrowernumber");
291                 var borrower_data = $("#borrower_data"+borrowernumber).val();
292                 select_user( borrowernumber, JSON.parse(borrower_data) );
293             });
294
295             $("body").on("click",".patron_preview", function( e ){
296                 e.preventDefault();
297                 var borrowernumber = $(this).data("borrowernumber");
298                 var page = "/cgi-bin/koha/members/moremember.pl?print=brief&borrowernumber=" + borrowernumber;
299                 $("#patronPreview .modal-body").load( page + " div.container-fluid" );
300                 $('#patronPreview').modal({show:true});
301             });
302
303             $("#patronPreview").on('hidden.bs.modal', function (e) {
304                 $("#patronPreview .modal-body").html("<img src=\"[% interface | html %]/[% theme | html %]/img/spinner-small.gif\" alt=\"\" /> Loading");
305             });
306
307         });
308
309         function filter() {
310             search = 1;
311             $("#firstletter_filter").val('');
312             $("#searchresults").show();
313
314             let table_dt = patrons_table.DataTable();
315             [% FOR c IN columns %]
316                 [% SWITCH c %]
317                 [% CASE 'branch' %]
318                     let library_id = $("#branchcode_filter").val();
319                     patrons_table.find('thead tr:eq(1) th[data-filter="libraries"] select').val(library_id);
320                     table_dt.column([% loop.count - 1 %]).search(library_id);
321                 [% CASE 'category' %]
322                     let category_id = $("#categorycode_filter").val();
323                     patrons_table.find('thead tr:eq(1) th[data-filter="categories"] select').val(category_id);
324                     table_dt.column([% loop.count - 1 %]).search(category_id);
325                 [% END %]
326             [% END %]
327             table_dt.search($("#searchmember_filter").val());
328             table_dt.draw();
329             return false;
330         }
331
332         // User has clicked on a letter
333         function filterByFirstLetterSurname(letter) {
334             $("#firstletter_filter").val(letter);
335             search = 1;
336             $("#searchresults").show();
337             patrons_table.fnDraw();
338         }
339
340         // modify parent window owner element
341         [% IF selection_type == 'add' %]
342             function add_user(borrowernumber, borrowername) {
343                 var p = window.opener;
344                 // In one place (serials/routing.tt), the page is reload on every add
345                 // We have to wait for the page to be there
346                 function wait_for_opener () {
347                     if ( ! $(opener.document).find('body').size() ) {
348                         setTimeout(wait_for_opener, 500);
349                     } else {
350                         [%# Note that add_user could sent data instead of borrowername too %]
351                         $("#info").hide();
352                         $("#error").hide();
353                         if ( p.add_user(borrowernumber, borrowername) < 0 ) {
354                             $("#error").html(_("Patron '%s' is already in the list.").format(borrowername));
355                             $("#error").show();
356                         } else {
357                             $("#info").html(_("Patron '%s' added.").format(borrowername));
358                             $("#info").show();
359                         }
360                     }
361                 }
362                 wait_for_opener();
363             }
364         [% ELSIF selection_type == 'select' %]
365             function select_user(borrowernumber, data) {
366                 var p = window.opener;
367                 [%  IF callback %]
368                     p.[% callback | html %](borrowernumber, data);
369                 [%  ELSE %]
370                     p.select_user(borrowernumber, data);
371                 [%  END %]
372                 window.close();
373             }
374         [% END %]
375     </script>
376 [% END %]
377
378 [% SET popup_window = 1 %]
379 [% INCLUDE 'intranet-bottom.inc' %]