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