vendor/terminal42/contao-ajaxform/AjaxForm.php line 54

Open in your IDE?
  1. <?php
  2. /*
  3.  * ajaxform extension for Contao Open Source CMS
  4.  *
  5.  * @copyright  Copyright (c) 2008-2017, terminal42 gmbh
  6.  * @author     terminal42 gmbh <info@terminal42.ch>
  7.  * @license    http://opensource.org/licenses/lgpl-3.0.html LGPL
  8.  * @link       http://github.com/terminal42/contao-ajaxform
  9.  */
  10. use Contao\CoreBundle\Exception\ResponseException;
  11. use Contao\Environment;
  12. use Contao\Form;
  13. use Contao\FrontendTemplate;
  14. use Symfony\Component\HttpFoundation\Response;
  15. class AjaxForm extends Form
  16. {
  17.     /**
  18.      * Template.
  19.      *
  20.      * @var string
  21.      */
  22.     protected $strTemplate 'ajaxform';
  23.     protected static $objStatic;
  24.     /**
  25.      * Generate the form.
  26.      *
  27.      * @return string
  28.      */
  29.     public function generate()
  30.     {
  31.         static::$objStatic $this;
  32.         $formId = ($this->formID !== '') ? 'auto_' $this->formID 'auto_form_' $this->id;
  33.         // Pass parent content or module model to the ajax form templates
  34.         $this->parentModel $this->objParent;
  35.         if (Environment::get('isAjaxRequest') && Environment::get('httpContaoAjaxForm') === $formId) {
  36.             $this->strTemplate 'ajaxform_inline';
  37.             $this->customTpl 'ajaxform_inline';
  38.             static::sendResponse(parent::generate());
  39.         }
  40.         // Replace the default Contao 4 template
  41.         if ($this->customTpl === 'form_wrapper') {
  42.             $this->customTpl $this->strTemplate;
  43.         }
  44.         return parent::generate();
  45.     }
  46.     /**
  47.      * Override the reload method.
  48.      */
  49.     public static function reload()
  50.     {
  51.         static::$objStatic->Template = new FrontendTemplate('ajaxform_confirm');
  52.         static::$objStatic->Template->parentModel = static::$objStatic->objParent;
  53.         static::$objStatic->Template->message = static::$objStatic->objParent->text;
  54.         if (Environment::get('isAjaxRequest')) {
  55.             static::sendResponse(static::$objStatic->objParent->text ? static::$objStatic->Template->parse() : 'true');
  56.         }
  57.         if (static::$objStatic->objParent->text) {
  58.             return;
  59.         }
  60.     }
  61.     /**
  62.      * Compile.
  63.      */
  64.     protected function compile()
  65.     {
  66.         parent::compile();
  67.         // Use the complete URL if the action is not available
  68.         if (!$this->Template->action) {
  69.             $this->Template->action Environment::get('uri');
  70.         }
  71.     }
  72.     /**
  73.      * Override the jumpToOrReload method to always reload.
  74.      *
  75.      * @param array|int $intId
  76.      * @param null      $strParams
  77.      * @param null      $strForceLang
  78.      */
  79.     protected function jumpToOrReload($intId$strParams null$strForceLang null)
  80.     {
  81.         $this->reload();
  82.     }
  83.     /**
  84.      * Replace insert tags and send response.
  85.      *
  86.      * @param string $content
  87.      */
  88.     private static function sendResponse($content)
  89.     {
  90.         if (\Contao\System::getContainer()->has('contao.insert_tag.parser')) {
  91.             $content \Contao\System::getContainer()->get('contao.insert_tag.parser')->replaceInline($content);
  92.         } else {
  93.             $content \Contao\Controller::replaceInsertTags($content);
  94.         }
  95.         throw new ResponseException(new Response($content));
  96.     }
  97. }