Inital commit
This commit is contained in:
@@ -11,39 +11,63 @@
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Action;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Thelia\Core\Event\Newsletter\NewsletterEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
|
||||
use Thelia\Mailer\MailerFactory;
|
||||
use Thelia\Model\ConfigQuery;
|
||||
use Thelia\Model\NewsletterQuery;
|
||||
use Thelia\Model\Newsletter as NewsletterModel;
|
||||
|
||||
/**
|
||||
* Class Newsletter
|
||||
* @package Thelia\Action
|
||||
* @author Manuel Raynaud <mraynaud@openstudio.fr>
|
||||
* @author Manuel Raynaud <manu@raynaud.io>
|
||||
*/
|
||||
class Newsletter extends BaseAction implements EventSubscriberInterface
|
||||
{
|
||||
/** @var MailerFactory */
|
||||
protected $mailer;
|
||||
|
||||
/** @var EventDispatcherInterface */
|
||||
protected $dispatcher;
|
||||
|
||||
public function __construct(MailerFactory $mailer, EventDispatcherInterface $dispatcher)
|
||||
{
|
||||
$this->mailer = $mailer;
|
||||
$this->dispatcher = $dispatcher;
|
||||
}
|
||||
|
||||
public function subscribe(NewsletterEvent $event)
|
||||
{
|
||||
$newsletter = new NewsletterModel();
|
||||
// test if the email is already registered and unsubscribed
|
||||
if (null === $newsletter = NewsletterQuery::create()->findOneByEmail($event->getEmail())) {
|
||||
$newsletter = new NewsletterModel();
|
||||
}
|
||||
|
||||
$newsletter
|
||||
->setEmail($event->getEmail())
|
||||
->setFirstname($event->getFirstname())
|
||||
->setLastname($event->getLastname())
|
||||
->setLocale($event->getLocale())
|
||||
->setUnsubscribed(false)
|
||||
->save();
|
||||
|
||||
$event->setNewsletter($newsletter);
|
||||
|
||||
if (ConfigQuery::getNotifyNewsletterSubscription()) {
|
||||
$this->dispatcher->dispatch(TheliaEvents::NEWSLETTER_CONFIRM_SUBSCRIPTION, $event);
|
||||
}
|
||||
}
|
||||
|
||||
public function unsubscribe(NewsletterEvent $event)
|
||||
{
|
||||
if (null !== $nl = NewsletterQuery::create()->findPk($event->getId())) {
|
||||
$nl->delete();
|
||||
$nl
|
||||
->setUnsubscribed(true)
|
||||
->save();
|
||||
|
||||
$event->setNewsletter($nl);
|
||||
}
|
||||
@@ -63,31 +87,33 @@ class Newsletter extends BaseAction implements EventSubscriberInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of event names this subscriber wants to listen to.
|
||||
*
|
||||
* The array keys are event names and the value can be:
|
||||
*
|
||||
* * The method name to call (priority defaults to 0)
|
||||
* * An array composed of the method name to call and the priority
|
||||
* * An array of arrays composed of the method names to call and respective
|
||||
* priorities, or 0 if unset
|
||||
*
|
||||
* For instance:
|
||||
*
|
||||
* * array('eventName' => 'methodName')
|
||||
* * array('eventName' => array('methodName', $priority))
|
||||
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
|
||||
*
|
||||
* @return array The event names to listen to
|
||||
*
|
||||
* @api
|
||||
* @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()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
TheliaEvents::NEWSLETTER_SUBSCRIBE => array('subscribe', 128),
|
||||
TheliaEvents::NEWSLETTER_UPDATE => array('update', 128),
|
||||
TheliaEvents::NEWSLETTER_UNSUBSCRIBE => array('unsubscribe', 128)
|
||||
TheliaEvents::NEWSLETTER_UNSUBSCRIBE => array('unsubscribe', 128),
|
||||
TheliaEvents::NEWSLETTER_CONFIRM_SUBSCRIPTION => array('confirmSubscription', 128)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user