Bug 23019: (follow-up) Fix usability issues
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / modules / tools / stage-marc-import.tt
1 [% USE raw %]
2 [% USE Asset %]
3 [% SET footerjs = 1 %]
4 [% INCLUDE 'doc-head-open.inc' %]
5 <title>Koha &rsaquo; Tools &rsaquo; Stage MARC records for import</title>
6 [% INCLUDE 'doc-head-close.inc' %]
7 <style>
8     #fileuploadstatus,#fileuploadfailed,#fileuploadcancel,#jobpanel,#jobstatus,#jobfailed { display : none; }
9 </style>
10
11 [% Asset.css("css/humanmsg.css") | $raw %]
12
13 </head>
14 <body id="tools_stage-marc-import" class="tools">
15 [% INCLUDE 'header.inc' %]
16 [% INCLUDE 'cat-search.inc' %]
17
18 <div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/tools/tools-home.pl">Tools</a> &rsaquo; [% IF ( uploadmarc ) %]<a href="/cgi-bin/koha/tools/stage-marc-import.pl">Stage MARC records for import</a> &rsaquo; Upload Results[% ELSE %]Stage MARC records for import[% END %]</div>
19
20 <div class="main container-fluid">
21     <div class="row">
22         <div class="col-sm-10 col-sm-push-2">
23             <main>
24
25 [% IF ( uploadmarc ) %]
26 <div id="toolbar" class="btn-toolbar">
27         <a class="btn btn-default" href="/cgi-bin/koha/tools/stage-marc-import.pl"><i class="fa fa-plus"></i> Stage MARC records</a>
28         <a class="btn btn-default" href="/cgi-bin/koha/tools/manage-marc-import.pl?import_batch_id=[% import_batch_id | html %]"><i class="fa fa-list-ul"></i> Manage staged records</a>
29 </div>
30 [% END %]
31
32 <h1>Stage MARC records for import</h1>
33 [% IF ( uploadmarc ) %]
34 <p>MARC staging results :</p>
35 <ul>
36     [% SWITCH (record_type) %]
37     [% CASE 'biblio' %]
38         <li>Processing bibliographic records</li>
39     [% CASE 'auth' %]
40         <li>Processing authority records</li>
41     [% END %]
42         <li>[% total | html %]  records in file</li>
43         <li>[% import_errors | html %] records not staged because of MARC error</li>
44         <li>[% staged | html %] records staged</li>
45     [% IF ( checked_matches ) %]
46         <li>[% matched | html %] records with at least one match in catalog per matching rule 
47         &quot;[% matcher_code | html %]&quot;</li>
48     [% ELSE %]
49         [% IF ( matcher_failed ) %]
50           <li>Record matching failed -- unable to retrieve selected matching rule.</li>
51         [% ELSE %]
52           <li>Did not check for matches with existing records in catalog</li>
53         [% END %]
54     [% END %]
55     [% IF record_type == 'biblio' %]
56         <li>[% num_items | html %] item records found and staged</li>
57     [% END %]
58         [% IF ( label_batch ) %]
59           <li>New label batch created: # [% label_batch | html %] </li>
60     [% END %]
61 </ul>
62 [% IF basketno && booksellerid %]
63 <p>
64     <a id="addtobasket" class="btn btn-default" href="/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=[% import_batch_id | html %]&basketno=[% basketno | html %]&booksellerid=[% booksellerid | html %]">Add staged files to basket</a>
65 </p>
66 [% END %]
67 [% ELSE %]
68 <ul>
69     <li>Select a MARC file to stage in the import reservoir.  It will be parsed, and each valid record staged for later import into the catalog.</li>
70     <li>You can enter a name for this import. It may be useful, when creating a record, to remember where the suggested MARC data comes from!</li>
71 </ul>
72 <form method="post" action="[% SCRIPT_NAME | html %]" id="uploadfile" enctype="multipart/form-data">
73 <fieldset class="rows" id="uploadform">
74 <legend>Stage records into the reservoir</legend>
75 <ol>
76         <li>
77         <div id="fileuploadform">
78                 <label for="fileToUpload">Select the file to stage: </label>
79                 <input type="file" id="fileToUpload" name="fileToUpload" />
80         </div>  </li>
81 </ol>
82     <fieldset class="action">
83         <button id="fileuploadbutton">Upload file</button>
84         <button id="fileuploadcancel">Cancel</button>
85     </fieldset>
86 </fieldset>
87                 
88     <div id="fileuploadpanel">
89         <div id="fileuploadstatus" class="progress_panel">Upload progress:
90             <progress id="fileuploadprogress" max="100" value="0">
91             </progress>
92             <span class="fileuploadpercent">0</span>%
93         </div>
94         <div id="fileuploadfailed"></div>
95     </div>
96 </form>
97
98 <fieldset class="rows" id="profile_fieldset">
99     <legend>Profile settings</legend>
100     <ol>
101         <li>
102             <label for="profile">Pre-fill values with profile</label>
103             <select name="profile" id="profile">
104                 <option value="">Do not use profile</option>
105             </select>
106             <div class="hint">When you select a profile it pre-fills your form with profile values.</div>
107             <div class="hint">Later you can modify your form and that's what matters on import.</div>
108         </li>
109     </ol>
110 </fieldset>
111
112     <form method="post" id="processfile" action="[% SCRIPT_NAME | html %]" enctype="multipart/form-data">
113 [% IF basketno && booksellerid %]
114     <input type="hidden" name="basketno" id="basketno" value="[% basketno | html %]" />
115     <input type="hidden" name="booksellerid" id="booksellerid" value="[% booksellerid | html %]" />
116 [% END %]
117     <input type="hidden" name="profile_id" id="profile_id"/>
118 <fieldset class="rows">
119         <input type="hidden" name="uploadedfileid" id="uploadedfileid" value="" />
120         <input type="hidden" name="runinbackground" id="runinbackground" value="" />
121         <input type="hidden" name="completedJobID" id="completedJobID" value="" />
122         <ol><li>
123                 <label for="comments">Comments about this file: </label>
124                 <input type="text" id="comments" name="comments" />
125                 
126         </li>
127     <li>
128         <label for='record_type'>Record type:</label>
129         <select name='record_type' id='record_type'>
130             <option value='biblio' selected='selected'>Bibliographic</option>
131             <option value='auth'>Authority</option>
132         </select>
133     </li>
134         <li>
135                 <label for="encoding">Character encoding: </label>
136             <select name="encoding" id="encoding">
137                 <option value="UTF-8" selected="selected">UTF-8 (Default)</option>
138                 <option value="MARC-8">MARC 8</option>
139                 <option value="ISO_5426">ISO 5426</option>
140                 <option value="ISO_6937">ISO 6937</option>
141                 <option value="ISO_8859-1">ISO 8859-1</option>
142                 <option value="EUC-KR">EUC-KR</option>
143             </select>
144         </li>
145     <li>
146         <label for='format'>Format:</label>
147         <select name='format' id='format'>
148             <option value='ISO2709'>MARC</option>
149             <option value='MARCXML'>MARCXML</option>
150             [% FOREACH p IN plugins %]
151                 <option value="[% p.metadata.class | html %]">[% p.metadata.name | html %] ( other format via plugin)</option>
152             [% END %]
153         </select>
154     </li>
155 </ol></fieldset>
156
157   [% IF MarcModificationTemplatesLoop %]
158     <fieldset class="rows">
159       <legend>Use MARC Modification Template:</legend>
160       <ol>
161         <li>
162           <label for="comments">Modify record using the following template: </label>
163           <select name="marc_modification_template_id" id="marc_modification_template_id">
164             <option value="">Do not use.</option>
165               [% FOREACH mmt IN MarcModificationTemplatesLoop %]
166                 <option value="[% mmt.template_id | html %]">[% mmt.name | html %]</option>
167               [% END %]
168           </select>
169         </li>
170       </ol>
171     </fieldset>
172   [% END %]
173
174   <fieldset class="rows">
175     <legend>Look for existing records in catalog?</legend>
176     <ol><li><label for="matcher">Record matching rule:</label>
177     <select name="matcher" id="matcher">
178        <option value="">Do not look for matching records</option> 
179        [% FOREACH available_matcher IN available_matchers %]
180           <option value="[% available_matcher.matcher_id | html %]">[% available_matcher.code | html %] ([% available_matcher.description | html %])
181           </option>
182        [% END %]
183     </select>
184     </li>
185       <li><label for="overlay_action">Action if matching record found: </label>
186            [% INCLUDE 'tools-overlay-action.inc' %]
187       </li>
188       <li><label for="nomatch_action">Action if no match is found: </label>
189            [% INCLUDE 'tools-nomatch-action.inc' %]
190       </li>
191     </ol>
192   </fieldset>
193   <fieldset class="rows" id="items">
194     <legend>Check for embedded item record data?</legend>
195     <ol>
196       <li class="radio">
197         <input type="radio" id="parse_itemsyes" name="parse_items" value="1" checked="checked" />
198         <label for="parse_itemsyes">Yes</label>
199       </li>
200       <li class="radio">
201         <input type="radio" id="parse_itemsno" name="parse_items" value="0" />
202         <label for="parse_itemsno">No</label>
203       </li>
204     </ol>
205     <ol>
206       <li><label for="item_action">How to process items: </label>
207            [% INCLUDE 'tools-item-action.inc' %]
208       </li>
209     </ol>
210   </fieldset>
211   <fieldset class="action">
212     <input type="button" id="mainformsubmit" value="Stage for import" />
213     <button id="add_profile" disabled>Save profile</button>
214     <input type="text" id="profile_name" name="profile_name" placeholder="Profile name"/>
215     <button id="del_profile" disabled>Remove profile</button>
216   </fieldset>
217  
218        <div id="jobpanel"><div id="jobstatus" class="progress_panel">Job progress: <div id="jobprogress"></div> <span id="jobprogresspercent">0</span>%</div>
219      <div id="jobfailed"></div></div>
220   
221 </form>
222 [% END %]
223
224             </main>
225         </div> <!-- /.col-sm-10.col-sm-push-2 -->
226
227         <div class="col-sm-2 col-sm-pull-10">
228             <aside>
229                 [% INCLUDE 'tools-menu.inc' %]
230             </aside>
231         </div> <!-- /.col-sm-2.col-sm-pull-10 -->
232      </div> <!-- /.row -->
233
234 [% MACRO jsinclude BLOCK %]
235     [% Asset.js("js/tools-menu.js") | $raw %]
236     [% Asset.js("lib/jquery/plugins/humanmsg.js") | $raw %]
237     [% Asset.js("js/background-job-progressbar.js") | $raw %]
238     [% Asset.js("js/file-upload.js") | $raw %]
239     <script>
240         var xhr;
241         var PROFILE_SAVE_MSG = _("Profile saved");
242         var PROFILE_DEL_MSG = _("Profile deleted");
243         $(document).ready(function(){
244             $("#processfile").hide();
245             $('#profile_fieldset').hide();
246             $("#record_type").change(function() {
247                 if ($(this).val() == 'auth') {
248                     $('#items').hide();
249                 } else {
250                     $('#items').show();
251                 }
252             });
253             $("#fileuploadbutton").on("click",function(e){
254                 e.preventDefault();
255                 StartUpload();
256             });
257             $("#fileuploadcancel").on("click",function(e){
258                 e.preventDefault();
259                 CancelUpload();
260             });
261             $("#mainformsubmit").on("click",function(){
262                 return CheckForm( document.getElementById("processfile"));
263             });
264             getProfiles();
265             $('#profile').change(function(){
266                 if(this.value=='') {
267                     $("#mod_profile, #del_profile").prop("disabled",true);
268                     $("#profile_id").val("");
269                     $("#comments").val("");
270                     $("#record_type").val('biblio').change();
271                     $("#encoding").val('UTF-8').change();
272                     $("#format").val('ISO2709').change();
273                     $("#marc_modification_template_id").val("").change();
274                     $("#matcher").val("").change();
275                     $("#overlay_action").val('replace').change();
276                     $("#nomatch_action").val('create_new').change();
277                     $("#parse_itemsyes").prop("checked", true).change();
278                     $("#item_action").val('always_add').change();
279                     $("#profile_name").val('').keyup();
280                 } else {
281                     const profile = $('option:selected', this).data('profile');
282                     $("#profile_id").val(profile.profile_id);
283                     $("#mod_profile, #del_profile").prop("disabled", null);
284                     $("#comments").val(profile.comments);
285                     $("#record_type").val(profile.record_type).change();
286                     $("#encoding").val(profile.encoding).change();
287                     $("#format").val(profile.format).change();
288                     $("#marc_modification_template_id").val(profile.template_id).change();
289                     $("#matcher").val(profile.matcher_id).change();
290                     $("#overlay_action").val(profile.overlay_action).change();
291                     $("#nomatch_action").val(profile.nomatch_action).change();
292                     $("input[name='parse_items'][value='"+(profile.parse_items?'1':'0')+"']").prop("checked", true).change();
293                     $("#item_action").val(profile.item_action).change();
294                     $("#profile_name").val(profile.name).keyup();
295                 }
296             });
297
298             $("#profile_name").keyup(function(){
299                 $("#add_profile").prop("disabled", this.value.trim()=='');
300             });
301
302             $("#add_profile").click(function(event) {
303                 event.preventDefault();
304                 var name = $("#profile_name").val().trim();
305                 if(!name) {
306                     alert(_("Profile must have a name"));
307                     return;
308                 }
309
310                 var profile = $("#profile option[value!='']")
311                     .map(function() {
312                         return $(this).data('profile');
313                     })
314                     .filter(function() {
315                         return this.name == name;
316                     });
317
318                 if(profile.length) {
319                     if(!confirm(_("There is another profile with this name.")+"\n\n"+_("Do you want to update it with new values?"))) {
320                         return;
321                     }
322                 }
323
324                 new Promise(function(resolve, reject) {
325
326                     const params = {
327                         comments: $("#comments").val() || null,
328                         record_type: $("#record_type").val() || null,
329                         encoding: $("#encoding").val() || null,
330                         format: $("#format").val() || null,
331                         template_id: $("#marc_modification_template_id").val() || null,
332                         matcher_id: $("#matcher").val() || null,
333                         overlay_action: $("#overlay_action").val() || null,
334                         nomatch_action: $("#nomatch_action").val() || null,
335                         parse_items: !!parseInt($("input[name='parse_items']:checked").val()) || null,
336                         item_action: $("#item_action").val() || null,
337                         name: name
338                     };
339
340                     if(profile.length) {
341                         $.ajax({
342                             url: "/api/v1/import_batch_profiles/"+profile[0].profile_id,
343                             method: "PUT",
344                             data: JSON.stringify(params),
345                             contentType: 'application/json'
346                         })
347                         .done(resolve)
348                         .fail(reject);
349                     } else {
350                         $.ajax({
351                             url: "/api/v1/import_batch_profiles/",
352                             method: "POST",
353                             data: JSON.stringify(params),
354                             contentType: 'application/json'
355                         })
356                         .done(resolve)
357                         .fail(reject);
358                     }
359                 })
360                 .then(function(profile) {
361                     humanMsg.displayAlert(PROFILE_SAVE_MSG);
362                     return getProfiles(profile.profile_id);
363                 })
364                 .catch(function(error) {
365                     alert(_("An error occurred")+"\n\n"+error);
366                 })
367             });
368
369             $("#del_profile").click(function(event) {
370                 event.preventDefault();
371                 var id = $("#profile").val();
372                 if(!id) return;
373                 if(!confirm(_("Are you sure you want to delete this profile?"))) {
374                     return;
375                 }
376                 new Promise(function(resolve, reject) {
377                     $.ajax({
378                         url: "/api/v1/import_batch_profiles/"+id,
379                         method: "DELETE"
380                     })
381                     .done(resolve)
382                     .fail(reject);
383                 })
384                 .then(function() {
385                     humanMsg.displayAlert(PROFILE_DEL_MSG);
386                     return getProfiles();
387                 })
388                 .catch(function(error) {
389                     alert(_("An error occurred")+"\n\n"+error);
390                 })
391             });
392         });
393
394         function CheckForm(f) {
395             if ($("#fileToUpload").value == '') {
396                 alert(_("Please upload a file first."));
397             } else {
398                 return submitBackgroundJob(f);
399             }
400             return false;
401         }
402         function StartUpload() {
403             if( $('#fileToUpload').prop('files').length == 0 ) return;
404             $('#fileuploadbutton').hide();
405             $("#fileuploadfailed").hide();
406             $("#processfile").hide();
407             $('#profile_fieldset').hide();
408             $("#fileuploadstatus").show();
409             $("#uploadedfileid").val('');
410             xhr= AjaxUpload( $('#fileToUpload'), $('#fileuploadprogress'), 'temp=1', cbUpload );
411             $("#fileuploadcancel").show();
412         }
413         function CancelUpload() {
414             if( xhr ) xhr.abort();
415             $("#fileuploadstatus").hide();
416             $('#fileuploadbutton').show();
417             $("#fileuploadcancel").hide();
418             $("#fileuploadfailed").show();
419             $("#fileuploadfailed").text( _("Upload status: Cancelled ") );
420         }
421         function cbUpload( status, fileid, errors ) {
422             if( status=='done' ) {
423                 $("#uploadedfileid").val( fileid );
424                 $('#fileToUpload').prop('disabled',true);
425                 $('#fileuploadbutton').prop('disabled',true);
426                 $('#fileuploadbutton').show();
427                 $("#fileuploadcancel").hide();
428                 var filename=$('#fileToUpload').prop('files')[0].name;
429                 if( filename.match( new RegExp(/\.[^.]+xml$/) ) ) {
430                     $('#format').val('MARCXML');
431                 }
432                 $("#processfile").show();
433                 $('#profile_fieldset').show();
434             } else {
435                 var errMsgs = [ _("Error code 0 not used"), _("File already exists"), _("Directory is not writeable"), _("Root directory for uploads not defined"), _("Temporary directory for uploads not defined") ];
436                 var errCode = errors[$('#fileToUpload').prop('files')[0].name].code;
437                 $('#fileuploadbutton').show();
438                 $("#fileuploadcancel").hide();
439                 $("#fileuploadstatus").hide();
440                 $("#fileuploadfailed").show();
441                 $("#fileuploadfailed").text( _("Upload status: ") +
442                     ( status=='failed'? _("Failed") + " - (" + errCode + ") " + errMsgs[errCode]:
443                     ( status=='denied'? _("Denied"): status ))
444                 );
445             }
446         }
447
448         function getProfiles(id) {
449             const select = $("#profile");
450             $("option[value!='']", select).remove();
451             return new Promise(function(resolve, reject) {
452                 $.ajax("/api/v1/import_batch_profiles")
453                 .then(resolve, reject);
454             })
455             .then(function(profiles) {
456                 profiles.forEach(function(profile) {
457                     const opt = $("<option/>");
458                     select.append(opt);
459                     if(id && profile.profile_id == id) {
460                         opt.prop('selected', true);
461                     }
462                     opt.attr("value", profile.profile_id);
463                     opt.html(profile.name);
464                     opt.data("profile", profile);
465                 });
466             })
467             .then(function(){
468                 select.change();
469             });
470         }
471
472
473     </script>
474 [% END %]
475
476 [% INCLUDE 'intranet-bottom.inc' %]