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