BxBasePage.php 16 KB

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