BxBasePage.php 11 KB

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