BxBaseCover.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Cover representation.
  11. * @see BxDolCover
  12. */
  13. class BxBaseCover extends BxDolCover
  14. {
  15. protected $_bJsCssAdded = false;
  16. protected $_oTemplate;
  17. public function __construct ($oTemplate)
  18. {
  19. parent::__construct ();
  20. if ($oTemplate)
  21. $this->_oTemplate = $oTemplate;
  22. else
  23. $this->_oTemplate = BxDolTemplate::getInstance();
  24. }
  25. /**
  26. * Display Cover.
  27. */
  28. public function display ()
  29. {
  30. $this->_addJsCss();
  31. $sResult = null;
  32. $aParams = [];
  33. if(!empty($this->_aOptions) && is_array($this->_aOptions)){
  34. $aParams = array_merge($this->_aOptiondDefault, $this->_aOptions);
  35. }
  36. else{
  37. $oPage = BxDolPage::getObjectInstanceByURI();
  38. if(empty($oPage) || !is_a($oPage, 'BxDolPage'))
  39. return $this->displayEmpty();
  40. $mixedOptions = $oPage->getPageCoverParams();
  41. if(empty($mixedOptions) || !is_array($mixedOptions))
  42. return $this->displayEmpty();
  43. if(!$this->_sCoverImageUrl) {
  44. $iId = (int)getParam('sys_site_cover_common');
  45. if($iId != 0)
  46. $this->setCoverImageUrl(array('id' => $iId, 'transcoder' => BX_DOL_TRANSCODER_OBJ_COVER));
  47. }
  48. if($this->_sCoverImageUrl)
  49. $mixedOptions['bx_if:bg'] = array (
  50. 'condition' => true,
  51. 'content' => array('image_url' => $this->_sCoverImageUrl),
  52. );
  53. $mixedOptions['bx_if:empty_cover_class'] = array (
  54. 'condition' => empty($this->_sCoverImageUrl),
  55. 'content' => array(),
  56. );
  57. $aParams = array_merge($this->_aOptiondDefault, $mixedOptions);
  58. }
  59. //--- The cover title is the page's <h1>. Content view pages deliberately leave it empty and
  60. //--- headline the content instead, so the heading element is only emitted when there is a title.
  61. $aParams['bx_if:title'] = array (
  62. 'condition' => !empty($aParams['title']),
  63. 'content' => array('title' => isset($aParams['title']) ? $aParams['title'] : ''),
  64. );
  65. /**
  66. * @hooks
  67. * @hookdef hook-system-display_cover 'system', 'display_cover' - hook to override page cover code to be output
  68. * - $unit_name - equals `system`
  69. * - $action - equals `display_cover`
  70. * - $object_id - not used
  71. * - $sender_id - not used
  72. * - $extra_params - array of additional params with the following array keys:
  73. * - `res` - [string] by ref, when null the default generation mechanism will be used, can be overridden in hook processing
  74. * - `template_name` - [string] by ref, template name, can be overridden in hook processing
  75. * - `params` - [array] by ref, additional params array as key&value pairs, can be overridden in hook processing
  76. * @hook @ref hook-system-display_cover
  77. */
  78. bx_alert('system', 'display_cover', 0, 0, [
  79. 'res' => &$sResult,
  80. 'template_name' => &$this->_sTemplateName,
  81. 'params' => &$aParams,
  82. 'template_name_ref' => &$this->_sTemplateName,
  83. 'params_ref' => &$aParams,
  84. ]);
  85. if ($sResult != null)
  86. return $sResult;
  87. return $this->_oTemplate->parseHtmlByName($this->_sTemplateName, $aParams);
  88. }
  89. /**
  90. * Display Empty Cover.
  91. */
  92. public function displayEmpty ()
  93. {
  94. $aTmplVarsTitle = ($sTitle = $this->_oTemplate->getPageHeader()) ? [
  95. 'title' => bx_process_output(strip_tags($sTitle))
  96. ] : false;
  97. return $this->_oTemplate->parseHtmlByName($this->_sTemplateNameEmpty, [
  98. 'bx_if:title' => [
  99. 'condition' => $aTmplVarsTitle !== false,
  100. 'content' => $aTmplVarsTitle,
  101. ],
  102. ]);
  103. }
  104. /**
  105. * Add css/js files which are needed for display and functionality.
  106. */
  107. protected function _addJsCss()
  108. {
  109. if ($this->_bJsCssAdded)
  110. return;
  111. $this->_oTemplate->addCss(array('cover.css'));
  112. $this->_bJsCssAdded = true;
  113. }
  114. }
  115. /** @} */