From 5bf9050a12b58c037d4a0daa922222adf28f99b6 Mon Sep 17 00:00:00 2001 From: touffies Date: Tue, 5 Nov 2013 17:33:55 +0100 Subject: [PATCH] Delete address with ajax callback function address.delete() --- .../Thelia/Config/Resources/routing/front.xml | 1 + .../Controller/Front/AddressController.php | 40 ++++++++++++++++-- templates/default/account.html | 2 +- templates/default/assets/js/script.js | 41 ++++++++++++++----- templates/default/order-delivery.html | 2 +- 5 files changed, 70 insertions(+), 16 deletions(-) diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index cf7797cc9..4d811599c 100755 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -110,6 +110,7 @@ + Thelia\Controller\Front\CartController::changeItem Thelia\Controller\Front\CartController::changeItem cart diff --git a/core/lib/Thelia/Controller/Front/AddressController.php b/core/lib/Thelia/Controller/Front/AddressController.php index 6ab6e72e8..77af4991e 100644 --- a/core/lib/Thelia/Controller/Front/AddressController.php +++ b/core/lib/Thelia/Controller/Front/AddressController.php @@ -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) diff --git a/templates/default/account.html b/templates/default/account.html index 0174f1a3a..863104615 100644 --- a/templates/default/account.html +++ b/templates/default/account.html @@ -119,7 +119,7 @@
{intl l="Edit"} {if $DEFAULT != 1} - {intl l="Cancel"} + {intl l="Cancel"} {/if}
diff --git a/templates/default/assets/js/script.js b/templates/default/assets/js/script.js index 041379a67..c6f695114 100644 --- a/templates/default/assets/js/script.js +++ b/templates/default/assets/js/script.js @@ -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(); + } } } } diff --git a/templates/default/order-delivery.html b/templates/default/order-delivery.html index 2cbea4ff2..c38b3fb0f 100644 --- a/templates/default/order-delivery.html +++ b/templates/default/order-delivery.html @@ -98,7 +98,7 @@
{intl l="Edit"} {if $DEFAULT != 1} - {intl l="Cancel"} + {intl l="Cancel"} {/if}