Delete address with ajax callback function address.delete()

This commit is contained in:
touffies
2013-11-05 17:33:55 +01:00
parent 1426984f55
commit 5bf9050a12
5 changed files with 70 additions and 16 deletions

View File

@@ -110,6 +110,7 @@
</route>
<route id="cart.update.quantity" path="/cart/update">
<default key="_controller">Thelia\Controller\Front\CartController::changeItem</default>
<default key="_controller">Thelia\Controller\Front\CartController::changeItem</default>
<default key="_view">cart</default>
</route>

View File

@@ -156,17 +156,51 @@ class AddressController extends BaseFrontController
public function deleteAction($address_id)
{
$this->checkAuth();
$error_message = false;
$customer = $this->getSecurityContext()->getCustomerUser();
$address = AddressQuery::create()->findPk($address_id);
if (!$address || $customer->getId() != $address->getCustomerId()) {
$this->redirectToRoute('default');
// If Ajax Request
if ($this->getRequest()->isXmlHttpRequest()) {
return $this->jsonResponse(json_encode(array(
"success" => false,
"message" => "Error during address deletion process"
)));
} else {
$this->redirectToRoute('default');
}
}
$this->dispatch(TheliaEvents::ADDRESS_DELETE, new AddressEvent($address));
try {
$this->dispatch(TheliaEvents::ADDRESS_DELETE, new AddressEvent($address));
} catch (\Exception $e) {
$error_message = $e->getMessage();
}
$this->redirectToRoute('default', array('view'=>'account'));
\Thelia\Log\Tlog::getInstance()->error(sprintf('Error during address deletion : %s', $error_message));
// If Ajax Request
if ($this->getRequest()->isXmlHttpRequest()) {
if ($error_message) {
$response = $this->jsonResponse(json_encode(array(
"success" => false,
"message" => $error_message
)));
} else {
$response = $this->jsonResponse(json_encode(array(
"success" => true,
"message" => ""
)));;
}
return $response;
} else {
$this->redirectToRoute('default', array('view'=>'account'));
}
}
protected function createAddressEvent($form)

View File

@@ -119,7 +119,7 @@
<div class="group-btn">
<a href="{url path="/address/update/{$ID}"}" class="btn btn-edit-address" data-toggle="tooltip" title="{intl l="Edit this address"}"><i class="icon-pencil"></i> <span>{intl l="Edit"}</span></a>
{if $DEFAULT != 1}
<a href="{url path="/address/delete/{$ID}"}" class="btn btn-remove-address" data-confirm="{intl l="Do you really want to delete this address ?"}" title="{intl l="Remove this address"}" data-toggle="tooltip"><i class="icon-remove"></i> <span>{intl l="Cancel"}</span></a>
<a href="{url path="/address/delete/{$ID}"}" class="btn btn-remove-address" data-confirm="{intl l="Do you really want to delete this address ?"}" data-confirm-callback="address.delete" title="{intl l="Remove this address"}" data-toggle="tooltip"><i class="icon-remove"></i> <span>{intl l="Cancel"}</span></a>
{/if}
</div>
</td>

View File

@@ -1,7 +1,20 @@
/* JQUERY PREVENT CONFLICT */
(function($) {
/* ------------------------------------------------------------------
/* ------------------------------------------------------------------
callback Function -------------------------------------------------- */
var confirmCallback = {
'address.delete': function($elm){
$.post($elm.attr('href'), function(data){
if(data.success)
$elm.closest('tr').remove();
else
bootbox.alert(data.message);
});
}
}
/* ------------------------------------------------------------------
onLoad Function -------------------------------------------------- */
$(document).ready(function(){
@@ -49,19 +62,25 @@
// Confirm Dialog
$(document).on('click.confirm', '[data-confirm]', function (e) {
var $this = $(this),
href = $this.attr('href'),
title = $this.attr('data-confirm') != '' ? $this.attr('data-confirm') : 'Are you sure?';
var $this = $(this),
href = $this.attr('href'),
callback = $this.attr('data-confirm-callback'),
title = $this.attr('data-confirm') != '' ? $this.attr('data-confirm') : 'Are you sure?';
bootbox.confirm(title, function(confirm) {
bootbox.confirm(title, function(confirm) {
if(confirm){
if(href){
window.location.href = href;
//Check if callback and if it's a function
if (callback && $.isFunction(confirmCallback[callback])) {
confirmCallback[callback]($this);
} else {
// If forms
var $form = $this.closest("form");
if($form.size() > 0){
$form.submit();
if(href){
window.location.href = href;
} else {
// If forms
var $form = $this.closest("form");
if($form.size() > 0){
$form.submit();
}
}
}
}

View File

@@ -98,7 +98,7 @@
<div class="group-btn">
<a href="{url path="/address/update/{$ID}"}" class="btn btn-edit-address" data-toggle="tooltip" title="{intl l="Edit this address"}"><i class="icon-pencil"></i> <span>{intl l="Edit"}</span></a>
{if $DEFAULT != 1}
<a href="{url path="/address/delete/{$ID}"}" class="btn btn-remove-address" data-confirm="{intl l="Do you really want to delete this address ?"}" title="{intl l="Remove this address"}" data-toggle="tooltip"><i class="icon-remove"></i> <span>{intl l="Cancel"}</span></a>
<a href="{url path="/address/delete/{$ID}"}" class="btn btn-remove-address" data-confirm="{intl l="Do you really want to delete this address ?"}" data-confirm-callback="address.delete" title="{intl l="Remove this address"}" data-toggle="tooltip"><i class="icon-remove"></i> <span>{intl l="Cancel"}</span></a>
{/if}
</div>
</td>