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

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