// Fix products attachments uploader

This commit is contained in:
Jerome Nadaud
2013-11-29 12:10:09 +01:00
parent 85f445b7cc
commit e1568a67a2
4 changed files with 182 additions and 144 deletions
@@ -65,92 +65,9 @@
{l s='File:'}
</span>
</label>
<div class="col-lg-8">
<input id="attachment_file" type="file" name="attachment_file" data-url="{$current}&token={$token}&id_product={$obj->id}&ajax=1&action=AddAttachment" class="hide" />
<button class="ladda-button btn btn-default" data-style="expand-right" data-size="s" type="button" id="attachment_file-add-button">
<i class="icon-folder-open"></i>
{l s='Add file'}
</button>
<div class="alert alert-danger" id="attachment_file-errors" style="display:none"></div>
<div class="alert alert-success" id="attachment_file-success" style="display:none">{l s='Upload successful'}</div>
</div>
{$attachment_uploader}
</div>
<script type="text/javascript">
function humanizeSize(bytes) {
if (typeof bytes !== 'number')
return '';
if (bytes >= 1000000000)
return (bytes / 1000000000).toFixed(2) + ' GB';
if (bytes >= 1000000)
return (bytes / 1000000).toFixed(2) + ' MB';
return (bytes / 1000).toFixed(2) + ' KB';
}
$(document).ready(function() {
var attachment_file_add_button = Ladda.create( document.querySelector('#attachment_file-add-button' ));
var attachment_file_total_files = 0;
$('#attachment_file').fileupload({
dataType: 'json',
autoUpload: true,
singleFileUploads: true,
{if isset ($post_max_size)}maxFileSize: {$post_max_size},{/if}
start: function (e) {
attachment_file_add_button.start();
},
fail: function (e, data) {
$('#attachment_file-errors').html(data.errorThrown.message).show();
},
done: function (e, data) {
if (data.result) {
if (typeof data.result.attachment_file !== 'undefined') {
if (typeof data.result.attachment_file.error !== 'undefined' && data.result.attachment_file.error.length > 0)
$.each(data.result.attachment_file.error, function(index, error) {
$('#attachment_file-errors').append('<p><strong>'+data.result.attachment_file.name+'</strong> : '+error+'</p>').show();
});
else {
$('#attachment_file-success').show();
$('#selectAttachment1').append('<option value="'+data.result.attachment_file.id_attachment+'">'+data.result.attachment_file.filename+'</option>');
}
}
}
},
}).on('fileuploadalways', function (e, data) {
attachment_file_add_button.stop();
}).on('fileuploadprocessalways', function (e, data) {
var index = data.index, file = data.files[index];
if (file.error) {
$('#attachment_file-errors').append('<strong>'+file.name+'</strong> ('+humanizeSize(file.size)+') : '+file.error).show();
}
}).on('fileuploadsubmit', function (e, data) {
var params = new Object();
$('input[id^="attachment_name_"]').each(function()
{
id = $(this).prop("id").replace("attachment_name_", "attachment_name[") + "]";
params[id] = $(this).val();
});
$('textarea[id^="attachment_description_"]').each(function()
{
id = $(this).prop("id").replace("attachment_description_", "attachment_description[") + "]";
params[id] = $(this).val();
});
data.formData = params;
});
$('#attachment_file-add-button').on('click', function(e) {
e.preventDefault();
$('#attachment_file-success').hide();
$('#attachment_file-errors').html('').hide();
$('#attachment_file').trigger('click');
});
});
</script>
<hr/>
<div class="form-group">
@@ -0,0 +1,116 @@
{*
* 2007-2013 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2013 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*}
<div class="col-lg-8">
<input id="{$id}" type="file" name="{$name}"{if isset($url)} data-url="{$url}"{/if} class="hide" />
<button class="btn btn-default" data-style="expand-right" data-size="s" type="button" id="{$id}-add-button">
<i class="icon-plus-sign"></i> {l s='Add file'}
</button>
<div class="alert alert-success" id="{$id}-success" style="display:none">{l s='Upload successful'}</div>
<div class="alert alert-danger" id="{$id}-errors" style="display:none"></div>
</div>
<script type="text/javascript">
function humanizeSize(bytes)
{
if (typeof bytes !== 'number') {
return '';
}
if (bytes >= 1000000000) {
return (bytes / 1000000000).toFixed(2) + ' GB';
}
if (bytes >= 1000000) {
return (bytes / 1000000).toFixed(2) + ' MB';
}
return (bytes / 1000).toFixed(2) + ' KB';
}
$( document ).ready(function() {
var {$id}_add_button = Ladda.create( document.querySelector('#{$id}-add-button' ));
var {$id}_total_files = 0;
$('#{$id}').fileupload({
dataType: 'json',
autoUpload: true,
singleFileUploads: true,
maxFileSize: {$post_max_size},
success: function (e) {
},
start: function (e) {
{$id}_add_button.start();
},
fail: function (e, data) {
$('#{$id}-errors').html(data.errorThrown.message).show();
},
done: function (e, data) {
if (data.result) {
if (typeof data.result.attachment_file !== 'undefined') {
if (typeof data.result.attachment_file.error !== 'undefined' && data.result.attachment_file.error.length > 0)
$.each(data.result.attachment_file.error, function(index, error) {
$('#{$id}-errors').append('<p><strong>'+data.result.attachment_file.name+'</strong> : '+error+'</p>').show();
});
else {
$('#{$id}-success').show();
$('#selectAttachment1').append('<option value="'+data.result.attachment_file.id_attachment+'">'+data.result.attachment_file.filename+'</option>');
}
}
}
},
}).on('fileuploadalways', function (e, data) {
{$id}_add_button.stop();
}).on('fileuploadprocessalways', function (e, data) {
var index = data.index, file = data.files[index];
if (file.error)
$('#{$id}-errors').append('<div class="row"><strong>'+file.name+'</strong> ('+humanizeSize(file.size)+') : '+file.error+'</div>').show();
}).on('fileuploadsubmit', function (e, data) {
var params = new Object();
$('input[id^="attachment_name_"]').each(function()
{
id = $(this).prop("id").replace("attachment_name_", "attachment_name[") + "]";
params[id] = $(this).val();
});
$('textarea[id^="attachment_description_"]').each(function()
{
id = $(this).prop("id").replace("attachment_description_", "attachment_description[") + "]";
params[id] = $(this).val();
});
data.formData = params;
});
$('#{$id}-add-button').on('click', function() {
$('#{$id}-success').html('').hide();
$('#{$id}-errors').html('').hide();
{$id}_total_files = 0;
$('#{$id}').trigger('click');
});
});
</script>