From c7cdfaf5bb4693ca6a8f4c24b26e1b7091c31d74 Mon Sep 17 00:00:00 2001 From: mMarinetti Date: Thu, 22 Dec 2011 13:43:23 +0000 Subject: [PATCH] // fix #PSTEST-125 --- admin-dev/themes/template/products/images.tpl | 55 ++++++++++--------- classes/AdminController.php | 12 +++- controllers/admin/AdminProductsController.php | 29 +++++++--- js/admin.js | 18 +++--- 4 files changed, 67 insertions(+), 47 deletions(-) diff --git a/admin-dev/themes/template/products/images.tpl b/admin-dev/themes/template/products/images.tpl index b28ed79e8..feab413c2 100644 --- a/admin-dev/themes/template/products/images.tpl +++ b/admin-dev/themes/template/products/images.tpl @@ -219,41 +219,44 @@ $("#listImage").append("
  • " + filename + "
    \"\"

  • "); } }); - + + /** + * on success function + */ + function afterDeleteProductImage(data) + { + data = $.parseJSON(data); + if (data) + { + cover = 0; + id = data.content.id; + if(data.status == 'ok') + { + if ($("#" + id).find(".covered").attr("src") == "../img/admin/enabled.gif") + cover = 1; + $("#" + id).remove(); + } + if (cover) + $("#imageTable tr").eq(1).find(".covered").attr("src", "../img/admin/enabled.gif"); + $("#countImage").html(parseInt($("#countImage").html()) - 1); + refreshImagePositions($("#imageTable")); + } + } + $('.delete_product_image').die().live('click', function(e) { e.preventDefault(); id = $(this).parent().parent().attr('id'); if (confirm("{/literal}{l s='Are you sure?' js=1}{literal}")) - $.ajax( - { - url : "ajax-tab.php", - data : {"action":"deleteProductImage", + doAdminAjax({ + "action":"deleteProductImage", "id_image":id, "id_product" : {/literal}{$id_product}{literal}, "id_category" : {/literal}{$id_category_default}{literal}, "token" : "{/literal}{$token}{literal}", "tab" : "AdminProducts", - "ajax" : 1 }, - success : function (data) - { - data = jQuery.parseJSON(data); - if (data) - { - cover = 0; - if(data.status == 'ok') - { - if ($("#" + id).find(".covered").attr("src") == "../img/admin/enabled.gif") - cover = 1; - $("#" + id).remove(); - } - if (cover) - $("#imageTable tr").eq(1).find(".covered").attr("src", "../img/admin/enabled.gif"); - $("#countImage").html(parseInt($("#countImage").html()) - 1); - refreshImagePositions($("#imageTable")); - } - } - }); + "ajax" : 1 }, afterDeleteProductImage + ); }); $('.covered').die().live('click', function(e) @@ -298,7 +301,7 @@ function updateImagePositon(json) { doAdminAjax( - { + { "action":"updateImagePosition", "json":json, "token" : "{/literal}{$token}{literal}", diff --git a/classes/AdminController.php b/classes/AdminController.php index 8c95c4cea..af6d22bd3 100644 --- a/classes/AdminController.php +++ b/classes/AdminController.php @@ -230,7 +230,7 @@ class AdminControllerCore extends Controller protected $_redirect = true; /** @var array Name and directory where class image are located */ - public $fieldImageSettings = array(); + public $fieldImageSettings = null; /** @var string Image type */ public $imageType = 'jpg'; @@ -528,8 +528,9 @@ class AdminControllerCore extends Controller */ public function processDelete($token) { - if (Validate::isLoadedObject($object = $this->loadObject()) && isset($this->fieldImageSettings)) + if (Validate::isLoadedObject($object = $this->loadObject())) { + $res = true; // check if request at least one object with noZeroObject if (isset($object->noZeroObject) && count(call_user_func(array($this->className, $object->noZeroObject))) <= 1) { @@ -543,7 +544,12 @@ class AdminControllerCore extends Controller { if ($this->deleted) { - $object->deleteImage(); + if (isset($this->fieldImageSettings)) + $res = $object->deleteImage(); + + if (!$res) + $this->_errors[] = Tools::displayError('Unable to delete associated images'); + $object->deleted = 1; if ($object->update()) $this->redirect_after = self::$currentIndex.'&conf=1&token='.$token; diff --git a/controllers/admin/AdminProductsController.php b/controllers/admin/AdminProductsController.php index d8507e778..f461a30da 100644 --- a/controllers/admin/AdminProductsController.php +++ b/controllers/admin/AdminProductsController.php @@ -1239,22 +1239,33 @@ class AdminProductsControllerCore extends AdminController public function ajaxProcessDeleteProductImage() { + $this->json = true; + $this->display = 'content'; + $res = true; + /* Delete product image */ $image = new Image((int)Tools::getValue('id_image')); - $image->delete(); + $this->content['id'] = $image->id; + $res &= $image->delete(); + // if deleted image was the cover, change it to the first one if (!Image::getCover($image->id_product)) { - $first_img = Db::getInstance()->getRow(' - SELECT `id_image` FROM `'._DB_PREFIX_.'image` - WHERE `id_product` = '.(int)$image->id_product); - Db::getInstance()->Execute(' + $res &= Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'image` SET `cover` = 1 - WHERE `id_image` = '.(int)$first_img['id_image']); + WHERE `id_product` = '.(int)$image->id_product.' LIMIT 1'); } - @unlink(_PS_TMP_IMG_DIR_.'/product_'.$image->id_product.'.jpg'); - @unlink(_PS_TMP_IMG_DIR_.'/product_mini_'.$image->id_product.'.jpg'); - $this->content = '{"status":"ok"}'; + if(file_exists(_PS_TMP_IMG_DIR_.'/product_'.$image->id_product.'.jpg')) + $res &= @unlink(_PS_TMP_IMG_DIR_.'/product_'.$image->id_product.'.jpg'); + if(file_exists(_PS_TMP_IMG_DIR_.'/product_mini_'.$image->id_product.'.jpg')) + $res &= @unlink(_PS_TMP_IMG_DIR_.'/product_mini_'.$image->id_product.'.jpg'); + + if ($res) + $this->confirmations[] = $this->_conf[7]; + else + $this->_errors[] = Tools::displayError('Error on deleting product image'); + + $this->status = 'ok'; } protected function _validateSpecificPrice($id_shop, $id_currency, $id_country, $id_group, $id_customer, $price, $from_quantity, $reduction, $reduction_type, $from, $to) diff --git a/js/admin.js b/js/admin.js index a8d4d9a5d..4f32403d0 100644 --- a/js/admin.js +++ b/js/admin.js @@ -929,14 +929,8 @@ function refreshImagePositions(imageTable) var reg = /_[0-9]$/g; var up_reg = new RegExp("imgPosition=[0-9]+&"); - imageTable.find("tbody tr").each(function(i) { - // Update link position - // Up links - $(this).find("td.dragHandle a:first").attr("href", $(this).find("td.dragHandle a:first").attr("href").replace(up_reg, "imgPosition="+ i +"&"));//, "imgPosition="+ (i - 1) +"&")); - // Down links - $(this).find("td.dragHandle a:last").attr("href", $(this).find("td.dragHandle a:last").attr("href").replace(up_reg, "imgPosition="+ (i + 2) +"&")); - // Position image cell - $(this).find("td.positionImage").html(i + 1); + imageTable.find("tbody tr").each(function(i,el) { + $(el).find("td.positionImage").html(i + 1); }); imageTable.find("tr td.dragHandle a:hidden").show(); imageTable.find("tr td.dragHandle:first a:first").hide(); @@ -944,13 +938,16 @@ function refreshImagePositions(imageTable) } -function doAdminAjax(data) +function doAdminAjax(data, success_func, error_func) { $.ajax( { url : 'index.php', data : data, success : function(data){ + if (success_func) + return success_func(data); + data = $.parseJSON(data); if(data.confirmations.length != 0) showSuccessMessage(data.confirmations); @@ -958,6 +955,9 @@ function doAdminAjax(data) showErrorMessage(data.error); }, error : function(data){ + if (error_func) + return error_func(data); + alert("[TECHNICAL ERROR]"); } });