From 654220882a5e687e71314a7a17d4becad1f1e908 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 9 Aug 2013 10:19:36 +0200 Subject: [PATCH] move dispatch during customer creation process in post/preInsert methods --- core/lib/Thelia/Action/Category.php | 2 +- core/lib/Thelia/Action/Customer.php | 30 +++++++++++++++++++---------- core/lib/Thelia/Model/Customer.php | 18 +++++++++++++---- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/core/lib/Thelia/Action/Category.php b/core/lib/Thelia/Action/Category.php index 03b919850..e83fe5eaf 100644 --- a/core/lib/Thelia/Action/Category.php +++ b/core/lib/Thelia/Action/Category.php @@ -69,7 +69,7 @@ class Category extends BaseAction implements EventSubscriberInterface $successUrl = str_replace('_ID_', $category->getId(), $categoryCreationForm->getSuccessUrl()); // Redirect to the success URL - Redirect::exec($successUrl); + $this->redirect($successUrl); } catch (PropelException $e) { Tlog::getInstance()->error(sprintf('error during creating category with message "%s"', $e->getMessage())); diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index 4dbda1287..916e53a96 100755 --- a/core/lib/Thelia/Action/Customer.php +++ b/core/lib/Thelia/Action/Customer.php @@ -66,8 +66,7 @@ class Customer extends BaseAction implements EventSubscriberInterface $data = $form->getData(); $customer = new CustomerModel(); - - $event->getDispatcher()->dispatch(TheliaEvents::BEFORE_CREATECUSTOMER, $event); + $customer->setDispatcher($event->getDispatcher()); $customer->createOrUpdate( $data["title"], @@ -85,8 +84,7 @@ class Customer extends BaseAction implements EventSubscriberInterface $request->getSession()->getLang() ); - $customerEvent = new CustomerEvent($customer); - $event->getDispatcher()->dispatch(TheliaEvents::AFTER_CREATECUSTOMER, $customerEvent); + // Connect the newly created user,and redirect to the success URL $this->processSuccessfullLogin($event, $customer, $customerCreationForm, true); @@ -261,13 +259,25 @@ class Customer extends BaseAction implements EventSubscriberInterface */ protected function processSuccessfullLogin(ActionEvent $event, CustomerModel $user, BaseForm $form, $sendLoginEvent = false) { - // Success -> store user in security context - $this->getSecurityContext()->setUser($user); + $successUrl = $form->getSuccessUrl(); + if ($this->securityContext->getContext() === SecurityContext::CONTEXT_FRONT_OFFICE) { + $this->processSuccessfullFrontEndLogin($event, $user, $form, $sendLoginEvent); + } else { + $successUrl = str_replace("__ID__", $user->getId(), $successUrl); + } + + // Redirect to the success URL + $this->redirect($successUrl); + } + + protected function processSuccessfullFrontEndLogin(ActionEvent $event, CustomerModel $user, BaseForm $form, $sendLoginEvent = false) + { + // Success -> store user in security context + $this->getSecurityContext()->setUser($user); + + if ($sendLoginEvent) $event->getDispatcher()->dispatch(TheliaEvents::CUSTOMER_LOGIN, $event); - if ($sendLoginEvent) $event->getDispatcher()->dispatch(TheliaEvents::CUSTOMER_LOGIN, $event); - // Redirect to the success URL - $this->redirect($form->getSuccessUrl()); } /** @@ -276,7 +286,7 @@ class Customer extends BaseAction implements EventSubscriberInterface * @return SecurityContext the security context */ protected function getSecurityContext() { - $this->securityContext->setContext(SecurityContext::CONTEXT_FRONT_OFFICE); + //$this->securityContext->setContext(SecurityContext::CONTEXT_FRONT_OFFICE); return $this->securityContext; } diff --git a/core/lib/Thelia/Model/Customer.php b/core/lib/Thelia/Model/Customer.php index 5d36b639a..d0a520d3c 100755 --- a/core/lib/Thelia/Model/Customer.php +++ b/core/lib/Thelia/Model/Customer.php @@ -3,6 +3,7 @@ namespace Thelia\Model; use Symfony\Component\Config\Definition\Exception\Exception; +use Thelia\Core\Event\CustomerEvent; use Thelia\Model\Base\Customer as BaseCustomer; use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; @@ -105,16 +106,25 @@ class Customer extends BaseCustomer implements UserInterface public function preInsert(ConnectionInterface $con = null) { - $customerRef = new CustomRefEvent($this); + $this->setRef($this->generateRef()); + $customerEvent = new CustomerEvent($this); + if (!is_null($this->dispatcher)) { - $this->dispatcher->dispatch(TheliaEvents::CREATECUSTOMER_CUSTOMREF, $customerRef); + $this->dispatcher->dispatch(TheliaEvents::BEFORE_CREATECUSTOMER, $customerEvent); } - $this->setRef($customerRef->hasRef()? $customerRef->getRef() : $this->generateRef()); - return true; } + public function postInsert(ConnectionInterface $con = null) + { + $customerEvent = new CustomerEvent($this); + if (!is_null($this->dispatcher)) { + $this->dispatcher->dispatch(TheliaEvents::AFTER_CREATECUSTOMER, $customerEvent); + } + + } + protected function generateRef() { return uniqid(substr($this->getLastname(), 0, (strlen($this->getLastname()) >= 3) ? 3 : strlen($this->getLastname())), true);