BxBasePage.php 11 KB

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