// Blockcategories thumnail uploader fix

This commit is contained in:
Jerome Nadaud
2013-11-07 16:44:55 +01:00
parent 18718792be
commit b51b388eaa
7 changed files with 203 additions and 95 deletions
@@ -312,11 +312,6 @@ class AdminCategoriesControllerCore extends AdminController
}
}
public function ajaxProcessuploadThumbnailImages()
{
Hook::exec('actionBackOfficeCategory');
}
public function initProcess()
{
if (Tools::isSubmit('add'.$this->table.'root'))
@@ -616,9 +611,9 @@ class AdminCategoriesControllerCore extends AdminController
Tools::redirectAdmin(self::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminCategories').'&conf=7');
}
Hook::exec('actionBackOfficeCategoryRemoveThumbnail');
return parent::postProcess();
}
public function processForceDeleteImage()
{
$category = $this->loadObject(true);
-6
View File
@@ -283,11 +283,5 @@
<hook id="displayBackOfficeCategory" live_edit="0">
<name>displayBackOfficeCategory</name><title>Display new elements in the Back Office, tab AdminCategories</title><description>This hook launches modules when the AdminCategories tab is displayed in the Back Office</description>
</hook>
<hook id="actionBackOfficeCategory" live_edit="0">
<name>actionBackOfficeCategory</name><title>Process new elements in the Back Office, tab AdminCategories</title><description>This hook process modules when the AdminCategories tab is displayed in the Back Office</description>
</hook>
<hook id="actionBackOfficeCategoryRemoveThumbnail" live_edit="0">
<name>actionBackOfficeCategoryRemoveThumbnail</name><title>Delete elements in the Back Office, tab AdminCategories</title><description>This hook process modules when the AdminCategories tab call delete thumbnail</description>
</hook>
</entities>
</entity_hook>
+23 -81
View File
@@ -44,7 +44,18 @@ class BlockCategories extends Module
public function install()
{
if (!parent::install() ||
// Prepare tab
$tab = new Tab();
$tab->active = 1;
$tab->class_name = "AdminBlockCategories";
$tab->name = array();
foreach (Language::getLanguages(true) as $lang)
$tab->name[$lang['id_lang']] = 'BlockCategories';
$tab->id_parent = -1;
$tab->module = $this->name;
if (!$tab->add() ||
!parent::install() ||
!$this->registerHook('leftColumn') ||
!$this->registerHook('footer') ||
!$this->registerHook('header') ||
@@ -55,8 +66,6 @@ class BlockCategories extends Module
!$this->registerHook('actionAdminMetaControllerUpdate_optionsBefore') ||
!$this->registerHook('actionAdminLanguagesControllerStatusBefore') ||
!$this->registerHook('displayBackOfficeCategory') ||
!$this->registerHook('actionBackOfficeCategory') ||
!$this->registerHook('actionBackOfficeCategoryRemoveThumbnail') ||
!Configuration::updateValue('BLOCK_CATEG_MAX_DEPTH', 4) ||
!Configuration::updateValue('BLOCK_CATEG_DHTML', 1))
return false;
@@ -65,6 +74,14 @@ class BlockCategories extends Module
public function uninstall()
{
$id_tab = (int)Tab::getIdFromClassName('AdminBlockCategories');
if ($id_tab)
{
$tab = new Tab($id_tab);
$tab->delete();
}
if (!parent::uninstall() ||
!Configuration::deleteByName('BLOCK_CATEG_MAX_DEPTH') ||
!Configuration::deleteByName('BLOCK_CATEG_DHTML'))
@@ -135,93 +152,18 @@ class BlockCategories extends Module
{
$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;
$files[$i]['delete_url'] = Context::getContext()->link->getAdminLink('AdminBlockCategories').'&deleteThumb='.$i.'&id_category='.(int)$category->id;
}
}
$helper = new HelperImageUploader();
$helper->setMultiple(true)->setUseAjax(true)->setName('thumbnail')->setFiles($files)->setMaxFiles(3)->setUrl(
Context::getContext()->link->getAdminLink('AdminCategories').'&ajax=1&id_category='.$category->id.'&action=uploadThumbnailImages');
Context::getContext()->link->getAdminLink('AdminBlockCategories').'&ajax=1&id_category='.$category->id
.'&action=uploadThumbnailImages');
$this->smarty->assign('helper', $helper->render());
return $this->display(__FILE__, 'views/blockcategories_admin.tpl');
}
public function hookActionBackOfficeCategory($params)
{
$category = new Category((int)Tools::getValue('id_category'));
if (isset($_FILES['thumbnail']))
{
//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();
// Evaluate the memory required to resize the image: if it's too much, you can't resize it.
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. ');
// Copy new image
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.');
if (count($errors))
$total_errors = array_merge($total_errors, $errors);
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 (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'))
$this->context->controller->errors[] = Tools::displayError('Error while delete');
Tools::redirectAdmin(Context::getContext()->link->getAdminLink('AdminCategories').'&id_category='
.(int)Tools::getValue('id_category').'&updatecategory');
}
}
public function hookLeftColumn($params)
{
$category = false;
@@ -0,0 +1,108 @@
<?php
/*
* 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
*/
/**
* @since 1.6.0
*/
class AdminBlockCategoriesController extends ModuleAdminController
{
public function postProcess()
{
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')
&& !unlink(_PS_CAT_IMG_DIR_.(int)Tools::getValue('id_category').'-'.(int)$id_thumb.'_thumb.jpg'))
$this->context->controller->errors[] = Tools::displayError('Error while delete');
Tools::redirectAdmin(Context::getContext()->link->getAdminLink('AdminCategories').'&id_category='
.(int)Tools::getValue('id_category').'&updatecategory');
}
parent::postProcess();
}
public function ajaxProcessuploadThumbnailImages()
{
$category = new Category((int)Tools::getValue('id_category'));
if (isset($_FILES['thumbnail']))
{
//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('thumbnail');
$files = $helper->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();
// Evaluate the memory required to resize the image: if it's too much, you can't resize it.
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. ');
// Copy new image
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.');
if (count($errors))
$total_errors = array_merge($total_errors, $errors);
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('AdminBlockCategories').'&deleteThumb='
.$id.'&id_category='.(int)$category->id.'&updatecategory';
}
if (count($total_errors))
$this->context->controller->errors = array_merge($this->context->controller->errors, $total_errors);
die(Tools::jsonEncode(array('thumbnail' => $files)));
}
}
}
@@ -0,0 +1,35 @@
<?php
/*
* 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
*/
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;
@@ -0,0 +1,35 @@
<?php
/*
* 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
*/
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;
@@ -5,6 +5,5 @@ if (!defined('_PS_VERSION_'))
function upgrade_module_2_1($object)
{
return ($object->registerHook('displayBackOfficeCategory') && $object->registerHook('actionBackOfficeCategory')
&& $object->registerHook('actionBackOfficeCategoryRemoveThumbnail'));
return $object->registerHook('displayBackOfficeCategory');
}