// Add new uploader helper to blockcategories
This commit is contained in:
1 parent
d78a268730
commit
bf2d54e286
5 files changed
+89
-80
No files matched your search
@@ -37,10 +37,6 @@
|
|||||||
{else}
|
{else}
|
||||||
{$smarty.block.parent}
|
{$smarty.block.parent}
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
{if $input.name == 'image'}
|
|
||||||
{$displayBackOfficeCategory}
|
|
||||||
{/if}
|
|
||||||
{/block}
|
{/block}
|
||||||
{block name="description"}
|
{block name="description"}
|
||||||
{$smarty.block.parent}
|
{$smarty.block.parent}
|
||||||
@@ -52,4 +48,10 @@
|
|||||||
{$input.customer}</p>
|
{$input.customer}</p>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
{/block}
|
||||||
|
{block name="input_row"}
|
||||||
|
{$smarty.block.parent}
|
||||||
|
{if ($input.name == 'image')}
|
||||||
|
{$displayBackOfficeCategory}
|
||||||
|
{/if}
|
||||||
{/block}
|
{/block}
|
||||||
@@ -312,6 +312,11 @@ class AdminCategoriesControllerCore extends AdminController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function ajaxProcessuploadThumbnailImages()
|
||||||
|
{
|
||||||
|
Hook::exec('actionBackOfficeCategory');
|
||||||
|
}
|
||||||
|
|
||||||
public function initProcess()
|
public function initProcess()
|
||||||
{
|
{
|
||||||
if (Tools::isSubmit('add'.$this->table.'root'))
|
if (Tools::isSubmit('add'.$this->table.'root'))
|
||||||
@@ -610,12 +615,10 @@ class AdminCategoriesControllerCore extends AdminController
|
|||||||
if (Tools::isSubmit('forcedeleteImage'))
|
if (Tools::isSubmit('forcedeleteImage'))
|
||||||
Tools::redirectAdmin(self::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminCategories').'&conf=7');
|
Tools::redirectAdmin(self::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminCategories').'&conf=7');
|
||||||
}
|
}
|
||||||
|
|
||||||
Hook::exec('actionBackOfficeCategory');
|
|
||||||
|
|
||||||
|
Hook::exec('actionBackOfficeCategoryRemoveThumbnail');
|
||||||
return parent::postProcess();
|
return parent::postProcess();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function processForceDeleteImage()
|
public function processForceDeleteImage()
|
||||||
{
|
{
|
||||||
$category = $this->loadObject(true);
|
$category = $this->loadObject(true);
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ class BlockCategories extends Module
|
|||||||
!$this->registerHook('actionAdminLanguagesControllerStatusBefore') ||
|
!$this->registerHook('actionAdminLanguagesControllerStatusBefore') ||
|
||||||
!$this->registerHook('displayBackOfficeCategory') ||
|
!$this->registerHook('displayBackOfficeCategory') ||
|
||||||
!$this->registerHook('actionBackOfficeCategory') ||
|
!$this->registerHook('actionBackOfficeCategory') ||
|
||||||
|
!$this->registerHook('actionBackOfficeCategoryRemoveThumbnail') ||
|
||||||
!Configuration::updateValue('BLOCK_CATEG_MAX_DEPTH', 4) ||
|
!Configuration::updateValue('BLOCK_CATEG_MAX_DEPTH', 4) ||
|
||||||
!Configuration::updateValue('BLOCK_CATEG_DHTML', 1))
|
!Configuration::updateValue('BLOCK_CATEG_DHTML', 1))
|
||||||
return false;
|
return false;
|
||||||
@@ -118,66 +119,102 @@ class BlockCategories extends Module
|
|||||||
public function hookDisplayBackOfficeCategory($params)
|
public function hookDisplayBackOfficeCategory($params)
|
||||||
{
|
{
|
||||||
$category = new Category((int)Tools::getValue('id_category'));
|
$category = new Category((int)Tools::getValue('id_category'));
|
||||||
|
$files = array();
|
||||||
|
|
||||||
if ($category->level_depth != 2)
|
if ($category->level_depth != 2)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for ($i=0;$i<3;$i++)
|
for ($i=0;$i<3;$i++)
|
||||||
$images[$i] = ImageManager::thumbnail(_PS_CAT_IMG_DIR_.(int)$category->id.'-'.$i.'_thumb.jpg', $this->context->controller->table.'_'.(int)$category->id.'-'.$i.'_thumb.jpg', 100, 'jpg', true);
|
{
|
||||||
|
if (file_exists(_PS_CAT_IMG_DIR_.(int)$category->id.'-'.$i.'_thumb.jpg'))
|
||||||
|
{
|
||||||
|
$files[$i]['type'] = HelperImageUploader::TYPE_IMAGE;
|
||||||
|
$files[$i]['image'] = ImageManager::thumbnail(_PS_CAT_IMG_DIR_.(int)$category->id.'-'.$i.'_thumb.jpg', $this->context->controller->table.'_'.(int)$category->id.'-'.$i.'_thumb.jpg', 100, 'jpg', true, true);
|
||||||
|
$files[$i]['delete_url'] = Context::getContext()->link->getAdminLink('AdminCategories').'&deleteThumb='.$i.'&id_category='.(int)$category->id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$this->smarty->assign(array(
|
$helper = new HelperImageUploader();
|
||||||
'name' => 'thumb',
|
$helper->setMultiple(true)->setUseAjax(true)->setName('thumbnail')->setFiles($files)->setMaxFiles(3)->setUrl(
|
||||||
'images' => $images
|
Context::getContext()->link->getAdminLink('AdminCategories').'&ajax=1&id_category='.$category->id.'&action=uploadThumbnailImages');
|
||||||
));
|
$this->smarty->assign('helper', $helper->render());
|
||||||
|
|
||||||
return $this->display(__FILE__, 'views/blockcategories_admin.tpl');
|
return $this->display(__FILE__, 'views/blockcategories_admin.tpl');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hookActionBackOfficeCategory($params)
|
public function hookActionBackOfficeCategory($params)
|
||||||
{
|
{
|
||||||
$total_errors = array();
|
$category = new Category((int)Tools::getValue('id_category'));
|
||||||
|
|
||||||
for ($i=0; $i<3; $i++)
|
if (isset($_FILES['thumbnail']))
|
||||||
if (isset($_FILES['thumb-'.$i]) && $_FILES['thumb-'.$i]['size'] > 0)
|
{
|
||||||
|
//Get total of image already present in directory
|
||||||
|
$files = scandir(_PS_CAT_IMG_DIR_);
|
||||||
|
$assigned_keys = array();
|
||||||
|
$allowed_keys = array(0, 1, 2);
|
||||||
|
|
||||||
|
foreach ($files as $file) {
|
||||||
|
$matches = array();
|
||||||
|
|
||||||
|
if (preg_match('/'.$category->id.'-([0-9])?_thumb.jpg/i', $file, $matches) === 1)
|
||||||
|
$assigned_keys[] = (int)$matches[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
$available_keys = array_diff($allowed_keys, $assigned_keys);
|
||||||
|
$helper = new HelperImageUploader('files');
|
||||||
|
$files = $helper->setName('thumbnail')->process();
|
||||||
|
$total_errors = array();
|
||||||
|
|
||||||
|
if (count($available_keys) < count($files))
|
||||||
{
|
{
|
||||||
|
$total_errors[] = sprintf(Tools::displayError('You cannot upload more than %s files'), count($available_keys));
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($files as $key => &$file)
|
||||||
|
{
|
||||||
|
$id = array_shift($available_keys);
|
||||||
$errors = array();
|
$errors = array();
|
||||||
// Check image validity
|
|
||||||
$max_size = (int)Configuration::get('PS_PRODUCT_PICTURE_MAX_SIZE');
|
|
||||||
|
|
||||||
if ($error = ImageManager::validateUpload($_FILES['thumb-'.$i], Tools::getMaxUploadSize($max_size)))
|
|
||||||
$errors[] = $error;
|
|
||||||
|
|
||||||
$tmp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS');
|
|
||||||
|
|
||||||
if (!$tmp_name)
|
|
||||||
$errors[] = Tools::displayError('Invalid Temporary directory');
|
|
||||||
|
|
||||||
if (!move_uploaded_file($_FILES['thumb-'.$i]['tmp_name'], $tmp_name))
|
|
||||||
$errors[] = Tools::displayError('Error uploading thumbnail image');
|
|
||||||
|
|
||||||
// Evaluate the memory required to resize the image: if it's too much, you can't resize it.
|
// Evaluate the memory required to resize the image: if it's too much, you can't resize it.
|
||||||
if (!ImageManager::checkImageMemoryLimit($tmp_name))
|
if (!ImageManager::checkImageMemoryLimit($file['save_path']))
|
||||||
$errors[] = Tools::displayError('Due to memory limit restrictions, this image cannot be loaded. Please increase your memory_limit value via your server\'s configuration settings. ');
|
$errors[] = Tools::displayError('Due to memory limit restrictions, this image cannot be loaded. Please increase your memory_limit value via your server\'s configuration settings. ');
|
||||||
|
|
||||||
// Copy new image
|
// Copy new image
|
||||||
if (empty($errors) && !ImageManager::resize($tmp_name, _PS_CAT_IMG_DIR_.(int)Tools::getValue('id_category').'-'.$i.'_thumb.jpg'))
|
if (empty($errors) && !ImageManager::resize($file['save_path'], _PS_CAT_IMG_DIR_
|
||||||
|
.(int)Tools::getValue('id_category').'-'.$id.'_thumb.jpg'))
|
||||||
$errors[] = Tools::displayError('An error occurred while uploading the image.');
|
$errors[] = Tools::displayError('An error occurred while uploading the image.');
|
||||||
|
|
||||||
if (count($errors))
|
if (count($errors))
|
||||||
$total_errors = array_merge($total_errors, $errors);
|
$total_errors = array_merge($total_errors, $errors);
|
||||||
|
|
||||||
unlink($tmp_name);
|
unlink($file['save_path']);
|
||||||
|
//Necesary to prevent hacking
|
||||||
|
unset($file['save_path']);
|
||||||
|
|
||||||
|
//Add image preview and delete url
|
||||||
|
$file['image'] = ImageManager::thumbnail(_PS_CAT_IMG_DIR_.(int)$category->id.'-'.$id.'_thumb.jpg',
|
||||||
|
$this->context->controller->table.'_'.(int)$category->id.'-'.$id.'_thumb.jpg', 100, 'jpg', true, true);
|
||||||
|
$file['delete_url'] = Context::getContext()->link->getAdminLink('AdminCategories').'&deleteThumb='
|
||||||
|
.$id.'&id_category='.(int)$category->id;
|
||||||
|
$id++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (count($total_errors))
|
||||||
|
$this->context->controller->errors = array_merge($this->context->controller->errors, $total_errors);
|
||||||
|
|
||||||
|
die(Tools::jsonEncode(array('thumbnail' => $files)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hookActionBackOfficeCategoryRemoveThumbnail()
|
||||||
|
{
|
||||||
if (($id_thumb = Tools::getValue('deleteThumb', false)) !== false)
|
if (($id_thumb = Tools::getValue('deleteThumb', false)) !== false)
|
||||||
{
|
{
|
||||||
if (file_exists(_PS_CAT_IMG_DIR_.(int)Tools::getValue('id_category').'-'.(int)$id_thumb.'_thumb.jpg')
|
if (file_exists(_PS_CAT_IMG_DIR_.(int)Tools::getValue('id_category').'-'.(int)$id_thumb.'_thumb.jpg')
|
||||||
&& !unlink(_PS_CAT_IMG_DIR_.(int)Tools::getValue('id_category').'-'.(int)$id_thumb.'_thumb.jpg'))
|
&& !unlink(_PS_CAT_IMG_DIR_.(int)Tools::getValue('id_category').'-'.(int)$id_thumb.'_thumb.jpg'))
|
||||||
$this->context->controller->errors[] = Tools::displayError('Error while delete');
|
$this->context->controller->errors[] = Tools::displayError('Error while delete');
|
||||||
}
|
|
||||||
|
|
||||||
if (count($total_errors))
|
Tools::redirectAdmin(Context::getContext()->link->getAdminLink('AdminCategories').'&id_category='
|
||||||
$this->context->controller->errors = array_merge($this->context->controller->errors, $total_errors);
|
.(int)Tools::getValue('id_category').'&updatecategory');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hookLeftColumn($params)
|
public function hookLeftColumn($params)
|
||||||
|
|||||||
@@ -5,5 +5,6 @@ if (!defined('_PS_VERSION_'))
|
|||||||
|
|
||||||
function upgrade_module_2_1($object)
|
function upgrade_module_2_1($object)
|
||||||
{
|
{
|
||||||
return ($object->registerHook('displayBackOfficeCategory') && $object->registerHook('actionBackOfficeCategory'));
|
return ($object->registerHook('displayBackOfficeCategory') && $object->registerHook('actionBackOfficeCategory')
|
||||||
|
&& $object->registerHook('actionBackOfficeCategoryRemoveThumbnail'));
|
||||||
}
|
}
|
||||||
@@ -1,44 +1,10 @@
|
|||||||
{for $foo=0 to 2}
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<input id="{$name}-{$foo}" type="file" name="{$name}-{$foo}" class="hide"/>
|
<label class="control-label col-lg-3">
|
||||||
<div class="dummyfile input-group">
|
<span class="label-tooltip" data-toggle="tooltip" data-html="true" title="" data-original-title="{l s='You can upload a maximum of 3 images'}">
|
||||||
<span class="input-group-addon"><i class="icon-file"></i></span>
|
{l s='Thumbnails:'}
|
||||||
<input id="{$name}-name-{$foo}" type="text" class="disabled" readonly/>
|
|
||||||
<span class="input-group-btn">
|
|
||||||
<button id="{$name}-selectbutton-{$foo}" type="button" class="btn btn-default">
|
|
||||||
<i class="icon-folder-open"></i> {l s='Choose a file'}
|
|
||||||
</button>
|
|
||||||
</span>
|
</span>
|
||||||
|
</label>
|
||||||
|
<div class="col-lg-4">
|
||||||
|
{$helper}
|
||||||
</div>
|
</div>
|
||||||
{if isset($images.$foo) && $images.$foo}
|
</div>
|
||||||
<div class="clearfix"> </div>
|
|
||||||
<div id="image" class="img-thumbnail">
|
|
||||||
{$images.$foo}
|
|
||||||
<div class="text-center">
|
|
||||||
<a class="btn btn-default" href="{$current}&token={$token}&deleteThumb={$foo|intval}&id_category={$smarty.get.id_category|intval}">
|
|
||||||
<i class="icon-remove text-danger"></i> {l s='Delete'}
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{/if}
|
|
||||||
</div>
|
|
||||||
{/for}
|
|
||||||
|
|
||||||
<script>
|
|
||||||
$(document).ready(function() {
|
|
||||||
$('button[id|="{$name}-selectbutton"]').click(function(e) {
|
|
||||||
var id = $(this).prop('id').replace('{$name}-selectbutton-', '');
|
|
||||||
$('#{$name}-'+id).trigger('click');
|
|
||||||
});
|
|
||||||
$('input[id|="{$name}-name"]').click(function(e){
|
|
||||||
var id = $(this).prop('id').replace('{$name}-name-', '');
|
|
||||||
$('#{$name}-'+id).trigger('click');
|
|
||||||
});
|
|
||||||
$('input[id|="{$name}"]').change(function(e){
|
|
||||||
var val = $(this).val();
|
|
||||||
var id = $(this).prop('id').replace('{$name}-', '');
|
|
||||||
var file = val.split(/[\\/]/);
|
|
||||||
$('#{$name}-name-'+id).val(file[file.length-1]);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
Reference in new issue
Block a user