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 [% WRAPPER 'header.inc' %]
13 [% INCLUDE 'cat-search.inc' %]
16 [% WRAPPER 'sub-header.inc' %]
17 <nav id="breadcrumbs" aria-label="Breadcrumb" class="breadcrumb">
20 <a href="/cgi-bin/koha/mainpage.pl">Home</a>
23 <a href="/cgi-bin/koha/tools/tools-home.pl">Tools</a>
26 <a href="/cgi-bin/koha/tools/quotes.pl">Quote editor</a>
29 <a href="#" aria-current="page">
37 <div class="main container-fluid">
39 <div class="col-sm-10 col-sm-push-2">
41 <div id="toolbar" class="btn-toolbar" style="visibility: hidden; position: absolute">
42 <div class="btn-group"><a class="btn btn-default" id="save_quotes" href="#"><i class="fa fa-save"></i> Save quotes</a></div>
43 <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>
46 <h1>Quote uploader</h1>
48 <div id="messages" style="display: none;">
49 <div class="import_success dialog message" style="display: none;"></div>
50 <div class="import_error dialog alert" style="display: none;"></div>
53 <div id="instructions">
54 <fieldset id="file_uploader_help" class="rows">
55 <legend>Instructions</legend>
56 <div id="file_uploader_inst">
58 <li>The quote uploader accepts standard csv files with two columns: "source","text"</li>
59 <li>Click the "Choose file" button and select the csv file to be uploaded.</li>
60 <li>The file will be imported into an editable table for review prior to saving.</li>
63 <div id="file_editor_inst">
65 <li>Click on any field to edit the contents; Press the <Enter> key to save edit.</li>
66 <li>Click the 'Save quotes' button in the toolbar to save the entire batch of quotes.</li>
72 <fieldset id="file_uploader" class="rows">
73 <legend>Upload quotes</legend>
74 <div id="file_upload">
75 <input type="file" name="file" />
76 <button id="cancel_upload" style="display:none">Cancel upload</button>
77 <div id="progress_bar"><div class="percent">0%</div></div>
80 <table id="quotes_editor" style="visibility: hidden;">
85 <th class="noExport">Actions</th>
89 <!-- tbody content is generated by DataTables -->
92 <td>Loading data...</td>
97 <fieldset id="footer" class="action" style="visibility: hidden;">
101 </div> <!-- /.col-sm-10.col-sm-push-2 -->
103 <div class="col-sm-2 col-sm-pull-10">
105 [% INCLUDE 'tools-menu.inc' %]
107 </div> <!-- /.col-sm-2.col-sm-pull-10 -->
108 </div> <!-- /.row -->
110 [% MACRO jsinclude BLOCK %]
111 [% Asset.js("js/tools-menu.js") | $raw %]
112 [% INCLUDE 'datatables.inc' %]
113 [% Asset.js("lib/jquery/plugins/jquery.jeditable.mini.js") | $raw %]
115 var oTable; //DataTable object
116 $(document).ready(function() {
118 $("#cancel_upload").on("click",function(e){
122 $("#cancel_quotes").on("click",function(){
123 return confirm( _("Are you sure you want to cancel this import?") );
127 // FileReader() code copied and hacked from:
128 // http://www.html5rocks.com/en/tutorials/file/dndfiles/
129 // fnCSVToArray() gratefully borrowed from:
130 // http://www.bennadel.com/blog/1504-Ask-Ben-Parsing-CSV-Strings-With-Javascript-Exec-Regular-Expression-Command.htm
133 var progress = document.querySelector('.percent');
134 $("#server_response").hide();
136 function yuiGetData() {
137 fnGetData(document.getElementById('quotes_editor'));
140 function fnAbortRead() {
144 function fnErrorHandler(evt) {
145 switch(evt.target.error.code) {
146 case evt.target.error.NOT_FOUND_ERR:
147 alert(_("File not found!"));
149 case evt.target.error.NOT_READABLE_ERR:
150 alert(_("File is not readable"));
152 case evt.target.error.ABORT_ERR:
155 alert(_("An error occurred reading this file."));
159 function fnUpdateProgress(evt) {
160 // evt is an ProgressEvent.
161 if (evt.lengthComputable) {
162 var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
163 // Increase the progress bar length.
164 if (percentLoaded < 100) {
165 progress.style.width = percentLoaded + '%';
166 progress.textContent = percentLoaded + '%';
171 function fnCSVToArray( strData, strDelimiter ){
172 // This will parse a delimited string into an array of
173 // arrays. The default delimiter is the comma, but this
174 // can be overriden in the second argument.
176 // Check to see if the delimiter is defined. If not,
177 // then default to comma.
178 strDelimiter = (strDelimiter || ",");
180 strData = escape_str(strData);
182 // Create a regular expression to parse the CSV values.
183 var objPattern = new RegExp(
186 "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
188 "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
190 "([^\"\\" + strDelimiter + "\\r\\n]*))"
195 // Create an array to hold our data. Give the array
196 // a default empty first row.
199 // Create an array to hold our individual pattern
201 var arrMatches = null;
203 // Keep looping over the regular expression matches
204 // until we can no longer find a match.
205 while (arrMatches = objPattern.exec( strData )){
207 // Get the delimiter that was found.
208 var strMatchedDelimiter = arrMatches[ 1 ];
210 // Check to see if the given delimiter has a length
211 // (is not the start of string) and if it matches
212 // field delimiter. If it does not, then we know
213 // that this delimiter is a row delimiter.
214 if ( strMatchedDelimiter.length && (strMatchedDelimiter != strDelimiter) ){
215 // Since we have reached a new row of data,
216 // add an empty row to our data array.
217 // Note: if there is not more data, we will have to remove this row later
221 // Now that we have our delimiter out of the way,
222 // let's check to see which kind of value we
223 // captured (quoted or unquoted).
224 if (arrMatches[ 2 ]){
225 // We found a quoted value. When we capture
226 // this value, unescape any double quotes.
227 var strMatchedValue = arrMatches[ 2 ].replace(
228 new RegExp( "\"\"", "g" ),
231 } else if (arrMatches[3]){
232 // We found a non-quoted value.
233 var strMatchedValue = arrMatches[ 3 ];
235 // There is no more valid data so remove the row we added earlier
236 // Is there a better way? Perhaps a look-ahead regexp?
237 arrData.splice(arrData.length-1, 1);
240 // Now that we have our value string, let's add
241 // it to the data array.
242 if ( arrData[ arrData.length - 1 ] ) {
243 arrData[ arrData.length - 1 ].push( strMatchedValue );
245 $("#messages .import_error").text(_("Something went wrong, check your CSV file."));
249 // Return the parsed data.
253 function fnDataTable(aaData) {
254 for(var i=0; i<aaData.length; i++) {
255 aaData[i].unshift(i+1); // Add a column w/quote number
258 /* Transition from the quote file uploader to the quote file editor interface */
259 $('#toolbar').css("visibility","visible");
260 $('#toolbar').css("position","");
261 $('#file_editor_inst').css("visibility", "visible");
262 $('#file_editor_inst').css("position", "");
263 $('#file_uploader_inst').css("visibility", "hidden");
264 $('#save_quotes').css("visibility","visible");
265 $('#file_uploader').css("visibility","hidden");
266 $('#file_uploader').css("position","absolute");
267 $('#file_uploader').css("top","-150px");
268 $('#quotes_editor').css("visibility","visible");
269 $("#save_quotes").on("click", yuiGetData);
271 oTable = $('#quotes_editor').dataTable( {
272 "bAutoWidth" : false,
275 "sPaginationType" : "full_numbers",
276 "sDom": '<"top pager"iflp>rt<"bottom pager"flp><"clear">',
280 "sTitle" : _("Number"),
284 "sTitle" : _("Source"),
288 "sTitle" : _("Quote"),
292 "fnPreDrawCallback": function(oSettings) {
295 "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
296 /* do foo on various cells in the current row */
297 var quoteNum = $('td', nRow)[0].innerHTML;
298 $(nRow).attr("id", quoteNum); /* set row ids to quote number */
299 /* apply no_edit id to noEditFields */
300 noEditFields = [0]; /* number */
301 for (i=0; i<noEditFields.length; i++) {
302 $('td', nRow)[noEditFields[i]].setAttribute("id","no_edit");
306 "fnDrawCallback": function(oSettings) {
307 /* Apply the jEditable handlers to the table on all fields w/o the no_edit id */
308 $('#quotes_editor tbody td[id!="no_edit"]').editable( function(value, settings) {
309 value = escape_str(value);
310 var cellPosition = oTable.fnGetPosition( this );
311 oTable.fnUpdate(value, cellPosition[0], cellPosition[1], false, false);
315 "callback" : function( sValue, y ) {
316 oTable.fnDraw(false); /* no filter/sort or we lose our pagination */
322 $('#footer').css("visibility","visible");
325 function fnHandleFileSelect(evt) {
326 // Reset progress indicator on new file selection.
327 progress.style.width = '0%';
328 progress.textContent = '0%';
330 reader = new FileReader();
331 reader.onerror = fnErrorHandler;
332 reader.onprogress = fnUpdateProgress;
333 reader.onabort = function(e) {
334 alert(_("File read cancelled"));
335 parent.location='quotes-upload.pl';
337 reader.onloadstart = function(e) {
338 $('#cancel_upload').show();
339 $('#progress_bar').addClass("loading");
341 reader.onload = function(e) {
342 // Ensure that the progress bar displays 100% at the end.
343 progress.style.width = '100%';
344 progress.textContent = '100%';
345 $('#cancel_upload').hide();
346 quotes = fnCSVToArray(e.target.result, ',');
350 // perform various sanity checks on the target file prior to uploading...
351 var fileType = evt.target.files[0].type || 'unknown';
352 var fileSizeInK = Math.round(evt.target.files[0].size/1024);
354 if (!fileType.match(/comma-separated-values|csv|excel|calc/i)) {
355 alert(_("Uploads limited to csv. Incorrect filetype: %s").format(fileType));
356 parent.location='quotes-upload.pl';
359 if (fileSizeInK > 512) {
360 if (!confirm(_("%s %s KB Do you really want to upload this file?").format(evt.target.files[0].name, fileSizeInK))) {
361 parent.location='quotes-upload.pl';
365 // Read in the image file as a text string.
366 reader.readAsText(evt.target.files[0]);
369 $('#file_upload').one('change', fnHandleFileSelect);
371 var MSG_IMPORT_SUCCESS = _("%s quotes imported successfully");
372 var MSG_IMPORT_ERROR = _("%s quotes have not been imported. An error occurred");
373 function fnGetData(element) {
374 var lines = oTable.fnGetData();
375 $(lines).each(function(line){
376 var s = this[1].replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
377 var t = this[2].replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
378 var data = {source: s, text: t};
379 var success = 0; var error = 0;
381 url : "/api/v1/quotes",
383 data : JSON.stringify(data),
384 dataType : "application/json",
385 success : function(data) {
386 $("#messages").show();
387 var import_success = $("#messages .import_success");
388 import_success.show();
389 import_success.data("nb")
390 nb_success = import_success.data("nb") || 0;
392 $("#messages .import_success").text(MSG_IMPORT_SUCCESS.format(nb_success));
393 import_success.data("nb", nb_success);
395 error : function(xhr) {
396 if (xhr.status==201) { this.success(null, "Created", xhr); return; }
397 $("#messages").show();
398 var import_error = $("#messages .import_error");
400 import_error.data("nb")
401 nb_error = import_error.data("nb") || 0;
403 $("#messages .import_error").text(MSG_IMPORT_ERROR.format(nb_error));
404 import_error.data("nb", nb_error);
410 }); // $(document).ready
414 [% INCLUDE 'intranet-bottom.inc' %]