BxTemplFunctions.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. if(!empty($aParams['g'])) {
  56. list($iDlWidth, $iDlHeight) = bx_get_svg_image_size($sFileUrl);
  57. $fDlAspectRation = $iDlHeight ? $iDlWidth / $iDlHeight : BxDolDesigns::getAspectRatioDefault($sType);
  58. $iLogoWidth = $iLogoHeight * $fDlAspectRation;
  59. }
  60. else
  61. $iLogoWidth = $oDesigns->{'getSite' . $aParams['uc'] . 'Width'}();
  62. $sLogoWidth = $iLogoWidth > 0 ? 'width:' . round($iLogoWidth/16, 3) . 'rem;' : '';
  63. $aTmplVarsImages[$sType] = [
  64. 'id' => 'bx-' . str_replace('_', '-', $sType),
  65. 'src' => $sFileUrl,
  66. 'alt' => $sAltAttr,
  67. 'class' => '',
  68. 'style' => $sLogoWidth . ' ' . $sLogoHeight,
  69. ];
  70. ${'b' . $aParams['uc']} = true;
  71. }
  72. $sLogoInline = $sLogoInlineClass = '';
  73. if($aImages['logo']['g'] && ($sLogoInline = $this->getMainLogoInline()) != '') {
  74. $bLogo = true;
  75. $bLogoDark = false;
  76. unset($aTmplVarsImages['logo'], $aTmplVarsImages['logo_dark']);
  77. }
  78. $sMarkInline = $sMarkInlineClass = '';
  79. if($aImages['mark']['g'] && ($sMarkInline = $this->getMainMarkInline()) != '') {
  80. $bMark = true;
  81. $bMarkDark = false;
  82. unset($aTmplVarsImages['mark'], $aTmplVarsImages['mark_dark']);
  83. }
  84. if($bLogo) {
  85. if($bLogoDark && !$bMark)
  86. $aTmplVarsImages['logo']['class'] = 'block dark:hidden';
  87. if(!$bLogoDark && $bMark) {
  88. $sLogoInlineClass = 'hidden lg:block';
  89. if(isset($aTmplVarsImages['logo']))
  90. $aTmplVarsImages['logo']['class'] = $sLogoInlineClass;
  91. }
  92. if($bLogoDark && $bMark)
  93. $aTmplVarsImages['logo']['class'] = 'hidden dark:hidden lg:block';
  94. }
  95. if($bLogoDark) {
  96. $aTmplVarsImages['logo_dark']['class'] = 'hidden dark:block';
  97. if($bMark || $bMarkDark)
  98. $aTmplVarsImages['logo_dark']['class'] = 'hidden dark:lg:block';
  99. }
  100. if($bMark) {
  101. $sMarkInlineClass = 'block lg:hidden';
  102. if(isset($aTmplVarsImages['mark']))
  103. $aTmplVarsImages['mark']['class'] = $sMarkInlineClass;
  104. if($bMarkDark)
  105. $aTmplVarsImages['mark']['class'] = 'block dark:hidden lg:hidden';
  106. }
  107. if($bMarkDark) {
  108. $aTmplVarsImages['mark_dark']['class'] = 'hidden dark:block dark:lg:hidden';
  109. }
  110. $aAttrs = [
  111. 'href' => BX_DOL_URL_ROOT,
  112. 'title' => $sAltAttr
  113. ];
  114. if(!empty($aParams['attrs']) && is_array($aParams['attrs']))
  115. $aAttrs = array_merge($aAttrs, $aParams['attrs']);
  116. $sTmplName = 'logo_main.html';
  117. $aTmplVars = [
  118. 'attrs' => bx_convert_array2attrs($aAttrs),
  119. 'bx_if:show_title' => [
  120. 'condition' => !$bLogo && !$sLogoInline,
  121. 'content' => [
  122. 'logo' => $sTitle,
  123. ]
  124. ],
  125. 'bx_if:show_logo_inline' => [
  126. 'condition' => !empty($sLogoInline),
  127. 'content' => [
  128. 'class' => $sLogoInlineClass,
  129. 'height' => $oModule->_oConfig->getLogoHeight() . 'px',
  130. 'content' => $sLogoInline
  131. ]
  132. ],
  133. 'bx_if:show_mark_inline' => [
  134. 'condition' => !empty($sMarkInline),
  135. 'content' => [
  136. 'class' => $sMarkInlineClass,
  137. 'height' => $oModule->_oConfig->getMarkHeight() . 'px',
  138. 'content' => $sMarkInline
  139. ]
  140. ]
  141. ];
  142. foreach($aImages as $sType => $aParams) {
  143. if(($sTypeIf = 'bx_if:show_' . $sType) && isset($aTmplVarsImages[$sType]))
  144. $aTmplVars[$sTypeIf] = [
  145. 'condition' => true,
  146. 'content' => $aTmplVarsImages[$sType]
  147. ];
  148. else
  149. $aTmplVars[$sTypeIf] = [
  150. 'condition' => false,
  151. 'content' => []
  152. ];
  153. }
  154. bx_alert('system', 'get_logo', 0, 0, [
  155. 'tmpl_name' => &$sTmplName,
  156. 'tmpl_vars' => &$aTmplVars,
  157. 'tmpl_name_ref' => &$sTmplName,
  158. 'tmpl_vars_ref' => &$aTmplVars,
  159. ]);
  160. return $this->_oTemplate->parseHtmlByName($sTmplName, $aTmplVars);
  161. }
  162. public function TemplPageAddComponent($sKey)
  163. {
  164. $mixedResult = false;
  165. switch($sKey) {
  166. case 'sys_header_width':
  167. $mixedResult = '';
  168. if(getParam('bx_artificer_header_stretched') != 'on')
  169. $mixedResult = parent::TemplPageAddComponent($sKey);
  170. break;
  171. case 'sys_toolbar_search':
  172. if(getParam('bx_artificer_header_search') == 'on')
  173. $mixedResult = parent::TemplPageAddComponent($sKey);
  174. else
  175. $mixedResult = '';
  176. break;
  177. default:
  178. $mixedResult = parent::TemplPageAddComponent($sKey);
  179. }
  180. return $mixedResult;
  181. }
  182. public function getColorSchemeSwitcher()
  183. {
  184. $oModule = BxDolModule::getInstance($this->_sModule);
  185. if($oModule->_oConfig->getColorScheme() != 'auto')
  186. return '';
  187. $aMenu = [
  188. ['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')],
  189. ['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')],
  190. ['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')],
  191. ];
  192. $oMenu = new BxTemplMenu(['template' => 'menu_vertical.html', 'menu_id'=> $this->_sModule . '-css-menu', 'menu_items' => $aMenu]);
  193. return $oModule->_oTemplate->parseHtmlByName('color_scheme_switcher.html', [
  194. 'popup' => $this->transBox('bx-sb-theme-switcher-menu', $oMenu->getCode(), true)
  195. ]);
  196. }
  197. protected function getInjFooterPopupMenus()
  198. {
  199. $sContent = '';
  200. $oSearch = new BxTemplSearch();
  201. $oSearch->setLiveSearch(true);
  202. $sContent .= $this->_oTemplate->parsePageByName('search.html', array(
  203. 'search_form' => $oSearch->getForm(BX_DB_CONTENT_ONLY),
  204. 'results' => $oSearch->getResultsContainer(),
  205. ));
  206. if(isLogged())
  207. $sContent .= $this->_oTemplate->getMenu('sys_add_content');
  208. return $sContent;
  209. }
  210. protected function getInjFooterSidebarSite()
  211. {
  212. $oSearch = new BxTemplSearch();
  213. $oSearch->setLiveSearch(true);
  214. $sSearch = $oSearch->getForm(BX_DB_CONTENT_ONLY);
  215. $sLogoUrl = $this->getMainLogoUrl();
  216. $bLogoUrl = !empty($sLogoUrl);
  217. $sLogoText = BxDolDesigns::getInstance()->getSiteLogoAlt();
  218. if(empty($sLogoText))
  219. $sLogoText = getParam('site_title');
  220. return $this->_oTemplate->parsePageByName('sidebar_site.html', array(
  221. 'bx_if:show_logo_image' => array(
  222. 'condition' => $bLogoUrl,
  223. 'content' => array(
  224. 'logo_url' => $sLogoUrl,
  225. 'logo_text' => $sLogoText
  226. )
  227. ),
  228. 'bx_if:show_logo_text' => array(
  229. 'condition' => !$bLogoUrl,
  230. 'content' => array(
  231. 'logo_text' => $sLogoText
  232. )
  233. ),
  234. 'search_form' => $sSearch,
  235. ));
  236. }
  237. protected function getInjFooterSidebarAccount()
  238. {
  239. $oProfile = BxDolProfile::getInstance();
  240. if(!$oProfile)
  241. return '';
  242. $sMenuAccountPopup = 'sys_account_popup';
  243. $oMenuAccountPopup = BxTemplMenu::getObjectInstance($sMenuAccountPopup);
  244. return $this->_oTemplate->parsePageByName('sidebar_account.html', [
  245. 'color_scheme_switcher' => $this->getColorSchemeSwitcher(),
  246. 'ap_menu_object' => $sMenuAccountPopup,
  247. 'bx_repeat:ap_menu_items' => $oMenuAccountPopup->getMenuItems(),
  248. ]);
  249. }
  250. }
  251. /** @} */