vendor/contao/core-bundle/src/Resources/contao/classes/BackendTemplate.php line 69

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. use Symfony\Component\HttpFoundation\Response;
  11. /**
  12.  * Provide methods to handle back end templates.
  13.  *
  14.  * @property string $ua
  15.  * @property array  $javascripts
  16.  * @property array  $stylesheets
  17.  * @property string $mootools
  18.  * @property string $attributes
  19.  * @property string $badgeTitle
  20.  */
  21. class BackendTemplate extends Template
  22. {
  23.     use BackendTemplateTrait;
  24.     /**
  25.      * Add a hook to modify the template output
  26.      *
  27.      * @return string
  28.      */
  29.     public function parse()
  30.     {
  31.         $strBuffer parent::parse();
  32.         // HOOK: add custom parse filters
  33.         if (isset($GLOBALS['TL_HOOKS']['parseBackendTemplate']) && \is_array($GLOBALS['TL_HOOKS']['parseBackendTemplate']))
  34.         {
  35.             foreach ($GLOBALS['TL_HOOKS']['parseBackendTemplate'] as $callback)
  36.             {
  37.                 $this->import($callback[0]);
  38.                 $strBuffer $this->{$callback[0]}->{$callback[1]}($strBuffer$this->strTemplate);
  39.             }
  40.         }
  41.         return $strBuffer;
  42.     }
  43.     /**
  44.      * Return a response object
  45.      *
  46.      * @return Response The response object
  47.      */
  48.     public function getResponse()
  49.     {
  50.         $response parent::getResponse();
  51.         $response->headers->set('Cache-Control''no-cache, no-store');
  52.         return $response->setPrivate();
  53.     }
  54.     /**
  55.      * Compile the template
  56.      *
  57.      * @internal Do not call this method in your code. It will be made private in Contao 5.0.
  58.      */
  59.     protected function compile()
  60.     {
  61.         // Backwards compatibility (see #3074 and #6277)
  62.         $this->ua Environment::get('agent')->class;
  63.         if (Config::get('fullscreen'))
  64.         {
  65.             $this->ua .= ' fullscreen';
  66.         }
  67.         $this->addBackendConfig();
  68.         // Style sheets
  69.         if (!empty($GLOBALS['TL_CSS']) && \is_array($GLOBALS['TL_CSS']))
  70.         {
  71.             $strStyleSheets '';
  72.             $objCombiner = new Combiner();
  73.             foreach (array_unique($GLOBALS['TL_CSS']) as $stylesheet)
  74.             {
  75.                 $options StringUtil::resolveFlaggedUrl($stylesheet);
  76.                 if ($options->static)
  77.                 {
  78.                     $objCombiner->add($stylesheet$options->mtime$options->media);
  79.                 }
  80.                 else
  81.                 {
  82.                     $strStyleSheets .= Template::generateStyleTag($this->addStaticUrlTo($stylesheet), $options->media$options->mtime);
  83.                 }
  84.             }
  85.             if ($objCombiner->hasEntries())
  86.             {
  87.                 $strStyleSheets Template::generateStyleTag($objCombiner->getCombinedFile(), 'all') . $strStyleSheets;
  88.             }
  89.             $this->stylesheets .= $strStyleSheets;
  90.         }
  91.         // JavaScripts
  92.         if (!empty($GLOBALS['TL_JAVASCRIPT']) && \is_array($GLOBALS['TL_JAVASCRIPT']))
  93.         {
  94.             $objCombiner = new Combiner();
  95.             $objCombinerAsync = new Combiner();
  96.             $strJavaScripts '';
  97.             foreach (array_unique($GLOBALS['TL_JAVASCRIPT']) as $javascript)
  98.             {
  99.                 $options StringUtil::resolveFlaggedUrl($javascript);
  100.                 if ($options->static)
  101.                 {
  102.                     $options->async $objCombinerAsync->add($javascript$options->mtime) : $objCombiner->add($javascript$options->mtime);
  103.                 }
  104.                 else
  105.                 {
  106.                     $strJavaScripts .= Template::generateScriptTag($this->addStaticUrlTo($javascript), $options->async$options->mtime);
  107.                 }
  108.             }
  109.             if ($objCombiner->hasEntries())
  110.             {
  111.                 $strJavaScripts Template::generateScriptTag($objCombiner->getCombinedFile()) . $strJavaScripts;
  112.             }
  113.             if ($objCombinerAsync->hasEntries())
  114.             {
  115.                 $strJavaScripts Template::generateScriptTag($objCombinerAsync->getCombinedFile(), true) . $strJavaScripts;
  116.             }
  117.             $this->javascripts .= $strJavaScripts;
  118.         }
  119.         // MooTools scripts (added at the page bottom)
  120.         if (!empty($GLOBALS['TL_MOOTOOLS']) && \is_array($GLOBALS['TL_MOOTOOLS']))
  121.         {
  122.             $this->mootools .= implode(''array_unique($GLOBALS['TL_MOOTOOLS']));
  123.         }
  124.         $strBuffer $this->parse();
  125.         $strBuffer = static::replaceOldBePaths($strBuffer);
  126.         // HOOK: add custom output filter
  127.         if (isset($GLOBALS['TL_HOOKS']['outputBackendTemplate']) && \is_array($GLOBALS['TL_HOOKS']['outputBackendTemplate']))
  128.         {
  129.             foreach ($GLOBALS['TL_HOOKS']['outputBackendTemplate'] as $callback)
  130.             {
  131.                 $this->import($callback[0]);
  132.                 $strBuffer $this->{$callback[0]}->{$callback[1]}($strBuffer$this->strTemplate);
  133.             }
  134.         }
  135.         $this->strBuffer $strBuffer;
  136.         parent::compile();
  137.     }
  138.     /**
  139.      * Add the contao.backend configuration
  140.      */
  141.     private function addBackendConfig(): void
  142.     {
  143.         $container System::getContainer();
  144.         if ($container->hasParameter('contao.backend.attributes'))
  145.         {
  146.             $attributes $container->getParameter('contao.backend.attributes');
  147.             if (!empty($attributes) && \is_array($attributes))
  148.             {
  149.                 $this->attributes ' ' implode(' 'array_map(
  150.                     static function ($v$k) { return sprintf('data-%s="%s"'$k$v); },
  151.                     $attributes,
  152.                     array_keys($attributes)
  153.                 ));
  154.             }
  155.         }
  156.         if ($container->hasParameter('contao.backend.custom_css'))
  157.         {
  158.             $css $container->getParameter('contao.backend.custom_css');
  159.             if (!empty($css) && \is_array($css))
  160.             {
  161.                 if (!\is_array($GLOBALS['TL_CSS']))
  162.                 {
  163.                     $GLOBALS['TL_CSS'] = array();
  164.                 }
  165.                 $GLOBALS['TL_CSS'] = array_merge($GLOBALS['TL_CSS'], $css);
  166.             }
  167.         }
  168.         if ($container->hasParameter('contao.backend.custom_js'))
  169.         {
  170.             $js $container->getParameter('contao.backend.custom_js');
  171.             if (!empty($js) && \is_array($js))
  172.             {
  173.                 if (!\is_array($GLOBALS['TL_JAVASCRIPT']))
  174.                 {
  175.                     $GLOBALS['TL_JAVASCRIPT'] = array();
  176.                 }
  177.                 $GLOBALS['TL_JAVASCRIPT'] = array_merge($GLOBALS['TL_JAVASCRIPT'], $js);
  178.             }
  179.         }
  180.         if ($container->hasParameter('contao.backend.badge_title'))
  181.         {
  182.             $this->badgeTitle $container->getParameter('contao.backend.badge_title');
  183.         }
  184.     }
  185. }
  186. class_alias(BackendTemplate::class, 'BackendTemplate');