BxDolStudioTemplate.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 UnaStudio UNA Studio
  7. * @{
  8. */
  9. define('BX_PAGE_COLUMN_DUAL', 3); ///< page, with 2 columns
  10. class BxDolStudioTemplate extends BxDolTemplate implements iBxDolSingleton
  11. {
  12. protected $_sFolderModuleIcons;
  13. protected function __construct()
  14. {
  15. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  16. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  17. parent::__construct();
  18. $this->_sRootPath = BX_DOL_DIR_STUDIO;
  19. $this->_sRootUrl = BX_DOL_URL_STUDIO;
  20. $this->_sPrefix = 'BxDolStudioTemplate';
  21. $this->_sInjectionsTable = 'sys_injections_admin';
  22. $this->_sInjectionsCache = 'sys_injections_admin.inc';
  23. $aCode = self::retrieveCode();
  24. $this->_sCodeKey = BX_DOL_STUDIO_TEMPLATE_CODE_KEY;
  25. $aCodeStudio = self::retrieveCode($this->_sCodeKey, $this->_sMixKey, $this->_sRootPath);
  26. $sCodeDefault = getParam('template');
  27. if($aCodeStudio !== false && $aCodeStudio[0] != $sCodeDefault)
  28. $aCode = $aCodeStudio;
  29. list(
  30. $this->_sCode,
  31. $this->_sName,
  32. $this->_sSubPath
  33. ) = $aCode;
  34. $this->_iMix = 0;
  35. if(is_array($this->_sCode))
  36. list($this->_sCode, $this->_iMix) = $this->_sCode;
  37. $this->_sFolderModuleIcons = 'images/modules/';
  38. $this->addLocation('studio', $this->_sRootPath, $this->_sRootUrl);
  39. $this->addLocationJs('system_admin_js', $this->_sRootPath . 'js/' , $this->_sRootUrl . 'js/');
  40. }
  41. /**
  42. * Prevent cloning the instance
  43. */
  44. public function __clone()
  45. {
  46. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  47. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  48. }
  49. /**
  50. * Get singleton instance of the class
  51. */
  52. public static function getInstance()
  53. {
  54. if (!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
  55. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolStudioTemplate();
  56. $GLOBALS['bxDolClasses'][__CLASS__]->init();
  57. }
  58. return $GLOBALS['bxDolClasses'][__CLASS__];
  59. }
  60. function init()
  61. {
  62. parent::init();
  63. //--- Add default CSS in output
  64. $this->addCssSystem(array(
  65. 'common.less',
  66. 'default.less',
  67. 'general.css',
  68. 'menu.css',
  69. ));
  70. bx_import('BxTemplStudioConfig');
  71. $this->_oTemplateConfig = BxTemplStudioConfig::getInstance();
  72. bx_import('BxTemplStudioFunctions');
  73. $this->_oTemplateFunctions = BxTemplStudioFunctions::getInstance($this);
  74. }
  75. function getIconUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  76. {
  77. if(($sModuleIcon = $this->_getModuleIcon('url', $sName)) !== false)
  78. return $sModuleIcon;
  79. return parent::getIconUrl($sName, $sCheckIn);
  80. }
  81. function getIconPath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  82. {
  83. if(($sModuleIcon = $this->_getModuleIcon('path', $sName)) !== false)
  84. return $sModuleIcon;
  85. return parent::getIconPath($sName, $sCheckIn);
  86. }
  87. function _getAbsoluteLocation($sType, $sFolder, $sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  88. {
  89. return parent::_getAbsoluteLocation($sType, $sFolder, $sName, BX_DOL_TEMPLATE_CHECK_IN_BASE);
  90. }
  91. function parseSystemKey($sKey, $mixedKeyWrapperHtml = null, $bProcessInjection = true)
  92. {
  93. $sRet = '';
  94. switch( $sKey ) {
  95. case 'version':
  96. $sRet = bx_get_ver();
  97. break;
  98. case 'page_breadcrumb':
  99. $sRet = $this->getPageBreadcrumb();
  100. break;
  101. case 'page_viewport':
  102. $sRet = getParam('sys_viewport_meta_tag');
  103. if(empty($sRet))
  104. $sRet = 'width=device-width, initial-scale=1';
  105. break;
  106. case 'popup_loading':
  107. $s = $this->parsePageByName('popup_loading.html', array());
  108. $sRet = BxTemplFunctions::getInstance()->transBox('bx-popup-loading', $s, true);
  109. break;
  110. case 'dol_images':
  111. $sRet = $this->_processJsImages();
  112. break;
  113. case 'dol_lang':
  114. $sRet = $this->_processJsTranslations();
  115. break;
  116. case 'dol_options':
  117. $sRet = $this->_processJsOptions();
  118. break;
  119. case 'menu_top':
  120. $sRet = BxTemplStudioMenuTop::getInstance()->getCode();
  121. break;
  122. case 'copyright':
  123. $sRet = _t( '_copyright', date('Y') ) . getVersionComment();
  124. break;
  125. case 'class_name':
  126. $sRet = 'bx-dir-' . strtolower(bx_lang_direction());
  127. break;
  128. case 'meta_info':
  129. // Studio pages carry the Studio's own icons (the UNA logo favicon); the site's custom icons still take precedence when set.
  130. $sRet = parent::parseSystemKey($sKey, $mixedKeyWrapperHtml, false);
  131. $sRet = preg_replace('/<link rel="(icon|apple-touch-icon)"[^>]*\/>/', '', $sRet) . BxTemplStudioFunctions::getInstance()->getMetaIcons();
  132. break;
  133. default:
  134. $sRet = parent::parseSystemKey($sKey, $mixedKeyWrapperHtml, false);
  135. }
  136. return $this->processInjection($this->getPageNameIndex(), $sKey, $sRet);
  137. }
  138. function getModuleIconUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  139. {
  140. return $this->_getAbsoluteLocation('url', $this->_sFolderModuleIcons, $sName, $sCheckIn);
  141. }
  142. function getModuleIconPath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  143. {
  144. return $this->_getAbsoluteLocation('path', $this->_sFolderModuleIcons, $sName, $sCheckIn);
  145. }
  146. function setPageBreadcrumb($aItems)
  147. {
  148. $this->aPage['breadcrumb'] = $aItems;
  149. }
  150. function getPageBreadcrumb()
  151. {
  152. if(empty($this->aPage['breadcrumb']) || !is_array($this->aPage['breadcrumb']))
  153. return "";
  154. $aItems = array();
  155. foreach($this->aPage['breadcrumb'] as $aItem) {
  156. $bLink = isset($aItem['link']) && $aItem['link'] != '';
  157. $aItems[] = array(
  158. 'bx_if:show_link' => array(
  159. 'condition' => $bLink,
  160. 'content' => array(
  161. 'link' => $bLink ? $aItem['link'] : '',
  162. 'title' => _t($aItem['title'])
  163. )
  164. ),
  165. 'bx_if:show_text' => array(
  166. 'condition' => !$bLink,
  167. 'content' => array(
  168. 'title' => _t($aItem['title'])
  169. )
  170. )
  171. );
  172. }
  173. return $this->parseHtmlByName('breadcrumb.html', array('bx_repeat:items' => $aItems));
  174. }
  175. function displayMsg ($mixed, $bTranslate = false, $iPage = BX_PAGE_DEFAULT, $iDesignBox = BX_DB_CONTENT_ONLY)
  176. {
  177. $iCode = 200;
  178. $sMessage = '';
  179. if(is_array($mixed))
  180. list($iCode, $sMessage) = $mixed;
  181. else
  182. $sMessage = $mixed;
  183. switch($iCode) {
  184. case 403:
  185. header('HTTP/1.0 403 Forbidden');
  186. header('Status: 403 Forbidden');
  187. break;
  188. case 404:
  189. header('HTTP/1.0 404 Not Found');
  190. header('Status: 404 Not Found');
  191. break;
  192. }
  193. $sTitle = $bTranslate ? _t($sMessage) : $sMessage;
  194. $sContent = $this->parseHtmlByName('page_not_found.html', [
  195. 'content' => DesignBoxContent('', MsgBox($sTitle), $iDesignBox)
  196. ]);
  197. $this->setPageNameIndex($iPage);
  198. $this->setPageHeader($sTitle);
  199. $this->setPageContent('page_main_code', $sContent);
  200. $this->getPageCode();
  201. exit;
  202. }
  203. function displayPage(&$oPage)
  204. {
  205. if(($mixedResult = $oPage->checkAction()) !== false)
  206. return echoJson($mixedResult);
  207. $sPageMenu = $oPage->getPageMenu();
  208. $sPageCode = $oPage->getPageCode();
  209. if($sPageCode === false)
  210. $this->displayMsg(($sError = $oPage->getError(false)) !== false ? $sError : '_sys_txt_error_occured', true, BX_PAGE_DEFAULT, BX_DB_PADDING_NO_CAPTION);
  211. $this->setPageNameIndex($oPage->getPageIndex());
  212. $this->setPageHeader($oPage->getPageHeader());
  213. $this->setPageDescription($oPage->getPageDescription());
  214. $this->setPageContent('page_caption_code', $oPage->getPageCaption());
  215. $this->setPageContent('page_attributes', $oPage->getPageAttributes());
  216. $this->setPageContent('page_menu_code', $sPageMenu);
  217. $this->setPageContent('page_main_code', $sPageCode);
  218. $this->addCss($oPage->getPageCss());
  219. $this->addJs($oPage->getPageJs());
  220. $this->getPageCode();
  221. }
  222. protected function _getModuleIcon($sType, $sName)
  223. {
  224. if(strpos($sName, '|') !== false) {
  225. list($sLocation, $sFile) = explode('|', $sName);
  226. if($sFile == 'std-icon.svg' && strpos($sLocation, '@') !== false) {
  227. list($sModule) = explode('@', $sLocation);
  228. if($sModule && ($sModuleIcon = $this->{'getModuleIcon' . ucfirst($sType)}($sModule . '.svg')))
  229. return $sModuleIcon;
  230. }
  231. }
  232. return false;
  233. }
  234. }
  235. /** @} */