BxBasePage.php 14 KB

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