vendor/contao/core-bundle/src/Resources/contao/classes/FrontendTemplateTrait.php line 186

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. /**
  11. * @property integer $id
  12. * @property string $keywords
  13. * @property string $content
  14. * @property array $sections
  15. * @property array $positions
  16. * @property array $matches
  17. *
  18. * @method static string getTemplate(string $strTemplate)
  19. *
  20. * @internal
  21. */
  22. trait FrontendTemplateTrait
  23. {
  24. /**
  25. * Return a custom layout section
  26. *
  27. * @param string $key The section name
  28. * @param string $template An optional template name
  29. */
  30. public function section($key, $template=null)
  31. {
  32. if (empty($this->sections[$key]))
  33. {
  34. return;
  35. }
  36. $this->id = $key;
  37. $this->content = $this->sections[$key];
  38. if ($template === null)
  39. {
  40. foreach ($this->positions as $position)
  41. {
  42. if (isset($position[$key]['template']))
  43. {
  44. $template = $position[$key]['template'];
  45. }
  46. }
  47. }
  48. if ($template === null)
  49. {
  50. $template = 'block_section';
  51. }
  52. include $this->getTemplate($template);
  53. }
  54. /**
  55. * Return the custom layout sections
  56. *
  57. * @param string $key An optional section name
  58. * @param string $template An optional template name
  59. */
  60. public function sections($key=null, $template=null)
  61. {
  62. if (!array_filter($this->sections))
  63. {
  64. return;
  65. }
  66. // The key does not match
  67. if ($key && !isset($this->positions[$key]))
  68. {
  69. return;
  70. }
  71. $matches = array();
  72. foreach ($this->positions[$key] as $id=>$section)
  73. {
  74. if (!empty($this->sections[$id]))
  75. {
  76. if (!isset($section['template']))
  77. {
  78. $section['template'] = 'block_section';
  79. }
  80. $section['content'] = $this->sections[$id];
  81. $matches[$id] = $section;
  82. }
  83. }
  84. // Return if the section is empty (see #1115)
  85. if (empty($matches))
  86. {
  87. return;
  88. }
  89. $this->matches = $matches;
  90. if ($template === null)
  91. {
  92. $template = 'block_sections';
  93. }
  94. include $this->getTemplate($template);
  95. }
  96. /**
  97. * Point to `Frontend::addToUrl()` in front end templates (see #6736)
  98. *
  99. * @param string $strRequest The request string to be added
  100. * @param boolean $blnIgnoreParams If true, the $_GET parameters will be ignored
  101. * @param array $arrUnset An optional array of keys to unset
  102. *
  103. * @return string The new URI string
  104. */
  105. public static function addToUrl($strRequest, $blnIgnoreParams=false, $arrUnset=array())
  106. {
  107. return Frontend::addToUrl($strRequest, $blnIgnoreParams, $arrUnset);
  108. }
  109. /**
  110. * Check whether there is an authenticated back end user
  111. *
  112. * @return boolean True if there is an authenticated back end user
  113. */
  114. public function hasAuthenticatedBackendUser()
  115. {
  116. return System::getContainer()->get('contao.security.token_checker')->hasBackendUser();
  117. }
  118. /**
  119. * Add the template output to the cache and add the cache headers
  120. *
  121. * @deprecated Deprecated since Contao 4.3, to be removed in Contao 5.0.
  122. * Use proper response caching headers instead.
  123. */
  124. protected function addToCache()
  125. {
  126. trigger_deprecation('contao/core-bundle', '4.3', 'Using "Contao\FrontendTemplate::addToCache()" has been deprecated and will no longer work in Contao 5.0. Use proper response caching headers instead.');
  127. }
  128. /**
  129. * Add the template output to the search index
  130. *
  131. * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0.
  132. * Use the kernel.terminate event instead.
  133. */
  134. protected function addToSearchIndex()
  135. {
  136. trigger_deprecation('contao/core-bundle', '4.0', 'Using "Contao\FrontendTemplate::addToSearchIndex()" has been deprecated and will no longer work in Contao 5.0. Use the "kernel.terminate" event instead.');
  137. }
  138. /**
  139. * Return a custom layout section
  140. *
  141. * @param string $strKey The section name
  142. *
  143. * @return string The section markup
  144. *
  145. * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0.
  146. * Use FrontendTemplate::section() instead.
  147. */
  148. public function getCustomSection($strKey)
  149. {
  150. trigger_deprecation('contao/core-bundle', '4.0', 'Using "Contao\FrontendTemplate::getCustomSection()" has been deprecated and will no longer work in Contao 5.0. Use "Contao\FrontendTemplate::section()" instead.');
  151. return '<div id="' . $strKey . '">' . $this->sections[$strKey] . '</div>' . "\n";
  152. }
  153. /**
  154. * Return all custom layout sections
  155. *
  156. * @param string $strKey An optional section name
  157. *
  158. * @return string The section markup
  159. *
  160. * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0.
  161. * Use FrontendTemplate::sections() instead.
  162. */
  163. public function getCustomSections($strKey=null)
  164. {
  165. trigger_deprecation('contao/core-bundle', '4.0', 'Using "Contao\FrontendTemplate::getCustomSections()" has been deprecated and will no longer work in Contao 5.0. Use "Contao\FrontendTemplate::sections()" instead.');
  166. if ($strKey && !isset($this->positions[$strKey]))
  167. {
  168. return '';
  169. }
  170. $tag = 'div';
  171. // Use the section tag for the main column
  172. if ($strKey == 'main')
  173. {
  174. $tag = 'section';
  175. }
  176. $sections = '';
  177. // Standardize the IDs (thanks to Tsarma) (see #4251)
  178. foreach ($this->positions[$strKey] as $sect)
  179. {
  180. if (isset($this->sections[$sect['id']]))
  181. {
  182. $sections .= "\n" . '<' . $tag . ' id="' . StringUtil::standardize($sect['id'], true) . '">' . "\n" . '<div class="inside">' . "\n" . $this->sections[$sect['id']] . "\n" . '</div>' . "\n" . '</' . $tag . '>' . "\n";
  183. }
  184. }
  185. if (!$sections)
  186. {
  187. return '';
  188. }
  189. return '<div class="custom">' . "\n" . $sections . "\n" . '</div>' . "\n";
  190. }
  191. }