BxBasePage.php 11 KB

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