<?php
/**
* SYMPATHIEMAGAZIN for Contao Open Source CMS
*
* Copyright (C) 2018 47GradNord - Agentur für Internetlösungen
*
* @license commercial
* @author Holger Neuner
*/
namespace CustomModule\Frontend\Modules;
use App\WebsiteBundle\PhotoCompetitionSession;
use Contao\System;
use CustomModule\Entity\PhotoCompetitionSubscriber;
use CustomModule\Models\PhotocompetitionSubscriberModel;
use CustomModule\PhotoCompetition\SubscriberSession;
use Haste\Form\Form;
use NotificationCenter\Model\Notification;
class ModPhotoCompetitionVerify extends AbstractModule
{
/**
* Template
* @var string
*/
protected $strTemplate = 'mod_PhotoCompetitionVerify';
/** @var string */
private $autoItem;
/**
* Display a wildcard in the back end
*
* @return string
*/
public function generate()
{
if (TL_MODE == 'BE')
{
/** @var \BackendTemplate|object $objTemplate */
$objTemplate = new \BackendTemplate('be_wildcard');
$objTemplate->wildcard = '### ' . utf8_strtoupper($GLOBALS['TL_LANG']['FMD']['47_ModPhotoCompetitionVerify'][0]) . ' ###';
$objTemplate->title = $this->headline;
$objTemplate->id = $this->id;
$objTemplate->link = $this->name;
$objTemplate->href = 'contao/main.php?do=themes&table=tl_module&act=edit&id=' . $this->id;
return $objTemplate->parse();
}
return parent::generate();
}
/**
* Generate the module
*/
protected function compile()
{
$this->autoItem = \Input::get('auto_item');
// Inital Request without Auto Item
if (null === $this->autoItem) {
return $this->redirectToStep('start');
}
// Verify Mail is calling
if('verify' === $this->autoItem)
{
/** @var PhotocompetitionSubscriberModel $validToken */
$validToken = PhotocompetitionSubscriberModel::findValidToken(\Input::get('token'));
if(null !== $validToken)
{
/** @var PhotocompetitionSubscriberModel $subscriberModel */
$subscriberModel = PhotocompetitionSubscriberModel::setEmailVerified(\Input::get('token'));
/** @var PhotoCompetitionSession $subscriberSession */
$subscriberSession = System::getContainer()->get('App\WebsiteBundle\PhotoCompetitionSession');
$subscriberSession->setSession(\App\WebsiteBundle\Entity\PhotoCompetitionSubscriber::buildFromModel($subscriberModel));
$this->redirectToStep('verificationSuccessfull');
} else {
$subscriberSession = System::getContainer()->get('App\WebsiteBundle\PhotoCompetitionSession');
$subscriberSession->removeSession();
$this->redirectToStep('verificationFailed');
}
}
/** @var Form $verifyMailForm */
$verifyMailForm = $this->getFormVerifyMail();
if($verifyMailForm->validate()) {
if($this->handleSubmitedEmailAddress($verifyMailForm->fetchAll()))
{
$this->redirectToStep('waitForVerification');
} else {
$this->redirectToStep('verificationFailed','email='.$verifyMailForm->fetchAll()['email']);
}
}
$this->Template->step = $this->autoItem;
$this->Template->showVerificationForm = ($this->autoItem === 'start') ? true : false;
$this->Template->showWaitForVerification = ($this->autoItem === 'waitForVerification') ? true : false;
$this->Template->showVerificationFailed = ($this->autoItem === 'verificationFailed') ? true : false;
$this->Template->showVerificationSuccessfull = ($this->autoItem === 'verificationSuccessfull') ? true : false;
$this->Template->verifyMailForm = $verifyMailForm->generate();
$this->Template->jumpToUpload = $this->redirectToFrontendPage($this->modPhotoCompetitionJumpToUpload, null, true);
$this->Template->givenEMail = \Input::get('email');
$this->Template->givenPage = $GLOBALS['objPage']->alias;
}
/**
* @param $fetchedData
* @return bool
*/
protected function handleSubmitedEmailAddress($fetchedData)
{
/** @var PhotoCompetitionSession $subscriberSession */
$subscriberSession = System::getContainer()->get('App\WebsiteBundle\PhotoCompetitionSession');
/** @var PhotocompetitionSubscriberModel $subscriberModel */
$subscriberModel = PhotocompetitionSubscriberModel::checkUniqueEMail($fetchedData['email']);
if(null !== $subscriberModel) {
return false;
}
/** @var \App\WebsiteBundle\Entity\PhotoCompetitionSubscriber $photoCompetitionSubscriber */
$photoCompetitionSubscriber = $subscriberSession->getSession();
$photoCompetitionSubscriber->setEmail($fetchedData['email']);
$photoCompetitionSubscriber->setVerifyToken($this->getUniqueString(32));
$subscriberSession->setSession($photoCompetitionSubscriber);
PhotocompetitionSubscriberModel::addMemberToVerifyEmailaddress($photoCompetitionSubscriber);
/** @var Notification $objNotification */
$objNotification = Notification::findByPk(\Config::get('photocompetion_notification_verifymail'));
// Set the language
$strLanguage = 'de';
// Set tokens
$arrTokens = array(
'admin_mail' => 'info@sympathiemagazin.de',
'admin_name' => 'SympathieMagazine',
'user_email' => $fetchedData['email'],
'user_firstname' => $fetchedData['firstname'],
'user_lastname' => $fetchedData['lastname'],
'user_city' => $fetchedData['city'],
'user_zip' => $fetchedData['zip'],
'user_country' => $fetchedData['country'],
'emailverification_link' => $this->getUrlToStep('verify').'?token='.$photoCompetitionSubscriber->getVerifyToken(),
'domain' => \Environment::get('base'),
);
$objNotification->send($arrTokens, $strLanguage);
return true;
}
/**
* @return Form
*/
protected function getFormVerifyMail()
{
/** @var Form $form */
$form = new Form('verifyMail', 'POST', function(Form $form) {
return \Input::post('FORM_SUBMIT') === $form->getFormId();
});
$form->addFormField('email', array(
'label' => 'Bitte geben Sie hier ihre E-Mail-Adresse ein:',
'inputType' => 'text',
'eval' => array('mandatory'=>true, 'rgxp'=>'email', 'class' => 'email')
));
$form->addSubmitFormField('submit', 'Weiter');
return $form;
}
}