// One step csv uploader
This commit is contained in:
1 parent
5e718394e3
commit
b58e8b6bb7
2 files changed
+121
-121
No files matched your search
@@ -110,13 +110,10 @@
|
|||||||
<div class="form-group" id="csv_file_uploader">
|
<div class="form-group" id="csv_file_uploader">
|
||||||
<label class="control-label col-lg-4">{l s='Select your CSV file'}</label>
|
<label class="control-label col-lg-4">{l s='Select your CSV file'}</label>
|
||||||
<div class="col-lg-8">
|
<div class="col-lg-8">
|
||||||
<input id="filename" type="file" name="filename[]" data-url="{$current}&token={$token}&ajax=1&action=uploadCsv" class="hide" />
|
<input id="file" type="file" name="file" data-url="{$current}&token={$token}&ajax=1&action=uploadCsv" class="hide" />
|
||||||
<button class="btn btn-default" data-style="expand-right" data-size="s" type="button" id="filename-add-button">
|
<button class="ladda-button btn btn-default" data-style="expand-right" data-size="s" type="button" id="file-add-button">
|
||||||
<i class="icon-plus-sign"></i> {l s='Add file'}
|
<i class="icon-plus-sign"></i> {l s='Add file'}
|
||||||
</button>
|
</button>
|
||||||
<button class="ladda-button btn btn-default" data-style="expand-right" type="button" id="filename-upload-button" style="display:none;">
|
|
||||||
<i class="icon-cloud-upload"></i> <span class="ladda-label">{l s='Upload file'}</span>
|
|
||||||
</button>
|
|
||||||
<p class="help-block">
|
<p class="help-block">
|
||||||
{l s="You can also use a file that you already have uploaded."}
|
{l s="You can also use a file that you already have uploaded."}
|
||||||
<a href="#" onclick="$('#csv_files_history').slideToggle();$('#csv_file_uploader').slideToggle(); return false;">{l s="Show history / FTP"}</a>.
|
<a href="#" onclick="$('#csv_files_history').slideToggle();$('#csv_file_uploader').slideToggle(); return false;">{l s="Show history / FTP"}</a>.
|
||||||
@@ -124,115 +121,82 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="row" style="display:none">
|
<div class="row" style="display:none">
|
||||||
<div class="alert alert-info" id="filename-files-list"></div>
|
<div class="alert alert-success" id="file-success">{l s='File uploaded'}</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="row" style="display:none">
|
<div class="row" style="display:none">
|
||||||
<div class="alert alert-success" id="filename-success"></div>
|
<div class="alert alert-danger" id="file-errors"></div>
|
||||||
</div>
|
|
||||||
<div class="row" style="display:none">
|
|
||||||
<div class="alert alert-danger" id="filename-errors"></div>
|
|
||||||
</div>
|
</div>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
function humanizeSize(bytes)
|
function humanizeSize(bytes)
|
||||||
{
|
{
|
||||||
if (typeof bytes !== 'number') {
|
if (typeof bytes !== 'number')
|
||||||
return '';
|
return '';
|
||||||
}
|
|
||||||
|
|
||||||
if (bytes >= 1000000000) {
|
if (bytes >= 1000000000)
|
||||||
return (bytes / 1000000000).toFixed(2) + ' GB';
|
return (bytes / 1000000000).toFixed(2) + ' GB';
|
||||||
}
|
|
||||||
|
|
||||||
if (bytes >= 1000000) {
|
if (bytes >= 1000000)
|
||||||
return (bytes / 1000000).toFixed(2) + ' MB';
|
return (bytes / 1000000).toFixed(2) + ' MB';
|
||||||
}
|
|
||||||
|
|
||||||
return (bytes / 1000).toFixed(2) + ' KB';
|
return (bytes / 1000).toFixed(2) + ' KB';
|
||||||
}
|
}
|
||||||
|
|
||||||
$( document ).ready(function() {
|
$( document ).ready(
|
||||||
var filename_upload_button = Ladda.create( document.querySelector('#filename-upload-button' ));
|
function()
|
||||||
var filename_total_files = 0;
|
{
|
||||||
|
var file_add_button = Ladda.create( document.querySelector('#file-add-button' ));
|
||||||
|
var file_total_files = 0;
|
||||||
|
|
||||||
$('#filename').fileupload({
|
$('#file').fileupload({
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
autoUpload: false,
|
autoUpload: true,
|
||||||
|
acceptFileTypes: /(\.|\/)(csv)$/i,
|
||||||
singleFileUploads: true,
|
singleFileUploads: true,
|
||||||
maxFileSize: {$post_max_size},
|
maxFileSize: {$post_max_size},
|
||||||
start: function (e) {
|
start: function (e)
|
||||||
filename_upload_button.start();
|
{
|
||||||
$('#filename-upload-button').unbind('click'); //Important as we bind it for every elements in add function
|
file_add_button.start();
|
||||||
},
|
},
|
||||||
fail: function (e, data) {
|
fail: function (e, data)
|
||||||
$('#filename-errors').html(data.errorThrown.message).parent().show();
|
{
|
||||||
|
$('#file-errors').html(data.errorThrown.message).parent().show();
|
||||||
},
|
},
|
||||||
done: function (e, data) {
|
done: function (e, data)
|
||||||
if (data.result) {
|
{
|
||||||
if (typeof data.result.filename !== 'undefined') {
|
if (data.result)
|
||||||
for (var i=0; i<data.result.filename.length; i++) {
|
{
|
||||||
if (data.result.filename[i] !== null) {
|
if (typeof data.result.file !== 'undefined')
|
||||||
if (typeof data.result.filename[i].error !== 'undefined' && data.result.filename[i].error != '') {
|
{
|
||||||
$('#filename-errors').html('<strong>'+data.result.filename[i].name+'</strong> : '+data.result.filename[i].error).parent().show();
|
if (typeof data.result.file.error !== 'undefined' && data.result.file.error != '')
|
||||||
}
|
$('#file-errors').html('<strong>'+data.result.file.name+'</strong> : '+data.result.file.error).parent().show();
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$(data.context).appendTo($('#filename-success'));
|
$('#file-success').parent().show();
|
||||||
$('#filename-success').parent().show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$(data.context).find('button').remove();
|
$(data.context).find('button').remove();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}).on('fileuploadalways', function (e, data) {
|
}).on('fileuploadalways', function (e, data)
|
||||||
filename_total_files--;
|
{
|
||||||
|
file_add_button.stop();
|
||||||
if (filename_total_files == 0)
|
}).on('fileuploadprocessalways', function (e, data)
|
||||||
{
|
{
|
||||||
filename_upload_button.stop();
|
|
||||||
$('#filename-upload-button').unbind('click');
|
|
||||||
$('#filename-files-list').parent().hide();
|
|
||||||
}
|
|
||||||
}).on('fileuploadadd', function(e, data) {
|
|
||||||
data.context = $('<div/>').addClass('row').appendTo($('#filename-files-list'));
|
|
||||||
var file_name = $('<span/>').append('<strong>'+data.files[0].name+'</strong> ('+humanizeSize(data.files[0].size)+')').appendTo(data.context);
|
|
||||||
|
|
||||||
var button = $('<button/>').addClass('btn btn-default pull-right').prop('type', 'button').html('<i class="icon-trash"></i> {l s='Remove file'}').appendTo(data.context).on('click', function() {
|
|
||||||
filename_total_files--;
|
|
||||||
data.files = null;
|
|
||||||
|
|
||||||
var total_elements = $(this).parent().siblings('div.row').length;
|
|
||||||
$(this).parent().remove();
|
|
||||||
|
|
||||||
if (total_elements == 0) {
|
|
||||||
$('#filename-files-list').html('').parent().hide();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$('#filename-files-list').parent().show();
|
|
||||||
$('#filename-upload-button').show().bind('click', function () {
|
|
||||||
if (data.files != null)
|
|
||||||
data.submit();
|
|
||||||
});
|
|
||||||
|
|
||||||
filename_total_files++;
|
|
||||||
}).on('fileuploadprocessalways', function (e, data) {
|
|
||||||
var index = data.index, file = data.files[index];
|
var index = data.index, file = data.files[index];
|
||||||
|
|
||||||
if (file.error) {
|
if (file.error)
|
||||||
$('#filename-errors').append('<div class="row"><strong>'+file.name+'</strong> ('+humanizeSize(file.size)+') : '+file.error+'</div>').parent().show();
|
{
|
||||||
|
$('#file-errors').append('<div class="row"><strong>'+file.name+'</strong> ('+humanizeSize(file.size)+') : '+file.error+'</div>').parent().show();
|
||||||
$(data.context).find('button').trigger('click');
|
$(data.context).find('button').trigger('click');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#filename-add-button').on('click', function() {
|
$('#file-add-button').on('click', function()
|
||||||
$('#filename-success').html('').parent().hide();
|
{
|
||||||
$('#filename-errors').html('').parent().hide();
|
$('#file-success').parent().hide();
|
||||||
$('#filename-files-list').parent().hide();
|
$('#file-errors').html('').parent().hide();
|
||||||
filename_total_files = 0;
|
$('#file').trigger('click');
|
||||||
$('#filename').trigger('click');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -480,6 +480,28 @@ class AdminImportControllerCore extends AdminController
|
|||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setMedia()
|
||||||
|
{
|
||||||
|
$admin_webpath = str_ireplace(_PS_ROOT_DIR_, '', _PS_ADMIN_DIR_);
|
||||||
|
$admin_webpath = preg_replace('/^'.preg_quote(DIRECTORY_SEPARATOR, '/').'/', '', $admin_webpath);
|
||||||
|
$bo_theme = ((Validate::isLoadedObject($this->context->employee)
|
||||||
|
&& $this->context->employee->bo_theme) ? $this->context->employee->bo_theme : 'default');
|
||||||
|
|
||||||
|
if (!file_exists(_PS_BO_ALL_THEMES_DIR_.$bo_theme.DIRECTORY_SEPARATOR
|
||||||
|
.'template'))
|
||||||
|
$bo_theme = 'default';
|
||||||
|
|
||||||
|
$this->addJs(__PS_BASE_URI__.$admin_webpath.'/themes/'.$bo_theme.'/js/vendor/jquery.ui.widget.js');
|
||||||
|
$this->addJs(__PS_BASE_URI__.$admin_webpath.'/themes/'.$bo_theme.'/js/jquery.iframe-transport.js');
|
||||||
|
$this->addJs(__PS_BASE_URI__.$admin_webpath.'/themes/'.$bo_theme.'/js/jquery.fileupload.js');
|
||||||
|
$this->addJs(__PS_BASE_URI__.$admin_webpath.'/themes/'.$bo_theme.'/js/jquery.fileupload-process.js');
|
||||||
|
$this->addJs(__PS_BASE_URI__.$admin_webpath.'/themes/'.$bo_theme.'/js/jquery.fileupload-validate.js');
|
||||||
|
$this->addJs(__PS_BASE_URI__.'js/vendor/spin.js');
|
||||||
|
$this->addJs(__PS_BASE_URI__.'js/vendor/ladda.js');
|
||||||
|
|
||||||
|
return parent::setMedia();
|
||||||
|
}
|
||||||
|
|
||||||
public function renderForm()
|
public function renderForm()
|
||||||
{
|
{
|
||||||
if (!is_writable(_PS_ADMIN_DIR_.'/import/'))
|
if (!is_writable(_PS_ADMIN_DIR_.'/import/'))
|
||||||
@@ -533,7 +555,20 @@ class AdminImportControllerCore extends AdminController
|
|||||||
if (isset($this->context->cookie->multiple_value_separator_selected) && $this->context->cookie->multiple_value_separator_selected)
|
if (isset($this->context->cookie->multiple_value_separator_selected) && $this->context->cookie->multiple_value_separator_selected)
|
||||||
$multiple_value_separator_selected = base64_decode($this->context->cookie->multiple_value_separator_selected);
|
$multiple_value_separator_selected = base64_decode($this->context->cookie->multiple_value_separator_selected);
|
||||||
|
|
||||||
|
//get post max size
|
||||||
|
$post_max_size = ini_get('post_max_size');
|
||||||
|
$bytes = trim($post_max_size);
|
||||||
|
$last = strtolower($post_max_size[strlen($post_max_size) - 1]);
|
||||||
|
|
||||||
|
switch ($last)
|
||||||
|
{
|
||||||
|
case 'g': $bytes *= 1024;
|
||||||
|
case 'm': $bytes *= 1024;
|
||||||
|
case 'k': $bytes *= 1024;
|
||||||
|
}
|
||||||
|
|
||||||
$this->tpl_form_vars = array(
|
$this->tpl_form_vars = array(
|
||||||
|
'post_max_size' => $bytes,
|
||||||
'module_confirmation' => (Tools::getValue('import')) && (isset($this->warnings) && !count($this->warnings)),
|
'module_confirmation' => (Tools::getValue('import')) && (isset($this->warnings) && !count($this->warnings)),
|
||||||
'path_import' => _PS_ADMIN_DIR_.'/import/',
|
'path_import' => _PS_ADMIN_DIR_.'/import/',
|
||||||
'entities' => $this->entities,
|
'entities' => $this->entities,
|
||||||
@@ -552,6 +587,46 @@ class AdminImportControllerCore extends AdminController
|
|||||||
return parent::renderForm();
|
return parent::renderForm();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function ajaxProcessuploadCsv()
|
||||||
|
{
|
||||||
|
$path = _PS_ADMIN_DIR_.'/import/'.date('YmdHis').'-';
|
||||||
|
|
||||||
|
if (isset($_FILES['file']) && !empty($_FILES['file']['error']))
|
||||||
|
{
|
||||||
|
switch ($_FILES['file']['error'])
|
||||||
|
{
|
||||||
|
case UPLOAD_ERR_INI_SIZE:
|
||||||
|
$_FILES['file']['error'] = Tools::displayError('The uploaded file exceeds the upload_max_filesize directive in php.ini. If your server configuration allows it, you may add a directive in your .htaccess.');
|
||||||
|
break;
|
||||||
|
case UPLOAD_ERR_FORM_SIZE:
|
||||||
|
$_FILES['file']['error'] = Tools::displayError('The uploaded file exceeds the post_max_size directive in php.ini.
|
||||||
|
If your server configuration allows it, you may add a directive in your .htaccess, for example:')
|
||||||
|
.'<br/><a href="'.$this->context->link->getAdminLink('AdminMeta').'" >
|
||||||
|
<code>php_value post_max_size 20M</code> '.
|
||||||
|
Tools::displayError('(click to open "Generators" page)').'</a>';
|
||||||
|
break;
|
||||||
|
break;
|
||||||
|
case UPLOAD_ERR_PARTIAL:
|
||||||
|
$_FILES['file']['error'] = Tools::displayError('The uploaded file was only partially uploaded.');
|
||||||
|
break;
|
||||||
|
break;
|
||||||
|
case UPLOAD_ERR_NO_FILE:
|
||||||
|
$_FILES['file']['error'] = Tools::displayError('No file was uploaded.');
|
||||||
|
break;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elseif (!preg_match('/.*\.csv$/i', $_FILES['file']['name']))
|
||||||
|
$_FILES['file']['error'] = Tools::displayError('The extension of your file should be .csv.');
|
||||||
|
elseif (!file_exists($_FILES['file']['tmp_name']) ||
|
||||||
|
!@move_uploaded_file($_FILES['file']['tmp_name'], $path.$_FILES['file']['name']))
|
||||||
|
$_FILES['file']['error'] = $this->l('An error occurred while uploading / copying the file.');
|
||||||
|
else
|
||||||
|
@chmod($path.$_FILES['file']['name'], 0664);
|
||||||
|
|
||||||
|
die(Tools::jsonEncode($_FILES));
|
||||||
|
}
|
||||||
|
|
||||||
public function renderView()
|
public function renderView()
|
||||||
{
|
{
|
||||||
$this->addJS(_PS_JS_DIR_.'adminImport.js');
|
$this->addJS(_PS_JS_DIR_.'adminImport.js');
|
||||||
@@ -2950,46 +3025,7 @@ class AdminImportControllerCore extends AdminController
|
|||||||
}
|
}
|
||||||
/* PrestaShop demo mode*/
|
/* PrestaShop demo mode*/
|
||||||
|
|
||||||
if (Tools::isSubmit('submitFileUpload'))
|
if (Tools::getValue('import'))
|
||||||
{
|
|
||||||
$path = _PS_ADMIN_DIR_.'/import/'.date('YmdHis').'-';
|
|
||||||
if (isset($_FILES['file']) && !empty($_FILES['file']['error']))
|
|
||||||
{
|
|
||||||
switch ($_FILES['file']['error'])
|
|
||||||
{
|
|
||||||
case UPLOAD_ERR_INI_SIZE:
|
|
||||||
$this->errors[] = Tools::displayError('The uploaded file exceeds the upload_max_filesize directive in php.ini. If your server configuration allows it, you may add a directive in your .htaccess.');
|
|
||||||
break;
|
|
||||||
case UPLOAD_ERR_FORM_SIZE:
|
|
||||||
$this->errors[] = Tools::displayError('The uploaded file exceeds the post_max_size directive in php.ini.
|
|
||||||
If your server configuration allows it, you may add a directive in your .htaccess, for example:')
|
|
||||||
.'<br/><a href="'.$this->context->link->getAdminLink('AdminMeta').'" >
|
|
||||||
<code>php_value post_max_size 20M</code> '.
|
|
||||||
Tools::displayError('(click to open "Generators" page)').'</a>';
|
|
||||||
break;
|
|
||||||
break;
|
|
||||||
case UPLOAD_ERR_PARTIAL:
|
|
||||||
$this->errors[] = Tools::displayError('The uploaded file was only partially uploaded.');
|
|
||||||
break;
|
|
||||||
break;
|
|
||||||
case UPLOAD_ERR_NO_FILE:
|
|
||||||
$this->errors[] = Tools::displayError('No file was uploaded.');
|
|
||||||
break;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
elseif (!preg_match('/.*\.csv$/i', $_FILES['file']['name']))
|
|
||||||
$this->errors[] = Tools::displayError('The extension of your file should be .csv.');
|
|
||||||
elseif (!file_exists($_FILES['file']['tmp_name']) ||
|
|
||||||
!@move_uploaded_file($_FILES['file']['tmp_name'], $path.$_FILES['file']['name']))
|
|
||||||
$this->errors[] = $this->l('An error occurred while uploading / copying the file.');
|
|
||||||
else
|
|
||||||
{
|
|
||||||
@chmod($path.$_FILES['file']['name'], 0664);
|
|
||||||
Tools::redirectAdmin(self::$currentIndex.'&token='.Tools::getValue('token').'&conf=18');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
elseif (Tools::getValue('import'))
|
|
||||||
{
|
{
|
||||||
// Check if the CSV file exist
|
// Check if the CSV file exist
|
||||||
if (Tools::getValue('csv'))
|
if (Tools::getValue('csv'))
|
||||||
|
|||||||
Reference in new issue
Block a user