From fa45df32757c156f5eafb8407e93a062fb0d656c Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Thu, 3 Oct 2013 15:53:30 +0200 Subject: [PATCH] allow to delete customer address --- core/lib/Thelia/Action/Customer.php | 21 ++++-- .../Thelia/Config/Resources/routing/admin.xml | 4 ++ .../Controller/Admin/BaseAdminController.php | 2 +- .../Controller/Admin/CustomerController.php | 30 +++++++++ .../Event/Customer/CustomerAddressEvent.php | 66 +++++++++++++++++++ core/lib/Thelia/Core/Event/TheliaEvents.php | 5 ++ templates/admin/default/customer-edit.html | 52 +++++++++------ 7 files changed, 153 insertions(+), 27 deletions(-) create mode 100644 core/lib/Thelia/Core/Event/Customer/CustomerAddressEvent.php diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index 061dba028..46a7396c1 100755 --- a/core/lib/Thelia/Action/Customer.php +++ b/core/lib/Thelia/Action/Customer.php @@ -25,6 +25,7 @@ namespace Thelia\Action; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Thelia\Core\Event\ActionEvent; +use Thelia\Core\Event\Customer\CustomerAddressEvent; use Thelia\Core\Event\Customer\CustomerCreateOrUpdateEvent; use Thelia\Core\Event\Customer\CustomerEvent; use Thelia\Core\Event\TheliaEvents; @@ -110,6 +111,15 @@ class Customer extends BaseAction implements EventSubscriberInterface $this->getSecurityContext()->clearCustomerUser(); } + public function deleteAddress(CustomerAddressEvent $event) + { + $address = $event->getAddress(); + + $address->delete(); + + $event->setAddress($address); + } + public function changePassword(ActionEvent $event) { // TODO @@ -148,11 +158,12 @@ class Customer extends BaseAction implements EventSubscriberInterface public static function getSubscribedEvents() { return array( - TheliaEvents::CUSTOMER_CREATEACCOUNT => array("create", 128), - TheliaEvents::CUSTOMER_UPDATEACCOUNT => array("modify", 128), - TheliaEvents::CUSTOMER_LOGOUT => array("logout", 128), - TheliaEvents::CUSTOMER_LOGIN => array("login" , 128), - TheliaEvents::CUSTOMER_DELETEACCOUNT => array("delete", 128), + TheliaEvents::CUSTOMER_CREATEACCOUNT => array('create', 128), + TheliaEvents::CUSTOMER_UPDATEACCOUNT => array('modify', 128), + TheliaEvents::CUSTOMER_LOGOUT => array('logout', 128), + TheliaEvents::CUSTOMER_LOGIN => array('login', 128), + TheliaEvents::CUSTOMER_DELETEACCOUNT => array('delete', 128), + TheliaEvents::CUSTOMER_ADDRESS_DELETE => array('deleteAddress', 128) ); } } diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 509162cbf..3caa96e36 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -121,6 +121,10 @@ Thelia\Controller\Admin\CustomerController::deleteAction + + Thelia\Controller\Admin\CustomerController::deleteAddressAction + + diff --git a/core/lib/Thelia/Controller/Admin/BaseAdminController.php b/core/lib/Thelia/Controller/Admin/BaseAdminController.php index 2401617f3..61fc80940 100755 --- a/core/lib/Thelia/Controller/Admin/BaseAdminController.php +++ b/core/lib/Thelia/Controller/Admin/BaseAdminController.php @@ -51,7 +51,7 @@ class BaseAdminController extends BaseController /** * Helper to append a message to the admin log. * - * @param unknown $message + * @param string $message */ public function adminLogAppend($message) { diff --git a/core/lib/Thelia/Controller/Admin/CustomerController.php b/core/lib/Thelia/Controller/Admin/CustomerController.php index f559a5bb0..0c320f0d4 100644 --- a/core/lib/Thelia/Controller/Admin/CustomerController.php +++ b/core/lib/Thelia/Controller/Admin/CustomerController.php @@ -22,13 +22,16 @@ /*************************************************************************************/ namespace Thelia\Controller\Admin; + use Propel\Runtime\Exception\PropelException; use Symfony\Component\Form\Form; +use Thelia\Core\Event\Customer\CustomerAddressEvent; use Thelia\Core\Event\Customer\CustomerCreateOrUpdateEvent; use Thelia\Core\Event\Customer\CustomerEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Form\CustomerModification; use Thelia\Form\Exception\FormValidationException; +use Thelia\Model\AddressQuery; use Thelia\Model\CustomerQuery; use Thelia\Core\Translation\Translator; @@ -53,6 +56,33 @@ class CustomerController extends BaseAdminController )); } + public function deleteAddressAction() + { + if (null !== $response = $this->checkAuth("admin.customer.update")) return $response; + + $address_id = $this->getRequest()->request->get('address_id'); + + try { + $address = AddressQuery::create()->findPk($address_id); + + if (null === $address) { + throw new \InvalidArgumentException(sprintf('%d address does not exists', $address_id)); + } + + $addressEvent = new CustomerAddressEvent($address); + + $this->dispatch(TheliaEvents::CUSTOMER_ADDRESS_DELETE, $addressEvent); + + $this->adminLogAppend(sprintf("address %d for customer %d removal", $address_id, $address->getCustomerId())); + } catch(\Exception $e) { + \Thelia\Log\Tlog::getInstance()->error(sprintf("error during address removal with message %s", $e->getMessage())); + } + + return $this->render("customer-edit", array( + "customer_id" => $address->getCustomerId() + )); + } + /** * update customer action * diff --git a/core/lib/Thelia/Core/Event/Customer/CustomerAddressEvent.php b/core/lib/Thelia/Core/Event/Customer/CustomerAddressEvent.php new file mode 100644 index 000000000..6c4c82d3d --- /dev/null +++ b/core/lib/Thelia/Core/Event/Customer/CustomerAddressEvent.php @@ -0,0 +1,66 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event\Customer; + +use Thelia\Core\Event\ActionEvent; +use Thelia\Model\Address; + + +/** + * Class CustomerAddressEvent + * @package Thelia\Core\Event\Customer + * @author Manuel Raynaud + */ +class CustomerAddressEvent extends ActionEvent +{ + /** + * @var \Thelia\Model\Address + */ + protected $address; + + /** + * @param Address $address + */ + public function __construct(Address $address) + { + $this->address = $address; + } + + /** + * @param \Thelia\Model\Address $address + */ + public function setAddress($address) + { + $this->address = $address; + } + + /** + * @return \Thelia\Model\Address + */ + public function getAddress() + { + return $this->address; + } + +} \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 9ffe08bcb..4d724749a 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -76,6 +76,11 @@ final class TheliaEvents */ const CUSTOMER_DELETEACCOUNT = "action.deleteCustomer"; + /** + * sent on customer address removal + */ + const CUSTOMER_ADDRESS_DELETE = "action.customer.deleteAddress"; + /** * sent when a customer need a new password */ diff --git a/templates/admin/default/customer-edit.html b/templates/admin/default/customer-edit.html index 9bcba75b9..9c9a88486 100644 --- a/templates/admin/default/customer-edit.html +++ b/templates/admin/default/customer-edit.html @@ -179,7 +179,7 @@ - + @@ -342,21 +342,21 @@ {form_field form=$form field='label'}
- - + +
{/form_field} {form_field form=$form field='company'}
- - + +
{/form_field} {form_field form=$form field='title'}
- + + +
{/form_field} {form_field form=$form field='lastname'}
- - + +
{/form_field} {form_field form=$form field='address1'}
- - + +
{form_field form=$form field='address2'} - + {/form_field}
{form_field form=$form field='address3'} - + {/form_field}
{/form_field} {form_field form=$form field='zipcode'}
- - + +
{/form_field} {form_field form=$form field='city'}
- - + +
{/form_field} {form_field form=$form field='country'}
- +