vendor/contao/core-bundle/src/Resources/contao/elements/ContentElement.php line 240

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 Contao\Model\Collection;
  11. /**
  12.  * Parent class for content elements.
  13.  *
  14.  * @property integer $id
  15.  * @property integer $pid
  16.  * @property string  $ptable
  17.  * @property integer $sorting
  18.  * @property integer $tstamp
  19.  * @property string  $type
  20.  * @property string  $headline
  21.  * @property string  $text
  22.  * @property boolean $addImage
  23.  * @property boolean $inline
  24.  * @property boolean $overwriteMeta
  25.  * @property string  $singleSRC
  26.  * @property string  $alt
  27.  * @property string  $title
  28.  * @property string  $size
  29.  * @property string  $imagemargin
  30.  * @property string  $imageUrl
  31.  * @property boolean $fullsize
  32.  * @property string  $caption
  33.  * @property string  $floating
  34.  * @property string  $html
  35.  * @property string  $listtype
  36.  * @property string  $listitems
  37.  * @property string  $tableitems
  38.  * @property string  $summary
  39.  * @property boolean $thead
  40.  * @property boolean $tfoot
  41.  * @property boolean $tleft
  42.  * @property boolean $sortable
  43.  * @property integer $sortIndex
  44.  * @property string  $sortOrder
  45.  * @property string  $mooHeadline
  46.  * @property string  $mooStyle
  47.  * @property string  $mooClasses
  48.  * @property string  $highlight
  49.  * @property string  $code
  50.  * @property string  $url
  51.  * @property boolean $target
  52.  * @property boolean $overwriteLink
  53.  * @property string  $titleText
  54.  * @property string  $linkTitle
  55.  * @property string  $embed
  56.  * @property string  $rel
  57.  * @property boolean $useImage
  58.  * @property string  $multiSRC
  59.  * @property string  $orderSRC
  60.  * @property boolean $useHomeDir
  61.  * @property integer $perRow
  62.  * @property integer $perPage
  63.  * @property integer $numberOfItems
  64.  * @property string  $sortBy
  65.  * @property boolean $metaIgnore
  66.  * @property string  $galleryTpl
  67.  * @property string  $customTpl
  68.  * @property string  $playerSRC
  69.  * @property string  $youtube
  70.  * @property string  $vimeo
  71.  * @property string  $posterSRC
  72.  * @property string  $playerSize
  73.  * @property array   $playerOptions
  74.  * @property string  $playerPreload
  75.  * @property integer $playerStart
  76.  * @property integer $playerStop
  77.  * @property string  $playerCaption
  78.  * @property string  $playerAspect
  79.  * @property string  $playerColor
  80.  * @property array   $youtubeOptions
  81.  * @property array   $vimeoOptions
  82.  * @property boolean $splashImage
  83.  * @property integer $sliderDelay
  84.  * @property integer $sliderSpeed
  85.  * @property integer $sliderStartSlide
  86.  * @property boolean $sliderContinuous
  87.  * @property integer $cteAlias
  88.  * @property integer $articleAlias
  89.  * @property integer $article
  90.  * @property integer $form
  91.  * @property integer $module
  92.  * @property boolean $protected
  93.  * @property string  $groups
  94.  * @property boolean $guests
  95.  * @property string  $cssID
  96.  * @property boolean $invisible
  97.  * @property string  $start
  98.  * @property string  $stop
  99.  * @property string  $com_order
  100.  * @property integer $com_perPage
  101.  * @property boolean $com_moderate
  102.  * @property boolean $com_bbcode
  103.  * @property boolean $com_disableCaptcha
  104.  * @property boolean $com_requireLogin
  105.  * @property string  $com_template
  106.  * @property string  $classes
  107.  * @property string  $typePrefix
  108.  * @property integer $origId
  109.  * @property string  $hl
  110.  */
  111. abstract class ContentElement extends Frontend
  112. {
  113.     /**
  114.      * Template
  115.      * @var string
  116.      */
  117.     protected $strTemplate;
  118.     /**
  119.      * Column
  120.      * @var string
  121.      */
  122.     protected $strColumn;
  123.     /**
  124.      * Model
  125.      * @var ContentModel
  126.      */
  127.     protected $objModel;
  128.     /**
  129.      * Current record
  130.      * @var array
  131.      */
  132.     protected $arrData = array();
  133.     /**
  134.      * Style array
  135.      * @var array
  136.      */
  137.     protected $arrStyle = array();
  138.     /**
  139.      * Initialize the object
  140.      *
  141.      * @param ContentModel $objElement
  142.      * @param string       $strColumn
  143.      */
  144.     public function __construct($objElement$strColumn='main')
  145.     {
  146.         if ($objElement instanceof Model || $objElement instanceof Collection)
  147.         {
  148.             /** @var ContentModel $objModel */
  149.             $objModel $objElement;
  150.             if ($objModel instanceof Collection)
  151.             {
  152.                 $objModel $objModel->current();
  153.             }
  154.             $this->objModel $objModel;
  155.         }
  156.         parent::__construct();
  157.         $this->arrData $objElement->row();
  158.         $this->cssID StringUtil::deserialize($objElement->cssIDtrue);
  159.         if ($this->customTpl)
  160.         {
  161.             $request System::getContainer()->get('request_stack')->getCurrentRequest();
  162.             // Use the custom template unless it is a back end request
  163.             if (!$request || !System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
  164.             {
  165.                 $this->strTemplate $this->customTpl;
  166.             }
  167.         }
  168.         $arrHeadline StringUtil::deserialize($objElement->headline);
  169.         $this->headline = \is_array($arrHeadline) ? $arrHeadline['value'] ?? '' $arrHeadline;
  170.         $this->hl $arrHeadline['unit'] ?? 'h1';
  171.         $this->strColumn $strColumn;
  172.     }
  173.     /**
  174.      * Set an object property
  175.      *
  176.      * @param string $strKey
  177.      * @param mixed  $varValue
  178.      */
  179.     public function __set($strKey$varValue)
  180.     {
  181.         $this->arrData[$strKey] = $varValue;
  182.     }
  183.     /**
  184.      * Return an object property
  185.      *
  186.      * @param string $strKey
  187.      *
  188.      * @return mixed
  189.      */
  190.     public function __get($strKey)
  191.     {
  192.         return $this->arrData[$strKey] ?? parent::__get($strKey);
  193.     }
  194.     /**
  195.      * Check whether a property is set
  196.      *
  197.      * @param string $strKey
  198.      *
  199.      * @return boolean
  200.      */
  201.     public function __isset($strKey)
  202.     {
  203.         return isset($this->arrData[$strKey]);
  204.     }
  205.     /**
  206.      * Return the model
  207.      *
  208.      * @return Model
  209.      */
  210.     public function getModel()
  211.     {
  212.         return $this->objModel;
  213.     }
  214.     /**
  215.      * Parse the template
  216.      *
  217.      * @return string
  218.      */
  219.     public function generate()
  220.     {
  221.         if ($this->isHidden())
  222.         {
  223.             return '';
  224.         }
  225.         $this->Template = new FrontendTemplate($this->strTemplate);
  226.         $this->Template->setData($this->arrData);
  227.         $this->compile();
  228.         // Do not change this order (see #6191)
  229.         $this->Template->style = !empty($this->arrStyle) ? implode(' '$this->arrStyle) : '';
  230.         $this->Template->class trim('ce_' $this->type ' ' . ($this->cssID[1] ?? ''));
  231.         $this->Template->cssID = !empty($this->cssID[0]) ? ' id="' $this->cssID[0] . '"' '';
  232.         $this->Template->inColumn $this->strColumn;
  233.         if (!$this->Template->headline)
  234.         {
  235.             $this->Template->headline $this->headline;
  236.         }
  237.         if (!$this->Template->hl)
  238.         {
  239.             $this->Template->hl $this->hl;
  240.         }
  241.         if (!empty($this->objModel->classes) && \is_array($this->objModel->classes))
  242.         {
  243.             $this->Template->class .= ' ' implode(' '$this->objModel->classes);
  244.         }
  245.         // Tag the content element (see #2137)
  246.         if ($this->objModel !== null)
  247.         {
  248.             System::getContainer()->get('contao.cache.entity_tags')->tagWithModelInstance($this->objModel);
  249.         }
  250.         return $this->Template->parse();
  251.     }
  252.     protected function isHidden()
  253.     {
  254.         // Skip unsaved elements (see #2708)
  255.         if (isset($this->tstamp) && !$this->tstamp)
  256.         {
  257.             return true;
  258.         }
  259.         $isInvisible $this->invisible || ($this->start && $this->start time()) || ($this->stop && $this->stop <= time());
  260.         // The element is visible, so show it
  261.         if (!$isInvisible)
  262.         {
  263.             return false;
  264.         }
  265.         $tokenChecker System::getContainer()->get('contao.security.token_checker');
  266.         // Preview mode is enabled, so show the element
  267.         if ($tokenChecker->isPreviewMode())
  268.         {
  269.             return false;
  270.         }
  271.         $request System::getContainer()->get('request_stack')->getCurrentRequest();
  272.         // We are in the back end, so show the element
  273.         if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
  274.         {
  275.             return false;
  276.         }
  277.         return true;
  278.     }
  279.     /**
  280.      * Compile the content element
  281.      */
  282.     abstract protected function compile();
  283.     /**
  284.      * Find a content element in the TL_CTE array and return the class name
  285.      *
  286.      * @param string $strName The content element name
  287.      *
  288.      * @return string The class name
  289.      */
  290.     public static function findClass($strName)
  291.     {
  292.         foreach ($GLOBALS['TL_CTE'] as $v)
  293.         {
  294.             foreach ($v as $kk=>$vv)
  295.             {
  296.                 if ($kk == $strName)
  297.                 {
  298.                     return $vv;
  299.                 }
  300.             }
  301.         }
  302.         return '';
  303.     }
  304. }
  305. class_alias(ContentElement::class, 'ContentElement');