// Fix products attachments uploader
This commit is contained in:
@@ -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">
|
||||
|
||||
+116
@@ -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>
|
||||
@@ -40,6 +40,7 @@ class HelperUploaderCore extends Uploader
|
||||
private $_name;
|
||||
private $_max_files;
|
||||
private $_multiple;
|
||||
private $_post_max_size;
|
||||
protected $_template;
|
||||
private $_template_directory;
|
||||
private $_title;
|
||||
@@ -119,12 +120,6 @@ class HelperUploaderCore extends Uploader
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setTemplate($value)
|
||||
{
|
||||
$this->_template = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setName($value)
|
||||
{
|
||||
$this->_name = (string)$value;
|
||||
@@ -136,6 +131,26 @@ class HelperUploaderCore extends Uploader
|
||||
return $this->_name;
|
||||
}
|
||||
|
||||
public function setPostMaxSize($value)
|
||||
{
|
||||
$this->_post_max_size = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPostMaxSize()
|
||||
{
|
||||
if (!isset($this->_post_max_size))
|
||||
$this->_post_max_size = parent::getPostMaxSize();
|
||||
|
||||
return $this->_post_max_size;
|
||||
}
|
||||
|
||||
public function setTemplate($value)
|
||||
{
|
||||
$this->_template = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTemplate()
|
||||
{
|
||||
if (!isset($this->_template))
|
||||
@@ -240,37 +255,20 @@ class HelperUploaderCore extends Uploader
|
||||
.'template'))
|
||||
$bo_theme = 'default';
|
||||
|
||||
if ($this->getContext()->controller->ajax)
|
||||
{
|
||||
$html = '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/vendor/jquery.ui.widget.js"></script>';
|
||||
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.iframe-transport.js"></script>';
|
||||
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload.js"></script>';
|
||||
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload-process.js"></script>';
|
||||
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload-validate.js"></script>';
|
||||
}
|
||||
else
|
||||
{
|
||||
$html = '';
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/vendor/jquery.ui.widget.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.iframe-transport.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload-process.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload-validate.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.'js/vendor/spin.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.'js/vendor/ladda.js');
|
||||
}
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/vendor/jquery.ui.widget.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.iframe-transport.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload-process.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload-validate.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.'js/vendor/spin.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.'js/vendor/ladda.js');
|
||||
|
||||
if ($this->useAjax())
|
||||
if ($this->useAjax() && !isset($this->_template))
|
||||
$this->setTemplate(self::DEFAULT_AJAX_TEMPLATE);
|
||||
|
||||
$template = $this->getContext()->smarty->createTemplate(
|
||||
@@ -289,8 +287,7 @@ class HelperUploaderCore extends Uploader
|
||||
'drop_zone' => $this->getDropZone()
|
||||
));
|
||||
|
||||
$html .= $template->fetch();
|
||||
return $html;
|
||||
return $template->fetch();
|
||||
}
|
||||
|
||||
public function useAjax()
|
||||
|
||||
@@ -267,6 +267,28 @@ class AdminProductsControllerCore extends AdminController
|
||||
'position' => 'position'
|
||||
);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
protected function _cleanMetaKeywords($keywords)
|
||||
{
|
||||
@@ -3573,6 +3595,12 @@ class AdminProductsControllerCore extends AdminController
|
||||
$iso_tiny_mce = $this->context->language->iso_code;
|
||||
$iso_tiny_mce = (file_exists(_PS_JS_DIR_.'tiny_mce/langs/'.$iso_tiny_mce.'.js') ? $iso_tiny_mce : 'en');
|
||||
|
||||
$attachment_uploader = new HelperUploader('attachment_file');
|
||||
$attachment_uploader->setMultiple(false)->setUseAjax(true)->setUrl(
|
||||
Context::getContext()->link->getAdminLink('AdminProducts').'&ajax=1&id_product='.(int)$obj->id
|
||||
.'&action=AddAttachment')->setPostMaxSize((Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024 * 1024))
|
||||
->setTemplate('attachment_ajax.tpl');
|
||||
|
||||
$data->assign(array(
|
||||
'obj' => $obj,
|
||||
'table' => $this->table,
|
||||
@@ -3587,7 +3615,7 @@ class AdminProductsControllerCore extends AdminController
|
||||
'attachment_name' => $attachment_name,
|
||||
'attachment_description' => $attachment_description,
|
||||
'PS_ATTACHMENT_MAXIMUM_SIZE' => Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE'),
|
||||
'post_max_size' => (Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024 * 1024)
|
||||
'attachment_uploader' => $attachment_uploader->render()
|
||||
));
|
||||
}
|
||||
else
|
||||
@@ -3596,27 +3624,7 @@ class AdminProductsControllerCore extends AdminController
|
||||
else
|
||||
$this->displayWarning($this->l('You must save this product before adding attachements.'));
|
||||
|
||||
$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';
|
||||
|
||||
$html = '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/vendor/jquery.ui.widget.js"></script>';
|
||||
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.iframe-transport.js"></script>';
|
||||
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload.js"></script>';
|
||||
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload-process.js"></script>';
|
||||
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload-validate.js"></script>';
|
||||
|
||||
$this->tpl_form_vars['custom_form'] = $html.$data->fetch();
|
||||
$this->tpl_form_vars['custom_form'] = $data->fetch();
|
||||
}
|
||||
|
||||
public function initFormInformations($product)
|
||||
|
||||
Reference in New Issue
Block a user