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