system/modules/47_CustomModule/library/CustomModule/Frontend/Modules/ModVoucherDownload.php line 34

Open in your IDE?
  1. <?php
  2. /**
  3.  * Studienkreis Tourismus | Custom Module for Contao Open Source CMS
  4.  *
  5.  * Copyright (C) 2017 47GradNord - Agentur für Internetlösungen
  6.  *
  7.  * @license    commercial
  8.  * @author     Holger Neuner
  9.  */
  10. namespace CustomModule\Frontend\Modules;
  11. use Contao\FilesModel;
  12. use Contao\Model;
  13. use Contao\Module;
  14. use Contao\PageModel;
  15. use CustomModule\Models\DownloadcreditsModel;
  16. class ModVoucherDownload extends Module
  17. {
  18.     /**
  19.      * Template
  20.      * @var string
  21.      */
  22.     protected $strTemplate 'mod_VoucherDownload';
  23.     /**
  24.      * Display a wildcard in the back end
  25.      *
  26.      * @return string
  27.      */
  28.     public function generate()
  29.     {
  30.         if (TL_MODE == 'BE')
  31.         {
  32.             /** @var \BackendTemplate|object $objTemplate */
  33.             $objTemplate = new \BackendTemplate('be_wildcard');
  34.             $objTemplate->wildcard '### ' utf8_strtoupper($GLOBALS['TL_LANG']['FMD']['47_ModVoucherDownload'][0]) . ' ###';
  35.             $objTemplate->title $this->headline;
  36.             $objTemplate->id $this->id;
  37.             $objTemplate->link $this->name;
  38.             $objTemplate->href 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' $this->id;
  39.             return $objTemplate->parse();
  40.         }
  41.         return parent::generate();
  42.     }
  43.     /**
  44.      * Generate the module
  45.      */
  46.     protected function compile()
  47.     {
  48.         /** @var PageModel */
  49.         global $objPage;
  50.         $strAliasHtml $objPage->alias.'.html';
  51.         // ActionCode is submitted
  52.         if ( \Input::post('voucherCode') !='' )
  53.         {
  54.             /** @var Model $downloadCredit */
  55.             $downloadCredit DownloadcreditsModel::findOnePublishedByCode(\Input::post('voucherCode'));
  56.             if ( null === $downloadCredit )
  57.                 $this->redirectToLoactionWidthMessage($strAliasHtml,'message=failed&reason=noCode');
  58.             if ( null !== $downloadCredit )
  59.             {
  60.                 if ( $downloadCredit->expires time())
  61.                     $this->redirectToLoactionWidthMessage($strAliasHtml,'message=failed&reason=codeExpires');
  62.                 // Set started
  63.                 DownloadcreditsModel::startedById($downloadCredit->id);
  64.                 
  65.                 // get PDF File
  66.                 $file FilesModel::findByUuid($downloadCredit->singleSRC)->path;
  67.                 if ( null !== $file )
  68.                 {
  69.                     \Controller::sendFileToBrowser($file);
  70.                 }
  71.             }
  72.         }
  73.         $this->Template->strForm $this->getVoucherCodeForm();
  74.     }
  75.     private function getVoucherCodeForm()
  76.     {
  77.         $objForm = new \Haste\Form\Form('VoucherCodeValidate''POST', function($objHaste) {
  78.             return \Input::post('FORM_SUBMIT') === $objHaste->getFormId();
  79.         });
  80.         $objForm->addFormField('voucherCode', array(
  81.             'label'             => 'Gutschein-Code:',
  82.             'inputType'         => 'text',
  83.             'default'           => \Input::get('code'),
  84.             'eval'          => array('mandatory'=>true)
  85.         ));
  86.         $objForm->addFormField('submit', array(
  87.             'label'     => 'Absenden und Herunterladen',
  88.             'inputType' => 'submit',
  89.             'eval'      => array(
  90.                 'class' => 'btn btn-xl btn-skin margin-b-20',
  91.                 'customTpl' => 'form_customSubmit',)
  92.         ));
  93.         return $objForm;
  94.     }
  95.     /**
  96.      * 
  97.      * @param $varUrl
  98.      * @param $strMessageQuery
  99.      */
  100.     private function redirectToLoactionWidthMessage($varUrl$strMessageQuery)
  101.     {
  102.         \Controller::redirect(\Haste\Util\Url::addQueryString($strMessageQuery $varUrl));
  103.     }
  104. }