From 16275ef51a205fb2ffa8263039bdafbae8f24157 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 19 Feb 2014 10:41:26 +0100 Subject: [PATCH] create error message when a customer is deleted and already have orders. Fix #199 --- core/lib/Thelia/Action/Customer.php | 6 ++++ .../Controller/Admin/CustomerController.php | 22 ++++++++++-- .../Thelia/Exception/CustomerException.php | 34 +++++++++++++++++++ core/lib/Thelia/Model/Customer.php | 9 +++++ templates/backOffice/default/customers.html | 6 ++++ 5 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 core/lib/Thelia/Exception/CustomerException.php diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index 137ba33d8..6220aefe5 100644 --- a/core/lib/Thelia/Action/Customer.php +++ b/core/lib/Thelia/Action/Customer.php @@ -31,6 +31,8 @@ use Thelia\Core\Event\LostPasswordEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Security\SecurityContext; use Thelia\Core\Template\ParserInterface; +use Thelia\Core\Translation\Translator; +use Thelia\Exception\CustomerException; use Thelia\Mailer\MailerFactory; use Thelia\Model\ConfigQuery; use Thelia\Model\Customer as CustomerModel; @@ -106,6 +108,10 @@ class Customer extends BaseAction implements EventSubscriberInterface { if (null !== $customer = $event->getCustomer()) { + if (true === $customer->hasOrder()) { + throw new CustomerException(Translator::getInstance()->trans("Impossible to delete a customer who already have orders")); + } + $customer->delete(); } } diff --git a/core/lib/Thelia/Controller/Admin/CustomerController.php b/core/lib/Thelia/Controller/Admin/CustomerController.php index 2368e630a..ed8eb5651 100644 --- a/core/lib/Thelia/Controller/Admin/CustomerController.php +++ b/core/lib/Thelia/Controller/Admin/CustomerController.php @@ -27,6 +27,7 @@ use Thelia\Core\Security\Resource\AdminResources; use Thelia\Core\Event\Customer\CustomerCreateOrUpdateEvent; use Thelia\Core\Event\Customer\CustomerEvent; use Thelia\Core\Event\TheliaEvents; +use Thelia\Exception\CustomerException; use Thelia\Form\CustomerCreateForm; use Thelia\Form\CustomerUpdateForm; use Thelia\Model\CustomerQuery; @@ -178,13 +179,14 @@ class CustomerController extends AbstractCrudController ); } - protected function renderListTemplate($currentOrder) + protected function renderListTemplate($currentOrder, $customParams = array()) { - return $this->render('customers', array( + return $this->render('customers', array_merge(array( 'customer_order' => $currentOrder, 'display_customer' => 20, 'page' => $this->getRequest()->get('page', 1) - )); + ), $customParams) + ); } protected function redirectToListTemplate() @@ -203,4 +205,18 @@ class CustomerController extends AbstractCrudController { $this->redirectToRoute("admin.customer.update.view", $this->getEditionArguments()); } + + public function deleteAction() + { + try { + parent::deleteAction(); + } catch (CustomerException $e) { + $error_msg = $e->getMessage(); + } + + return $this->renderListTemplate($this->getCurrentListOrder(), array( + "removal_error" => true, + "error_message" => $error_msg + )); + } } diff --git a/core/lib/Thelia/Exception/CustomerException.php b/core/lib/Thelia/Exception/CustomerException.php new file mode 100644 index 000000000..48abf7020 --- /dev/null +++ b/core/lib/Thelia/Exception/CustomerException.php @@ -0,0 +1,34 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Exception; + + +/** + * Class CustomerException + * @package Thelia\Exception + * @author Manuel Raynaud + */ +class CustomerException extends \RuntimeException +{ +} \ No newline at end of file diff --git a/core/lib/Thelia/Model/Customer.php b/core/lib/Thelia/Model/Customer.php index 127a69a82..832526e6b 100644 --- a/core/lib/Thelia/Model/Customer.php +++ b/core/lib/Thelia/Model/Customer.php @@ -239,6 +239,15 @@ class Customer extends BaseCustomer implements UserInterface return $this->getRememberMeSerial(); } + public function hasOrder() + { + $order = OrderQuery::create() + ->filterByCustomerId($this->getId()) + ->count(); + + return $order > 0; + } + /** * {@inheritDoc} */ diff --git a/templates/backOffice/default/customers.html b/templates/backOffice/default/customers.html index eccb6e63e..477921719 100644 --- a/templates/backOffice/default/customers.html +++ b/templates/backOffice/default/customers.html @@ -20,6 +20,12 @@
+ {if $removal_error } +
+ + +
+ {/if}