1
0

BxBasePage.php 10 KB

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