Bug 14957: (QA follow-up) Update for bug 27846
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / modules / admin / marc-overlay-rules.tt
1 [% USE raw %]
2 [% USE Asset %]
3 [% SET footerjs = 1 %]
4 [% USE Koha %]
5 [% INCLUDE 'doc-head-open.inc' %]
6 <title>MARC overlay rules &rsaquo; Koha &rsaquo; Administration</title>
7 [% INCLUDE 'doc-head-close.inc' %]
8 [% Asset.css("css/datatables.css") | $raw %]
9 [% INCLUDE 'datatables.inc' %]
10
11 <style>
12     .required {
13         background-color: #C00;
14     }
15 </style>
16
17 </head>
18 <body id="admin_marc-overlay-rules" class="admin">
19 [% INCLUDE 'header.inc' %]
20 [% INCLUDE 'cat-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">
32                  MARC overlay rules
33             </a>
34         </li>
35     </ol>
36 </nav>
37
38 <div class="main container-fluid">
39 <div class="row">
40 <div class="col-sm-10 col-sm-push-2">
41
42 <h1>Manage MARC overlay rules</h1>
43
44 [% FOR m IN messages %]
45   <div class="dialog [% m.type | html %]">
46     [% SWITCH m.code %]
47     [% CASE 'invalid_tag_regexp' %]
48       Invalid regular expression "[% m.tag | html %]".
49     [% CASE 'invalid_control_field_actions' %]
50       Invalid combination of actions for tag [% m.tag | html %]. Control field rules do not allow "Appended: Append" and "Removed: Skip".
51     [% CASE %]
52       [% m.code | html %]
53     [% END %]
54   </div>
55 [% END %]
56
57 [% UNLESS Koha.Preference( 'MARCOverlayRules' ) %]
58     <div class="dialog message">
59         The <b>MARCOverlayRules</b> preference is not set, don't forget to enable it for rules to take effect.
60     </div>
61 [% END %]
62 [% IF removeConfirm %]
63 <div class="dialog alert">
64 <h3>Remove rule?</h3>
65 <p>Are you sure you want to remove the selected rule(s)?</p>
66
67 <form action="[% script_name | uri %]" method="GET">
68     <button type="submit" class="deny"><i class="fa fa-fw fa-remove"></i> No, do not remove</button>
69 </form>
70     <button type="button" class="approve" id="doremove"><i class="fa fa-fw fa-check"></i> Yes, remove</button>
71 </div>
72 [% END %]
73
74 <form action="[% script_name | uri %]" method="POST" id="marc-overlay-rules-form">
75 <table id="marc-overlay-rules">
76     <thead><tr>
77         <th>Rule</th>
78         <th>Module</th>
79         <th>Filter</th>
80         <th>Tag</th>
81         <th>Preset</th>
82         <th>Added <i id="info_added" data-toggle="tooltip" title="If a field matching the rule tag only exists in the incoming record" data-placement="right" class="fa fa-info-circle"></i></th>
83         <th>Appended <i id="info_appended" data-toggle="tooltip" title="If the original record has one or more fields matching with the rule tag, but one or more fields matching the rule tag differ in the incoming record" data-placement="right" class="fa fa-info-circle"></i></th>
84         <th>Removed <i id="info_removed" data-toggle="tooltip" title="If the original record has a field matching the rule tag, but the matching field is not in the incoming record" data-placement="right" class="fa fa-info-circle"></i></th>
85         <th>Deleted <i id="info_deleted" data-toggle="tooltip" title="If the original record has fields matching the rule tag, but no fields with this are found in the incoming record" data-placement="right" class="fa fa-info-circle"></i></th>
86         <th>Actions</th>
87         <th>&nbsp;</th>
88     </tr></thead>
89     [% UNLESS edit %]
90     <tfoot>
91         <tr class="rule-new">
92             <th>&nbsp;</th>
93             <th>
94                 <select name="module">
95                     <option value="source">Source</option>
96                     <option value="categorycode">User category</option>
97                     <option value="userid">Username</option>
98                 </select>
99             </th>
100             <th id="filter-container"></th>
101             <th><input type="text" size="5" name="tag"/></th>
102             <th>
103                 <select name="preset">
104                     <option value="" selected>Custom</option>
105                     <option value="Protect">Protect</option>
106                     <option value="Overwrite">Overwrite</option>
107                     <option value="Add new">Add new</option>
108                     <option value="Add and append">Add and append</option>
109                     <option value="Protect from deletion">Protect from deletion</option>
110                 </select>
111             </th>
112             <th class="rule-operation-action-edit">
113                 <select name="add">
114                     <option value="0">Skip</option>
115                     <option value="1">Add</option>
116                 </select>
117             </th>
118             <th class="rule-operation-action-edit">
119                 <select name="append">
120                     <option value="0">Skip</option>
121                     <option value="1">Append</option>
122                 </select>
123             </th>
124             <th class="rule-operation-action-edit">
125                 <select name="remove">
126                     <option value="0">Skip</option>
127                     <option value="1">Remove</option>
128                 </select>
129             </th>
130             <th class="rule-operation-action-edit">
131                 <select name="delete">
132                     <option value="0">Skip</option>
133                     <option value="1">Delete</option>
134                 </select>
135             </th>
136             <th><button class="btn btn-default btn-xs" title="Add" id="add"><i class="fa fa-plus"></i> Add rule</button></th>
137             <th><button id="btn_batchremove" disabled="disabled" class="btn btn-default btn-xs" title="Batch remove"><i class="fa fa-trash"></i> Delete selected</button></th>
138         </tr>
139     </tfoot>
140     [% END %]
141     <tbody>
142         [% FOREACH rule IN rules %]
143             <tr id="[% rule.id | html %]" class="rule[% IF rule.edit %]-edit[% END %]">
144             [% IF rule.edit %]
145                 <td>[% rule.id | html %]</td>
146                 <td>
147                     <select name="module">
148                         [% IF rule.module == "source" %]
149                             <option value="source" selected="selected">Source</option>
150                         [% ELSE %]
151                             <option value="source">Source</option>
152                         [% END %]
153                         [% IF rule.module == "categorycode" %]
154                             <option value="categorycode" selected="selected">User category</option>
155                         [% ELSE %]
156                             <option value="categorycode">User category</option>
157                         [% END %]
158                         [% IF rule.module == "userid" %]
159                             <option value="userid" selected="selected">Username</option>
160                         [% ELSE %]
161                             <option value="userid">Username</option>
162                         [% END %]
163                     </select>
164                 </td>
165                 <td id="filter-container" data-filter="[% rule.filter | html %]"></td>
166                 <td><input type="text" size="3" name="tag" value="[% rule.tag | html %]"/></td>
167                 <th>
168                     <select name="preset">
169                         <option value="" selected>Custom</option>
170                         <option value="Protect">Protect</option>
171                         <option value="Overwrite">Overwrite</option>
172                         <option value="Add new">Add new</option>
173                         <option value="Add and append">Add and append</option>
174                         <option value="Protect from deletion">Protect from deletion</option>
175                     </select>
176                 </th>
177                 <td class="rule-operation-action-edit">
178                     <select name="add">
179                         [% IF rule.add %]
180                             <option value="0">Skip</option>
181                             <option value="1" selected="selected">Add</option>
182                         [% ELSE %]
183                             <option value="0" selected="selected">Skip</option>
184                             <option value="1">Add</option>
185                         [% END %]
186                     </select>
187                 </td>
188                 <td class="rule-operation-action-edit">
189                     <select name="append">
190                         [% IF rule.append %]
191                             <option value="0">Skip</option>
192                             <option value="1" selected="selected">Append</option>
193                         [% ELSE %]
194                             <option value="0" selected="selected">Skip</option>
195                             <option value="1">Append</option>
196                         [% END %]
197                     </select>
198                 </td>
199                 <td class="rule-operation-action-edit">
200                     <select name="remove">
201                         [% IF rule.remove %]
202                             <option value="0">Skip</option>
203                             <option value="1" selected="selected">Remove</option>
204                         [% ELSE %]
205                             <option value="0" selected="selected">Skip</option>
206                             <option value="1">Remove</option>
207                         [% END %]
208                     </select>
209                 </td>
210                 <td class="rule-operation-action-edit">
211                     <select name="delete">
212                         [% IF rule.delete %]
213                             <option value="0">Skip</option>
214                             <option value="1" selected="selected">Delete</option>
215                         [% ELSE %]
216                             <option value="0" selected="selected">Skip</option>
217                             <option value="1">Delete</option>
218                         [% END %]
219                     </select>
220                 </td>
221                 <td class="actions">
222                     <button class="btn btn-default btn-xs" title="Save" id="doedit" value="[% rule.id | html %]"><i class="fa fa-check"></i> Save</button>
223                     <button type="submit" class="btn btn-default btn-xs" title="Cancel" ><i class="fa fa-times"></i> Cancel</button>
224                 </td>
225                 <td></td>
226             [% ELSE %]
227                 <td>[% rule.id | html %]</td>
228                 <td class="rule-module">[% rule.module | html %]</td>
229                 <td class="rule-filter">[% rule.filter | html %]</td>
230                 <td>[% rule.tag | html %]</td>
231                 <td class="rule-preset"></td>
232                 <td class="rule-operation-action" data-operation="add">[% IF rule.add %]Add[% ELSE %]Skip[% END %]</td>
233                 <td class="rule-operation-action" data-operation="append">[% IF rule.append %]Append[% ELSE %]Skip[% END %]</td>
234                 <td class="rule-operation-action" data-operation="remove">[% IF rule.remove %]Remove[% ELSE %]Skip[% END %]</td>
235                 <td class="rule-operation-action" data-operation="delete">[% IF rule.delete %]Delete[% ELSE %]Skip[% END %]</td>
236                 <td class="actions">
237                     <a href="?op=remove&id=[% rule.id | uri %]" title="Delete" class="btn btn-default btn-xs"><i class="fa fa-trash"></i> Delete</a>
238                     <a href="?op=edit&id=[% rule.id | uri %]" title="Edit" class="btn btn-default btn-xs"><i class="fa fa-pencil"></i> Edit</a>
239                 </td>
240                 <td>
241                     [% IF rule.removemarked %]
242                         <input type="checkbox" name="batchremove" value="[% rule.id | html %]" checked="checked"/>
243                     [% ELSE %]
244                         <input type="checkbox" name="batchremove" value="[% rule.id | html %]"/>
245                     [% END %]
246                 </td>
247             [% END %]
248             </tr>
249         [% END %]
250     </tbody>
251 </table>
252 </form>
253
254 <form action="[% script_name | uri %]" method="post">
255 <input type="hidden" name="op" value="redo-matching" />
256 </form>
257
258 </div>
259 <!-- /.col-sm-10.col-sm-push-2 -->
260
261 <div class="col-sm-2 col-sm-pull-10">
262     <aside>
263         [% INCLUDE 'admin-menu.inc' %]
264     </aside>
265 </div>
266
267 </div><!-- /.row -->
268 </div><!-- /main container-fluid -->
269
270 [% MACRO jsinclude BLOCK %]
271     <script>
272     function doSubmit(op, id) {
273         $('<input type="hidden"/>')
274         .attr('name', 'op')
275         .attr('value', op)
276         .appendTo('#marc-overlay-rules-form');
277
278         if(id) {
279             $('<input type="hidden"/>')
280             .attr('name', 'id')
281             .attr('value', id)
282             .appendTo('#marc-overlay-rules-form');
283         }
284
285         var valid = true;
286         if (op == 'add' || op == 'edit') {
287             var validate = [
288                 $('#marc-overlay-rules-form input[name="filter"]'),
289                 $('#marc-overlay-rules-form input[name="tag"]')
290             ];
291             for(var i = 0; i < validate.length; i++) {
292                 if (validate[i].length) {
293                     if(validate[i].val().length == 0) {
294                         validate[i].addClass('required');
295                         valid = false;
296                     } else {
297                         validate[i].removeClass('required');
298                     }
299                 }
300             }
301         }
302
303         if (valid) {
304             $('#marc-overlay-rules-form').submit();
305         }
306
307         return valid;
308     }
309
310     $(document).ready(function(){
311         $('#doremove').on('click', function(){
312             doSubmit('doremove');
313         });
314         $('#doedit').on('click', function(){
315             doSubmit('doedit', $("#doedit").attr('value'));
316         });
317         $('#add').on('click', function(){
318             doSubmit('add');
319             return false;
320         });
321         $('#btn_batchremove').on('click', function(){
322             doSubmit('remove');
323         });
324
325         /* Disable batch remove unless one or more checkboxes are checked */
326         $('input[name="batchremove"]').change(function() {
327             if($('input[name="batchremove"]:checked').length > 0) {
328                 $('#btn_batchremove').removeAttr('disabled');
329             } else {
330                 $('#btn_batchremove').attr('disabled', 'disabled');
331             }
332         });
333
334         $.fn.dataTable.ext.order['dom-input'] = function (settings, col) {
335             return this.api().column(col, { order: 'index' }).nodes()
336                 .map(function (td, i) {
337                     if($('input', td).val() != undefined) {
338                         return $('input', td).val();
339                     } else if($('select', td).val() != undefined) {
340                         return $('option[selected="selected"]', td).val();
341                     } else {
342                         return $(td).html();
343                     }
344                 });
345         }
346
347         $('#marc-overlay-rules').dataTable($.extend(true, {}, dataTablesDefaults, {
348             "aoColumns": [
349                 {"bSearchable": false, "bSortable": false},
350                 {"sSortDataType": "dom-input"},
351                 {"sSortDataType": "dom-input"},
352                 {"bSearchable": false, "sSortDataType": "dom-input"},
353                 {"bSearchable": false, "sSortDataType": "dom-input"},
354                 {"bSearchable": false, "sSortDataType": "dom-input"},
355                 {"bSearchable": false, "sSortDataType": "dom-input"},
356                 {"bSearchable": false, "sSortDataType": "dom-input"},
357                 {"bSearchable": false, "sSortDataType": "dom-input"},
358                 {"bSearchable": false, "bSortable": false},
359                 {"bSearchable": false, "bSortable": false}
360             ],
361             "pagingType": "simple"
362         }));
363
364         var overlay_rules_presets = {};
365         overlay_rules_presets[_("Protect")] = {
366           'add': 0,
367           'append': 0,
368           'remove': 0,
369           'delete': 0
370         };
371         overlay_rules_presets[_("Overwrite")] = {
372           'add': 1,
373           'append': 1,
374           'remove': 1,
375           'delete': 1
376         };
377         overlay_rules_presets[_("Add new")] = {
378           'add': 1,
379           'append': 0,
380           'remove': 0,
381           'delete': 0
382         };
383         overlay_rules_presets[_("Add and append")] = {
384           'add': 1,
385           'append': 1,
386           'remove': 0,
387           'delete': 0
388         };
389         overlay_rules_presets[_("Protect from deletion")] = {
390           'add': 1,
391           'append': 1,
392           'remove': 1,
393           'delete': 0
394         };
395
396         var overlay_rules_label_to_value = {};
397         overlay_rules_label_to_value[_("Add")] = 1;
398         overlay_rules_label_to_value[_("Append")] = 1;
399         overlay_rules_label_to_value[_("Remove")] = 1;
400         overlay_rules_label_to_value[_("Delete")] = 1;
401         overlay_rules_label_to_value[_("Skip")] = 0;
402
403         function hash_config(config) {
404           return JSON.stringify(config, Object.keys(config).sort());
405         }
406
407         var overlay_rules_preset_map = {};
408         $.each(overlay_rules_presets, function(preset, config) {
409           overlay_rules_preset_map[hash_config(config)] = preset;
410         });
411
412         function operations_config_overlay_rule_preset(config) {
413           return overlay_rules_preset_map[hash_config(config)] || '';
414         }
415
416         /* Set preset values according to operation config */
417         $('.rule').each(function() {
418           var $this = $(this);
419           var operations_config = {};
420           $('.rule-operation-action', $this).each(function() {
421             var $operation = $(this);
422             operations_config[$operation.data('operation')] = overlay_rules_label_to_value[$operation.text()];
423           });
424           $('.rule-preset', $this).text(
425             operations_config_overlay_rule_preset(operations_config) || _("Custom")
426           );
427         });
428
429         /* Listen to operations config changes and set presets accordingly */
430         $('.rule-operation-action-edit select').change(function() {
431           var operations_config = {};
432           var $parent_row = $(this).closest('tr');
433           $('.rule-operation-action-edit select', $parent_row).each(function() {
434             var $this = $(this);
435             operations_config[$this.attr('name')] = parseInt($this.val());
436           });
437           $('select[name="preset"]', $parent_row).val(
438               operations_config_overlay_rule_preset(operations_config)
439           );
440         });
441
442         /* Listen to preset changes and set operations config accordingly */
443         $('select[name="preset"]').change(function() {
444           var $this = $(this);
445           var $parent_row = $this.closest('tr');
446           var preset = $this.val();
447           if (preset) {
448             $.each(overlay_rules_presets[preset], function(operation, action) {
449               $('select[name="' + operation + '"]', $parent_row).val(action);
450             });
451           }
452         });
453
454         var module_filter_options = {
455           source: {
456             '*': '*',
457             batchmod: _("Batch record modification"),
458             intranet: _("Staff client MARC editor"),
459             batchimport: _("Staged MARC import"),
460             z3950: _("Z39.50"),
461             /* bulkmarcimport: _("bulkmarcimport.pl"), */
462             import_lexile: _("import_lexile.pl")
463           },
464           categorycode: {
465             '*': '*',
466             [% FOREACH categorycode IN categorycodes %]
467               [% categorycode.categorycode | html %]: "[% categorycode.description | html %]",
468             [% END %]
469           }
470         };
471
472         //Kind of hack: Replace filter value with label when one exist
473         $('.rule-module').each(function() {
474           var $this = $(this);
475           var module = $this.text();
476           if (module in module_filter_options) {
477             let $filter = $this.siblings('.rule-filter');
478             if ($filter.text() in module_filter_options[module]) {
479               $filter.text(module_filter_options[module][$filter.text()]);
480             }
481           }
482         });
483
484         var $filter_container = $('#filter-container');
485
486         /* Listen to module changes and set filter input accordingly */
487         $('select[name="module"]').change(function() {
488           var $this = $(this);
489           var module_name = $this.val();
490
491           /* Remove current element if any */
492           $filter_container.empty();
493
494           var filter_elem = null;
495           if (module_name in module_filter_options) {
496             // Create select element
497             filter_elem = document.createElement('select');
498             for (var filter_value in module_filter_options[module_name]) {
499               var option = document.createElement('option');
500               option.value = filter_value;
501               option.text = module_filter_options[module_name][filter_value];
502               filter_elem.appendChild(option);
503             }
504           }
505           else {
506             // Create text input element
507             filter_elem = document.createElement('input');
508             filter_elem.type = 'text';
509             filter_elem.setAttribute('size', 5);
510           }
511           filter_elem.name = 'filter';
512           filter_elem.id = 'filter';
513           $filter_container.append(filter_elem);
514         }).change(); // Trigger change
515
516         // Hack: set value if editing rule
517         if ($filter_container.data('filter')) {
518           $('#filter').val($filter_container.data('filter'));
519         }
520
521     });
522     </script>
523 [% END %]
524 [% INCLUDE 'intranet-bottom.inc' %]