public function subscribeAction() { $errorMessage = false; $newsletterForm = $this->createForm(FrontForm::NEWSLETTER); try { $form = $this->validateForm($newsletterForm); $event = new NewsletterEvent($form->get('email')->getData(), $this->getRequest()->getSession()->getLang()->getLocale()); /** @var Customer $customer */ if (null !== ($customer = $this->getSecurityContext()->getCustomerUser())) { $event->setFirstname($customer->getFirstname())->setLastname($customer->getLastname()); } else { $event->setFirstname($form->get('firstname')->getData())->setLastname($form->get('lastname')->getData()); } $this->dispatch(TheliaEvents::NEWSLETTER_SUBSCRIBE, $event); // If a success URL is defined in the form, redirect to it, otherwise use the defaut view if ($newsletterForm->hasSuccessUrl() && !$this->getRequest()->isXmlHttpRequest()) { return $this->generateSuccessRedirect($newsletterForm); } } catch (\Exception $e) { $errorMessage = $e->getMessage(); Tlog::getInstance()->error(sprintf('Error during newsletter subscription : %s', $errorMessage)); $newsletterForm->setErrorMessage($errorMessage); } // If Ajax Request if ($this->getRequest()->isXmlHttpRequest()) { return new JsonResponse(["success" => $errorMessage ? false : true, "message" => $errorMessage ? $errorMessage : $this->getTranslator()->trans("Thanks for signing up! We'll keep you posted whenever we have any new updates.", [], Front::MESSAGE_DOMAIN)], $errorMessage ? 500 : 200); } $this->getParserContext()->setGeneralError($errorMessage)->addForm($newsletterForm); // If an error URL is defined in the form, redirect to it, otherwise use the defaut view if ($errorMessage && $newsletterForm->hasErrorUrl()) { return $this->generateErrorRedirect($newsletterForm); } }
public function subscribeAction() { $error_message = false; $newsletterForm = new NewsletterForm($this->getRequest()); try { $form = $this->validateForm($newsletterForm); $event = new NewsletterEvent($form->get('email')->getData(), $this->getRequest()->getSession()->getLang()->getLocale()); if (null !== ($customer = $this->getSecurityContext()->getCustomerUser())) { $event->setFirstname($customer->getFirstname()); $event->setLastname($customer->getLastname()); } $this->dispatch(TheliaEvents::NEWSLETTER_SUBSCRIBE, $event); } catch (\Exception $e) { $error_message = $e->getMessage(); } \Thelia\Log\Tlog::getInstance()->error(sprintf('Error during newsletter subscription : %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" => "Thanks for signing up! We'll keep you posted whenever we have any new updates."))); } return $response; } else { $newsletterForm->setErrorMessage($error_message); $this->getParserContext()->addForm($newsletterForm)->setGeneralError($error_message); } }
/** * @depends testUpdate * @param NewsletterModel $newsletter */ public function testUnsubscribe(NewsletterModel $newsletter) { $event = new NewsletterEvent('*****@*****.**', 'en_US'); $event->setId($newsletter->getId()); $action = new Newsletter(); $action->unsubscribe($event); $deletedNewsletter = $event->getNewsletter(); $this->assertInstanceOf('Thelia\\Model\\Newsletter', $deletedNewsletter); $this->assertEquals(1, NewsletterQuery::create()->filterByEmail('*****@*****.**')->filterByUnsubscribed(true)->count()); }
/** * @depends testUpdate */ public function testUnsubscribe(NewsletterModel $newsletter) { $event = new NewsletterEvent('*****@*****.**', 'en_US'); $event->setId($newsletter->getId()); $action = new Newsletter(); $action->unsubscribe($event); $deletedNewsletter = $event->getNewsletter(); $this->assertInstanceOf('Thelia\\Model\\Newsletter', $deletedNewsletter); $this->assertTrue($deletedNewsletter->isDeleted()); }
public function update(NewsletterEvent $event) { if (null !== ($nl = NewsletterQuery::create()->findPk($event->getId()))) { $nl->setEmail($event->getEmail())->setFirstname($event->getFirstname())->setLastname($event->getLastname())->setLocale($event->getLocale())->save(); $event->setNewsletter($nl); } }
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); // We do not allow customer email modification $customerChangeEvent->setEmailUpdateAllowed(false); $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); return $this->generateSuccessRedirect($customerProfileUpdateForm); } catch (FormValidationException $e) { $message = $this->getTranslator()->trans("Please check your input: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); } catch (\Exception $e) { $message = $this->getTranslator()->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); } } }
/** * @since 2.3.0-alpha2 */ public function confirmSubscription(NewsletterEvent $event) { $this->mailer->sendEmailMessage('newsletter_subscription_confirmation', [ConfigQuery::getStoreEmail() => ConfigQuery::getStoreName()], [$event->getEmail() => $event->getFirstname() . " " . $event->getLastname()], ['email' => $event->getEmail(), 'firstname' => $event->getFirstname(), 'lastname' => $event->getLastname()], $event->getLocale()); }
protected function getEmailFromEvent(NewsletterEvent $event) { return NewsletterQuery::create()->findPk($event->getId())->getEmail(); }