<?php
/**
* Studienkreis Tourismus | Custom Module for Contao Open Source CMS
*
* Copyright (C) 2017 47GradNord - Agentur für Internetlösungen
*
* @license commercial
* @author Holger Neuner
*/
namespace CustomModule\Frontend\Modules;
use Contao\FilesModel;
use Contao\Model;
use Contao\Module;
use Contao\PageModel;
use CustomModule\Models\DownloadcreditsModel;
class ModVoucherDownload extends Module
{
/**
* Template
* @var string
*/
protected $strTemplate = 'mod_VoucherDownload';
/**
* 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_ModVoucherDownload'][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()
{
/** @var PageModel */
global $objPage;
$strAliasHtml = $objPage->alias.'.html';
// ActionCode is submitted
if ( \Input::post('voucherCode') !='' )
{
/** @var Model $downloadCredit */
$downloadCredit = DownloadcreditsModel::findOnePublishedByCode(\Input::post('voucherCode'));
if ( null === $downloadCredit )
$this->redirectToLoactionWidthMessage($strAliasHtml,'message=failed&reason=noCode');
if ( null !== $downloadCredit )
{
if ( $downloadCredit->expires < time())
$this->redirectToLoactionWidthMessage($strAliasHtml,'message=failed&reason=codeExpires');
// Set started
DownloadcreditsModel::startedById($downloadCredit->id);
// get PDF File
$file = FilesModel::findByUuid($downloadCredit->singleSRC)->path;
if ( null !== $file )
{
\Controller::sendFileToBrowser($file);
}
}
}
$this->Template->strForm = $this->getVoucherCodeForm();
}
private function getVoucherCodeForm()
{
$objForm = new \Haste\Form\Form('VoucherCodeValidate', 'POST', function($objHaste) {
return \Input::post('FORM_SUBMIT') === $objHaste->getFormId();
});
$objForm->addFormField('voucherCode', array(
'label' => 'Gutschein-Code:',
'inputType' => 'text',
'default' => \Input::get('code'),
'eval' => array('mandatory'=>true)
));
$objForm->addFormField('submit', array(
'label' => 'Absenden und Herunterladen',
'inputType' => 'submit',
'eval' => array(
'class' => 'btn btn-xl btn-skin margin-b-20',
'customTpl' => 'form_customSubmit',)
));
return $objForm;
}
/**
*
* @param $varUrl
* @param $strMessageQuery
*/
private function redirectToLoactionWidthMessage($varUrl, $strMessageQuery)
{
\Controller::redirect(\Haste\Util\Url::addQueryString($strMessageQuery , $varUrl));
}
}