change single image upload to multiple images upload in admin > catalog edit product
This commit is contained in:
parent
1943181c4a
commit
b82ca57e10
@ -1116,8 +1116,8 @@ class AdminProducts extends AdminTab
|
||||
public function addProductImage($product, $method = 'auto')
|
||||
{
|
||||
/* Updating an existing product image */
|
||||
if ($id_image = ((int)(Tools::getValue('id_image'))))
|
||||
{
|
||||
if ($id_image = ((int)(Tools::getValue('id_image')))) {
|
||||
|
||||
$image = new Image($id_image);
|
||||
if (!Validate::isLoadedObject($image))
|
||||
$this->_errors[] = Tools::displayError('An error occurred while loading object image.');
|
||||
@ -1131,52 +1131,106 @@ class AdminProducts extends AdminTab
|
||||
if (sizeof($this->_errors) OR !$image->update())
|
||||
$this->_errors[] = Tools::displayError('An error occurred while updating image.');
|
||||
elseif (isset($_FILES['image_product']['tmp_name']) AND $_FILES['image_product']['tmp_name'] != NULL)
|
||||
$this->copyImage($product->id, $image->id, $method);
|
||||
$this->copySingleImage($product->id, $image->id, $_FILES['image_product'], $method);
|
||||
}
|
||||
|
||||
if (isset($image) AND Validate::isLoadedObject($image) AND !file_exists(_PS_PROD_IMG_DIR_.$image->getExistingImgPath().'.'.$image->image_format))
|
||||
$image->delete();
|
||||
if (sizeof($this->_errors))
|
||||
return false;
|
||||
@unlink(_PS_TMP_IMG_DIR_.'/product_'.$product->id.'.jpg');
|
||||
@unlink(_PS_TMP_IMG_DIR_.'/product_mini_'.$product->id.'.jpg');
|
||||
|
||||
return ((isset($id_image) AND is_int($id_image) AND $id_image) ? $id_image : true);
|
||||
}
|
||||
|
||||
/* Adding a new product image */
|
||||
elseif (isset($_FILES['image_product']['name']) && $_FILES['image_product']['name'] != NULL )
|
||||
{
|
||||
/* Adding new product images */
|
||||
elseif (isset($_FILES['image_product']['name']) && is_array($_FILES['image_product']['name'])) {
|
||||
|
||||
if ($error = checkImageUploadError($_FILES['image_product']))
|
||||
$this->_errors[] = $error;
|
||||
// reorganise the array into an indexed array
|
||||
$images_product = [];
|
||||
for ($i=sizeof($_FILES['image_product']['error']); $i>0; $i--) {
|
||||
$images_product[] = [
|
||||
'name' => '',
|
||||
'type' => '',
|
||||
'tmp_name' => '',
|
||||
'error' => '',
|
||||
'size' => ''
|
||||
];
|
||||
}
|
||||
|
||||
if (!sizeof($this->_errors) AND isset($_FILES['image_product']['tmp_name']) AND $_FILES['image_product']['tmp_name'] != NULL)
|
||||
{
|
||||
if (!Validate::isLoadedObject($product))
|
||||
$this->_errors[] = Tools::displayError('Cannot add image because product add failed.');
|
||||
elseif (substr($_FILES['image_product']['name'], -4) == '.zip')
|
||||
return $this->uploadImageZip($product);
|
||||
else
|
||||
{
|
||||
$image = new Image();
|
||||
$image->id_product = (int)($product->id);
|
||||
$_POST['id_product'] = $image->id_product;
|
||||
$image->position = Image::getHighestPosition($product->id) + 1;
|
||||
if (($cover = Tools::getValue('cover')) == 1)
|
||||
Image::deleteCover($product->id);
|
||||
$image->cover = !$cover ? !sizeof($product->getImages(Configuration::get('PS_LANG_DEFAULT'))) : true;
|
||||
$this->validateRules('Image', 'image');
|
||||
$this->copyFromPost($image, 'image');
|
||||
if (!sizeof($this->_errors))
|
||||
{
|
||||
if (!$image->add())
|
||||
$this->_errors[] = Tools::displayError('Error while creating additional image');
|
||||
else
|
||||
$this->copyImage($product->id, $image->id, $method);
|
||||
$id_image = $image->id;
|
||||
}
|
||||
foreach ($_FILES['image_product'] as $key => $array_values) {
|
||||
$i=0;
|
||||
foreach ($array_values as $value) {
|
||||
$images_product[$i][$key] = $value;
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isset($image) AND Validate::isLoadedObject($image) AND !file_exists(_PS_PROD_IMG_DIR_.$image->getExistingImgPath().'.'.$image->image_format))
|
||||
$image->delete();
|
||||
if (sizeof($this->_errors))
|
||||
return false;
|
||||
@unlink(_PS_TMP_IMG_DIR_.'/product_'.$product->id.'.jpg');
|
||||
@unlink(_PS_TMP_IMG_DIR_.'/product_mini_'.$product->id.'.jpg');
|
||||
return ((isset($id_image) AND is_int($id_image) AND $id_image) ? $id_image : true);
|
||||
|
||||
|
||||
if (!Validate::isLoadedObject($product)) {
|
||||
$this->_errors[] = Tools::displayError('Cannot add image because product add failed.');
|
||||
}
|
||||
else {
|
||||
$is_first_uploaded_image_cover = Tools::getValue('cover') == 1;
|
||||
$cover_already_associated = false;
|
||||
|
||||
foreach ($images_product as $image_uploaded) {
|
||||
if ($error = checkImageUploadError($image_uploaded)) {
|
||||
$this->_errors[] = $error;
|
||||
}
|
||||
if (!sizeof($this->_errors) AND isset($image_uploaded['tmp_name']) AND $image_uploaded['tmp_name'] != NULL)
|
||||
{
|
||||
if (substr($image_uploaded['name'], -4) == '.zip') {
|
||||
return $this->uploadImageZip($product);
|
||||
}
|
||||
else {
|
||||
$image = new Image();
|
||||
$image->id_product = (int)($product->id);
|
||||
$_POST['id_product'] = $image->id_product;
|
||||
$image->position = Image::getHighestPosition($product->id) + 1;
|
||||
|
||||
$this->validateRules('Image', 'image');
|
||||
$this->copyFromPost($image, 'image');
|
||||
$image->cover = false;
|
||||
|
||||
if (!$cover_already_associated) {
|
||||
if ($is_first_uploaded_image_cover) {
|
||||
Image::deleteCover($product->id);
|
||||
$image->cover = true;
|
||||
}
|
||||
else {
|
||||
// the image is cover by default if no images exist.
|
||||
$image->cover = (0==sizeof($product->getImages(Configuration::get('PS_LANG_DEFAULT'))));
|
||||
}
|
||||
$cover_already_associated = true;
|
||||
}
|
||||
|
||||
if (!sizeof($this->_errors))
|
||||
{
|
||||
if (!$image->add())
|
||||
$this->_errors[] = Tools::displayError('Error while creating additional image');
|
||||
else
|
||||
$this->copySingleImage($product->id, $image->id, $image_uploaded, $method);
|
||||
$id_image = $image->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isset($image) AND Validate::isLoadedObject($image) AND !file_exists(_PS_PROD_IMG_DIR_.$image->getExistingImgPath().'.'.$image->image_format)) {
|
||||
$image->delete();
|
||||
}
|
||||
@unlink(_PS_TMP_IMG_DIR_.'/product_'.$product->id.'.jpg');
|
||||
@unlink(_PS_TMP_IMG_DIR_.'/product_mini_'.$product->id.'.jpg');
|
||||
$success = $success && ((isset($id_image) AND is_int($id_image) AND $id_image) ? $id_image : true);
|
||||
}
|
||||
|
||||
if (sizeof($this->_errors))
|
||||
return false;
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function uploadImageZip($product)
|
||||
@ -1271,12 +1325,14 @@ class AdminProducts extends AdminTab
|
||||
*
|
||||
* @param integer $id_product Product Id for product image filename
|
||||
* @param integer $id_image Image Id for product image filename
|
||||
* @param array $image_uploaded (equivalent to $_FILES['<image_uploaded_name>'])
|
||||
*/
|
||||
public function copyImage($id_product, $id_image, $method = 'auto')
|
||||
private function copySingleImage($id_product, $id_image, array $image_uploaded, $method = 'auto')
|
||||
{
|
||||
if (!isset($_FILES['image_product']['tmp_name']))
|
||||
if (!isset($image_uploaded['tmp_name'])) {
|
||||
return false;
|
||||
if ($error = checkImage($_FILES['image_product'], $this->maxImageSize))
|
||||
}
|
||||
if ($error = checkImage($image_uploaded, $this->maxImageSize))
|
||||
$this->_errors[] = $error;
|
||||
else
|
||||
{
|
||||
@ -1284,7 +1340,7 @@ class AdminProducts extends AdminTab
|
||||
|
||||
if (!$new_path = $image->getPathForCreation())
|
||||
$this->_errors[] = Tools::displayError('An error occurred during new folder creation');
|
||||
if (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') OR !move_uploaded_file($_FILES['image_product']['tmp_name'], $tmpName))
|
||||
if (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') OR !move_uploaded_file($image_uploaded['tmp_name'], $tmpName))
|
||||
$this->_errors[] = Tools::displayError('An error occurred during the image upload');
|
||||
elseif (!imageResize($tmpName, $new_path.'.'.$image->image_format))
|
||||
$this->_errors[] = Tools::displayError('An error occurred while copying image.');
|
||||
@ -1298,7 +1354,18 @@ class AdminProducts extends AdminTab
|
||||
|
||||
@unlink($tmpName);
|
||||
Module::hookExec('watermark', array('id_image' => $id_image, 'id_product' => $id_product));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a product image
|
||||
*
|
||||
* @param integer $id_product Product Id for product image filename
|
||||
* @param integer $id_image Image Id for product image filename
|
||||
*/
|
||||
public function copyImage($id_product, $id_image, $method = 'auto')
|
||||
{
|
||||
return $this->copySingleImage($id_product, $id_image, $_FILES['image_product'], $method);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -3481,6 +3548,16 @@ class AdminProducts extends AdminTab
|
||||
global $cookie, $currentIndex, $attributeJs, $images;
|
||||
|
||||
$countImages = (int)Db::getInstance()->getValue('SELECT COUNT(*) FROM '._DB_PREFIX_.'image WHERE id_product = '.(int)$obj->id);
|
||||
|
||||
if (Tools::getValue('id_image')) {
|
||||
$input_image_name = 'image_product';
|
||||
$input_image_multiple = '';
|
||||
}
|
||||
else {
|
||||
$input_image_name = 'image_product[]';
|
||||
$input_image_multiple = 'multiple';
|
||||
}
|
||||
|
||||
echo '
|
||||
<div class="tab-page" id="step2">
|
||||
<h4 class="tab">2. '.$this->l('Images').' ('.$countImages.')</h4>
|
||||
@ -3494,7 +3571,7 @@ class AdminProducts extends AdminTab
|
||||
<tr>
|
||||
<td class="col-left">'.$this->l('File:').'</td>
|
||||
<td style="padding-bottom:5px;">
|
||||
<input type="file" id="image_product" name="image_product" />
|
||||
<input type="file" id="image_product" name="'.$input_image_name.'" '.$input_image_multiple.' />
|
||||
<p>
|
||||
'.$this->l('Format:').' JPG, GIF, PNG. '.$this->l('Filesize:').' '.($this->maxImageSize / 1000).''.$this->l('Kb max.').'
|
||||
<br />'.$this->l('You can also upload a ZIP file containing several images. Thumbnails will be resized automatically.').'
|
||||
|
Loading…
Reference in New Issue
Block a user