From 382fbe230e496defa11657c05133e82a0a4fcc61 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 16 Aug 2013 11:01:32 +0200 Subject: [PATCH] update customer process creation --- core/lib/Thelia/Action/Customer.php | 64 +++++--------- .../Controller/Front/CustomerController.php | 38 +++++++- .../Thelia/Core/Event/CustomerCreateEvent.php | 88 ++++++++++++++++++- .../Core/Event/Internal/CustomerEvent.php | 9 ++ core/lib/Thelia/Core/Event/TheliaEvents.php | 5 ++ core/lib/Thelia/Model/Customer.php | 1 + 6 files changed, 161 insertions(+), 44 deletions(-) diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index c9bce7bb0..8e323e3a8 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\CustomerCreateEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Form\CustomerCreation; use Thelia\Form\CustomerModification; @@ -37,54 +38,37 @@ use Symfony\Component\Validator\Exception\ValidatorException; use Thelia\Core\Security\Exception\AuthenticationException; use Thelia\Core\Security\Exception\UsernameNotFoundException; use Propel\Runtime\Exception\PropelException; -use Thelia\Action\Exception\FormValidationException; class Customer extends BaseAction implements EventSubscriberInterface { - public function create(ActionEvent $event) + public function create(CustomerCreateEvent $event) { - $request = $event->getRequest(); - try { - $customerCreationForm = new CustomerCreation($request); + $customer = new CustomerModel(); + $customer->setDispatcher($this->getDispatcher()); - $form = $this->validateForm($customerCreationForm, "POST"); + $customer->createOrUpdate( + $event->getTitle(), + $event->getFirstname(), + $event->getLastname(), + $event->getAddress1(), + $event->getAddress2(), + $event->getAddress3(), + $event->getPhone(), + $event->getCellphone(), + $event->getZipcode(), + $event->getCity(), + $event->getCountry(), + $event->getEmail(), + $event->getPassword(), + $event->getLang(), + $event->getReseller(), + $event->getSponsor(), + $event->getDiscount() + ); - $data = $form->getData(); - $customer = new CustomerModel(); - $customer->setDispatcher($event->getDispatcher()); - - $customer->createOrUpdate( - $data["title"], - $data["firstname"], - $data["lastname"], - $data["address1"], - $data["address2"], - $data["address3"], - $data["phone"], - $data["cellphone"], - $data["zipcode"], - $data["country"], - $data["email"], - $data["password"], - $request->getSession()->getLang() - ); - - $event->customer = $customer; - - } catch (PropelException $e) { - - Tlog::getInstance()->error(sprintf('error during creating customer on action/createCustomer with message "%s"', $e->getMessage())); - - $message = "Failed to create your account, please try again."; - } catch (FormValidationException $e) { - - $message = $e->getMessage(); - } - - // The form has errors, propagate it. - $this->propagateFormError($customerCreationForm, $message, $event); + $event->setCustomer($customer); } public function modify(ActionEvent $event) diff --git a/core/lib/Thelia/Controller/Front/CustomerController.php b/core/lib/Thelia/Controller/Front/CustomerController.php index 69c055959..7182fac01 100644 --- a/core/lib/Thelia/Controller/Front/CustomerController.php +++ b/core/lib/Thelia/Controller/Front/CustomerController.php @@ -22,9 +22,12 @@ /*************************************************************************************/ namespace Thelia\Controller\Front; +use Propel\Runtime\Exception\PropelException; +use Thelia\Core\Event\CustomerCreateEvent; use Thelia\Core\Event\CustomerEvent; use Thelia\Core\Security\SecurityContext; use Thelia\Form\CustomerCreation; +use Thelia\Form\Exception\FormValidationException; use Thelia\Model\Customer; use Thelia\Core\Event\TheliaEvents; @@ -34,8 +37,41 @@ class CustomerController extends BaseFrontController { $request = $this->getRequest(); $customerCreation = new CustomerCreation($$request); + try { + $form = $this->validateForm($customerCreation, "post"); + + $data = $form->getData(); + + $customerCreateEvent = new CustomerCreateEvent( + $data["title"], + $data["firstname"], + $data["lastname"], + $data["address1"], + $data["address2"], + $data["address3"], + $data["phone"], + $data["cellphone"], + $data["zipcode"], + $data["city"], + $data["country"], + $data["email"], + $data["password"], + $request->getSession()->getLang(), + $data["reseller"], + $data["sponsor"], + $data["discount"] + ); + + $this->getDispatcher()->dispatch(TheliaEvents::CUSTOMER_CREATEACCOUNT, $customerCreateEvent); + + $this->processLogin($customerCreateEvent->getCustomer()); + + } catch (FormValidationException $e) { + + } catch (PropelException $e) { + + } - $form = $this->validateForm($customerCreation, "post"); } diff --git a/core/lib/Thelia/Core/Event/CustomerCreateEvent.php b/core/lib/Thelia/Core/Event/CustomerCreateEvent.php index 45bf4d639..425df6fea 100644 --- a/core/lib/Thelia/Core/Event/CustomerCreateEvent.php +++ b/core/lib/Thelia/Core/Event/CustomerCreateEvent.php @@ -10,7 +10,12 @@ namespace Thelia\Core\Event; -class CustomerCreateEvent { +use Symfony\Component\EventDispatcher\Event; +use Thelia\Model\Customer; + +class CustomerCreateEvent extends Event { + + //base parameters for creating new customer protected $title; protected $firstname; protected $lastname; @@ -18,11 +23,22 @@ class CustomerCreateEvent { protected $address2; protected $address3; protected $phone; + protected $cellphone; protected $zipcode; + protected $city; protected $country; protected $email; protected $password; protected $lang; + protected $reseller; + protected $sponsor; + protected $discount; + + /** + * @var \Thelia\Model\Customer + */ + protected $customer; + /** * @param int $title the title customer id @@ -32,13 +48,18 @@ class CustomerCreateEvent { * @param string $address2 * @param string $address3 * @param string $phone + * @param string $cellphone * @param string $zipcode + * @param string $city * @param int $country the country id * @param string $email * @param string $password plain password, don't put hash password, it will hashes again * @param $lang + * @param int $reseller if customer is a reseller + * @param int $sponsor customer's id sponsor + * @param float $discount */ - function __construct($title, $firstname, $lastname, $address1, $address2, $address3, $phone, $zipcode, $country, $email, $password, $lang) + function __construct($title, $firstname, $lastname, $address1, $address2, $address3, $phone, $cellphone, $zipcode, $city, $country, $email, $password, $lang, $reseller, $sponsor, $discount) { $this->address1 = $address1; $this->address2 = $address2; @@ -50,8 +71,12 @@ class CustomerCreateEvent { $this->lastname = $lastname; $this->password = $password; $this->phone = $phone; + $this->cellphone = $cellphone; $this->title = $title; $this->zipcode = $zipcode; + $this->reseller = $reseller; + $this->sponsor = $sponsor; + $this->discount = $discount; } /** @@ -134,6 +159,14 @@ class CustomerCreateEvent { return $this->phone; } + /** + * @return string + */ + public function getCellphone() + { + return $this->cellphone; + } + /** * @return int */ @@ -150,6 +183,55 @@ class CustomerCreateEvent { return $this->zipcode; } - + /** + * @return string + */ + public function getCity() + { + return $this->city; + } + + /** + * @return float + */ + public function getDiscount() + { + return $this->discount; + } + + /** + * @return int + */ + public function getReseller() + { + return $this->reseller; + } + + /** + * @return int + */ + public function getSponsor() + { + return $this->sponsor; + } + + /** + * @param Customer $customer + */ + public function setCustomer(Customer $customer) + { + $this->customer = $customer; + } + + /** + * @return Customer + */ + public function getCustomer() + { + return $this->customer; + } + + + } \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/Internal/CustomerEvent.php b/core/lib/Thelia/Core/Event/Internal/CustomerEvent.php index ff73e038a..46c9278cb 100644 --- a/core/lib/Thelia/Core/Event/Internal/CustomerEvent.php +++ b/core/lib/Thelia/Core/Event/Internal/CustomerEvent.php @@ -34,4 +34,13 @@ class CustomerEvent extends InternalEvent $this->customer = $customer; } + /** + * @return \Thelia\Model\Customer + */ + public function getCustomer() + { + return $this->customer; + } + + } diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index f64a071c7..e9ef07b55 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -56,6 +56,11 @@ final class TheliaEvents */ const CUSTOMER_LOGIN = "action.customer_login"; + /** + * sent on customer account creation + */ + const CUSTOMER_CREATEACCOUNT = "action.createCustomer"; + /** * Sent before the logout of the administrator. */ diff --git a/core/lib/Thelia/Model/Customer.php b/core/lib/Thelia/Model/Customer.php index 3d289d98d..a1270a408 100755 --- a/core/lib/Thelia/Model/Customer.php +++ b/core/lib/Thelia/Model/Customer.php @@ -54,6 +54,7 @@ class Customer extends BaseCustomer implements UserInterface * @param int $reseller * @param null $sponsor * @param int $discount + * @throws \Exception|\Symfony\Component\Config\Definition\Exception\Exception */ public function createOrUpdate($titleId, $firstname, $lastname, $address1, $address2, $address3, $phone, $cellphone, $zipcode, $city, $countryId, $email, $plainPassword = null, $lang = null, $reseller = 0, $sponsor = null, $discount = 0) {