BxTemplFunctions.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 UnaTemplate UNA Template Classes
  7. * @{
  8. */
  9. bx_import('BxDolDesigns');
  10. class BxTemplFunctions extends BxBaseFunctions
  11. {
  12. protected $_sModule;
  13. public function __construct($oTemplate = null)
  14. {
  15. parent::__construct($oTemplate);
  16. $this->_sModule = 'bx_artificer';
  17. }
  18. public function getMainLogoDarkUrl()
  19. {
  20. return BxDolDesigns::getInstance()->getSiteLogoDarkUrl();
  21. }
  22. public function getMainLogoInline()
  23. {
  24. return BxDolDesigns::getInstance()->getSiteLogoParam('logo_inline');
  25. }
  26. public function getMainMarkDarkUrl()
  27. {
  28. return BxDolDesigns::getInstance()->getSiteMarkDarkUrl();
  29. }
  30. public function getMainMarkInline()
  31. {
  32. return BxDolDesigns::getInstance()->getSiteLogoParam('mark_inline');
  33. }
  34. public function getMainLogo($aParams = [])
  35. {
  36. $oModule = BxDolModule::getInstance($this->_sModule);
  37. $oDesigns = BxDolDesigns::getInstance();
  38. $sTitle = getParam('site_title');
  39. $sAlt = $oDesigns->getSiteLogoAlt();
  40. if(empty($sAlt) && !empty($sTitle))
  41. $sAlt = $sTitle;
  42. $sAltAttr = bx_html_attribute($sAlt, BX_ESCAPE_STR_QUOTE);
  43. $aImages = [
  44. 'logo' => ['uc' => 'Logo', 'gi' => 'logo-generic.svg'],
  45. 'logo_dark' => ['uc' => 'LogoDark', 'gi' => 'logo-dark-generic.svg'],
  46. 'mark' => ['uc' => 'Mark', 'gi' => 'mark-generic.svg'],
  47. 'mark_dark' => ['uc' => 'MarkDark', 'gi' => 'mark-dark-generic.svg'],
  48. ];
  49. $aTmplVarsImages = [];
  50. $bLogo = $bLogoDark = $bMark = $bMarkDark = false;
  51. foreach($aImages as $sType => &$aParams)
  52. if(($aParams['g'] = false) || ($sFileUrl = $this->{'getMain' . $aParams['uc'] . 'Url'}()) !== false || ($aParams['g'] = (($sType == 'logo' || $aImages['logo']['g']) && ($sFileUrl = $this->_oTemplate->getImageUrl($aParams['gi'])) != ''))) {
  53. $iLogoHeight = (int)$oDesigns->{'getSite' . $aParams['uc'] . 'Height'}();
  54. $sLogoHeight = $iLogoHeight > 0 ? 'height:' . round($iLogoHeight/16, 3) . 'rem;' : '';
  55. /**
  56. * Generic (SVG) images get the height only, the browser derives the width from the image's own aspect ratio.
  57. * Uploaded images use the dimensions from Designs settings.
  58. */
  59. $iLogoWidth = empty($aParams['g']) ? (int)$oDesigns->{'getSite' . $aParams['uc'] . 'Width'}() : 0;
  60. $sLogoWidth = $iLogoWidth > 0 ? 'width:' . round($iLogoWidth/16, 3) . 'rem;' : '';
  61. $aTmplVarsImages[$sType] = [
  62. 'id' => 'bx-' . str_replace('_', '-', $sType),
  63. 'src' => $sFileUrl,
  64. 'alt' => $sAltAttr,
  65. 'class' => '',
  66. 'style' => $sLogoWidth . ' ' . $sLogoHeight,
  67. ];
  68. ${'b' . $aParams['uc']} = true;
  69. }
  70. $sLogoInline = $sLogoInlineClass = '';
  71. if($aImages['logo']['g'] && ($sLogoInline = $this->getMainLogoInline()) != '') {
  72. $bLogo = true;
  73. $bLogoDark = false;
  74. unset($aTmplVarsImages['logo'], $aTmplVarsImages['logo_dark']);
  75. }
  76. $sMarkInline = $sMarkInlineClass = '';
  77. if($aImages['mark']['g'] && ($sMarkInline = $this->getMainMarkInline()) != '') {
  78. $bMark = true;
  79. $bMarkDark = false;
  80. unset($aTmplVarsImages['mark'], $aTmplVarsImages['mark_dark']);
  81. }
  82. if($bLogo) {
  83. if($bLogoDark && !$bMark)
  84. $aTmplVarsImages['logo']['class'] = 'block dark:hidden';
  85. if(!$bLogoDark && $bMark) {
  86. $sLogoInlineClass = 'hidden lg:block';
  87. if(isset($aTmplVarsImages['logo']))
  88. $aTmplVarsImages['logo']['class'] = $sLogoInlineClass;
  89. }
  90. if($bLogoDark && $bMark)
  91. $aTmplVarsImages['logo']['class'] = 'hidden dark:hidden lg:block';
  92. }
  93. if($bLogoDark) {
  94. $aTmplVarsImages['logo_dark']['class'] = 'hidden dark:block';
  95. if($bMark || $bMarkDark)
  96. $aTmplVarsImages['logo_dark']['class'] = 'hidden dark:lg:block';
  97. }
  98. if($bMark) {
  99. $sMarkInlineClass = 'block lg:hidden';
  100. if(isset($aTmplVarsImages['mark']))
  101. $aTmplVarsImages['mark']['class'] = $sMarkInlineClass;
  102. if($bMarkDark)
  103. $aTmplVarsImages['mark']['class'] = 'block dark:hidden lg:hidden';
  104. }
  105. if($bMarkDark) {
  106. $aTmplVarsImages['mark_dark']['class'] = 'hidden dark:block dark:lg:hidden';
  107. }
  108. $aAttrs = [
  109. 'href' => BX_DOL_URL_ROOT,
  110. 'title' => $sAltAttr
  111. ];
  112. if(!empty($aParams['attrs']) && is_array($aParams['attrs']))
  113. $aAttrs = array_merge($aAttrs, $aParams['attrs']);
  114. $sTmplName = 'logo_main.html';
  115. $aTmplVars = [
  116. 'attrs' => bx_convert_array2attrs($aAttrs),
  117. 'bx_if:show_title' => [
  118. 'condition' => !$bLogo && !$sLogoInline,
  119. 'content' => [
  120. 'logo' => $sTitle,
  121. ]
  122. ],
  123. 'bx_if:show_logo_inline' => [
  124. 'condition' => !empty($sLogoInline),
  125. 'content' => [
  126. 'class' => $sLogoInlineClass,
  127. 'height' => $oModule->_oConfig->getLogoHeight() . 'px',
  128. 'content' => $sLogoInline
  129. ]
  130. ],
  131. 'bx_if:show_mark_inline' => [
  132. 'condition' => !empty($sMarkInline),
  133. 'content' => [
  134. 'class' => $sMarkInlineClass,
  135. 'height' => $oModule->_oConfig->getMarkHeight() . 'px',
  136. 'content' => $sMarkInline
  137. ]
  138. ]
  139. ];
  140. foreach($aImages as $sType => $aParams) {
  141. if(($sTypeIf = 'bx_if:show_' . $sType) && isset($aTmplVarsImages[$sType]))
  142. $aTmplVars[$sTypeIf] = [
  143. 'condition' => true,
  144. 'content' => $aTmplVarsImages[$sType]
  145. ];
  146. else
  147. $aTmplVars[$sTypeIf] = [
  148. 'condition' => false,
  149. 'content' => []
  150. ];
  151. }
  152. bx_alert('system', 'get_logo', 0, 0, [
  153. 'tmpl_name' => &$sTmplName,
  154. 'tmpl_vars' => &$aTmplVars,
  155. 'tmpl_name_ref' => &$sTmplName,
  156. 'tmpl_vars_ref' => &$aTmplVars,
  157. ]);
  158. return $this->_oTemplate->parseHtmlByName($sTmplName, $aTmplVars);
  159. }
  160. public function TemplPageAddComponent($sKey)
  161. {
  162. $mixedResult = false;
  163. switch($sKey) {
  164. case 'sys_header_width':
  165. $mixedResult = '';
  166. if(getParam('bx_artificer_header_stretched') != 'on')
  167. $mixedResult = parent::TemplPageAddComponent($sKey);
  168. break;
  169. case 'sys_toolbar_search':
  170. if(getParam('bx_artificer_header_search') == 'on')
  171. $mixedResult = parent::TemplPageAddComponent($sKey);
  172. else
  173. $mixedResult = '';
  174. break;
  175. default:
  176. $mixedResult = parent::TemplPageAddComponent($sKey);
  177. }
  178. return $mixedResult;
  179. }
  180. public function getColorSchemeSwitcher()
  181. {
  182. $oModule = BxDolModule::getInstance($this->_sModule);
  183. if($oModule->_oConfig->getColorScheme() != 'auto')
  184. return '';
  185. $aMenu = [
  186. ['id' => $this->_sModule . '-css-sun', 'name' => $this->_sModule . '-css-sun', 'class' => '', 'link' => 'javascript:void(0)', 'onclick' => 'javascript:oBxArtificerUtils.setColorScheme(1)', 'target' => '_self', 'icon' => 'sun', 'title' => _t('_bx_artificer_txt_color_scheme_light')],
  187. ['id' => $this->_sModule . '-css-moon', 'name' => $this->_sModule . '-css-moon', 'class' => '', 'link' => 'javascript:void(0)', 'onclick' => 'javascript:oBxArtificerUtils.setColorScheme(2)', 'target' => '_self', 'icon' => 'moon', 'title' => _t('_bx_artificer_txt_color_scheme_dark')],
  188. ['id' => $this->_sModule . '-css-desktop', 'name' => $this->_sModule . '-css-desktop', 'class' => '', 'link' => 'javascript:void(0)', 'onclick' => 'javascript:oBxArtificerUtils.setColorScheme(0)', 'target' => '_self', 'icon' => 'desktop', 'title' => _t('_bx_artificer_txt_color_scheme_system')],
  189. ];
  190. $oMenu = new BxTemplMenu(['template' => 'menu_vertical.html', 'menu_id'=> $this->_sModule . '-css-menu', 'menu_items' => $aMenu]);
  191. return $oModule->_oTemplate->parseHtmlByName('color_scheme_switcher.html', [
  192. 'popup' => $this->transBox('bx-sb-theme-switcher-menu', $oMenu->getCode(), true)
  193. ]);
  194. }
  195. protected function getInjFooterPopupMenus()
  196. {
  197. $sContent = '';
  198. $oSearch = new BxTemplSearch();
  199. $oSearch->setLiveSearch(true);
  200. $sContent .= $this->_oTemplate->parsePageByName('search.html', array(
  201. 'search_form' => $oSearch->getForm(BX_DB_CONTENT_ONLY),
  202. 'results' => $oSearch->getResultsContainer(),
  203. ));
  204. if(isLogged())
  205. $sContent .= $this->_oTemplate->getMenu('sys_add_content');
  206. return $sContent;
  207. }
  208. protected function getInjFooterSidebarSite()
  209. {
  210. $oSearch = new BxTemplSearch();
  211. $oSearch->setLiveSearch(true);
  212. $sSearch = $oSearch->getForm(BX_DB_CONTENT_ONLY);
  213. $sLogoUrl = $this->getMainLogoUrl();
  214. $bLogoUrl = !empty($sLogoUrl);
  215. $sLogoText = BxDolDesigns::getInstance()->getSiteLogoAlt();
  216. if(empty($sLogoText))
  217. $sLogoText = getParam('site_title');
  218. return $this->_oTemplate->parsePageByName('sidebar_site.html', array(
  219. 'bx_if:show_logo_image' => array(
  220. 'condition' => $bLogoUrl,
  221. 'content' => array(
  222. 'logo_url' => $sLogoUrl,
  223. 'logo_text' => $sLogoText
  224. )
  225. ),
  226. 'bx_if:show_logo_text' => array(
  227. 'condition' => !$bLogoUrl,
  228. 'content' => array(
  229. 'logo_text' => $sLogoText
  230. )
  231. ),
  232. 'search_form' => $sSearch,
  233. ));
  234. }
  235. protected function getInjFooterSidebarAccount()
  236. {
  237. $oProfile = BxDolProfile::getInstance();
  238. if(!$oProfile)
  239. return '';
  240. $sMenuAccountPopup = 'sys_account_popup';
  241. $oMenuAccountPopup = BxTemplMenu::getObjectInstance($sMenuAccountPopup);
  242. return $this->_oTemplate->parsePageByName('sidebar_account.html', [
  243. 'color_scheme_switcher' => $this->getColorSchemeSwitcher(),
  244. 'ap_menu_object' => $sMenuAccountPopup,
  245. 'bx_repeat:ap_menu_items' => $oMenuAccountPopup->getMenuItems(),
  246. ]);
  247. }
  248. }
  249. /** @} */