Bug 32910: (follow-up) Replace v4 icon names with v6
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / modules / admin / transfer_limits.tt
1 [% USE raw %]
2 [% USE To %]
3 [% USE Asset %]
4 [% USE Branches %]
5 [% USE Koha %]
6 [% USE ItemTypes %]
7 [% USE AuthorisedValues %]
8 [% SET footerjs = 1 %]
9 [% INCLUDE 'doc-head-open.inc' %]
10 <title>Library checkin and transfer policy &rsaquo; Administration &rsaquo; Koha</title>
11 [% INCLUDE 'doc-head-close.inc' %]
12 <style>td { text-align: center; } .sorted { min-width: 50%; }</style>
13 </head>
14
15 [% SET BranchTransferLimitsType = Koha.Preference('BranchTransferLimitsType') %]
16 [% SET branches = Branches.all %]
17
18 <body id="admin_branch_transfer_limits" class="admin">
19 [% WRAPPER 'header.inc' %]
20     [% INCLUDE 'prefs-admin-search.inc' %]
21 [% END %]
22
23 [% WRAPPER 'sub-header.inc' %]
24     [% WRAPPER breadcrumbs %]
25         [% WRAPPER breadcrumb_item %]
26             <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a>
27         [% END %]
28         [% WRAPPER breadcrumb_item bc_active= 1 %]
29             <span>Set library checkin and transfer policy</span>
30         [% END %]
31     [% END #/ WRAPPER breadcrumbs %]
32 [% END #/ WRAPPER sub-header.inc %]
33
34 <div class="main container-fluid">
35     <div class="row">
36         <div class="col-sm-10 col-sm-push-2">
37             <main>
38                 <h1>Checkin and transfer policy</h1>
39
40                 <p>
41                     [% IF BranchTransferLimitsType == "itemtype" %]
42                         <label for="value_selector">Select an item type:</label>
43                         <select id="value_selector">
44                             <option value="" selected></option>
45                             [% SET itemtypes = ItemTypes.Get %]
46                             [%  FOREACH i IN itemtypes %]
47                                 <option value="[% i.itemtype | html %]">[% i.description | html %]</option>
48                             [% END %]
49                         </select>
50                     [% ELSE #BranchTransferLimitsType == "ccode" %]
51                         <label for="value_selector">Select a collection:</label>
52                         <select id="value_selector">
53                             <option value="" selected></option>
54                             [% SET ccodes = AuthorisedValues.Get('CCODE') %]
55                             [%  FOREACH c IN ccodes %]
56                                 <option value="[% c.authorised_value | html %]">[% c.lib | html %]</option>
57                             [% END %]
58                         </select>
59                     [% END %]
60
61                     <span id="loading_limits">
62                         <i class="fa fa-spinner fa-pulse fa-fw"></i>
63                         <span>Loading...</span>
64                     </span>
65                 </p>
66
67                 <p class="help">Check the boxes for the libraries you allow your items to be transferred to.</p>
68                 <fieldset>
69                     <a id="check-all" class="limit-action" href="#"><i class="fa fa-check"></i> Check all</a>
70                     |
71                     <a id="uncheck-all" class="limit-action" href="#"><i class="fa fa-times"></i> Uncheck all</a>
72                     |
73                     <a href="/cgi-bin/koha/admin/branch_transfer_limits.pl">Switch to basic editor</a>
74                 </fieldset>
75
76                 <table id="transfer_limits" class="table table-striped table-bordered table-hover table-condensed">
77
78                     <thead>
79                         <tr>
80                             <td>&nbsp;</td>
81                             <td>&nbsp;</td>
82                             [% FOREACH to IN branches %]
83                                 <td>
84                                     <p><a class="btn btn-default btn-xs check-all-col limit-action" data-to="[% to.branchcode | html %]" href="#"><i class="fa fa-check"></i> Check</a></p>
85                                     <p><a class="btn btn-default btn-xs uncheck-all-col limit-action" data-to="[% to.branchcode | html %]" href="#"><i class="fa fa-times"></i> Uncheck</a></p>
86                                 </td>
87                             [% END %]
88                         </tr>
89
90                         <tr>
91                             <td>&nbsp;</td>
92                             <th>From / To</th>
93                             [% FOREACH b IN branches %]
94                                 <th style="word-break: break-all !important" title="[% b.branchname | html %]">[% b.branchname | html %]</th>
95                             [% END %]
96                         </tr>
97                     </thead>
98
99                     <tbody>
100                         [% FOREACH from IN branches %]
101                             <tr>
102                                 <td>
103                                     <p><a class="btn btn-default btn-xs check-all-row limit-action" data-from="[% from.branchcode | html %]" href="#"><i class="fa fa-check"></i> Check</a></p>
104                                     <p><a class="btn btn-default btn-xs uncheck-all-row limit-action" data-from="[% from.branchcode | html %]" href="#"><i class="fa fa-times"></i> Uncheck</a></p>
105                                 </td>
106                                 <th>[% from.branchname | html %]</th>
107                                 [% FOREACH to IN branches %]
108                                     <td class="checkbox-cell">
109                                         [% IF to.branchcode == from.branchcode %]
110                                             &nbsp;
111                                         [% ELSE %]
112                                             <input class="limit-checkboxes from-[% from.branchcode | html %] to-[% to.branchcode | html %]" id="limit-[% from.branchcode | html %]-[% to.branchcode | html %]" type="checkbox" title="From: [%  from.branchname | html %], To: [%  to.branchname | html %]" checked/>
113                                             <i id="spinner-limit-[% from.branchcode | html %]-[% to.branchcode | html %]" class="spinner fa fa-spinner fa-pulse fa-fw"></i>
114                                         [% END %]
115                                     </td>
116                                 [% END %]
117                             </tr>
118                         [% END %]
119                     </tbody>
120                 </table>
121             </main>
122         </div> <!-- /.col-sm-10.col-sm-push-2 -->
123
124         <div class="col-sm-2 col-sm-pull-10">
125             <aside>
126                 [% INCLUDE 'admin-menu.inc' %]
127             </aside>
128         </div> <!-- /.col-sm-2.col-sm-pull-10 -->
129     </div> <!-- /.row -->
130
131 [% MACRO jsinclude BLOCK %]
132     [% Asset.js("js/admin-menu.js") | $raw %]
133     [% INCLUDE 'datatables.inc' %]
134     <script>
135         const branchTransferLimitsType = "[% BranchTransferLimitsType | html %]";
136         const val_type = branchTransferLimitsType == "itemtype" ? "item_type" : "collection_code";
137         const branches = [% To.json(branches) | $raw %];
138
139         $('#loading_limits').hide();
140         $('.spinner').hide();
141
142         $(document).ready(function() {
143             $("#check-all").click(function() {
144                 const val = $('#value_selector').val();
145
146                 $('.limit-action').addClass('disabled');
147                 $('#value_selector').prop('disabled',true);
148
149                 let checkboxes = [];
150                 $(".limit-checkboxes").each(function() {
151                     const checkbox = $(this);
152                     if (checkbox.data('limit_id')) {
153                         checkboxes.push(checkbox);
154                         checkbox.hide();
155                         $(`#spinner-${checkbox.attr('id')}`).show();
156                     }
157                 });
158
159                 del_limits( checkboxes, val );
160                 return false;
161             });
162
163
164             $("#uncheck-all").click(function() {
165                 const val = $('#value_selector').val();
166
167                 $('.limit-action').addClass('disabled');
168                 $('#value_selector').prop('disabled',true);
169
170                 let checkboxes = [];
171                 $(".limit-checkboxes").each(function() {
172                     const checkbox = $(this);
173                     if (!checkbox.data('limit_id')) {
174                         checkboxes.push(checkbox);
175                         checkbox.hide();
176                         $(`#spinner-${checkbox.attr('id')}`).show();
177                     }
178                 });
179
180                 add_limits( checkboxes, val );
181                 return false;
182             });
183
184             $('.check-all-col').click(function() {
185                 let checkboxes = [];
186                 const to = $(this).data('to');
187                 const val = $('#value_selector').val();
188
189                 $('.limit-action').addClass('disabled');
190                 $('#value_selector').prop('disabled',true);
191
192                 $(`.to-${to}`).each(function() {
193                     const checkbox = $(this);
194                     if (checkbox.data('limit_id')) {
195                         checkboxes.push(checkbox);
196                         checkbox.hide();
197                         $(`#spinner-${checkbox.attr('id')}`).show();
198                     }
199                 });
200
201                 del_limits( checkboxes, val, to );
202                 return false;
203             });
204
205             $('.uncheck-all-col').click(function() {
206                 let checkboxes = [];
207                 const to = $(this).data('to');
208                 const val = $('#value_selector').val();
209
210                 $('.limit-action').addClass('disabled');
211                 $('#value_selector').prop('disabled',true);
212
213                 $(`.to-${to}`).each(function() {
214                     const checkbox = $(this);
215                     if (!checkbox.data('limit_id')) {
216                         checkbox.hide();
217                         $(`#spinner-${checkbox.attr('id')}`).show();
218                     }
219                 });
220
221                 add_limits( checkboxes, val, to );
222                 return false;
223             });
224
225             $('.check-all-row').click(function() {
226                 let checkboxes = [];
227                 const from = $(this).data('from');
228                 const val = $('#value_selector').val();
229
230                 $('.limit-action').addClass('disabled');
231                 $('#value_selector').prop('disabled',true);
232
233                 $(`.from-${from}`).each(function() {
234                     const checkbox = $(this);
235                     if (checkbox.data('limit_id')) {
236                         checkboxes.push(checkbox);
237                         checkbox.hide();
238                         $(`#spinner-${checkbox.attr('id')}`).show();
239                     }
240                 });
241
242                 del_limits( checkboxes, val, null, from );
243                 return false;
244             });
245
246
247             $('.uncheck-all-row').click(function() {
248                 let checkboxes = [];
249                 const from = $(this).data('from');
250                 const val = $('#value_selector').val();
251
252                 $('.limit-action').addClass('disabled');
253                 $('#value_selector').prop('disabled',true);
254
255                 $(`.from-${from}`).each(function() {
256                     const checkbox = $(this);
257                     if (!checkbox.data('limit_id')) {
258                         checkbox.hide();
259                         $(`#spinner-${checkbox.attr('id')}`).show();
260                     }
261                 });
262
263                 add_limits( checkboxes, val, null, from );
264                 return false;
265             });
266
267             $(".checkbox-cell").click(function(e) {
268                 var checkbox = $(this).find(".limit-checkboxes").get(0);
269                 if (checkbox && !checkbox.disabled) {
270                     if (e.target != checkbox) {
271                         checkbox.checked = !checkbox.checked;
272                         $(checkbox).change();
273                     }
274                 }
275             });
276
277             $("#value_selector").on('change', function() {
278                 const val = $('#value_selector').val();
279                 window.history.replaceState(null, "", `/cgi-bin/koha/admin/transfer_limits.pl?code=${val}`);
280                 updateTransferLimitsTable();
281             });
282
283             $(".limit-checkboxes").on('change', function() {
284                 const checkbox = $(this);
285                 const id = checkbox.attr('id');
286
287                 checkbox.hide();
288                 $(`#spinner-${id}`).show();
289
290                 const limit_id = checkbox.data('limit_id');
291
292                 if (limit_id) { // limit id exists, so limit needs to be deleted
293                     delLimit(checkbox);
294                 } else { // limit does not exist, needs to be created
295                     addLimit(checkbox);
296                 }
297             });
298
299             updateTransferLimitsTable();
300
301             const queryString = window.location.search;
302             const urlParams = new URLSearchParams(queryString);
303             const code = urlParams.get('code');
304             if ( code ) {
305                 $('#value_selector').val(code);
306                 updateTransferLimitsTable();
307             }
308         });
309
310         function delLimit(checkbox) {
311             const id = checkbox.attr('id');
312             const limit_id = checkbox.data('limit_id');
313
314             return $.ajax({
315                 url: `/api/v1/transfer_limits/${limit_id}`,
316                 type: 'DELETE',
317                 success: function(result) {
318                     checkbox.data('limit_id', null);
319                     checkbox.attr('checked', true);
320                     $(`#spinner-${id}`).hide();
321                     checkbox.show();
322                 },
323                 error: function(xhr, status, error) {
324                     var errorMessage = xhr.status + ': ' + xhr.statusText
325                     alert('Error - ' + errorMessage);
326                 }
327             });
328         }
329
330         function addLimit(checkbox) {
331             const id = checkbox.attr('id');
332             const parts = id.split('-');
333             const from = parts[1];
334             const to = parts[2];
335
336             const val = $('#value_selector').val();
337
338             let data = {
339                 to_library_id: to,
340                 from_library_id: from,
341             };
342             data[val_type] = val;
343             return $.ajax({
344                 url: `/api/v1/transfer_limits`,
345                 type: 'POST',
346                 data: JSON.stringify(data),
347                 dataType: 'json',
348                 success: function(result) {
349                     checkbox.data('limit_id', result.limit_id);
350                     checkbox.attr('checked', false);
351                     $(`#spinner-${id}`).hide();
352                     checkbox.show();
353                 },
354                 error: function(xhr, status, error) {
355                     var errorMessage = xhr.status + ': ' + xhr.statusText
356                     alert('Error - ' + errorMessage);
357                 }
358             });
359         }
360
361         function add_limits( checkboxes, val, to, from ){
362             let data = {};
363             data[val_type] = val;
364             if (to) data["to_library_id"] = to;
365             if (from) data["from_library_id"] = from;
366
367             return $.ajax({
368                 url: `/api/v1/transfer_limits/batch`,
369                 type: 'POST',
370                 data: JSON.stringify(data),
371                 dataType: 'json',
372                 success: function(result) {
373                     for ( i = 0; i < result.length; i++ ) {
374                         const r = result[i];
375                         let checkbox = $(`#limit-${r.from_library_id}-${r.to_library_id}`);
376                         const id = checkbox.attr('id');
377                         checkbox.data('limit_id', r.limit_id);
378                         checkbox.attr('checked', false);
379                         $(`#spinner-${id}`).hide();
380                         checkbox.show();
381                     }
382                 },
383                 complete: function() {
384                     $('.limit-action').removeClass('disabled');
385                     $('#value_selector').prop('disabled',false);
386                 },
387                 error: function(xhr, status, error) {
388                     var errorMessage = xhr.status + ': ' + xhr.statusText
389                     alert('Error - ' + errorMessage);
390                 }
391             });
392         }
393
394         function del_limits( checkboxes, val, to, from ){
395             let data = {};
396             data[val_type] = val;
397             if (to) data["to_library_id"] = to;
398             if (from) data["from_library_id"] = from;
399
400             return $.ajax({
401                 url: `/api/v1/transfer_limits/batch`,
402                 type: 'DELETE',
403                 data: JSON.stringify(data),
404                 dataType: 'json',
405                 success: function(result) {
406                     for ( i = 0; i < checkboxes.length; i++ ) {
407                         const checkbox = checkboxes[i];
408                         const id = checkbox.attr('id');
409                         checkbox.data('limit_id', '');
410                         checkbox.attr('checked', true);
411                         $(`#spinner-${id}`).hide();
412                         checkbox.show();
413                     }
414                 },
415                 complete: function() {
416                     $('.limit-action').removeClass('disabled');
417                     $('#value_selector').prop('disabled',false);
418                 },
419                 error: function(xhr, status, error) {
420                     var errorMessage = xhr.status + ': ' + xhr.statusText
421                     alert('Error - ' + errorMessage);
422                 }
423             });
424         }
425
426         function updateTransferLimitsTable() {
427             const val = $('#value_selector').val();
428             const url = `/api/v1/transfer_limits?_per_page=-1&q={"${val_type}": "${val}"}`;
429
430             if ( val ) {
431                 $('#transfer_limits').show();
432             } else {
433                 $('#transfer_limits').hide();
434             }
435
436             $(".limit-checkboxes").attr("disabled", true);
437             $(".limit-checkboxes").attr("checked", false);
438
439             if (val) {
440                 $('#loading_limits').show();
441
442                 $.ajax({
443                     dataType: "json",
444                     url: url,
445                     success: function(data) {
446                         $(".limit-checkboxes").attr("disabled", false);
447                         $(".limit-checkboxes").attr("checked", true);
448                         $(".limit-checkboxes").data('limit_id', null);
449
450                         for (var i = 0; i < data.length; i++) {
451                             let limit = data[i];
452                             let checkbox = $(`#limit-${limit.from_library_id}-${limit.to_library_id}`);
453                             checkbox.attr('checked', false);
454                             checkbox.data('limit_id', limit.limit_id);
455                         }
456                     },
457                     complete: function() {
458                         $('#loading_limits').hide();
459                     },
460                     error: function(xhr, status, error) {
461                         var errorMessage = xhr.status + ': ' + xhr.statusText
462                         alert('Error - ' + errorMessage);
463                     }
464                 });
465             }
466         }
467     </script>
468 [% END %]
469 [% INCLUDE 'intranet-bottom.inc' %]