public function execute()
{
parent::execute();
// Define email from the subscribe widget
$email = $this->getEmail();
// Create the form
$form = $this->createForm($this->get('mailmotor.form.subscription'), new Subscription($email, FRONTEND_LANGUAGE));
$form->handleRequest($this->get('request'));
if (!$form->isValid()) {
$this->tpl->assign('form', $form->createView());
if ($form->isSubmitted()) {
$this->tpl->assign('mailmotorSubscribeHasFormError', true);
}
$this->loadTemplate();
$this->parse();
return;
}
$redirectLink = FrontendNavigation::getURLForBlock('Mailmotor', 'Subscribe') . '?subscribed=true';
/** @var Subscription $subscription */
$subscription = $form->getData();
try {
// The command bus will handle the unsubscription
$this->get('command_bus')->handle($subscription);
} catch (NotImplementedException $e) {
// fallback for when no mail-engine is chosen in the Backend
$this->get('event_dispatcher')->dispatch(NotImplementedSubscribedEvent::EVENT_NAME, new NotImplementedSubscribedEvent($subscription));
$redirectLink .= '&double-opt-in=false';
}
$redirectLink .= '#mailmotorSubscribeForm';
return $this->redirect($redirectLink);
}