3 [% INCLUDE 'doc-head-open.inc' %]
4 <title>Koha › Tools › Quote uploader</title>
5 [% INCLUDE 'doc-head-close.inc' %]
6 [% Asset.css("css/uploader.css") %]
7 [% Asset.css("css/quotes.css") %]
8 [% Asset.css("css/datatables.css") %]
11 <body id="tools_quotes" class="tools">
12 [% INCLUDE 'header.inc' %]
13 [% INCLUDE 'cat-search.inc' %]
15 <div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> › <a href="/cgi-bin/koha/tools/tools-home.pl">Tools</a> › <a href="/cgi-bin/koha/tools/quotes.pl">Quote editor</a> › Quote uploader</div>
17 <div id="doc3" class="yui-t2">
21 [% INCLUDE 'quotes-upload-toolbar.inc' %]
22 <h2>Quote uploader</h2>
23 <div id="instructions">
24 <fieldset id="file_uploader_help" class="rows">
25 <legend>Instructions</legend>
26 <div id="file_uploader_inst">
28 <li>The quote uploader accepts standard csv files with two columns: "source","text"</li>
29 <li>Click the "Choose File" button and select the csv file to be uploaded.</li>
30 <li>The file will be imported into an editable table for review prior to saving.</li>
33 <div id="file_editor_inst">
35 <li>Click on any field to edit the contents; Press the <Enter> key to save edit.</li>
36 <li>Click on one or more quote numbers to select entire quotes for deletion; Click the 'Delete Quote(s)' button to delete selected quotes.</li>
37 <li>Click the 'Save Quotes' button in the toolbar to save the entire batch of quotes.</li>
42 <fieldset id="file_uploader" class="rows">
43 <legend>Upload quotes</legend>
44 <div id="file_upload">
45 <input type="file" name="file" />
46 <button id="cancel_upload" style="display:none">Cancel upload</button>
47 <div id="progress_bar"><div class="percent">0%</div></div>
50 <table id="quotes_editor" style="visibility: hidden;">
59 <!-- tbody content is generated by DataTables -->
62 <td>Loading data...</td>
67 <fieldset id="footer" class="action" style="visibility: hidden;">
71 <div class="yui-b noprint">
72 [% INCLUDE 'tools-menu.inc' %]
76 [% MACRO jsinclude BLOCK %]
77 [% Asset.js("js/tools-menu.js") %]
78 [% INCLUDE 'datatables.inc' %]
79 [% Asset.js("lib/jquery/plugins/jquery.jeditable.mini.js") %]
80 <script type="text/javascript">
81 var oTable; //DataTable object
82 $(document).ready(function() {
84 $("#cancel_upload").on("click",function(e){
88 $("#cancel_quotes").on("click",function(){
89 return confirm( _("Are you sure you want to cancel this import?") );
93 // FileReader() code copied and hacked from:
94 // http://www.html5rocks.com/en/tutorials/file/dndfiles/
95 // fnCSVToArray() gratefully borrowed from:
96 // http://www.bennadel.com/blog/1504-Ask-Ben-Parsing-CSV-Strings-With-Javascript-Exec-Regular-Expression-Command.htm
99 var progress = document.querySelector('.percent');
100 $("#server_response").hide();
102 function yuiGetData() {
103 fnGetData(document.getElementById('quotes_editor'));
106 function fnAbortRead() {
110 function fnErrorHandler(evt) {
111 switch(evt.target.error.code) {
112 case evt.target.error.NOT_FOUND_ERR:
113 alert(_("File Not Found!"));
115 case evt.target.error.NOT_READABLE_ERR:
116 alert(_("File is not readable"));
118 case evt.target.error.ABORT_ERR:
121 alert(_("An error occurred reading this file."));
125 function fnUpdateProgress(evt) {
126 // evt is an ProgressEvent.
127 if (evt.lengthComputable) {
128 var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
129 // Increase the progress bar length.
130 if (percentLoaded < 100) {
131 progress.style.width = percentLoaded + '%';
132 progress.textContent = percentLoaded + '%';
137 function fnCSVToArray( strData, strDelimiter ){
138 // This will parse a delimited string into an array of
139 // arrays. The default delimiter is the comma, but this
140 // can be overriden in the second argument.
142 // Check to see if the delimiter is defined. If not,
143 // then default to comma.
144 strDelimiter = (strDelimiter || ",");
146 // Create a regular expression to parse the CSV values.
147 var objPattern = new RegExp(
150 "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
152 "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
154 "([^\"\\" + strDelimiter + "\\r\\n]*))"
159 // Create an array to hold our data. Give the array
160 // a default empty first row.
163 // Create an array to hold our individual pattern
165 var arrMatches = null;
167 // Keep looping over the regular expression matches
168 // until we can no longer find a match.
169 while (arrMatches = objPattern.exec( strData )){
171 // Get the delimiter that was found.
172 var strMatchedDelimiter = arrMatches[ 1 ];
174 // Check to see if the given delimiter has a length
175 // (is not the start of string) and if it matches
176 // field delimiter. If it does not, then we know
177 // that this delimiter is a row delimiter.
178 if ( strMatchedDelimiter.length && (strMatchedDelimiter != strDelimiter) ){
179 // Since we have reached a new row of data,
180 // add an empty row to our data array.
181 // Note: if there is not more data, we will have to remove this row later
185 // Now that we have our delimiter out of the way,
186 // let's check to see which kind of value we
187 // captured (quoted or unquoted).
188 if (arrMatches[ 2 ]){
189 // We found a quoted value. When we capture
190 // this value, unescape any double quotes.
191 var strMatchedValue = arrMatches[ 2 ].replace(
192 new RegExp( "\"\"", "g" ),
195 } else if (arrMatches[3]){
196 // We found a non-quoted value.
197 var strMatchedValue = arrMatches[ 3 ];
199 // There is no more valid data so remove the row we added earlier
200 // Is there a better way? Perhaps a look-ahead regexp?
201 arrData.splice(arrData.length-1, 1);
204 // Now that we have our value string, let's add
205 // it to the data array.
206 arrData[ arrData.length - 1 ].push( strMatchedValue );
209 // Return the parsed data.
213 function fnDataTable(aaData) {
214 for(var i=0; i<aaData.length; i++) {
215 aaData[i].unshift(i+1); // Add a column w/quote number
218 /* Transition from the quote file uploader to the quote file editor interface */
219 $('#toolbar').css("visibility","visible");
220 $('#toolbar').css("position","");
221 $('#file_editor_inst').css("visibility", "visible");
222 $('#file_editor_inst').css("position", "");
223 $('#file_uploader_inst').css("visibility", "hidden");
224 $('#save_quotes').css("visibility","visible");
225 $('#file_uploader').css("visibility","hidden");
226 $('#file_uploader').css("position","absolute");
227 $('#file_uploader').css("top","-150px");
228 $('#quotes_editor').css("visibility","visible");
229 $("#save_quotes").on("click", yuiGetData);
230 $("#delete_quote").on("click", fnClickDeleteRow);
232 oTable = $('#quotes_editor').dataTable( {
233 "bAutoWidth" : false,
236 "sPaginationType" : "full_numbers",
237 "sDom": '<"top pager"iflp>rt<"bottom pager"flp><"clear">',
241 "sTitle" : _("Number"),
245 "sTitle" : _("Source"),
249 "sTitle" : _("Quote"),
253 "fnPreDrawCallback": function(oSettings) {
256 "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
257 /* do foo on various cells in the current row */
258 var quoteNum = $('td', nRow)[0].innerHTML;
259 $(nRow).attr("id", quoteNum); /* set row ids to quote number */
260 $('td:eq(0)', nRow).click(function() {$(this.parentNode).toggleClass('selected',this.clicked);}); /* add row selectors */
261 $('td:eq(0)', nRow).attr("title", _("Click ID to select/deselect quote"));
262 /* apply no_edit id to noEditFields */
263 noEditFields = [0]; /* number */
264 for (i=0; i<noEditFields.length; i++) {
265 $('td', nRow)[noEditFields[i]].setAttribute("id","no_edit");
269 "fnDrawCallback": function(oSettings) {
270 /* Apply the jEditable handlers to the table on all fields w/o the no_edit id */
271 $('#quotes_editor tbody td[id!="no_edit"]').editable( function(value, settings) {
272 var cellPosition = oTable.fnGetPosition( this );
273 oTable.fnUpdate(value, cellPosition[0], cellPosition[1], false, false);
277 "callback" : function( sValue, y ) {
278 oTable.fnDraw(false); /* no filter/sort or we lose our pagination */
284 $('#footer').css("visibility","visible");
287 function fnHandleFileSelect(evt) {
288 // Reset progress indicator on new file selection.
289 progress.style.width = '0%';
290 progress.textContent = '0%';
292 reader = new FileReader();
293 reader.onerror = fnErrorHandler;
294 reader.onprogress = fnUpdateProgress;
295 reader.onabort = function(e) {
296 alert(_("File read cancelled"));
297 parent.location='quotes-upload.pl';
299 reader.onloadstart = function(e) {
300 $('#cancel_upload').show();
301 $('#progress_bar').addClass("loading");
303 reader.onload = function(e) {
304 // Ensure that the progress bar displays 100% at the end.
305 progress.style.width = '100%';
306 progress.textContent = '100%';
307 $('#cancel_upload').hide();
308 quotes = fnCSVToArray(e.target.result, ',');
312 // perform various sanity checks on the target file prior to uploading...
313 var fileType = evt.target.files[0].type || 'unknown';
314 var fileSizeInK = Math.round(evt.target.files[0].size/1024);
316 if (!fileType.match(/comma-separated-values|csv|excel|calc/i)) {
317 alert(_("Uploads limited to csv. Incorrect filetype: %s").format(fileType));
318 parent.location='quotes-upload.pl';
321 if (fileSizeInK > 512) {
322 if (!confirm(_("%s %s KB Do you really want to upload this file?").format(evt.target.files[0].name, fileSizeInK))) {
323 parent.location='quotes-upload.pl';
327 // Read in the image file as a text string.
328 reader.readAsText(evt.target.files[0]);
331 $('#file_upload').one('change', fnHandleFileSelect);
335 function fnGetData(element) {
337 url : "/cgi-bin/koha/tools/quotes/quotes-upload_ajax.pl",
339 contentType : "application/x-www-form-urlencoded", // we must claim this mimetype or CGI will not decode the URL encoding
342 "quote" : encodeURI ( JSON.stringify(oTable.fnGetData()) ),
345 success : function(){
346 var response = JSON.parse(jqXHR.responseText);
347 if (response.success) {
348 alert(_("%s quotes saved.").format(response.records));
349 window.location.reload(true); // is this the best route?
351 alert(_("%s quotes saved, but an error has occurred. Please ask your administrator to check the server log for more details.").format(response.records));
352 window.location.reload(true); // is this the best route?
358 function fnClickDeleteRow() {
359 var idsToDelete = oTable.$('.selected').map(function() {
363 alert(_("Please select a quote(s) by clicking the quote id(s) you desire to delete."));
365 else if (confirm(_("Are you sure you wish to delete quote(s) %s?").format(idsToDelete))) {
366 oTable.$('.selected').each(function(){
367 oTable.fnDeleteRow(this);
374 [% INCLUDE 'intranet-bottom.inc' %]