public function execute()
{
parent::execute();
// Define email from the unsubscribe widget
$email = $this->getEmail();
// Create the form
$form = $this->createForm($this->get('mailmotor.form.unsubscription'), new Unsubscription($email, FRONTEND_LANGUAGE));
$form->handleRequest($this->get('request'));
if (!$form->isValid()) {
$this->tpl->assign('form', $form->createView());
if ($form->isSubmitted()) {
$this->tpl->assign('mailmotorUnsubscribeHasFormError', true);
}
$this->loadTemplate();
$this->parse();
return;
}
/** @var Unsubscription $unsubscription */
$unsubscription = $form->getData();
try {
// The command bus will handle the unsubscription
$this->get('command_bus')->handle($unsubscription);
// fallback for when no mail-engine is chosen in the Backend
} catch (NotImplementedException $e) {
$this->get('event_dispatcher')->dispatch(NotImplementedUnsubscribedEvent::EVENT_NAME, new NotImplementedUnsubscribedEvent($unsubscription));
}
return $this->redirect(FrontendNavigation::getURLForBlock('Mailmotor', 'Unsubscribe') . '?unsubscribed=true' . '#mailmotorUnsubscribeForm');
}