1
0

BxBasePage.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  10. /**
  11. * Page representation.
  12. * @see BxDolPage
  13. */
  14. class BxBasePage extends BxDolPage
  15. {
  16. protected $_oTemplate;
  17. protected $_sStorage; //--- Storage object for page's images like custom cover, HTML block attachments, etc.
  18. protected $_oPageCacheObject = null;
  19. public function __construct ($aObject, $oTemplate)
  20. {
  21. parent::__construct ($aObject);
  22. if ($oTemplate)
  23. $this->_oTemplate = $oTemplate;
  24. else
  25. $this->_oTemplate = BxDolTemplate::getInstance();
  26. $this->_sStorage = 'sys_images';
  27. }
  28. /**
  29. * Get page code with automatic caching, adding necessary css/js files and system template vars.
  30. * @return string.
  31. */
  32. public function getCode ()
  33. {
  34. if (bx_get('dynamic') && ($iBlockId = (int)bx_get('pageBlock'))) {
  35. if (!$this->_isVisiblePage($this->_aObject)) {
  36. header('HTTP/1.0 403 Forbidden');
  37. exit;
  38. }
  39. bx_alert('system', 'page_output_block', 0, false, array(
  40. 'page_name' => $this->_sObject,
  41. 'page_object' => $this,
  42. 'page_query' => $this->_oQuery,
  43. 'block_id' => (int)$iBlockId,
  44. ));
  45. header( 'Content-type:text/html;charset=utf-8' );
  46. echo $this->_getBlockOnlyCode($iBlockId);
  47. exit;
  48. }
  49. if (!$this->_isVisiblePage($this->_aObject))
  50. return $this->_getPageAccessDeniedMsg ();
  51. $this->_addJsCss();
  52. $this->_addSysTemplateVars();
  53. $this->_selectMenu();
  54. $this->_setSubmenu();
  55. if (!getParam('sys_page_cache_enable') || !$this->_aObject['cache_lifetime'])
  56. return $this->_getPageCode();
  57. $oCache = $this->_getPageCacheObject();
  58. $sKey = $this->_getPageCacheKey();
  59. $mixedRet = $oCache->getData($sKey, $this->_aObject['cache_lifetime']);
  60. if ($mixedRet !== null) {
  61. return $mixedRet;
  62. } else {
  63. $sPageCode = $this->_getPageCode();
  64. $oCache->setData($sKey, $sPageCode, $this->_aObject['cache_lifetime']);
  65. }
  66. bx_alert('system', 'page_output', 0, false, array(
  67. 'page_name' => $this->_sObject,
  68. 'page_object' => $this,
  69. 'page_query' => $this->_oQuery,
  70. 'page_code' => &$sPageCode,
  71. ));
  72. return $sPageCode;
  73. }
  74. /**
  75. * Is page cover enabled.
  76. * @return string
  77. */
  78. public function isPageCover()
  79. {
  80. return (int)$this->_aObject['cover'] == 1;
  81. }
  82. public function setPageCover($bCover = true)
  83. {
  84. $this->_aObject['cover'] = (bool)$bCover;
  85. }
  86. public function getPageCoverImage($bTranscoder = true)
  87. {
  88. $iId = (int)$this->_aObject['cover_image'];
  89. if(empty($iId)) {
  90. $iId = (int)getParam('sys_site_cover_common');
  91. if(empty($iId))
  92. return array();
  93. }
  94. $aResult = array(
  95. 'id' => $iId
  96. );
  97. if($bTranscoder)
  98. $aResult['transcoder'] = BX_DOL_TRANSCODER_OBJ_COVER;
  99. else
  100. $aResult['object'] = $this->_sStorage;
  101. return $aResult;
  102. }
  103. public function getPageCoverParams()
  104. {
  105. $aParams = BxDolMenu::getObjectInstance('sys_site_submenu')->getPageCoverParams();
  106. if(!empty($aParams) && is_array($aParams))
  107. return $aParams;
  108. return array (
  109. 'title' => $this->_getPageTitle(),
  110. 'actions' => '',
  111. 'bx_if:image' => array (
  112. 'condition' => false,
  113. 'content' => array(),
  114. ),
  115. 'bx_if:icon' => array (
  116. 'condition' => false,
  117. 'content' => array(),
  118. ),
  119. );
  120. }
  121. /**
  122. * Get block title.
  123. * @return string
  124. */
  125. public function getBlockTitle ($aBlock)
  126. {
  127. return $this->_replaceMarkers(_t($aBlock['title']));
  128. }
  129. /**
  130. * Get page code only.
  131. * @return string
  132. */
  133. protected function _getPageCode ()
  134. {
  135. $aHiddenOn = array(
  136. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  137. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  138. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide'
  139. );
  140. $aVars = array (
  141. 'page_id' => 'bx-page-' . $this->_aObject['uri'],
  142. );
  143. $aBlocks = $this->_oQuery->getPageBlocks();
  144. foreach ($aBlocks as $sKey => $aCell) {
  145. $sCell = '';
  146. foreach ($aCell as $aBlock) {
  147. $sContentWithBox = $this->_getBlockCode($aBlock);
  148. $sHiddenOn = '';
  149. if(!empty($aBlock['hidden_on']))
  150. foreach($aHiddenOn as $iHiddenOn => $sClass)
  151. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  152. $sHiddenOn .= ' ' . $sClass;
  153. if ($sContentWithBox)
  154. $sCell .= '<div class="bx-page-block-container bx-def-padding-sec-topbottom' . $sHiddenOn . '" id="bx-page-block-' . $aBlock['id'] . '">' . $sContentWithBox . '</div>';
  155. }
  156. $aVars[$sKey] = $sCell;
  157. }
  158. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  159. }
  160. /**
  161. * Get one block code only.
  162. * @return string
  163. */
  164. protected function _getBlockOnlyCode ($iBlockId)
  165. {
  166. $aBlock = $this->_oQuery->getPageBlock((int)$iBlockId);
  167. return $this->_getBlockCode($aBlock);
  168. }
  169. /**
  170. * Get block code.
  171. * @return string
  172. */
  173. protected function _getBlockCode($aBlock)
  174. {
  175. $sContentWithBox = '';
  176. $oFunctions = $this->_oTemplate->getTemplateFunctions();
  177. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  178. if ($this->_isVisibleBlock($aBlock)) {
  179. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  180. $mixedContent = $this->$sFunc($aBlock);
  181. $sTitle = $this->getBlockTitle($aBlock);
  182. if (is_array($mixedContent) && !empty($mixedContent['content'])) {
  183. $sContentWithBox = $oFunctions->designBoxContent(
  184. isset($mixedContent['title']) ? $mixedContent['title'] : $sTitle,
  185. $mixedContent['content'],
  186. isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'],
  187. isset($mixedContent['menu']) ? $mixedContent['menu'] : false
  188. );
  189. }
  190. elseif (is_string($mixedContent) && !empty($mixedContent)) {
  191. $sContentWithBox = $oFunctions->designBoxContent($sTitle, $mixedContent, $aBlock['designbox_id']);
  192. }
  193. }
  194. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  195. return $sContentWithBox;
  196. }
  197. /**
  198. * Add necessary js and css files.
  199. */
  200. protected function _addJsCss()
  201. {
  202. $this->_oTemplate->addCss('page_layouts.css');
  203. }
  204. /**
  205. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  206. */
  207. protected function _addSysTemplateVars ()
  208. {
  209. $sPageTitle = $this->_getPageTitle();
  210. if ($sPageTitle)
  211. BxDolTemplate::getInstance()->setPageHeader ($sPageTitle);
  212. $sMetaDesc = $this->_getPageMetaDesc();
  213. if ($sMetaDesc)
  214. BxDolTemplate::getInstance()->setPageDescription ($sMetaDesc);
  215. $sMetaKeywords = $this->_getPageMetaKeywords();
  216. if ($sMetaKeywords)
  217. BxDolTemplate::getInstance()->addPageKeywords ($sMetaKeywords);
  218. $sMetaRobots = $this->_getPageMetaRobots();
  219. if ($sMetaRobots)
  220. BxDolTemplate::getInstance()->setPageMetaRobots ($sMetaRobots);
  221. }
  222. /**
  223. * Select menu from page properties.
  224. */
  225. protected function _selectMenu ()
  226. {
  227. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  228. }
  229. /**
  230. * Set page submenu if it's specified
  231. */
  232. protected function _setSubmenu ()
  233. {
  234. if(empty($this->_aObject['submenu']))
  235. return;
  236. BxDolMenu::getObjectInstance('sys_site_submenu')->setObjectSubmenu($this->_aObject['submenu']);
  237. }
  238. /**
  239. * Get content for 'raw' block type.
  240. * @return string
  241. */
  242. protected function _getBlockRaw ($aBlock)
  243. {
  244. return '<div class="bx-page-raw-container">' . $this->_replaceMarkers(BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())) . '</div>';
  245. }
  246. /**
  247. * Get content for 'html' block type.
  248. * @return string
  249. */
  250. protected function _getBlockHtml ($aBlock)
  251. {
  252. return '<div class="bx-page-html-container">' . $this->_replaceMarkers($aBlock['content']) . '</div>';
  253. }
  254. /**
  255. * Get content for 'lang' block type.
  256. * @return string
  257. */
  258. protected function _getBlockLang ($aBlock)
  259. {
  260. return '<div class="bx-page-lang-container">' . $this->_replaceMarkers(_t(trim($aBlock['content']))) . '</div>';
  261. }
  262. /**
  263. * Get content for 'image' block type.
  264. * @return string
  265. */
  266. protected function _getBlockImage ($aBlock)
  267. {
  268. if (empty($aBlock['content']))
  269. return false;
  270. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  271. $iFileId = (int)$iFileId;
  272. if (!$iFileId)
  273. return false;
  274. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  275. if (!$oStorage)
  276. return false;
  277. $sUrl = $oStorage->getFileUrlById($iFileId);
  278. if (!$sUrl)
  279. return false;
  280. $sStyleAdd = '';
  281. if ('center' == $sAlign || 'left' == $sAlign || 'right' == $sAlign)
  282. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  283. return '<div class="bx-page-image-container" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  284. }
  285. /**
  286. * Get content for 'rss' block type.
  287. * @return string
  288. */
  289. protected function _getBlockRss ($aBlock)
  290. {
  291. if (empty($aBlock['content']))
  292. return false;
  293. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  294. $iNum = (int)$iNum;
  295. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  296. }
  297. /**
  298. * Get content for 'menu' block type.
  299. * @return string
  300. */
  301. protected function _getBlockMenu ($aBlock)
  302. {
  303. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  304. return $oMenu ? $oMenu->getCode () : '';
  305. }
  306. /**
  307. * Get content for 'service' block type.
  308. * @return string
  309. */
  310. protected function _getBlockService ($aBlock)
  311. {
  312. return BxDolService::callSerialized($aBlock['content'], $this->_aMarkers);
  313. }
  314. /**
  315. * Get page title.
  316. * @return string
  317. */
  318. protected function _getPageTitle()
  319. {
  320. return $this->_replaceMarkers(_t($this->_aObject['title']));
  321. }
  322. /**
  323. * Get page meta description.
  324. * @return string
  325. */
  326. protected function _getPageMetaDesc()
  327. {
  328. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  329. }
  330. /**
  331. * Get page meta keywords.
  332. * @return string
  333. */
  334. protected function _getPageMetaKeywords()
  335. {
  336. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  337. }
  338. /**
  339. * Get page meta robots.
  340. * @return string
  341. */
  342. protected function _getPageMetaRobots()
  343. {
  344. return _t($this->_aObject['meta_robots']);
  345. }
  346. /**
  347. * Get access denied message HTML.
  348. * @return string
  349. */
  350. protected function _getPageAccessDeniedMsg ()
  351. {
  352. return MsgBox(_t('_Access denied'));
  353. }
  354. /**
  355. * Get page cache object.
  356. * @return cache object instance
  357. */
  358. protected function _getPageCacheObject ()
  359. {
  360. if ($this->_oPageCacheObject != null) {
  361. return $this->_oPageCacheObject;
  362. } else {
  363. $sEngine = getParam('sys_page_cache_engine');
  364. $this->_oPageCacheObject = bx_instance ('BxDolCache' . $sEngine);
  365. if (!$this->_oPageCacheObject->isAvailable())
  366. $this->_oPageCacheObject = bx_instance ('BxDolCacheFile');
  367. return $this->_oPageCacheObject;
  368. }
  369. }
  370. /**
  371. * Get page cache key.
  372. * @param $isPrefixOnly return key prefix only.
  373. * @return string
  374. */
  375. protected function _getPageCacheKey ($isPrefixOnly = false)
  376. {
  377. $s = 'page_' . $this->_aObject['object'] . '_';
  378. if ($isPrefixOnly)
  379. return $s;
  380. $s .= $this->_getPageCacheParams ();
  381. $s .= bx_site_hash() . '.php';
  382. return $s;
  383. }
  384. /**
  385. * Additional cache key. In the case of dynamic page.
  386. * For example - profile page, where each profile has own page.
  387. * @return string
  388. */
  389. protected function _getPageCacheParams ()
  390. {
  391. return '';
  392. }
  393. /**
  394. * Clean page cache.
  395. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  396. * @return string
  397. */
  398. protected function cleanCache ($isDelAllWithPagePrefix = false)
  399. {
  400. $oCache = $this->_getPageCacheObject ();
  401. $sKey = $this->_getPageCacheKey($isDelAllWithPagePrefix);
  402. if ($isDelAllWithPagePrefix)
  403. return $oCache->removeAllByPrefix($sKey);
  404. else
  405. return $oCache->delData($sKey);
  406. }
  407. }
  408. /** @} */