BxBasePage.php 11 KB

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