Bug 27631: (follow-up) Various corrections
[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 [% USE KohaSpan %]
6 [% INCLUDE 'doc-head-open.inc' %]
7 <title>MARC overlay rules &rsaquo; Koha &rsaquo; Administration</title>
8 [% INCLUDE 'doc-head-close.inc' %]
9
10 <style>
11     .required {
12         background-color: #C00;
13     }
14 </style>
15
16 </head>
17 <body id="admin_marc-overlay-rules" class="admin">
18 [% INCLUDE 'header.inc' %]
19 [% INCLUDE 'cat-search.inc' %]
20
21     <nav id="breadcrumbs" aria-label="Breadcrumb" class="breadcrumb">
22         <ol>
23             <li>
24                 <a href="/cgi-bin/koha/mainpage.pl">Home</a>
25             </li>
26             <li>
27                 <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a>
28             </li>
29             <li>
30                 <a href="#" aria-current="page">
31                      MARC overlay rules
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
41                 <h1>Manage MARC overlay rules</h1>
42
43                 [% FOR m IN messages %]
44                 <div class="dialog [% m.type | html %]">
45                     [% SWITCH m.code %]
46                     [% CASE 'invalid_tag_regexp' %]
47                       <span>Invalid regular expression "[% m.tag | html %]".</span>
48                     [% CASE 'invalid_control_field_actions' %]
49                       <span>Invalid combination of actions for tag [% m.tag | html %]. Control field rules do not allow "Appended: Append" and "Removed: Skip".</span>
50                     [% CASE %]
51                       <span>[% m.code | html %]</span>
52                     [% END %]
53                 </div>
54                 [% END %]
55
56                 [% UNLESS Koha.Preference( 'MARCOverlayRules' ) %]
57                 [% SET pref_MARCOverlayRules_link = '<a href="/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=MARCOverlayRules">MARCOverlayRules</a>' %]
58                 <div class="dialog message">
59                     The [%  pref_MARCOverlayRules_link | $raw | $KohaSpan %] 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="/cgi-bin/koha/admin/marc-overlay-rules.pl" 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="/cgi-bin/koha/admin/marc-overlay-rules.pl" 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="/cgi-bin/koha/admin/marc-overlay-rules.pl" method="post">
255                     <input type="hidden" name="op" value="redo-matching" />
256                 </form>
257
258             </div><!-- /.col-sm-10.col-sm-push-2 -->
259
260             <div class="col-sm-2 col-sm-pull-10">
261                 <aside>
262                     [% INCLUDE 'admin-menu.inc' %]
263                 </aside>
264             </div>
265
266         </div><!-- /.row -->
267     </div><!-- /main container-fluid -->
268
269 [% MACRO jsinclude BLOCK %]
270     [% Asset.js("js/admin-menu.js") | $raw %]
271     [% INCLUDE 'datatables.inc' %]
272
273     <script>
274     $(document).ready(function(){
275         function doSubmit(op, id) {
276             $('<input type="hidden"/>')
277             .attr('name', 'op')
278             .attr('value', op)
279             .appendTo('#marc-overlay-rules-form');
280
281             if(id) {
282                 $('<input type="hidden"/>')
283                 .attr('name', 'id')
284                 .attr('value', id)
285                 .appendTo('#marc-overlay-rules-form');
286             }
287
288             var valid = true;
289             if (op == 'add' || op == 'edit') {
290                 var validate = [
291                     $('#marc-overlay-rules-form input[name="filter"]'),
292                     $('#marc-overlay-rules-form input[name="tag"]')
293                 ];
294                 for(var i = 0; i < validate.length; i++) {
295                     if (validate[i].length) {
296                         if(validate[i].val().length == 0) {
297                             validate[i].addClass('required');
298                             valid = false;
299                         } else {
300                             validate[i].removeClass('required');
301                         }
302                     }
303                 }
304             }
305
306             if (valid) {
307                 $('#marc-overlay-rules-form').submit();
308             }
309
310             return valid;
311         }
312
313         $('#doremove').on('click', function(){
314             doSubmit('doremove');
315         });
316         $('#doedit').on('click', function(){
317             doSubmit('doedit', $("#doedit").attr('value'));
318         });
319         $('#add').on('click', function(){
320             doSubmit('add');
321             return false;
322         });
323         $('#btn_batchremove').on('click', function(){
324             doSubmit('remove');
325         });
326
327         /* Disable batch remove unless one or more checkboxes are checked */
328         $('input[name="batchremove"]').change(function() {
329             if($('input[name="batchremove"]:checked').length > 0) {
330                 $('#btn_batchremove').removeAttr('disabled');
331             } else {
332                 $('#btn_batchremove').attr('disabled', 'disabled');
333             }
334         });
335
336         $.fn.dataTable.ext.order['dom-input'] = function (settings, col) {
337             return this.api().column(col, { order: 'index' }).nodes()
338                 .map(function (td, i) {
339                     if($('input', td).val() != undefined) {
340                         return $('input', td).val();
341                     } else if($('select', td).val() != undefined) {
342                         return $('option[selected="selected"]', td).val();
343                     } else {
344                         return $(td).html();
345                     }
346                 });
347         }
348
349         $('#marc-overlay-rules').dataTable($.extend(true, {}, dataTablesDefaults, {
350             "aoColumns": [
351                 {"bSearchable": false, "bSortable": false},
352                 {"sSortDataType": "dom-input"},
353                 {"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, "sSortDataType": "dom-input"},
359                 {"bSearchable": false, "sSortDataType": "dom-input"},
360                 {"bSearchable": false, "bSortable": false},
361                 {"bSearchable": false, "bSortable": false}
362             ],
363             "pagingType": "simple"
364         }));
365
366         var overlay_rules_presets = {};
367         overlay_rules_presets[_("Protect")] = {
368           'add': 0,
369           'append': 0,
370           'remove': 0,
371           'delete': 0
372         };
373         overlay_rules_presets[_("Overwrite")] = {
374           'add': 1,
375           'append': 1,
376           'remove': 1,
377           'delete': 1
378         };
379         overlay_rules_presets[_("Add new")] = {
380           'add': 1,
381           'append': 0,
382           'remove': 0,
383           'delete': 0
384         };
385         overlay_rules_presets[_("Add and append")] = {
386           'add': 1,
387           'append': 1,
388           'remove': 0,
389           'delete': 0
390         };
391         overlay_rules_presets[_("Protect from deletion")] = {
392           'add': 1,
393           'append': 1,
394           'remove': 1,
395           'delete': 0
396         };
397
398         var overlay_rules_label_to_value = {};
399         overlay_rules_label_to_value[_("Add")] = 1;
400         overlay_rules_label_to_value[_("Append")] = 1;
401         overlay_rules_label_to_value[_("Remove")] = 1;
402         overlay_rules_label_to_value[_("Delete")] = 1;
403         overlay_rules_label_to_value[_("Skip")] = 0;
404
405         function hash_config(config) {
406           return JSON.stringify(config, Object.keys(config).sort());
407         }
408
409         var overlay_rules_preset_map = {};
410         $.each(overlay_rules_presets, function(preset, config) {
411           overlay_rules_preset_map[hash_config(config)] = preset;
412         });
413
414         function operations_config_overlay_rule_preset(config) {
415           return overlay_rules_preset_map[hash_config(config)] || '';
416         }
417
418         /* Set preset values according to operation config */
419         $('.rule').each(function() {
420           var $this = $(this);
421           var operations_config = {};
422           $('.rule-operation-action', $this).each(function() {
423             var $operation = $(this);
424             operations_config[$operation.data('operation')] = overlay_rules_label_to_value[$operation.text()];
425           });
426           $('.rule-preset', $this).text(
427             operations_config_overlay_rule_preset(operations_config) || _("Custom")
428           );
429         });
430
431         /* Listen to operations config changes and set presets accordingly */
432         $('.rule-operation-action-edit select').change(function() {
433           var operations_config = {};
434           var $parent_row = $(this).closest('tr');
435           $('.rule-operation-action-edit select', $parent_row).each(function() {
436             var $this = $(this);
437             operations_config[$this.attr('name')] = parseInt($this.val());
438           });
439           $('select[name="preset"]', $parent_row).val(
440               operations_config_overlay_rule_preset(operations_config)
441           );
442         });
443
444         /* Listen to preset changes and set operations config accordingly */
445         $('select[name="preset"]').change(function() {
446           var $this = $(this);
447           var $parent_row = $this.closest('tr');
448           var preset = $this.val();
449           if (preset) {
450             $.each(overlay_rules_presets[preset], function(operation, action) {
451               $('select[name="' + operation + '"]', $parent_row).val(action);
452             });
453           }
454         });
455
456         var module_filter_options = {
457           source: {
458             '*': '*',
459             batchmod: _("Batch record modification"),
460             intranet: _("Staff client MARC editor"),
461             batchimport: _("Staged MARC import"),
462             z3950: _("Z39.50"),
463             /* bulkmarcimport: _("bulkmarcimport.pl"), */
464             import_lexile: _("import_lexile.pl")
465           },
466           categorycode: {
467             '*': '*',
468             [% FOREACH categorycode IN categorycodes %]
469               [% categorycode.categorycode | html %]: "[% categorycode.description | html %]",
470             [% END %]
471           }
472         };
473
474         //Kind of hack: Replace filter value with label when one exist
475         $('.rule-module').each(function() {
476           var $this = $(this);
477           var module = $this.text();
478           if (module in module_filter_options) {
479             let $filter = $this.siblings('.rule-filter');
480             if ($filter.text() in module_filter_options[module]) {
481               $filter.text(module_filter_options[module][$filter.text()]);
482             }
483           }
484         });
485
486         var $filter_container = $('#filter-container');
487
488         /* Listen to module changes and set filter input accordingly */
489         $('select[name="module"]').change(function() {
490           var $this = $(this);
491           var module_name = $this.val();
492
493           /* Remove current element if any */
494           $filter_container.empty();
495
496           var filter_elem = null;
497           if (module_name in module_filter_options) {
498             // Create select element
499             filter_elem = document.createElement('select');
500             for (var filter_value in module_filter_options[module_name]) {
501               var option = document.createElement('option');
502               option.value = filter_value;
503               option.text = module_filter_options[module_name][filter_value];
504               filter_elem.appendChild(option);
505             }
506           }
507           else {
508             // Create text input element
509             filter_elem = document.createElement('input');
510             filter_elem.type = 'text';
511             filter_elem.setAttribute('size', 5);
512           }
513           filter_elem.name = 'filter';
514           filter_elem.id = 'filter';
515           $filter_container.append(filter_elem);
516         }).change(); // Trigger change
517
518         // Hack: set value if editing rule
519         if ($filter_container.data('filter')) {
520           $('#filter').val($filter_container.data('filter'));
521         }
522
523     });
524     </script>
525 [% END %]
526 [% INCLUDE 'intranet-bottom.inc' %]