[*] BO : #PSTEST-1157 - Ajax deletion of virtual file
This commit is contained in:
@@ -52,11 +52,11 @@
|
||||
$("#upload-confirmation td").html('<div class="error">{l s='Error:'} ' + msg + '</div>');
|
||||
else
|
||||
{
|
||||
$('#upload_input').remove();
|
||||
$('#upload_input').hide();
|
||||
$('#file_missing').hide();
|
||||
$('#virtual_product_name').attr('value', fileName);
|
||||
$('#upload-confirmation div').prepend('{l s='The file'} "<a class="link" href="get-file-admin.php?file='+msg+'&filename='+fileName+'">'+fileName+'</a>" {l s='has successfully been uploaded'}' +
|
||||
'<input type="hidden" id="virtual_product_filename" name="virtual_product_filename" value="' + msg + '" />');
|
||||
$('#upload-confirmation div').prepend('<span>{l s='The file'} "<a class="link" href="get-file-admin.php?file='+msg+'&filename='+fileName+'">'+fileName+'</a>" {l s='has successfully been uploaded'}' +
|
||||
'<input type="hidden" id="virtual_product_filename" name="virtual_product_filename" value="' + msg + '" /></span>');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -138,31 +138,28 @@
|
||||
<input type="hidden" id="virtual_product_id" name="virtual_product_id" value="{$product->productDownload->id}" />
|
||||
{/if}
|
||||
<table cellpadding="5" style="float: left; margin-left: 10px;">
|
||||
{if !$product->productDownload->checkFile()}
|
||||
{if $show_file_input}
|
||||
<tr id="upload_input">
|
||||
<td class="col-left">
|
||||
<label id="virtual_product_file_label" for="virtual_product_file" class="t">{l s='Upload a file'}</label>
|
||||
</td>
|
||||
<td class="col-right">
|
||||
<input type="file" id="virtual_product_file" name="virtual_product_file" onchange="uploadFile();" maxlength="{$upload_max_filesize}" />
|
||||
<p class="preference_description">{l s='Your server\'s maximum upload file size is'}: {$upload_max_filesize} {l s='MB'}</p>
|
||||
</td>
|
||||
</tr>
|
||||
{/if}
|
||||
<tr id="upload-confirmation" style="display:none">
|
||||
<td colspan=2>
|
||||
{if $up_filename}
|
||||
<input type="hidden" id="virtual_product_filename" name="virtual_product_filename" value="{$up_filename}" />
|
||||
{/if}
|
||||
<div class="conf">
|
||||
<a id="delete_downloadable_product" onclick="return confirm('{l s='Delete this file'}')" href="{$currentIndex}&deleteVirtualProduct=true&token={$token}&id_product={$product->id}" class="red">
|
||||
<img src="../img/admin/delete.gif" alt="{l s='Delete this file'}"/>
|
||||
</a>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
{else}
|
||||
<tr id="upload_input" {if $is_file}style="display:none"{/if}>
|
||||
<td class="col-left">
|
||||
<label id="virtual_product_file_label" for="virtual_product_file" class="t">{l s='Upload a file'}</label>
|
||||
</td>
|
||||
<td class="col-right">
|
||||
<input type="file" id="virtual_product_file" name="virtual_product_file" onchange="uploadFile();" maxlength="{$upload_max_filesize}" />
|
||||
<p class="preference_description">{l s='Your server\'s maximum upload file size is'}: {$upload_max_filesize} {l s='MB'}</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="upload-confirmation" style="display:none">
|
||||
<td colspan=2>
|
||||
{if $up_filename}
|
||||
<input type="hidden" id="virtual_product_filename" name="virtual_product_filename" value="{$up_filename}" />
|
||||
{/if}
|
||||
<div class="conf">
|
||||
<a class="delete_virtual_product" id="delete_downloadable_product" onclick="return confirm('{l s='Delete this file'}')" href="{$currentIndex}&deleteVirtualProduct=true&token={$token}&id_product={$product->id}" class="red">
|
||||
<img src="../img/admin/delete.gif" alt="{l s='Delete this file'}"/>
|
||||
</a>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
{if $is_file}
|
||||
<tr>
|
||||
<td class="col-left">
|
||||
<input type="hidden" id="virtual_product_filename" name="virtual_product_filename" value="{$product->productDownload->filename}" />
|
||||
@@ -170,7 +167,7 @@
|
||||
</td>
|
||||
<td class="col-right">
|
||||
{$product->productDownload->getHtmlLink(false, true)}
|
||||
<a onclick="return confirm('{l s='Delete this file'})')" href="{$currentIndex}&deleteVirtualProduct=true&token={$token}&id_product={$product->id}" class="red">
|
||||
<a onclick="return confirm('{l s='Delete this file'})')" href="{$currentIndex}&deleteVirtualProduct=true&token={$token}&id_product={$product->id}" class="red delete_virtual_product">
|
||||
<img src="../img/admin/delete.gif" alt="{l s='Delete this file'}"/>
|
||||
</a>
|
||||
</td>
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
*}
|
||||
{if isset($json)}
|
||||
{
|
||||
"status":"{$status}",
|
||||
"status" : "{$status}",
|
||||
"confirmations" : {$confirmations},
|
||||
"informations" : {$informations},
|
||||
"error" : {$errors},
|
||||
|
||||
@@ -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';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user