complete controller and newsletter listener

This commit is contained in:
Manuel Raynaud
2013-10-22 10:01:02 +02:00
parent 888c2c6ab9
commit c9074892b2
3 changed files with 44 additions and 5 deletions

View File

@@ -25,7 +25,8 @@ namespace Thelia\Action;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Newsletter\NewsletterEvent; use Thelia\Core\Event\Newsletter\NewsletterEvent;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Thelia\Tests\Action\BaseAction; use Thelia\Action\BaseAction;
use Thelia\Model\Newsletter as NewsletterModel;
/** /**
@@ -38,7 +39,14 @@ class Newsletter extends BaseAction implements EventSubscriberInterface
public function subscribe(NewsletterEvent $event) public function subscribe(NewsletterEvent $event)
{ {
$newsletter = new NewsletterModel();
$newsletter
->setEmail($event->getEmail())
->setFirstname($event->getFirstname())
->setLastname($event->getLastname())
->setLocale($event->getLocale())
->save();
} }
/** /**

View File

@@ -22,9 +22,9 @@
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\Controller\Front; namespace Thelia\Controller\Front;
use Thelia\Core\Event\Newsletter\NewsletterEvent; use Thelia\Core\Event\Newsletter\NewsletterEvent;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Form\NewsletterForm; use Thelia\Form\NewsletterForm;
@@ -45,7 +45,10 @@ class NewsletterController extends BaseFrontController
$form = $this->validateForm($newsletterForm); $form = $this->validateForm($newsletterForm);
$event = new NewsletterEvent($form->get('email')->getData()); $event = new NewsletterEvent(
$form->get('email')->getData(),
$this->getRequest()->getSession()->getLang()->getLocale()
);
if (null !== $customer = $this->getSecurityContext()->getCustomerUser()) if (null !== $customer = $this->getSecurityContext()->getCustomerUser())
{ {
@@ -55,7 +58,7 @@ class NewsletterController extends BaseFrontController
$this->dispatch(TheliaEvents::NEWSLETTER_SUBSCRIBE, $event); $this->dispatch(TheliaEvents::NEWSLETTER_SUBSCRIBE, $event);
} catch(FormValidationException $e) { } catch(\Exception $e) {
$error_message = $e->getMessage(); $error_message = $e->getMessage();
} }

View File

@@ -47,9 +47,15 @@ class NewsletterEvent extends ActionEvent
*/ */
protected $lastname; protected $lastname;
function __construct($email) /**
* @var string current locale
*/
protected $locale;
function __construct($email, $locale)
{ {
$this->email = $email; $this->email = $email;
$this->locale = $locale;
} }
/** /**
@@ -112,6 +118,28 @@ class NewsletterEvent extends ActionEvent
return $this->lastname; return $this->lastname;
} }
/**
* @param string $locale
*
* @return $this
*/
public function setLocale($locale)
{
$this->locale = $locale;
return $this;
}
/**
* @return string
*/
public function getLocale()
{
return $this->locale;
}
} }