BxBasePage.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  10. /**
  11. * Page representation.
  12. * @see BxDolPage
  13. */
  14. class BxBasePage extends BxDolPage
  15. {
  16. protected $_oTemplate;
  17. protected $_oPageCacheObject = null;
  18. public function __construct ($aObject, $oTemplate)
  19. {
  20. parent::__construct ($aObject);
  21. if ($oTemplate)
  22. $this->_oTemplate = $oTemplate;
  23. else
  24. $this->_oTemplate = BxDolTemplate::getInstance();
  25. }
  26. /**
  27. * Get page code with automatic caching, adding necessary css/js files and system template vars.
  28. * @return string.
  29. */
  30. public function getCode ()
  31. {
  32. if (bx_get('dynamic') && ($iBlockId = (int)bx_get('pageBlock'))) {
  33. if (!$this->_isVisiblePage($this->_aObject)) {
  34. header('HTTP/1.0 403 Forbidden');
  35. exit;
  36. }
  37. bx_alert('system', 'page_output_block', 0, false, array(
  38. 'page_name' => $this->_sObject,
  39. 'page_object' => $this,
  40. 'page_query' => $this->_oQuery,
  41. 'block_id' => (int)$iBlockId,
  42. ));
  43. header( 'Content-type:text/html;charset=utf-8' );
  44. echo $this->_getBlockOnlyCode($iBlockId);
  45. exit;
  46. }
  47. if (!$this->_isVisiblePage($this->_aObject))
  48. return $this->_getPageAccessDeniedMsg ();
  49. $this->_addJsCss();
  50. $this->_addSysTemplateVars();
  51. $this->_selectMenu();
  52. if (!getParam('sys_page_cache_enable') || !$this->_aObject['cache_lifetime'])
  53. return $this->_getPageCode();
  54. $oCache = $this->_getPageCacheObject();
  55. $sKey = $this->_getPageCacheKey();
  56. $mixedRet = $oCache->getData($sKey, $this->_aObject['cache_lifetime']);
  57. if ($mixedRet !== null) {
  58. return $mixedRet;
  59. } else {
  60. $sPageCode = $this->_getPageCode();
  61. $oCache->setData($sKey, $sPageCode, $this->_aObject['cache_lifetime']);
  62. }
  63. bx_alert('system', 'page_output', 0, false, array(
  64. 'page_name' => $this->_sObject,
  65. 'page_object' => $this,
  66. 'page_query' => $this->_oQuery,
  67. 'page_code' => &$sPageCode,
  68. ));
  69. return $sPageCode;
  70. }
  71. /**
  72. * Get block title.
  73. * @return string
  74. */
  75. public function getBlockTitle ($aBlock)
  76. {
  77. return $this->_replaceMarkers(_t($aBlock['title']));
  78. }
  79. /**
  80. * Get page code only.
  81. * @return string
  82. */
  83. protected function _getPageCode ()
  84. {
  85. $aHiddenOn = array(
  86. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  87. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  88. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide'
  89. );
  90. $aVars = array ();
  91. $aBlocks = $this->_oQuery->getPageBlocks();
  92. foreach ($aBlocks as $sKey => $aCell) {
  93. $sCell = '';
  94. foreach ($aCell as $aBlock) {
  95. $sContentWithBox = $this->_getBlockCode($aBlock);
  96. $sHiddenOn = '';
  97. if(!empty($aBlock['hidden_on']))
  98. foreach($aHiddenOn as $iHiddenOn => $sClass)
  99. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  100. $sHiddenOn .= ' ' . $sClass;
  101. if ($sContentWithBox)
  102. $sCell .= '<div class="bx-page-block-container bx-def-padding-sec-topbottom' . $sHiddenOn . '" id="bx-page-block-' . $aBlock['id'] . '">' . $sContentWithBox . '</div>';
  103. }
  104. $aVars[$sKey] = $sCell;
  105. }
  106. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  107. }
  108. /**
  109. * Get one block code only.
  110. * @return string
  111. */
  112. protected function _getBlockOnlyCode ($iBlockId)
  113. {
  114. $aBlock = $this->_oQuery->getPageBlock((int)$iBlockId);
  115. return $this->_getBlockCode($aBlock);
  116. }
  117. /**
  118. * Get block code.
  119. * @return string
  120. */
  121. protected function _getBlockCode($aBlock)
  122. {
  123. $sContentWithBox = '';
  124. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  125. if ($this->_isVisibleBlock($aBlock)) {
  126. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  127. $mixedContent = $this->$sFunc($aBlock);
  128. $sTitle = $this->getBlockTitle($aBlock);
  129. if (is_array($mixedContent) && !empty($mixedContent['content'])) {
  130. $sContentWithBox = DesignBoxContent(
  131. isset($mixedContent['title']) ? $mixedContent['title'] : $sTitle,
  132. $mixedContent['content'],
  133. isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'],
  134. isset($mixedContent['menu']) ? $mixedContent['menu'] : false
  135. );
  136. }
  137. elseif (is_string($mixedContent) && !empty($mixedContent)) {
  138. $sContentWithBox = DesignBoxContent($sTitle, $mixedContent, $aBlock['designbox_id']);
  139. }
  140. }
  141. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  142. return $sContentWithBox;
  143. }
  144. /**
  145. * Add necessary js and css files.
  146. */
  147. protected function _addJsCss()
  148. {
  149. $this->_oTemplate->addCss('page_layouts.css');
  150. }
  151. /**
  152. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  153. */
  154. protected function _addSysTemplateVars ()
  155. {
  156. $sPageTitle = $this->_getPageTitle();
  157. if ($sPageTitle)
  158. $this->_oTemplate->setPageHeader ($sPageTitle);
  159. $sMetaDesc = $this->_getPageMetaDesc();
  160. if ($sMetaDesc)
  161. $this->_oTemplate->setPageDescription ($sMetaDesc);
  162. $sMetaKeywords = $this->_getPageMetaKeywords();
  163. if ($sMetaKeywords)
  164. $this->_oTemplate->addPageKeywords ($sMetaKeywords);
  165. $sMetaRobots = $this->_getPageMetaRobots();
  166. if ($sMetaRobots)
  167. $this->_oTemplate->setPageMetaRobots ($sMetaRobots);
  168. }
  169. /**
  170. * Select menu from page properties.
  171. */
  172. protected function _selectMenu ()
  173. {
  174. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  175. }
  176. /**
  177. * Get content for 'raw' block type.
  178. * @return string
  179. */
  180. protected function _getBlockRaw ($aBlock)
  181. {
  182. return '<div class="bx-page-raw-container">' . $this->_replaceMarkers($aBlock['content']) . '</div>';
  183. }
  184. /**
  185. * Get content for 'html' block type.
  186. * @return string
  187. */
  188. protected function _getBlockHtml ($aBlock)
  189. {
  190. return '<div class="bx-page-html-container">' . $this->_replaceMarkers($aBlock['content']) . '</div>';
  191. }
  192. /**
  193. * Get content for 'lang' block type.
  194. * @return string
  195. */
  196. protected function _getBlockLang ($aBlock)
  197. {
  198. return '<div class="bx-page-lang-container">' . $this->_replaceMarkers(_t(trim($aBlock['content']))) . '</div>';
  199. }
  200. /**
  201. * Get content for 'image' block type.
  202. * @return string
  203. */
  204. protected function _getBlockImage ($aBlock)
  205. {
  206. if (empty($aBlock['content']))
  207. return false;
  208. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  209. $iFileId = (int)$iFileId;
  210. if (!$iFileId)
  211. return false;
  212. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  213. if (!$oStorage)
  214. return false;
  215. $sUrl = $oStorage->getFileUrlById($iFileId);
  216. if (!$sUrl)
  217. return false;
  218. $sStyleAdd = '';
  219. if ('center' == $sAlign || 'left' == $sAlign || 'right' == $sAlign)
  220. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  221. return '<div class="bx-page-image-container" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  222. }
  223. /**
  224. * Get content for 'rss' block type.
  225. * @return string
  226. */
  227. protected function _getBlockRss ($aBlock)
  228. {
  229. if (empty($aBlock['content']))
  230. return false;
  231. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  232. $iNum = (int)$iNum;
  233. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  234. }
  235. /**
  236. * Get content for 'menu' block type.
  237. * @return string
  238. */
  239. protected function _getBlockMenu ($aBlock)
  240. {
  241. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  242. return $oMenu ? $oMenu->getCode () : '';
  243. }
  244. /**
  245. * Get content for 'service' block type.
  246. * @return string
  247. */
  248. protected function _getBlockService ($aBlock)
  249. {
  250. return BxDolService::callSerialized($aBlock['content'], $this->_aMarkers);
  251. }
  252. /**
  253. * Get page title.
  254. * @return string
  255. */
  256. protected function _getPageTitle()
  257. {
  258. return $this->_replaceMarkers(_t($this->_aObject['title']));
  259. }
  260. /**
  261. * Get page meta description.
  262. * @return string
  263. */
  264. protected function _getPageMetaDesc()
  265. {
  266. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  267. }
  268. /**
  269. * Get page meta keywords.
  270. * @return string
  271. */
  272. protected function _getPageMetaKeywords()
  273. {
  274. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  275. }
  276. /**
  277. * Get page meta robots.
  278. * @return string
  279. */
  280. protected function _getPageMetaRobots()
  281. {
  282. return _t($this->_aObject['meta_robots']);
  283. }
  284. /**
  285. * Get access denied message HTML.
  286. * @return string
  287. */
  288. protected function _getPageAccessDeniedMsg ()
  289. {
  290. return MsgBox(_t('_Access denied'));
  291. }
  292. /**
  293. * Get page cache object.
  294. * @return cache object instance
  295. */
  296. protected function _getPageCacheObject ()
  297. {
  298. if ($this->_oPageCacheObject != null) {
  299. return $this->_oPageCacheObject;
  300. } else {
  301. $sEngine = getParam('sys_page_cache_engine');
  302. $this->_oPageCacheObject = bx_instance ('BxDolCache' . $sEngine);
  303. if (!$this->_oPageCacheObject->isAvailable())
  304. $this->_oPageCacheObject = bx_instance ('BxDolCacheFile');
  305. return $this->_oPageCacheObject;
  306. }
  307. }
  308. /**
  309. * Get page cache key.
  310. * @param $isPrefixOnly return key prefix only.
  311. * @return string
  312. */
  313. protected function _getPageCacheKey ($isPrefixOnly = false)
  314. {
  315. $s = 'page_' . $this->_aObject['object'] . '_';
  316. if ($isPrefixOnly)
  317. return $s;
  318. $s .= $this->_getPageCacheParams ();
  319. $s .= bx_site_hash() . '.php';
  320. return $s;
  321. }
  322. /**
  323. * Additional cache key. In the case of dynamic page.
  324. * For example - profile page, where each profile has own page.
  325. * @return string
  326. */
  327. protected function _getPageCacheParams ()
  328. {
  329. return '';
  330. }
  331. /**
  332. * Clean page cache.
  333. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  334. * @return string
  335. */
  336. protected function cleanCache ($isDelAllWithPagePrefix = false)
  337. {
  338. $oCache = $this->_getPageCacheObject ();
  339. $sKey = $this->_getPageCacheKey($isDelAllWithPagePrefix);
  340. if ($isDelAllWithPagePrefix)
  341. return $oCache->removeAllByPrefix($sKey);
  342. else
  343. return $oCache->delData($sKey);
  344. }
  345. }
  346. /** @} */