From 9e0dedcb45e5d283e5fbb45d9486ecb3ed821fc9 Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Tue, 22 Apr 2014 12:12:53 +0200 Subject: [PATCH] Fixed module translations --- .../Front/Controller/AddressController.php | 453 +++++----- .../Front/Controller/CustomerController.php | 783 +++++++++--------- .../Front/Controller/OrderController.php | 8 +- local/modules/Front/Front.php | 6 +- 4 files changed, 624 insertions(+), 626 deletions(-) diff --git a/local/modules/Front/Controller/AddressController.php b/local/modules/Front/Controller/AddressController.php index 1ab3e324f..4b2de4568 100644 --- a/local/modules/Front/Controller/AddressController.php +++ b/local/modules/Front/Controller/AddressController.php @@ -1,226 +1,227 @@ -. */ -/* */ -/*************************************************************************************/ - -namespace Front\Controller; -use Thelia\Controller\Front\BaseFrontController; -use Thelia\Core\Event\Address\AddressCreateOrUpdateEvent; -use Thelia\Core\Event\Address\AddressEvent; -use Thelia\Core\Event\TheliaEvents; -use Thelia\Core\Translation\Translator; -use Thelia\Form\AddressCreateForm; -use Thelia\Form\AddressUpdateForm; -use Thelia\Form\Exception\FormValidationException; -use Thelia\Model\AddressQuery; -use Thelia\Model\Customer; - -/** - * Class AddressController - * @package Thelia\Controller\Front - * @author Manuel Raynaud - */ -class AddressController extends BaseFrontController -{ - - /** - * Controller for generate modal containing update form - * Check if request is a XmlHttpRequest and address owner is the current customer - * @param $address_id - */ - public function generateModalAction($address_id) - { - - $this->checkAuth(); - $this->checkXmlHttpRequest(); - - } - - /** - * Create controller. - * Check if customer is logged in - * - * Dispatch TheliaEvents::ADDRESS_CREATE event - */ - public function createAction() - { - - $this->checkAuth(); - - $addressCreate = new AddressCreateForm($this->getRequest()); - - try { - $customer = $this->getSecurityContext()->getCustomerUser(); - - $form = $this->validateForm($addressCreate, "post"); - $event = $this->createAddressEvent($form); - $event->setCustomer($customer); - - $this->dispatch(TheliaEvents::ADDRESS_CREATE, $event); - $this->redirectSuccess($addressCreate); - - } catch (FormValidationException $e) { - $message = sprintf(Translator::getInstance()->trans("Please check your input: %s"), $e->getMessage()); - } catch (\Exception $e) { - $message = sprintf(Translator::getInstance()->trans("Sorry, an error occured: %s"), $e->getMessage()); - } - - if ($message !== false) { - \Thelia\Log\Tlog::getInstance()->error(sprintf("Error during address creation process : %s", $message)); - - $addressCreate->setErrorMessage($message); - - $this->getParserContext() - ->addForm($addressCreate) - ->setGeneralError($message) - ; - } - } - - public function updateViewAction($address_id) - { - $this->checkAuth(); - - $customer = $this->getSecurityContext()->getCustomerUser(); - $address = AddressQuery::create()->findPk($address_id); - - if (!$address || $customer->getId() != $address->getCustomerId()) { - $this->redirectToRoute('default'); - } - - $this->getParserContext()->set("address_id", $address_id); - } - - public function processUpdateAction($address_id) - { - $this->checkAuth(); - $request = $this->getRequest(); - - $addressUpdate = new AddressUpdateForm($request); - - try { - $customer = $this->getSecurityContext()->getCustomerUser(); - - $form = $this->validateForm($addressUpdate); - - $address = AddressQuery::create()->findPk($address_id); - - if (null === $address) { - $this->redirectToRoute('default'); - } - - if ($address->getCustomer()->getId() != $customer->getId()) { - $this->redirectToRoute('default'); - } - - $event = $this->createAddressEvent($form); - $event->setAddress($address); - - $this->dispatch(TheliaEvents::ADDRESS_UPDATE, $event); - - $this->redirectSuccess($addressUpdate); - } catch (FormValidationException $e) { - $message = sprintf(Translator::getInstance()->trans("Please check your input: %s"), $e->getMessage()); - } catch (\Exception $e) { - $message = sprintf(Translator::getInstance()->trans("Sorry, an error occured: %s"), $e->getMessage()); - } - $this->getParserContext()->set("address_id", $address_id); - if ($message !== false) { - \Thelia\Log\Tlog::getInstance()->error(sprintf("Error during address creation process : %s", $message)); - - $addressUpdate->setErrorMessage($message); - - $this->getParserContext() - ->addForm($addressUpdate) - ->setGeneralError($message) - ; - } - } - - 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()) { - // 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'); - } - } - - try { - $this->dispatch(TheliaEvents::ADDRESS_DELETE, new AddressEvent($address)); - } catch (\Exception $e) { - $error_message = $e->getMessage(); - } - - \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) - { - return new AddressCreateOrUpdateEvent( - $form->get("label")->getData(), - $form->get("title")->getData(), - $form->get("firstname")->getData(), - $form->get("lastname")->getData(), - $form->get("address1")->getData(), - $form->get("address2")->getData(), - $form->get("address3")->getData(), - $form->get("zipcode")->getData(), - $form->get("city")->getData(), - $form->get("country")->getData(), - $form->get("cellphone")->getData(), - $form->get("phone")->getData(), - $form->get("company")->getData(), - $form->get("is_default")->getData() - ); - } -} +. */ +/* */ +/*************************************************************************************/ + +namespace Front\Controller; +use Front\Front; +use Thelia\Controller\Front\BaseFrontController; +use Thelia\Core\Event\Address\AddressCreateOrUpdateEvent; +use Thelia\Core\Event\Address\AddressEvent; +use Thelia\Core\Event\TheliaEvents; +use Thelia\Core\Translation\Translator; +use Thelia\Form\AddressCreateForm; +use Thelia\Form\AddressUpdateForm; +use Thelia\Form\Exception\FormValidationException; +use Thelia\Model\AddressQuery; +use Thelia\Model\Customer; + +/** + * Class AddressController + * @package Thelia\Controller\Front + * @author Manuel Raynaud + */ +class AddressController extends BaseFrontController +{ + + /** + * Controller for generate modal containing update form + * Check if request is a XmlHttpRequest and address owner is the current customer + * @param $address_id + */ + public function generateModalAction($address_id) + { + + $this->checkAuth(); + $this->checkXmlHttpRequest(); + + } + + /** + * Create controller. + * Check if customer is logged in + * + * Dispatch TheliaEvents::ADDRESS_CREATE event + */ + public function createAction() + { + + $this->checkAuth(); + + $addressCreate = new AddressCreateForm($this->getRequest()); + + try { + $customer = $this->getSecurityContext()->getCustomerUser(); + + $form = $this->validateForm($addressCreate, "post"); + $event = $this->createAddressEvent($form); + $event->setCustomer($customer); + + $this->dispatch(TheliaEvents::ADDRESS_CREATE, $event); + $this->redirectSuccess($addressCreate); + + } catch (FormValidationException $e) { + $message = Translator::getInstance()->trans("Please check your input: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); + } catch (\Exception $e) { + $message = Translator::getInstance()->trans("Sorry, an error occured: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); + } + + if ($message !== false) { + \Thelia\Log\Tlog::getInstance()->error(sprintf("Error during address creation process : %s", $message)); + + $addressCreate->setErrorMessage($message); + + $this->getParserContext() + ->addForm($addressCreate) + ->setGeneralError($message) + ; + } + } + + public function updateViewAction($address_id) + { + $this->checkAuth(); + + $customer = $this->getSecurityContext()->getCustomerUser(); + $address = AddressQuery::create()->findPk($address_id); + + if (!$address || $customer->getId() != $address->getCustomerId()) { + $this->redirectToRoute('default'); + } + + $this->getParserContext()->set("address_id", $address_id); + } + + public function processUpdateAction($address_id) + { + $this->checkAuth(); + $request = $this->getRequest(); + + $addressUpdate = new AddressUpdateForm($request); + + try { + $customer = $this->getSecurityContext()->getCustomerUser(); + + $form = $this->validateForm($addressUpdate); + + $address = AddressQuery::create()->findPk($address_id); + + if (null === $address) { + $this->redirectToRoute('default'); + } + + if ($address->getCustomer()->getId() != $customer->getId()) { + $this->redirectToRoute('default'); + } + + $event = $this->createAddressEvent($form); + $event->setAddress($address); + + $this->dispatch(TheliaEvents::ADDRESS_UPDATE, $event); + + $this->redirectSuccess($addressUpdate); + } catch (FormValidationException $e) { + $message = Translator::getInstance()->trans("Please check your input: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); + } catch (\Exception $e) { + $message = Translator::getInstance()->trans("Sorry, an error occured: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); + } + $this->getParserContext()->set("address_id", $address_id); + if ($message !== false) { + \Thelia\Log\Tlog::getInstance()->error(sprintf("Error during address creation process : %s", $message)); + + $addressUpdate->setErrorMessage($message); + + $this->getParserContext() + ->addForm($addressUpdate) + ->setGeneralError($message) + ; + } + } + + 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()) { + // 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'); + } + } + + try { + $this->dispatch(TheliaEvents::ADDRESS_DELETE, new AddressEvent($address)); + } catch (\Exception $e) { + $error_message = $e->getMessage(); + } + + \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) + { + return new AddressCreateOrUpdateEvent( + $form->get("label")->getData(), + $form->get("title")->getData(), + $form->get("firstname")->getData(), + $form->get("lastname")->getData(), + $form->get("address1")->getData(), + $form->get("address2")->getData(), + $form->get("address3")->getData(), + $form->get("zipcode")->getData(), + $form->get("city")->getData(), + $form->get("country")->getData(), + $form->get("cellphone")->getData(), + $form->get("phone")->getData(), + $form->get("company")->getData(), + $form->get("is_default")->getData() + ); + } +} diff --git a/local/modules/Front/Controller/CustomerController.php b/local/modules/Front/Controller/CustomerController.php index ec8c83c82..150711749 100644 --- a/local/modules/Front/Controller/CustomerController.php +++ b/local/modules/Front/Controller/CustomerController.php @@ -1,391 +1,392 @@ -. */ -/* */ -/*************************************************************************************/ -namespace Front\Controller; - -use Thelia\Controller\Front\BaseFrontController; -use Thelia\Core\Event\Customer\CustomerCreateOrUpdateEvent; -use Thelia\Core\Event\Customer\CustomerLoginEvent; -use Thelia\Core\Event\LostPasswordEvent; -use Thelia\Core\Event\Newsletter\NewsletterEvent; -use Thelia\Core\Security\Authentication\CustomerUsernamePasswordFormAuthenticator; -use Thelia\Core\Security\Exception\AuthenticationException; -use Thelia\Core\Security\Exception\UsernameNotFoundException; -use Thelia\Core\Translation\Translator; -use Thelia\Form\CustomerCreateForm; -use Thelia\Form\CustomerLogin; -use Thelia\Form\CustomerLostPasswordForm; -use Thelia\Form\CustomerPasswordUpdateForm; -use Thelia\Form\CustomerProfileUpdateForm; -use Thelia\Form\Exception\FormValidationException; -use Thelia\Model\Customer; -use Thelia\Core\Event\TheliaEvents; -use Thelia\Model\NewsletterQuery; -use Thelia\Tools\URL; -use Thelia\Log\Tlog; -use Thelia\Core\Security\Exception\WrongPasswordException; - -/** - * Class CustomerController - * @package Thelia\Controller\Front - * @author Manuel Raynaud - */ -class CustomerController extends BaseFrontController -{ - use \Thelia\Cart\CartTrait; - - public function newPasswordAction() - { - if (! $this->getSecurityContext()->hasCustomerUser()) { - $message = false; - - $passwordLost = new CustomerLostPasswordForm($this->getRequest()); - - try { - - $form = $this->validateForm($passwordLost); - - $event = new LostPasswordEvent($form->get("email")->getData()); - - $this->dispatch(TheliaEvents::LOST_PASSWORD, $event); - - } catch (FormValidationException $e) { - $message = sprintf(Translator::getInstance()->trans("Please check your input: %s"), $e->getMessage()); - } catch (\Exception $e) { - $message = sprintf(Translator::getInstance()->trans("Sorry, an error occured: %s"), $e->getMessage()); - } - - if ($message !== false) { - Tlog::getInstance()->error(sprintf("Error during customer creation process : %s. Exception was %s", $message, $e->getMessage())); - - $passwordLost->setErrorMessage($message); - - $this->getParserContext() - ->addForm($passwordLost) - ->setGeneralError($message) - ; - } - } - } - - /** - * Create a new customer. - * On success, redirect to success_url if exists, otherwise, display the same view again. - */ - public function createAction() - { - if (! $this->getSecurityContext()->hasCustomerUser()) { - - $message = false; - - $customerCreation = new CustomerCreateForm($this->getRequest()); - - try { - $form = $this->validateForm($customerCreation, "post"); - - $customerCreateEvent = $this->createEventInstance($form->getData()); - - $this->dispatch(TheliaEvents::CUSTOMER_CREATEACCOUNT, $customerCreateEvent); - - $newCustomer = $customerCreateEvent->getCustomer(); - - // Newsletter - if (true === $form->get('newsletter')->getData()) { - $newsletterEmail = $newCustomer->getEmail(); - $nlEvent = new NewsletterEvent($newsletterEmail, $this->getRequest()->getSession()->getLang()->getLocale()); - $nlEvent->setFirstname($newCustomer->getFirstname()); - $nlEvent->setLastname($newCustomer->getLastname()); - - // Security : Check if this new Email address already exist - if (null !== $newsletter = NewsletterQuery::create()->findOneByEmail($newsletterEmail)) { - $nlEvent->setId($newsletter->getId()); - $this->dispatch(TheliaEvents::NEWSLETTER_UPDATE, $nlEvent); - } else { - $this->dispatch(TheliaEvents::NEWSLETTER_SUBSCRIBE, $nlEvent); - } - } - - $this->processLogin($customerCreateEvent->getCustomer()); - - $cart = $this->getCart($this->getDispatcher(), $this->getRequest()); - if ($cart->getCartItems()->count() > 0) { - $this->redirectToRoute('cart.view'); - } else { - $this->redirectSuccess($customerCreation); - } - } catch (FormValidationException $e) { - $message = sprintf(Translator::getInstance()->trans("Please check your input: %s"), $e->getMessage()); - } catch (\Exception $e) { - $message = sprintf(Translator::getInstance()->trans("Sorry, an error occured: %s"), $e->getMessage()); - } - - if ($message !== false) { - Tlog::getInstance()->error(sprintf("Error during customer creation process : %s. Exception was %s", $message, $e->getMessage())); - - $customerCreation->setErrorMessage($message); - - $this->getParserContext() - ->addForm($customerCreation) - ->setGeneralError($message) - ; - } - } - } - - /** - * Update customer data. On success, redirect to success_url if exists. - * Otherwise, display the same view again. - */ - public function viewAction() - { - $this->checkAuth(); - - $customer = $this->getSecurityContext()->getCustomerUser(); - $data = array( - 'id' => $customer->getId(), - 'title' => $customer->getTitleId(), - 'firstname' => $customer->getFirstName(), - 'lastname' => $customer->getLastName(), - 'email' => $customer->getEmail(), - 'newsletter' => null !== NewsletterQuery::create()->findOneByEmail($customer->getEmail()), - ); - - $customerProfileUpdateForm = new CustomerProfileUpdateForm($this->getRequest(), 'form', $data); - - // Pass it to the parser - $this->getParserContext()->addForm($customerProfileUpdateForm); - } - - public function updatePasswordAction() - { - if ($this->getSecurityContext()->hasCustomerUser()) { - $message = false; - - $customerPasswordUpdateForm = new CustomerPasswordUpdateForm($this->getRequest()); - - try { - $customer = $this->getSecurityContext()->getCustomerUser(); - - $form = $this->validateForm($customerPasswordUpdateForm, "post"); - - $customerChangeEvent = $this->createEventInstance($form->getData()); - $customerChangeEvent->setCustomer($customer); - $this->dispatch(TheliaEvents::CUSTOMER_UPDATEPROFILE, $customerChangeEvent); - - $this->redirectSuccess($customerPasswordUpdateForm); - - } catch (FormValidationException $e) { - $message = sprintf(Translator::getInstance()->trans("Please check your input: %s"), $e->getMessage()); - } catch (\Exception $e) { - $message = sprintf(Translator::getInstance()->trans("Sorry, an error occured: %s"), $e->getMessage()); - } - - if ($message !== false) { - Tlog::getInstance()->error(sprintf("Error during customer password modification process : %s.", $message)); - - $customerPasswordUpdateForm->setErrorMessage($message); - - $this->getParserContext() - ->addForm($customerPasswordUpdateForm) - ->setGeneralError($message) - ; - } - } - } - - public function updateAction() - { - if ($this->getSecurityContext()->hasCustomerUser()) { - - $message = false; - - $customerProfileUpdateForm = new CustomerProfileUpdateForm($this->getRequest()); - - try { - $customer = $this->getSecurityContext()->getCustomerUser(); - $newsletterOldEmail = $customer->getEmail(); - - $form = $this->validateForm($customerProfileUpdateForm, "post"); - - $customerChangeEvent = $this->createEventInstance($form->getData()); - $customerChangeEvent->setCustomer($customer); - $this->dispatch(TheliaEvents::CUSTOMER_UPDATEPROFILE, $customerChangeEvent); - - $updatedCustomer = $customerChangeEvent->getCustomer(); - - // Newsletter - if (true === $form->get('newsletter')->getData()) { - $nlEvent = new NewsletterEvent($updatedCustomer->getEmail(), $this->getRequest()->getSession()->getLang()->getLocale()); - $nlEvent->setFirstname($updatedCustomer->getFirstname()); - $nlEvent->setLastname($updatedCustomer->getLastname()); - - if (null !== $newsletter = NewsletterQuery::create()->findOneByEmail($newsletterOldEmail)) { - $nlEvent->setId($newsletter->getId()); - $this->dispatch(TheliaEvents::NEWSLETTER_UPDATE, $nlEvent); - } else { - $this->dispatch(TheliaEvents::NEWSLETTER_SUBSCRIBE, $nlEvent); - } - } else { - if (null !== $newsletter = NewsletterQuery::create()->findOneByEmail($newsletterOldEmail)) { - $nlEvent = new NewsletterEvent($updatedCustomer->getEmail(), $this->getRequest()->getSession()->getLang()->getLocale()); - $nlEvent->setId($newsletter->getId()); - $this->dispatch(TheliaEvents::NEWSLETTER_UNSUBSCRIBE, $nlEvent); - } - } - - $this->processLogin($updatedCustomer); - - $this->redirectSuccess($customerProfileUpdateForm); - - } catch (FormValidationException $e) { - $message = sprintf(Translator::getInstance()->trans("Please check your input: %s"), $e->getMessage()); - } catch (\Exception $e) { - $message = sprintf(Translator::getInstance()->trans("Sorry, an error occured: %s"), $e->getMessage()); - } - - if ($message !== false) { - Tlog::getInstance()->error(sprintf("Error during customer modification process : %s.", $message)); - - $customerProfileUpdateForm->setErrorMessage($message); - - $this->getParserContext() - ->addForm($customerProfileUpdateForm) - ->setGeneralError($message) - ; - } - } - } - - /** - * Perform user login. On a successful login, the user is redirected to the URL - * found in the success_url form parameter, or / if none was found. - * - * If login is not successfull, the same view is displayed again. - * - */ - public function loginAction() - { - if (! $this->getSecurityContext()->hasCustomerUser()) { - $message = false; - - $request = $this->getRequest(); - $customerLoginForm = new CustomerLogin($request); - - try { - - $form = $this->validateForm($customerLoginForm, "post"); - - // If User is a new customer - if ($form->get('account')->getData() == 0 && !$form->get("email")->getErrors()) { - $this->redirectToRoute("customer.create.process", array("email" => $form->get("email")->getData())); - } else { - - try { - - $authenticator = new CustomerUsernamePasswordFormAuthenticator($request, $customerLoginForm); - - $customer = $authenticator->getAuthentifiedUser(); - - $this->processLogin($customer); - - $this->redirectSuccess($customerLoginForm); - - } catch (UsernameNotFoundException $e) { - $message = "Wrong email or password. Please try again"; - } catch (WrongPasswordException $e) { - $message = "Wrong email or password. Please try again"; - } catch (AuthenticationException $e) { - $message = "Wrong email or password. Please try again"; - } - - } - - } catch (FormValidationException $e) { - $message = sprintf(Translator::getInstance()->trans("Please check your input: %s"), $e->getMessage()); - } catch (\Exception $e) { - $message = sprintf(Translator::getInstance()->trans("Sorry, an error occured: %s"), $e->getMessage()); - } - - if ($message !== false) { - Tlog::getInstance()->error(sprintf("Error during customer login process : %s. Exception was %s", $message, $e->getMessage())); - - $customerLoginForm->setErrorMessage($message); - - $this->getParserContext()->addForm($customerLoginForm); - } - } - } - - /** - * Perform customer logout. - */ - public function logoutAction() - { - if ($this->getSecurityContext()->hasCustomerUser()) { - $this->dispatch(TheliaEvents::CUSTOMER_LOGOUT); - } - - // Redirect to home page - $this->redirect(URL::getInstance()->getIndexPage()); - } - - /** - * Dispatch event for customer login action - * - * @param Customer $customer - */ - protected function processLogin(Customer $customer) - { - $this->dispatch(TheliaEvents::CUSTOMER_LOGIN, new CustomerLoginEvent($customer)); - } - - /** - * @param $data - * @return \Thelia\Core\Event\Customer\CustomerCreateOrUpdateEvent - */ - private function createEventInstance($data) - { - $customerCreateEvent = new CustomerCreateOrUpdateEvent( - isset($data["title"])?$data["title"]:null, - isset($data["firstname"])?$data["firstname"]:null, - isset($data["lastname"])?$data["lastname"]:null, - isset($data["address1"])?$data["address1"]:null, - isset($data["address2"])?$data["address2"]:null, - isset($data["address3"])?$data["address3"]:null, - isset($data["phone"])?$data["phone"]:null, - isset($data["cellphone"])?$data["cellphone"]:null, - isset($data["zipcode"])?$data["zipcode"]:null, - isset($data["city"])?$data["city"]:null, - isset($data["country"])?$data["country"]:null, - isset($data["email"])?$data["email"]:null, - isset($data["password"]) ? $data["password"]:null, - $this->getRequest()->getSession()->getLang()->getId(), - isset($data["reseller"])?$data["reseller"]:null, - isset($data["sponsor"])?$data["sponsor"]:null, - isset($data["discount"])?$data["discount"]:null, - isset($data["company"])?$data["company"]:null, - null - ); - - return $customerCreateEvent; - } -} +. */ +/* */ +/*************************************************************************************/ +namespace Front\Controller; + +use Front\Front; +use Thelia\Controller\Front\BaseFrontController; +use Thelia\Core\Event\Customer\CustomerCreateOrUpdateEvent; +use Thelia\Core\Event\Customer\CustomerLoginEvent; +use Thelia\Core\Event\LostPasswordEvent; +use Thelia\Core\Event\Newsletter\NewsletterEvent; +use Thelia\Core\Security\Authentication\CustomerUsernamePasswordFormAuthenticator; +use Thelia\Core\Security\Exception\AuthenticationException; +use Thelia\Core\Security\Exception\UsernameNotFoundException; +use Thelia\Core\Translation\Translator; +use Thelia\Form\CustomerCreateForm; +use Thelia\Form\CustomerLogin; +use Thelia\Form\CustomerLostPasswordForm; +use Thelia\Form\CustomerPasswordUpdateForm; +use Thelia\Form\CustomerProfileUpdateForm; +use Thelia\Form\Exception\FormValidationException; +use Thelia\Model\Customer; +use Thelia\Core\Event\TheliaEvents; +use Thelia\Model\NewsletterQuery; +use Thelia\Tools\URL; +use Thelia\Log\Tlog; +use Thelia\Core\Security\Exception\WrongPasswordException; + +/** + * Class CustomerController + * @package Thelia\Controller\Front + * @author Manuel Raynaud + */ +class CustomerController extends BaseFrontController +{ + use \Thelia\Cart\CartTrait; + + public function newPasswordAction() + { + if (! $this->getSecurityContext()->hasCustomerUser()) { + $message = false; + + $passwordLost = new CustomerLostPasswordForm($this->getRequest()); + + try { + + $form = $this->validateForm($passwordLost); + + $event = new LostPasswordEvent($form->get("email")->getData()); + + $this->dispatch(TheliaEvents::LOST_PASSWORD, $event); + + } catch (FormValidationException $e) { + $message = Translator::getInstance()->trans("Please check your input: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); + } catch (\Exception $e) { + $message = Translator::getInstance()->trans("Sorry, an error occured: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); + } + + if ($message !== false) { + Tlog::getInstance()->error(sprintf("Error during customer creation process : %s. Exception was %s", $message, $e->getMessage())); + + $passwordLost->setErrorMessage($message); + + $this->getParserContext() + ->addForm($passwordLost) + ->setGeneralError($message) + ; + } + } + } + + /** + * Create a new customer. + * On success, redirect to success_url if exists, otherwise, display the same view again. + */ + public function createAction() + { + if (! $this->getSecurityContext()->hasCustomerUser()) { + + $message = false; + + $customerCreation = new CustomerCreateForm($this->getRequest()); + + try { + $form = $this->validateForm($customerCreation, "post"); + + $customerCreateEvent = $this->createEventInstance($form->getData()); + + $this->dispatch(TheliaEvents::CUSTOMER_CREATEACCOUNT, $customerCreateEvent); + + $newCustomer = $customerCreateEvent->getCustomer(); + + // Newsletter + if (true === $form->get('newsletter')->getData()) { + $newsletterEmail = $newCustomer->getEmail(); + $nlEvent = new NewsletterEvent($newsletterEmail, $this->getRequest()->getSession()->getLang()->getLocale()); + $nlEvent->setFirstname($newCustomer->getFirstname()); + $nlEvent->setLastname($newCustomer->getLastname()); + + // Security : Check if this new Email address already exist + if (null !== $newsletter = NewsletterQuery::create()->findOneByEmail($newsletterEmail)) { + $nlEvent->setId($newsletter->getId()); + $this->dispatch(TheliaEvents::NEWSLETTER_UPDATE, $nlEvent); + } else { + $this->dispatch(TheliaEvents::NEWSLETTER_SUBSCRIBE, $nlEvent); + } + } + + $this->processLogin($customerCreateEvent->getCustomer()); + + $cart = $this->getCart($this->getDispatcher(), $this->getRequest()); + if ($cart->getCartItems()->count() > 0) { + $this->redirectToRoute('cart.view'); + } else { + $this->redirectSuccess($customerCreation); + } + } catch (FormValidationException $e) { + $message = Translator::getInstance()->trans("Please check your input: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); + } catch (\Exception $e) { + $message = Translator::getInstance()->trans("Sorry, an error occured: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); + } + + if ($message !== false) { + Tlog::getInstance()->error(sprintf("Error during customer creation process : %s. Exception was %s", $message, $e->getMessage())); + + $customerCreation->setErrorMessage($message); + + $this->getParserContext() + ->addForm($customerCreation) + ->setGeneralError($message) + ; + } + } + } + + /** + * Update customer data. On success, redirect to success_url if exists. + * Otherwise, display the same view again. + */ + public function viewAction() + { + $this->checkAuth(); + + $customer = $this->getSecurityContext()->getCustomerUser(); + $data = array( + 'id' => $customer->getId(), + 'title' => $customer->getTitleId(), + 'firstname' => $customer->getFirstName(), + 'lastname' => $customer->getLastName(), + 'email' => $customer->getEmail(), + 'newsletter' => null !== NewsletterQuery::create()->findOneByEmail($customer->getEmail()), + ); + + $customerProfileUpdateForm = new CustomerProfileUpdateForm($this->getRequest(), 'form', $data); + + // Pass it to the parser + $this->getParserContext()->addForm($customerProfileUpdateForm); + } + + public function updatePasswordAction() + { + if ($this->getSecurityContext()->hasCustomerUser()) { + $message = false; + + $customerPasswordUpdateForm = new CustomerPasswordUpdateForm($this->getRequest()); + + try { + $customer = $this->getSecurityContext()->getCustomerUser(); + + $form = $this->validateForm($customerPasswordUpdateForm, "post"); + + $customerChangeEvent = $this->createEventInstance($form->getData()); + $customerChangeEvent->setCustomer($customer); + $this->dispatch(TheliaEvents::CUSTOMER_UPDATEPROFILE, $customerChangeEvent); + + $this->redirectSuccess($customerPasswordUpdateForm); + + } catch (FormValidationException $e) { + $message = Translator::getInstance()->trans("Please check your input: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); + } catch (\Exception $e) { + $message = Translator::getInstance()->trans("Sorry, an error occured: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); + } + + if ($message !== false) { + Tlog::getInstance()->error(sprintf("Error during customer password modification process : %s.", $message)); + + $customerPasswordUpdateForm->setErrorMessage($message); + + $this->getParserContext() + ->addForm($customerPasswordUpdateForm) + ->setGeneralError($message) + ; + } + } + } + + public function updateAction() + { + if ($this->getSecurityContext()->hasCustomerUser()) { + + $message = false; + + $customerProfileUpdateForm = new CustomerProfileUpdateForm($this->getRequest()); + + try { + $customer = $this->getSecurityContext()->getCustomerUser(); + $newsletterOldEmail = $customer->getEmail(); + + $form = $this->validateForm($customerProfileUpdateForm, "post"); + + $customerChangeEvent = $this->createEventInstance($form->getData()); + $customerChangeEvent->setCustomer($customer); + $this->dispatch(TheliaEvents::CUSTOMER_UPDATEPROFILE, $customerChangeEvent); + + $updatedCustomer = $customerChangeEvent->getCustomer(); + + // Newsletter + if (true === $form->get('newsletter')->getData()) { + $nlEvent = new NewsletterEvent($updatedCustomer->getEmail(), $this->getRequest()->getSession()->getLang()->getLocale()); + $nlEvent->setFirstname($updatedCustomer->getFirstname()); + $nlEvent->setLastname($updatedCustomer->getLastname()); + + if (null !== $newsletter = NewsletterQuery::create()->findOneByEmail($newsletterOldEmail)) { + $nlEvent->setId($newsletter->getId()); + $this->dispatch(TheliaEvents::NEWSLETTER_UPDATE, $nlEvent); + } else { + $this->dispatch(TheliaEvents::NEWSLETTER_SUBSCRIBE, $nlEvent); + } + } else { + if (null !== $newsletter = NewsletterQuery::create()->findOneByEmail($newsletterOldEmail)) { + $nlEvent = new NewsletterEvent($updatedCustomer->getEmail(), $this->getRequest()->getSession()->getLang()->getLocale()); + $nlEvent->setId($newsletter->getId()); + $this->dispatch(TheliaEvents::NEWSLETTER_UNSUBSCRIBE, $nlEvent); + } + } + + $this->processLogin($updatedCustomer); + + $this->redirectSuccess($customerProfileUpdateForm); + + } catch (FormValidationException $e) { + $message = Translator::getInstance()->trans("Please check your input: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); + } catch (\Exception $e) { + $message = Translator::getInstance()->trans("Sorry, an error occured: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); + } + + if ($message !== false) { + Tlog::getInstance()->error(sprintf("Error during customer modification process : %s.", $message)); + + $customerProfileUpdateForm->setErrorMessage($message); + + $this->getParserContext() + ->addForm($customerProfileUpdateForm) + ->setGeneralError($message) + ; + } + } + } + + /** + * Perform user login. On a successful login, the user is redirected to the URL + * found in the success_url form parameter, or / if none was found. + * + * If login is not successfull, the same view is displayed again. + * + */ + public function loginAction() + { + if (! $this->getSecurityContext()->hasCustomerUser()) { + $message = false; + + $request = $this->getRequest(); + $customerLoginForm = new CustomerLogin($request); + + try { + + $form = $this->validateForm($customerLoginForm, "post"); + + // If User is a new customer + if ($form->get('account')->getData() == 0 && !$form->get("email")->getErrors()) { + $this->redirectToRoute("customer.create.process", array("email" => $form->get("email")->getData())); + } else { + + try { + + $authenticator = new CustomerUsernamePasswordFormAuthenticator($request, $customerLoginForm); + + $customer = $authenticator->getAuthentifiedUser(); + + $this->processLogin($customer); + + $this->redirectSuccess($customerLoginForm); + + } catch (UsernameNotFoundException $e) { + $message = "Wrong email or password. Please try again"; + } catch (WrongPasswordException $e) { + $message = "Wrong email or password. Please try again"; + } catch (AuthenticationException $e) { + $message = "Wrong email or password. Please try again"; + } + + } + + } catch (FormValidationException $e) { + $message = Translator::getInstance()->trans("Please check your input: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); + } catch (\Exception $e) { + $message = Translator::getInstance()->trans("Sorry, an error occured: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); + } + + if ($message !== false) { + Tlog::getInstance()->error(sprintf("Error during customer login process : %s. Exception was %s", $message, $e->getMessage())); + + $customerLoginForm->setErrorMessage($message); + + $this->getParserContext()->addForm($customerLoginForm); + } + } + } + + /** + * Perform customer logout. + */ + public function logoutAction() + { + if ($this->getSecurityContext()->hasCustomerUser()) { + $this->dispatch(TheliaEvents::CUSTOMER_LOGOUT); + } + + // Redirect to home page + $this->redirect(URL::getInstance()->getIndexPage()); + } + + /** + * Dispatch event for customer login action + * + * @param Customer $customer + */ + protected function processLogin(Customer $customer) + { + $this->dispatch(TheliaEvents::CUSTOMER_LOGIN, new CustomerLoginEvent($customer)); + } + + /** + * @param $data + * @return \Thelia\Core\Event\Customer\CustomerCreateOrUpdateEvent + */ + private function createEventInstance($data) + { + $customerCreateEvent = new CustomerCreateOrUpdateEvent( + isset($data["title"])?$data["title"]:null, + isset($data["firstname"])?$data["firstname"]:null, + isset($data["lastname"])?$data["lastname"]:null, + isset($data["address1"])?$data["address1"]:null, + isset($data["address2"])?$data["address2"]:null, + isset($data["address3"])?$data["address3"]:null, + isset($data["phone"])?$data["phone"]:null, + isset($data["cellphone"])?$data["cellphone"]:null, + isset($data["zipcode"])?$data["zipcode"]:null, + isset($data["city"])?$data["city"]:null, + isset($data["country"])?$data["country"]:null, + isset($data["email"])?$data["email"]:null, + isset($data["password"]) ? $data["password"]:null, + $this->getRequest()->getSession()->getLang()->getId(), + isset($data["reseller"])?$data["reseller"]:null, + isset($data["sponsor"])?$data["sponsor"]:null, + isset($data["discount"])?$data["discount"]:null, + isset($data["company"])?$data["company"]:null, + null + ); + + return $customerCreateEvent; + } +} diff --git a/local/modules/Front/Controller/OrderController.php b/local/modules/Front/Controller/OrderController.php index 0054fd6f4..5067af316 100644 --- a/local/modules/Front/Controller/OrderController.php +++ b/local/modules/Front/Controller/OrderController.php @@ -102,11 +102,11 @@ class OrderController extends BaseFrontController $this->redirectToRoute("order.invoice"); } catch (FormValidationException $e) { - $message = sprintf(Translator::getInstance()->trans("Please check your input: %s"), $e->getMessage()); + $message = Translator::getInstance()->trans("Please check your input: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); } catch (PropelException $e) { $this->getParserContext()->setGeneralError($e->getMessage()); } catch (\Exception $e) { - $message = sprintf(Translator::getInstance()->trans("Sorry, an error occured: %s"), $e->getMessage()); + $message = Translator::getInstance()->trans("Sorry, an error occured: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); } if ($message !== false) { @@ -158,11 +158,11 @@ class OrderController extends BaseFrontController $this->redirectToRoute("order.payment.process"); } catch (FormValidationException $e) { - $message = sprintf(Translator::getInstance()->trans("Please check your input: %s"), $e->getMessage()); + $message = Translator::getInstance()->trans("Please check your input: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); } catch (PropelException $e) { $this->getParserContext()->setGeneralError($e->getMessage()); } catch (\Exception $e) { - $message = sprintf(Translator::getInstance()->trans("Sorry, an error occured: %s"), $e->getMessage()); + $message = Translator::getInstance()->trans("Sorry, an error occured: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); } if ($message !== false) { diff --git a/local/modules/Front/Front.php b/local/modules/Front/Front.php index 67498ba24..c085f27fe 100644 --- a/local/modules/Front/Front.php +++ b/local/modules/Front/Front.php @@ -27,9 +27,5 @@ use Thelia\Module\BaseModule; class Front extends BaseModule { - /** - * YOU HAVE TO IMPLEMENT HERE ABSTRACT METHODD FROM BaseModule Class - * Like install and destroy - */ - + const MESSAGE_DOMAIN = 'front'; }