diff --git a/admin-dev/themes/default/template/controllers/products/virtualproduct.tpl b/admin-dev/themes/default/template/controllers/products/virtualproduct.tpl index 7caaf9b53..03f85757e 100644 --- a/admin-dev/themes/default/template/controllers/products/virtualproduct.tpl +++ b/admin-dev/themes/default/template/controllers/products/virtualproduct.tpl @@ -52,11 +52,11 @@ $("#upload-confirmation td").html('
{l s='Error:'} ' + msg + '
'); else { - $('#upload_input').remove(); + $('#upload_input').hide(); $('#file_missing').hide(); $('#virtual_product_name').attr('value', fileName); - $('#upload-confirmation div').prepend('{l s='The file'} "'+fileName+'" {l s='has successfully been uploaded'}' + - ''); + $('#upload-confirmation div').prepend('{l s='The file'} "'+fileName+'" {l s='has successfully been uploaded'}' + + ''); } } } @@ -138,31 +138,28 @@ {/if} - {if !$product->productDownload->checkFile()} - {if $show_file_input} - - - - - {/if} - - - - {else} + + + + + + + + {if $is_file} diff --git a/admin-dev/themes/default/template/layout-ajax.tpl b/admin-dev/themes/default/template/layout-ajax.tpl index 952d8b976..0241d46f6 100644 --- a/admin-dev/themes/default/template/layout-ajax.tpl +++ b/admin-dev/themes/default/template/layout-ajax.tpl @@ -25,7 +25,7 @@ *} {if isset($json)} { - "status":"{$status}", + "status" : "{$status}", "confirmations" : {$confirmations}, "informations" : {$informations}, "error" : {$errors}, diff --git a/classes/controller/AdminController.php b/classes/controller/AdminController.php index f2194f0a1..1cb9ffe27 100644 --- a/classes/controller/AdminController.php +++ b/classes/controller/AdminController.php @@ -2578,5 +2578,31 @@ class AdminControllerCore extends Controller return $this->context->smarty->createTemplate($this->context->smarty->getTemplateDir(0).$tpl_name, $this->context->smarty); } + + /** + * Shortcut to set up a json success payload + * + * @param $message success message + */ + public function jsonConfirmation($message) + { + $this->json = true; + $this->confirmations[] = $message; + if ($this->status === '') + $this->status = 'ok'; + } + + /** + * Shortcut to set up a json error payload + * + * @param $message error message + */ + public function jsonError($message) + { + $this->json = true; + $this->errors[] = $message; + if ($this->status === '') + $this->status = 'error'; + } } diff --git a/controllers/admin/AdminProductsController.php b/controllers/admin/AdminProductsController.php index d2de7440d..8b7098c02 100644 --- a/controllers/admin/AdminProductsController.php +++ b/controllers/admin/AdminProductsController.php @@ -299,12 +299,18 @@ class AdminProductsControllerCore extends AdminController } } - public function processDeleteVirtualProduct() + public function ajaxProcessDeleteVirtualProduct() { if (!($id_product_download = ProductDownload::getIdFromIdAttribute((int)Tools::getValue('id_product'), 0))) - return false; - $product_download = new ProductDownload((int)$id_product_download); - return $product_download->deleteFile((int)$id_product_download); + $this->jsonError($this->l('Cannot retrieve file')); + else + { + $product_download = new ProductDownload((int)$id_product_download); + if (!$product_download->deleteFile((int)$id_product_download)) + $this->jsonError($this->l('Cannot delete file')); + else + $this->jsonConfirmation($this->_conf[1]); + } } public function processDeleteVirtualProductAttribute() @@ -1237,7 +1243,6 @@ class AdminProductsControllerCore extends AdminController public function ajaxProcessUpdateProductImageShopAsso() { - $this->json = true; if (($id_image = Tools::getValue('id_image')) && ($id_shop = (int)Tools::getValue('id_shop'))) if (Tools::getValue('active') == 'true') $res = Db::getInstance()->execute( @@ -1251,15 +1256,13 @@ class AdminProductsControllerCore extends AdminController ); if ($res) - $this->confirmations[] = $this->_conf[27]; + $this->jsonConfirmation($this->_conf[27]); else - $this->errors[] = Tools::displayError('Error on picture shop association'); - $this->status = 'ok'; + $this->jsonError(Tools::displayError('Error on picture shop association')); } public function ajaxProcessUpdateImagePosition() { - $this->json = true; $res = false; if ($json = Tools::getValue('json')) { @@ -1274,15 +1277,13 @@ class AdminProductsControllerCore extends AdminController } } if ($res) - $this->confirmations[] = $this->_conf[25]; + $this->jsonConfirmation($this->_conf[25]); else - $this->errors[] = Tools::displayError('Error on moving picture'); - $this->status = 'ok'; + $this->jsonError(Tools::displayError('Error on moving picture')); } public function ajaxProcessUpdateCover() { - $this->json = true; Image::deleteCover((int)Tools::getValue('id_product')); $img = new Image((int)Tools::getValue('id_image')); $img->cover = 1; @@ -1291,14 +1292,13 @@ class AdminProductsControllerCore extends AdminController @unlink(_PS_TMP_IMG_DIR_.'product_mini_'.(int)$img->id_product.'.jpg'); if ($img->update()) - $this->confirmations[] = $this->_conf[26]; + $this->jsonConfirmation($this->_conf[26]); else - $this->errors[] = Tools::displayError('Error on moving picture'); + $this->jsonError(Tools::displayError('Error on moving picture')); } public function ajaxProcessDeleteProductImage() { - $this->json = true; $this->display = 'content'; $res = true; /* Delete product image */ @@ -1320,11 +1320,9 @@ class AdminProductsControllerCore extends AdminController $res &= @unlink(_PS_TMP_IMG_DIR_.'product_mini_'.$image->id_product.'.jpg'); if ($res) - $this->confirmations[] = $this->_conf[7]; + $this->jsonConfirmation($this->_conf[7]); else - $this->errors[] = Tools::displayError('Error on deleting product image'); - - $this->status = 'ok'; + $this->jsonError(Tools::displayError('Error on deleting product image')); } protected function _validateSpecificPrice($id_shop, $id_currency, $id_country, $id_group, $id_customer, $price, $from_quantity, $reduction, $reduction_type, $from, $to, $id_combination = 0) @@ -2842,16 +2840,10 @@ class AdminProductsControllerCore extends AdminController if (empty($product->cache_default_attribute)) { - $data->assign('show_file_input', !strval(Tools::getValue('virtual_product_filename')) || $product->productDownload->id > 0); // found in informations and combination : to merge $data->assign('up_filename', strval(Tools::getValue('virtual_product_filename'))); $display_filename = ($product->productDownload->id > 0) ? $product->productDownload->display_filename : htmlentities(Tools::getValue('virtual_product_name'), ENT_COMPAT, 'UTF-8'); - if (!$product->productDownload->id || !$product->productDownload->active) - $hidden = 'display:none;'; - else - $hidden = ''; - $product->productDownload->nb_downloadable = ($product->productDownload->id > 0) ? $product->productDownload->nb_downloadable : htmlentities(Tools::getValue('virtual_product_nb_downloable'), ENT_COMPAT, 'UTF-8'); $product->productDownload->date_expiration = ($product->productDownload->id > 0) ? ((!empty($product->productDownload->date_expiration) && $product->productDownload->date_expiration != '0000-00-00 00:00:00') ? date('Y-m-d', strtotime($product->productDownload->date_expiration)) : '' ) : htmlentities(Tools::getValue('virtual_product_expiration_date'), ENT_COMPAT, 'UTF-8'); $product->productDownload->nb_days_accessible = ($product->productDownload->id > 0) ? $product->productDownload->nb_days_accessible : htmlentities(Tools::getValue('virtual_product_nb_days'), ENT_COMPAT, 'UTF-8'); @@ -2883,6 +2875,7 @@ class AdminProductsControllerCore extends AdminController $data->assign('currency', $currency); $data->assign($this->tpl_form_vars); $data->assign('link', $this->context->link); + $data->assign('is_file', $product->productDownload->checkFile()); $this->tpl_form_vars['product'] = $product; $this->tpl_form_vars['custom_form'] = $data->fetch(); } diff --git a/js/admin-products.js b/js/admin-products.js index cb43c7e5a..6d230e748 100644 --- a/js/admin-products.js +++ b/js/admin-products.js @@ -1307,6 +1307,29 @@ product_tabs['VirtualProduct'] = new function(){ $('#is_virtual_file_product').hide(); } }); + + // Bind file deletion + $(('#product-tab-content-VirtualProduct')).delegate('a.delete_virtual_product', 'click', function(e){ + e.preventDefault(); + if (!$('#virtual_product_id').val()) + { + $('#upload_input').show(); + $('#virtual_product_name').val(''); + $('#virtual_product_file').val(''); + $('#upload-confirmation').hide().find('span').remove(); + } + else + { + var object = this; + ajaxAction(this.href, 'deleteVirtualProduct', function(){ + $(object).closest('tr').remove(); + $('#upload_input').show(); + $('#virtual_product_name').val(''); + $('#virtual_product_file').val(''); + $('#virtual_product_id').remove(); + }); + } + }); } } @@ -1365,6 +1388,45 @@ function refreshImagePositions(imageTable) imageTable.find("tr td.dragHandle:last a:last").hide(); } +/** + * Generic ajax call for actions expecting a json return + * + * @param url + * @param action + * @param success_callback called if the return status is 'ok' (optional) + * @param failure_callback called if the return status is not 'ok' (optional) + */ +function ajaxAction (url, action, success_callback, failure_callback){ + $.ajax({ + url: url, + data: { + id_product: id_product, + action: action, + ajax: true + }, + dataType: 'json', + context: this, + async: false, + success: function(data) { + if (data.status == 'ok') + { + showSuccessMessage(data.confirmations); + if (typeof success_callback == 'function') + success_callback(); + } + else + { + showErrorMessage(data.error); + if (typeof failure_callback == 'function') + failure_callback(); + } + }, + error : function(data){ + showErrorMessage(("[TECHNICAL ERROR]")); + } + }); +}; + var tabs_manager = new ProductTabsManager(); tabs_manager.setTabs(product_tabs);
- - - -

{l s='Your server\'s maximum upload file size is'}: {$upload_max_filesize} {l s='MB'}

-
+ + + +

{l s='Your server\'s maximum upload file size is'}: {$upload_max_filesize} {l s='MB'}

+
@@ -170,7 +167,7 @@ {$product->productDownload->getHtmlLink(false, true)} - + {l s='Delete this file'}