BxBasePage.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. if (!function_exists('DesignBoxContent'))
  127. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  128. $sContentWithBox = DesignBoxContent($sTitle, $mixedContent, $aBlock['designbox_id']);
  129. }
  130. }
  131. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  132. return $sContentWithBox;
  133. }
  134. /**
  135. * Add necessary js and css files.
  136. */
  137. protected function _addJsCss()
  138. {
  139. $this->_oTemplate->addCss('page_layouts.css');
  140. }
  141. /**
  142. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  143. */
  144. protected function _addSysTemplateVars ()
  145. {
  146. $sPageTitle = $this->_getPageTitle();
  147. if ($sPageTitle)
  148. $this->_oTemplate->setPageHeader ($sPageTitle);
  149. $sMetaDesc = $this->_getPageMetaDesc();
  150. if ($sMetaDesc)
  151. $this->_oTemplate->setPageDescription ($sMetaDesc);
  152. $sMetaKeywords = $this->_getPageMetaKeywords();
  153. if ($sMetaKeywords)
  154. $this->_oTemplate->addPageKeywords ($sMetaKeywords);
  155. $sMetaRobots = $this->_getPageMetaRobots();
  156. if ($sMetaRobots)
  157. $this->_oTemplate->setPageMetaRobots ($sMetaRobots);
  158. }
  159. /**
  160. * Select menu from page properties.
  161. */
  162. protected function _selectMenu ()
  163. {
  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. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  203. if (!$oStorage)
  204. return false;
  205. $sUrl = $oStorage->getFileUrlById($iFileId);
  206. if (!$sUrl)
  207. return false;
  208. $sStyleAdd = '';
  209. if ('center' == $sAlign || 'left' == $sAlign || 'right' == $sAlign)
  210. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  211. return '<div class="bx-page-image-container" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  212. }
  213. /**
  214. * Get content for 'rss' block type.
  215. * @return string
  216. */
  217. protected function _getBlockRss ($aBlock)
  218. {
  219. if (empty($aBlock['content']))
  220. return false;
  221. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  222. $iNum = (int)$iNum;
  223. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  224. }
  225. /**
  226. * Get content for 'menu' block type.
  227. * @return string
  228. */
  229. protected function _getBlockMenu ($aBlock)
  230. {
  231. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  232. return $oMenu ? $oMenu->getCode () : '';
  233. }
  234. /**
  235. * Get content for 'service' block type.
  236. * @return string
  237. */
  238. protected function _getBlockService ($aBlock)
  239. {
  240. return BxDolService::callSerialized($aBlock['content'], $this->_aMarkers);
  241. }
  242. /**
  243. * Get page title.
  244. * @return string
  245. */
  246. protected function _getPageTitle()
  247. {
  248. return $this->_replaceMarkers(_t($this->_aObject['title']));
  249. }
  250. /**
  251. * Get page meta description.
  252. * @return string
  253. */
  254. protected function _getPageMetaDesc()
  255. {
  256. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  257. }
  258. /**
  259. * Get page meta keywords.
  260. * @return string
  261. */
  262. protected function _getPageMetaKeywords()
  263. {
  264. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  265. }
  266. /**
  267. * Get page meta robots.
  268. * @return string
  269. */
  270. protected function _getPageMetaRobots()
  271. {
  272. return _t($this->_aObject['meta_robots']);
  273. }
  274. /**
  275. * Get access denied message HTML.
  276. * @return string
  277. */
  278. protected function _getPageAccessDeniedMsg ()
  279. {
  280. return MsgBox(_t('_Access denied'));
  281. }
  282. /**
  283. * Get page cache object.
  284. * @return cache object instance
  285. */
  286. protected function _getPageCacheObject ()
  287. {
  288. if ($this->_oPageCacheObject != null) {
  289. return $this->_oPageCacheObject;
  290. } else {
  291. $sEngine = getParam('sys_page_cache_engine');
  292. $this->_oPageCacheObject = bx_instance ('BxDolCache' . $sEngine);
  293. if (!$this->_oPageCacheObject->isAvailable())
  294. $this->_oPageCacheObject = bx_instance ('BxDolCacheFile');
  295. return $this->_oPageCacheObject;
  296. }
  297. }
  298. /**
  299. * Get page cache key.
  300. * @param $isPrefixOnly return key prefix only.
  301. * @return string
  302. */
  303. protected function _getPageCacheKey ($isPrefixOnly = false)
  304. {
  305. $s = 'page_' . $this->_aObject['object'] . '_';
  306. if ($isPrefixOnly)
  307. return $s;
  308. $s .= $this->_getPageCacheParams ();
  309. $s .= bx_site_hash() . '.php';
  310. return $s;
  311. }
  312. /**
  313. * Additional cache key. In the case of dynamic page.
  314. * For example - profile page, where each profile has own page.
  315. * @return string
  316. */
  317. protected function _getPageCacheParams ()
  318. {
  319. return '';
  320. }
  321. /**
  322. * Clean page cache.
  323. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  324. * @return string
  325. */
  326. protected function cleanCache ($isDelAllWithPagePrefix = false)
  327. {
  328. $oCache = $this->_getPageCacheObject ();
  329. $sKey = $this->_getPageCacheKey($isDelAllWithPagePrefix);
  330. if ($isDelAllWithPagePrefix)
  331. return $oCache->removeAllByPrefix($sKey);
  332. else
  333. return $oCache->delData($sKey);
  334. }
  335. }
  336. /** @} */