4 [% INCLUDE 'doc-head-open.inc' %]
5 <title>Quote uploader › Tools › Koha</title>
6 [% INCLUDE 'doc-head-close.inc' %]
7 [% Asset.css("css/uploader.css") | $raw %]
8 [% Asset.css("css/quotes.css") | $raw %]
11 <body id="tools_quotes" class="tools">
12 [% INCLUDE 'header.inc' %]
13 [% INCLUDE 'cat-search.inc' %]
15 <nav id="breadcrumbs" aria-label="Breadcrumb" class="breadcrumb">
18 <a href="/cgi-bin/koha/mainpage.pl">Home</a>
21 <a href="/cgi-bin/koha/tools/tools-home.pl">Tools</a>
24 <a href="/cgi-bin/koha/tools/quotes.pl">Quote editor</a>
27 <a href="#" aria-current="page">
34 <div class="main container-fluid">
36 <div class="col-sm-10 col-sm-push-2">
38 <div id="toolbar" class="btn-toolbar" style="visibility: hidden; position: absolute">
39 <div class="btn-group"><a class="btn btn-default" id="save_quotes" href="#"><i class="fa fa-save"></i> Save quotes</a></div>
40 <div class="btn-group"><a href="/cgi-bin/koha/tools/quotes-upload.pl" id="cancel_quotes" class="btn btn-default"><i class="fa fa-remove"></i> Cancel import</a></div>
43 <h2>Quote uploader</h2>
45 <div id="messages" style="display: none;">
46 <div class="import_success dialog message" style="display: none;"></div>
47 <div class="import_errors dialog alert" style="display: none;"></div>
50 <div id="csv_error" class="alert" style="visibility: hidden;">Something went wrong, check your CSV file.</div>
51 <div id="instructions">
52 <fieldset id="file_uploader_help" class="rows">
53 <legend>Instructions</legend>
54 <div id="file_uploader_inst">
56 <li>The quote uploader accepts standard csv files with two columns: "source","text"</li>
57 <li>Click the "Choose file" button and select the csv file to be uploaded.</li>
58 <li>The file will be imported into an editable table for review prior to saving.</li>
61 <div id="file_editor_inst">
63 <li>Click on any field to edit the contents; Press the <Enter> key to save edit.</li>
64 <li>Click the 'Save quotes' button in the toolbar to save the entire batch of quotes.</li>
70 <fieldset id="file_uploader" class="rows">
71 <legend>Upload quotes</legend>
72 <div id="file_upload">
73 <input type="file" name="file" />
74 <button id="cancel_upload" style="display:none">Cancel upload</button>
75 <div id="progress_bar"><div class="percent">0%</div></div>
78 <table id="quotes_editor" style="visibility: hidden;">
83 <th class="noExport">Actions</th>
87 <!-- tbody content is generated by DataTables -->
90 <td>Loading data...</td>
95 <fieldset id="footer" class="action" style="visibility: hidden;">
99 </div> <!-- /.col-sm-10.col-sm-push-2 -->
101 <div class="col-sm-2 col-sm-pull-10">
103 [% INCLUDE 'tools-menu.inc' %]
105 </div> <!-- /.col-sm-2.col-sm-pull-10 -->
106 </div> <!-- /.row -->
108 [% MACRO jsinclude BLOCK %]
109 [% Asset.js("js/tools-menu.js") | $raw %]
110 [% INCLUDE 'datatables.inc' %]
111 [% Asset.js("lib/jquery/plugins/jquery.jeditable.mini.js") | $raw %]
113 var oTable; //DataTable object
114 $(document).ready(function() {
116 $("#cancel_upload").on("click",function(e){
120 $("#cancel_quotes").on("click",function(){
121 return confirm( _("Are you sure you want to cancel this import?") );
125 // FileReader() code copied and hacked from:
126 // http://www.html5rocks.com/en/tutorials/file/dndfiles/
127 // fnCSVToArray() gratefully borrowed from:
128 // http://www.bennadel.com/blog/1504-Ask-Ben-Parsing-CSV-Strings-With-Javascript-Exec-Regular-Expression-Command.htm
131 var progress = document.querySelector('.percent');
132 $("#server_response").hide();
134 function yuiGetData() {
135 fnGetData(document.getElementById('quotes_editor'));
138 function fnAbortRead() {
142 function fnErrorHandler(evt) {
143 switch(evt.target.error.code) {
144 case evt.target.error.NOT_FOUND_ERR:
145 alert(_("File not found!"));
147 case evt.target.error.NOT_READABLE_ERR:
148 alert(_("File is not readable"));
150 case evt.target.error.ABORT_ERR:
153 alert(_("An error occurred reading this file."));
157 function fnUpdateProgress(evt) {
158 // evt is an ProgressEvent.
159 if (evt.lengthComputable) {
160 var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
161 // Increase the progress bar length.
162 if (percentLoaded < 100) {
163 progress.style.width = percentLoaded + '%';
164 progress.textContent = percentLoaded + '%';
169 function fnCSVToArray( strData, strDelimiter ){
170 // This will parse a delimited string into an array of
171 // arrays. The default delimiter is the comma, but this
172 // can be overriden in the second argument.
174 // Check to see if the delimiter is defined. If not,
175 // then default to comma.
176 strDelimiter = (strDelimiter || ",");
178 strData = escape_str(strData);
180 // Create a regular expression to parse the CSV values.
181 var objPattern = new RegExp(
184 "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
186 "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
188 "([^\"\\" + strDelimiter + "\\r\\n]*))"
193 // Create an array to hold our data. Give the array
194 // a default empty first row.
197 // Create an array to hold our individual pattern
199 var arrMatches = null;
201 // Keep looping over the regular expression matches
202 // until we can no longer find a match.
203 while (arrMatches = objPattern.exec( strData )){
205 // Get the delimiter that was found.
206 var strMatchedDelimiter = arrMatches[ 1 ];
208 // Check to see if the given delimiter has a length
209 // (is not the start of string) and if it matches
210 // field delimiter. If it does not, then we know
211 // that this delimiter is a row delimiter.
212 if ( strMatchedDelimiter.length && (strMatchedDelimiter != strDelimiter) ){
213 // Since we have reached a new row of data,
214 // add an empty row to our data array.
215 // Note: if there is not more data, we will have to remove this row later
219 // Now that we have our delimiter out of the way,
220 // let's check to see which kind of value we
221 // captured (quoted or unquoted).
222 if (arrMatches[ 2 ]){
223 // We found a quoted value. When we capture
224 // this value, unescape any double quotes.
225 var strMatchedValue = arrMatches[ 2 ].replace(
226 new RegExp( "\"\"", "g" ),
229 } else if (arrMatches[3]){
230 // We found a non-quoted value.
231 var strMatchedValue = arrMatches[ 3 ];
233 // There is no more valid data so remove the row we added earlier
234 // Is there a better way? Perhaps a look-ahead regexp?
235 arrData.splice(arrData.length-1, 1);
238 // Now that we have our value string, let's add
239 // it to the data array.
240 if ( arrData[ arrData.length - 1 ] ) {
241 arrData[ arrData.length - 1 ].push( strMatchedValue );
243 $('#csv_error').css( 'visibility' , 'visible' );
247 // Return the parsed data.
251 function fnDataTable(aaData) {
252 for(var i=0; i<aaData.length; i++) {
253 aaData[i].unshift(i+1); // Add a column w/quote number
256 /* Transition from the quote file uploader to the quote file editor interface */
257 $('#toolbar').css("visibility","visible");
258 $('#toolbar').css("position","");
259 $('#file_editor_inst').css("visibility", "visible");
260 $('#file_editor_inst').css("position", "");
261 $('#file_uploader_inst').css("visibility", "hidden");
262 $('#save_quotes').css("visibility","visible");
263 $('#file_uploader').css("visibility","hidden");
264 $('#file_uploader').css("position","absolute");
265 $('#file_uploader').css("top","-150px");
266 $('#quotes_editor').css("visibility","visible");
267 $("#save_quotes").on("click", yuiGetData);
269 oTable = $('#quotes_editor').dataTable( {
270 "bAutoWidth" : false,
273 "sPaginationType" : "full_numbers",
274 "sDom": '<"top pager"iflp>rt<"bottom pager"flp><"clear">',
278 "sTitle" : _("Number"),
282 "sTitle" : _("Source"),
286 "sTitle" : _("Quote"),
290 "fnPreDrawCallback": function(oSettings) {
293 "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
294 /* do foo on various cells in the current row */
295 var quoteNum = $('td', nRow)[0].innerHTML;
296 $(nRow).attr("id", quoteNum); /* set row ids to quote number */
297 /* apply no_edit id to noEditFields */
298 noEditFields = [0]; /* number */
299 for (i=0; i<noEditFields.length; i++) {
300 $('td', nRow)[noEditFields[i]].setAttribute("id","no_edit");
304 "fnDrawCallback": function(oSettings) {
305 /* Apply the jEditable handlers to the table on all fields w/o the no_edit id */
306 $('#quotes_editor tbody td[id!="no_edit"]').editable( function(value, settings) {
307 value = escape_str(value);
308 var cellPosition = oTable.fnGetPosition( this );
309 oTable.fnUpdate(value, cellPosition[0], cellPosition[1], false, false);
313 "callback" : function( sValue, y ) {
314 oTable.fnDraw(false); /* no filter/sort or we lose our pagination */
320 $('#footer').css("visibility","visible");
323 function fnHandleFileSelect(evt) {
324 // Reset progress indicator on new file selection.
325 progress.style.width = '0%';
326 progress.textContent = '0%';
328 reader = new FileReader();
329 reader.onerror = fnErrorHandler;
330 reader.onprogress = fnUpdateProgress;
331 reader.onabort = function(e) {
332 alert(_("File read cancelled"));
333 parent.location='quotes-upload.pl';
335 reader.onloadstart = function(e) {
336 $('#cancel_upload').show();
337 $('#progress_bar').addClass("loading");
339 reader.onload = function(e) {
340 // Ensure that the progress bar displays 100% at the end.
341 progress.style.width = '100%';
342 progress.textContent = '100%';
343 $('#cancel_upload').hide();
344 quotes = fnCSVToArray(e.target.result, ',');
348 // perform various sanity checks on the target file prior to uploading...
349 var fileType = evt.target.files[0].type || 'unknown';
350 var fileSizeInK = Math.round(evt.target.files[0].size/1024);
352 if (!fileType.match(/comma-separated-values|csv|excel|calc/i)) {
353 alert(_("Uploads limited to csv. Incorrect filetype: %s").format(fileType));
354 parent.location='quotes-upload.pl';
357 if (fileSizeInK > 512) {
358 if (!confirm(_("%s %s KB Do you really want to upload this file?").format(evt.target.files[0].name, fileSizeInK))) {
359 parent.location='quotes-upload.pl';
363 // Read in the image file as a text string.
364 reader.readAsText(evt.target.files[0]);
367 $('#file_upload').one('change', fnHandleFileSelect);
369 var MSG_IMPORT_SUCCESS = _("%s quotes imported successfully");
370 var MSG_IMPORT_ERROR = _("%s quotes have not been imported. An error occurred");
371 function fnGetData(element) {
372 var lines = oTable.fnGetData();
373 $(lines).each(function(line){
374 var s = this[1].replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
375 var t = this[2].replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
376 var data = {source: s, text: t};
377 var success = 0; var error = 0;
379 url : "/api/v1/quotes",
381 data : JSON.stringify(data),
382 dataType : "application/json",
383 success : function(data) {
384 $("#messages").show();
385 var import_success = $("#messages .import_success");
386 import_success.show();
387 import_success.data("nb")
388 nb_success = import_success.data("nb") || 0;
390 $("#messages .import_success").text(MSG_IMPORT_SUCCESS.format(nb_success));
391 import_success.data("nb", nb_success);
393 error : function(xhr) {
394 if (xhr.status==201) { this.success(null, "Created", xhr); return; }
395 $("#messages").show();
396 var import_error = $("#messages .import_error");
398 import_error.data("nb")
399 nb_error = import_error.data("nb") || 0;
401 $("#messages .import_error").text(MSG_IMPORT_ERROR.format(nb_error));
402 import_error.data("nb", nb_error);
408 }); // $(document).ready
412 [% INCLUDE 'intranet-bottom.inc' %]