// Add uploader to products controller

This commit is contained in:
Jerome Nadaud
2013-11-08 16:29:07 +01:00
parent 034565bc6a
commit bf67bd56a3
5 changed files with 386 additions and 141 deletions
+108 -37
View File
@@ -1331,42 +1331,6 @@ class AdminProductsControllerCore extends AdminController
}
}
/* @todo rename to processaddproductimage */
public function ajaxProcessAddImage()
{
self::$currentIndex = 'index.php?tab=AdminProducts';
$allowedExtensions = array('jpeg', 'gif', 'png', 'jpg');
// max file size in bytes
$uploader = new FileUploader($allowedExtensions, $this->max_image_size);
$result = $uploader->handleUpload();
if (isset($result['success']))
{
$obj = new Image((int)$result['success']['id_image']);
// Associate image to shop from context
$shops = Shop::getContextListShopID();
$obj->associateTo($shops);
$json_shops = array();
foreach ($shops as $id_shop)
$json_shops[$id_shop] = true;
$json = array(
'name' => $result['success']['name'],
'status' => 'ok',
'id'=>$obj->id,
'path' => $obj->getExistingImgPath(),
'position' => $obj->position,
'legend' => $obj->legend,
'cover' => $obj->cover,
'shops' => $json_shops,
);
@unlink(_PS_TMP_IMG_DIR_.'product_'.(int)$obj->id_product.'.jpg');
@unlink(_PS_TMP_IMG_DIR_.'product_mini_'.(int)$obj->id_product.'_'.$this->context->shop->id.'.jpg');
die(Tools::jsonEncode($json));
}
else
die(Tools::jsonEncode($result));
}
public function ajaxProcessDeleteProductAttribute()
{
if (!Combination::isFeatureActive())
@@ -3650,6 +3614,108 @@ class AdminProductsControllerCore extends AdminController
}
}
public function ajaxProcessaddProductImage()
{
self::$currentIndex = 'index.php?tab=AdminProducts';
$product = new Product((int)Tools::getValue('id_product'));
$legends = Tools::getValue('legend');
if (!is_array($legends))
$legends = (array)$legends;
if (!Validate::isLoadedObject($product))
{
$files = array();
$files[0]['error'] = Tools::displayError('Cannot add image because product creation failed.');
}
$image_uploader = new HelperImageUploader('file');
$image_uploader->setAcceptTypes(array('jpeg', 'gif', 'png', 'jpg'))->setMaxSize($this->max_image_size);
$files = $image_uploader->process();
foreach ($files as &$file)
{
$image = new Image();
$image->id_product = (int)($product->id);
$image->position = Image::getHighestPosition($product->id) + 1;
foreach ($legends as $key => $legend)
if (!empty($legend))
$image->legend[(int)$key] = $legend;
if (!Image::getCover($image->id_product))
$image->cover = 1;
else
$image->cover = 0;
if (!$image->add())
$file['error'] = Tools::displayError('Error while creating additional image');
else
{
if (!$new_path = $image->getPathForCreation())
{
$file['error'] = Tools::displayError('An error occurred during new folder creation');
continue;
}
if (!ImageManager::resize($file['save_path'], $new_path.'.'.$image->image_format))
{
$file['error'] = Tools::displayError('An error occurred while copying image.');
continue;
}
else
{
$imagesTypes = ImageType::getImagesTypes('products');
foreach ($imagesTypes as $imageType)
{
/*
$theme = (Shop::isFeatureActive() ? '-'.$imageType['id_theme'] : '');
if (!ImageManager::resize($tmpName, $new_path.'-'.stripslashes($imageType['name']).$theme.'.'.$image->image_format, $imageType['width'], $imageType['height'], $image->image_format))
return array('error' => Tools::displayError('An error occurred while copying image:').' '.stripslashes($imageType['name']));
*/
if (!ImageManager::resize($file['save_path'], $new_path.'-'.stripslashes($imageType['name']).'.'.$image->image_format, $imageType['width'], $imageType['height'], $image->image_format))
{
$file['error'] = Tools::displayError('An error occurred while copying image:').' '.stripslashes($imageType['name']);
continue;
}
}
}
unlink($file['save_path']);
//Necesary to prevent hacking
unset($file['save_path']);
Hook::exec('actionWatermark', array('id_image' => $image->id, 'id_product' => $product->id));
if (!$image->update())
{
$file['error'] = Tools::displayError('Error while updating status');
continue;
}
// Associate image to shop from context
$shops = Shop::getContextListShopID();
$image->associateTo($shops);
$json_shops = array();
foreach ($shops as $id_shop)
$json_shops[$id_shop] = true;
$file['status'] = 'ok';
$file['id'] = $image->id;
$file['position'] = $image->position;
$file['cover'] = $image->cover;
$file['legend'] = $image->legend;
$file['path'] = $image->getExistingImgPath();
$file['shops'] = $json_shops;
@unlink(_PS_TMP_IMG_DIR_.'product_'.(int)$product->id.'.jpg');
@unlink(_PS_TMP_IMG_DIR_.'product_mini_'.(int)$product->id.'_'.$this->context->shop->id.'.jpg');
}
}
die(Tools::jsonEncode(array($image_uploader->getName() => $files)));
}
public function initFormImages($obj)
{
$data = $this->createTemplate($this->tpl_form);
@@ -3688,6 +3754,10 @@ class AdminProductsControllerCore extends AdminController
$current_shop_id = 0;
$languages = Language::getLanguages(true);
$image_uploader = new HelperImageUploader('file');
$image_uploader->setMultiple(true)->setUseAjax(true)->setUrl(
Context::getContext()->link->getAdminLink('AdminProducts').'&ajax=1&id_product='.(int)$obj->id
.'&action=addProductImage');
$data->assign(array(
'countImages' => $count_images,
@@ -3702,7 +3772,8 @@ class AdminProductsControllerCore extends AdminController
'currency' => $this->context->currency,
'current_shop_id' => $current_shop_id,
'languages' => $this->_languages,
'default_language' => (int)Configuration::get('PS_LANG_DEFAULT')
'default_language' => (int)Configuration::get('PS_LANG_DEFAULT'),
'image_uploader' => $image_uploader->render()
));
$type = ImageType::getByNameNType('%', 'products', 'height');