// Change product attachment upload system
This commit is contained in:
@@ -47,12 +47,14 @@
|
||||
<div class="form-group">
|
||||
<label class="control-label col-lg-3" for="attachment_description_{$id_lang}">{l s='Description:'} </label>
|
||||
<div class="col-lg-9">
|
||||
<div class="row">
|
||||
{include
|
||||
file="controllers/products/textarea_lang.tpl"
|
||||
languages=$languages
|
||||
input_name="attachment_description"
|
||||
input_value=$attachment_description
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -63,39 +65,92 @@
|
||||
{l s='File:'}
|
||||
</span>
|
||||
</label>
|
||||
<div class="col-lg-7">
|
||||
<div class="col-lg-8">
|
||||
<input id="attachement_file" type="file" name="attachment_file" class="hide" />
|
||||
<div class="dummyfile input-group">
|
||||
<span class="input-group-addon"><i class="icon-file"></i></span>
|
||||
<input id="attachement_filename" type="text" class="disabled" name="filename" readonly />
|
||||
<span class="input-group-btn">
|
||||
<button id="attachement_fileselectbutton" type="button" name="submitAddAttachments" class="btn btn-default">
|
||||
<i class="icon-folder-open"></i> {l s='Choose a file'}
|
||||
</button>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-lg-4">
|
||||
<button type="submit" name="submitAddAttachments" class="btn btn-default">
|
||||
<i class="icon-cloud-upload"></i> {l s='Upload attachment file'}
|
||||
</button>
|
||||
</div>
|
||||
<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>
|
||||
<script>
|
||||
$(document).ready(function(){
|
||||
$('#attachement_fileselectbutton').click(function(e){
|
||||
$('#attachement_file').trigger('click');
|
||||
});
|
||||
$('#attachement_file').change(function(e){
|
||||
var val = $(this).val();
|
||||
var file = val.split(/[\\/]/);
|
||||
$('#attachement_filename').val(file[file.length-1]);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</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">
|
||||
|
||||
@@ -444,6 +444,120 @@ class AdminProductsControllerCore extends AdminController
|
||||
}
|
||||
}
|
||||
|
||||
public function ajaxProcessAddAttachment()
|
||||
{
|
||||
if (isset($_FILES['attachment_file']))
|
||||
{
|
||||
if ((int)$_FILES['attachment_file']['error'] === 1)
|
||||
{
|
||||
$_FILES['attachment_file']['error'] = array();
|
||||
|
||||
$max_upload = (int)ini_get('upload_max_filesize');
|
||||
$max_post = (int)ini_get('post_max_size');
|
||||
$upload_mb = min($max_upload, $max_post);
|
||||
$_FILES['attachment_file']['error'][] = sprintf(
|
||||
$this->l('The file %1$s exceeds the size allowed by the server. The limit is set to %2$d MB.'),
|
||||
'<b>'.$_FILES['attachment_file']['name'].'</b> ',
|
||||
'<b>'.$upload_mb.'</b>'
|
||||
);
|
||||
}
|
||||
|
||||
$_FILES['attachment_file']['error'] = array();
|
||||
|
||||
$is_attachment_name_valid = false;
|
||||
$attachment_names = Tools::getValue('attachment_name');
|
||||
$attachment_descriptions = Tools::getValue('attachment_description');
|
||||
|
||||
if (!isset($attachment_names) || !$attachment_names)
|
||||
$attachment_names = array();
|
||||
|
||||
if (!isset($attachment_descriptions) || !$attachment_descriptions)
|
||||
$attachment_descriptions = array();
|
||||
|
||||
foreach ($attachment_names as $lang => $name)
|
||||
{
|
||||
if (Tools::strlen($name) > 0)
|
||||
$is_attachment_name_valid = true;
|
||||
|
||||
if (!Validate::isGenericName($name))
|
||||
$_FILES['attachment_file']['error'][] = sprintf(Tools::displayError('Invalid name for %s language'), Language::getLanguage((int)$lang)['name']);
|
||||
elseif (Tools::strlen($name) > 32)
|
||||
$_FILES['attachment_file']['error'][] = sprintf(Tools::displayError('The name for %1s language is too long (%2d chars max).'), Language::getLanguage((int)$lang)['name'], 32);
|
||||
}
|
||||
|
||||
foreach ($attachment_descriptions as $lang => $description)
|
||||
if (!Validate::isCleanHtml($description))
|
||||
$_FILES['attachment_file']['error'][] = sprintf(Tools::displayError('Invalid description for %s language'), Language::getLanguage((int)$lang)['name']);
|
||||
|
||||
if (!$is_attachment_name_valid)
|
||||
$_FILES['attachment_file']['error'][] = Tools::displayError('An attachment name is required.');
|
||||
|
||||
if (empty($_FILES['attachment_file']['error']))
|
||||
{
|
||||
if (is_uploaded_file($_FILES['attachment_file']['tmp_name']))
|
||||
{
|
||||
if ($_FILES['attachment_file']['size'] > (Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024 * 1024))
|
||||
$_FILES['attachment_file']['error'][] = sprintf(
|
||||
$this->l('The file is too large. Maximum size allowed is: %1$d kB. The file you\'re trying to upload is: %2$d kB.'),
|
||||
(Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024),
|
||||
number_format(($_FILES['attachment_file']['size'] / 1024), 2, '.', '')
|
||||
);
|
||||
else
|
||||
{
|
||||
do $uniqid = sha1(microtime());
|
||||
while (file_exists(_PS_DOWNLOAD_DIR_.$uniqid));
|
||||
if (!copy($_FILES['attachment_file']['tmp_name'], _PS_DOWNLOAD_DIR_.$uniqid))
|
||||
$_FILES['attachment_file']['error'][] = $this->l('File copy failed');
|
||||
@unlink($_FILES['attachment_file']['tmp_name']);
|
||||
}
|
||||
}
|
||||
else
|
||||
$_FILES['attachment_file']['error'][] = Tools::displayError('The file is missing.');
|
||||
|
||||
if (empty($_FILES['attachment_file']['error']) && isset($uniqid))
|
||||
{
|
||||
$attachment = new Attachment();
|
||||
|
||||
foreach ($attachment_names as $lang => $name)
|
||||
$attachment->name[(int)$lang] = $name;
|
||||
|
||||
foreach ($attachment_descriptions as $lang => $description)
|
||||
$attachment->description[(int)$lang] = $description;
|
||||
|
||||
$attachment->file = $uniqid;
|
||||
$attachment->mime = $_FILES['attachment_file']['type'];
|
||||
$attachment->file_name = $_FILES['attachment_file']['name'];
|
||||
|
||||
if (empty($attachment->mime) || Tools::strlen($attachment->mime) > 128)
|
||||
$_FILES['attachment_file']['error'][] = Tools::displayError('Invalid file extension');
|
||||
if (!Validate::isGenericName($attachment->file_name))
|
||||
$_FILES['attachment_file']['error'][] = Tools::displayError('Invalid file name');
|
||||
if (Tools::strlen($attachment->file_name) > 128)
|
||||
$_FILES['attachment_file']['error'][] = Tools::displayError('The file name is too long.');
|
||||
if (empty($this->errors))
|
||||
{
|
||||
$res = $attachment->add();
|
||||
if (!$res)
|
||||
$_FILES['attachment_file']['error'][] = Tools::displayError('This attachment was unable to be loaded into the database.');
|
||||
else
|
||||
{
|
||||
$_FILES['attachment_file']['id_attachment'] = $attachment->id;
|
||||
$_FILES['attachment_file']['filename'] = $attachment->name[$this->context->employee->id_lang];
|
||||
$id_product = (int)Tools::getValue($this->identifier);
|
||||
$res = $attachment->attachProduct($id_product);
|
||||
if (!$res)
|
||||
$_FILES['attachment_file']['error'][] = Tools::displayError('We were unable to associate this attachment to a product.');
|
||||
}
|
||||
}
|
||||
else
|
||||
$_FILES['attachment_file']['error'][] = Tools::displayError('Invalid file');
|
||||
}
|
||||
}
|
||||
|
||||
die(Tools::jsonEncode($_FILES));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload new attachment
|
||||
*
|
||||
@@ -3472,7 +3586,8 @@ class AdminProductsControllerCore extends AdminController
|
||||
'default_form_language' => (int)Configuration::get('PS_LANG_DEFAULT'),
|
||||
'attachment_name' => $attachment_name,
|
||||
'attachment_description' => $attachment_description,
|
||||
'PS_ATTACHMENT_MAXIMUM_SIZE' => Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE')
|
||||
'PS_ATTACHMENT_MAXIMUM_SIZE' => Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE'),
|
||||
'post_max_size' => (Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024 * 1024)
|
||||
));
|
||||
}
|
||||
else
|
||||
@@ -3481,7 +3596,27 @@ class AdminProductsControllerCore extends AdminController
|
||||
else
|
||||
$this->displayWarning($this->l('You must save this product before adding attachements.'));
|
||||
|
||||
$this->tpl_form_vars['custom_form'] = $data->fetch();
|
||||
$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();
|
||||
}
|
||||
|
||||
public function initFormInformations($product)
|
||||
|
||||
Reference in New Issue
Block a user