BxBasePage.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. $bResult = false;
  113. switch((int)$this->_aObject['cover']) {
  114. case 1: //--- Enabled for all
  115. $bResult = true;
  116. break;
  117. case 2: //--- Enabled for visitors only
  118. $bResult = !isLogged();
  119. break;
  120. case 3: //--- Enabled for members only
  121. $bResult = isLogged();
  122. break;
  123. }
  124. return $bResult;
  125. }
  126. public function setPageCover($bCover = true)
  127. {
  128. $this->_aObject['cover'] = (bool)$bCover;
  129. }
  130. public function getPageCoverImage($bTranscoder = true)
  131. {
  132. $iId = (int)$this->_aObject['cover_image'];
  133. if(empty($iId)) {
  134. $iId = (int)getParam('sys_site_cover_common');
  135. if(empty($iId))
  136. return array();
  137. }
  138. $aResult = array(
  139. 'id' => $iId
  140. );
  141. if($bTranscoder)
  142. $aResult['transcoder'] = BX_DOL_TRANSCODER_OBJ_COVER;
  143. else
  144. $aResult['object'] = $this->_sStorage;
  145. return $aResult;
  146. }
  147. public function getPageCoverParams()
  148. {
  149. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  150. if($oMenuSubmenu) {
  151. $aParams = $oMenuSubmenu->getPageCoverParams();
  152. if(!empty($aParams) && is_array($aParams))
  153. return $aParams;
  154. }
  155. return array (
  156. 'title' => $this->_getPageTitle(),
  157. 'actions' => '',
  158. 'bx_if:image' => array (
  159. 'condition' => false,
  160. 'content' => array(),
  161. ),
  162. 'bx_if:icon' => array (
  163. 'condition' => false,
  164. 'content' => array(),
  165. ),
  166. );
  167. }
  168. /**
  169. * Get block title.
  170. * @return string
  171. */
  172. public function getBlockTitle ($aBlock)
  173. {
  174. return $this->_replaceMarkers(_t($aBlock['title']), array('block_id' => $aBlock['id']));
  175. }
  176. /**
  177. * Get page array with all cells and blocks
  178. */
  179. public function getPage ()
  180. {
  181. return array(
  182. 'id' => $this->_aObject['id'],
  183. 'title' => $this->_getPageTitle(),
  184. 'uri' => $this->_aObject['uri'],
  185. 'author' => $this->_aObject['author'],
  186. 'added' => $this->_aObject['added'],
  187. 'module' => $this->getModule (),
  188. 'type' => $this->getType (),
  189. 'layout' => $this->_aObject['layout_id'],
  190. 'elements' => $this->getPageBlocks (),
  191. );
  192. }
  193. public function getPageBlocks ()
  194. {
  195. $aFieldsUnset = array('cell_id', 'active', 'copyable', 'deletable', 'object', 'text', 'text_updated', 'title_system', 'visible_for_levels');
  196. $aCells = $this->_oQuery->getPageBlocks();
  197. foreach ($aCells as $sKey => $aCell) {
  198. foreach ($aCell as $i => $aBlock) {
  199. if (!$this->_isVisibleBlock($aBlock))
  200. unset($aCells[$sKey][$i]);
  201. $this->processPageBlock($aCells[$sKey][$i], true);
  202. $aBlock = $aCells[$sKey][$i];
  203. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  204. $aCells[$sKey][$i]['content'] = method_exists($this, $sFunc) ? $this->$sFunc($aBlock) : $aBlock['content'];
  205. $aCells[$sKey][$i]['title'] = $this->getBlockTitle($aBlock);
  206. foreach ($aFieldsUnset as $s)
  207. unset($aCells[$sKey][$i][$s]);
  208. }
  209. }
  210. return $aCells;
  211. }
  212. /**
  213. * Get page code vars
  214. * @return string
  215. */
  216. protected function _getPageCodeVars ()
  217. {
  218. $aHiddenOn = array(
  219. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  220. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  221. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  222. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  223. );
  224. $aVars = array (
  225. 'page_id' => 'bx-page-' . $this->_aObject['uri'],
  226. );
  227. $aBlocks = $this->_oQuery->getPageBlocks();
  228. foreach ($aBlocks as $sKey => $aCell) {
  229. $sCell = '';
  230. foreach ($aCell as $aBlock) {
  231. $this->processPageBlock($aBlock, false);
  232. $sContentWithBox = $this->_getBlockCode($aBlock);
  233. $sHiddenOn = '';
  234. if(!empty($aBlock['hidden_on']))
  235. foreach($aHiddenOn as $iHiddenOn => $sClass)
  236. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  237. $sHiddenOn .= ' ' . $sClass;
  238. if ($sContentWithBox)
  239. $sCell .= '<div class="bx-page-block-container bx-def-padding-sec-topbottom' . $sHiddenOn . '" id="bx-page-block-' . $aBlock['id'] . '">' . $sContentWithBox . '</div>';
  240. }
  241. $aVars[$sKey] = $sCell;
  242. }
  243. return $aVars;
  244. }
  245. /**
  246. * Process block values, especially if someting need to be overrided
  247. */
  248. protected function processPageBlock(&$aBlock, $bApi = false)
  249. {
  250. }
  251. /**
  252. * Get page code only.
  253. * @return string
  254. */
  255. protected function _getPageCode ()
  256. {
  257. $aVars = $this->_getPageCodeVars ();
  258. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  259. }
  260. /**
  261. * Get one block code only.
  262. * @return string
  263. */
  264. protected function _getBlockOnlyCode ($iBlockId)
  265. {
  266. if (!($aBlock = $this->_oQuery->getPageBlock((int)$iBlockId)))
  267. return '';
  268. return $this->_getBlockCode($aBlock);
  269. }
  270. /**
  271. * Get block code.
  272. * @return string
  273. */
  274. protected function _getBlockCode($aBlock)
  275. {
  276. $sContentWithBox = '';
  277. $oFunctions = $this->_oTemplate->getTemplateFunctions();
  278. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  279. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  280. if ($this->_isVisibleBlock($aBlock) && method_exists($this, $sFunc)) {
  281. $mixedContent = $this->$sFunc($aBlock);
  282. $sTitle = $this->getBlockTitle($aBlock);
  283. if (is_array($mixedContent) && !empty($mixedContent['content'])) {
  284. $sContentWithBox = $oFunctions->designBoxContent(
  285. isset($mixedContent['title']) ? $mixedContent['title'] : $sTitle,
  286. $mixedContent['content'],
  287. isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'],
  288. isset($mixedContent['menu']) ? $mixedContent['menu'] : false
  289. );
  290. }
  291. elseif (is_string($mixedContent) && !empty($mixedContent)) {
  292. $sContentWithBox = $oFunctions->designBoxContent($sTitle, $mixedContent, $aBlock['designbox_id']);
  293. }
  294. }
  295. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  296. return $sContentWithBox;
  297. }
  298. /**
  299. * Add necessary js and css files.
  300. */
  301. protected function _addJsCss()
  302. {
  303. $this->_oTemplate->addCss('page_layouts.css');
  304. }
  305. /**
  306. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  307. */
  308. protected function _addSysTemplateVars ()
  309. {
  310. $oTemplate = BxDolTemplate::getInstance();
  311. $sPageTitle = $this->_getPageTitle();
  312. if ($sPageTitle)
  313. $oTemplate->setPageHeader ($sPageTitle);
  314. $sMetaDesc = $this->_getPageMetaDesc();
  315. if ($sMetaDesc)
  316. $oTemplate->setPageDescription ($sMetaDesc);
  317. $sMetaRobots = $this->_getPageMetaRobots();
  318. if ($sMetaRobots)
  319. $oTemplate->setPageMetaRobots ($sMetaRobots);
  320. $sMetaImage = $this->_getPageMetaImage();
  321. if ($sMetaImage)
  322. $oTemplate->addPageMetaImage($sMetaImage);
  323. $sMetaKeywords = $this->_getPageMetaKeywords();
  324. if ($sMetaKeywords)
  325. $oTemplate->addPageKeywords ($sMetaKeywords);
  326. }
  327. /**
  328. * Select menu from page properties.
  329. */
  330. protected function _selectMenu ()
  331. {
  332. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  333. }
  334. /**
  335. * Set page submenu if it's specified
  336. */
  337. protected function _setSubmenu ($aParams)
  338. {
  339. if(empty($this->_aObject['submenu']))
  340. return;
  341. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  342. if(!$oMenuSubmenu)
  343. return;
  344. $oMenuSubmenu->setObjectSubmenu($this->_aObject['submenu'], $aParams);
  345. }
  346. /**
  347. * Get content for 'raw' block type.
  348. * @return string
  349. */
  350. protected function _getBlockRaw ($aBlock)
  351. {
  352. $s = '<div class="bx-page-raw-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  353. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  354. $s = bx_process_macros($s);
  355. return $s;
  356. }
  357. /**
  358. * Get content for 'custom' block type.
  359. * @return string
  360. */
  361. protected function _getBlockCustom ($aBlock)
  362. {
  363. $s = '<div class="bx-page-custom-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  364. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  365. $s = bx_process_macros($s);
  366. return $s;
  367. }
  368. /**
  369. * Get content for 'html' block type.
  370. * @return string
  371. */
  372. protected function _getBlockHtml ($aBlock)
  373. {
  374. $s = '<div class="bx-page-html-container">' . $aBlock['content'] . '</div>';
  375. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  376. $s = bx_process_macros($s);
  377. return $s;
  378. }
  379. /**
  380. * Get content for 'wiki' block type.
  381. * @return string
  382. */
  383. protected function _getBlockWiki ($aBlock)
  384. {
  385. $oWiki = BxDolWiki::getObjectInstance($this->_aObject['module']);
  386. if (!$oWiki) {
  387. $sContent = _t('_sys_wiki_error_missing_wiki_object', $this->_aObject['module']);
  388. }
  389. else {
  390. $sContent = $oWiki->getBlockContent($aBlock['id'], false, (int)bx_get($aBlock['id'].'rev') ? (int)bx_get($aBlock['id'].'rev') : false);
  391. }
  392. $s = '<div id="bx-page-wiki-container-' . $aBlock['id'] . '" class="bx-page-wiki-container">' . $sContent . '</div>';
  393. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  394. $s = bx_process_macros($s);
  395. return $s;
  396. }
  397. /**
  398. * Get content for 'lang' block type.
  399. * @return string
  400. */
  401. protected function _getBlockLang ($aBlock)
  402. {
  403. $s = '<div class="bx-page-lang-container">' . _t(trim($aBlock['content'])) . '</div>';
  404. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  405. $s = bx_process_macros($s);
  406. return $s;
  407. }
  408. /**
  409. * Get content for 'image' block type.
  410. * @return string
  411. */
  412. protected function _getBlockImage ($aBlock)
  413. {
  414. if (empty($aBlock['content']))
  415. return false;
  416. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  417. $iFileId = (int)$iFileId;
  418. if (!$iFileId)
  419. return false;
  420. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  421. if (!$oStorage)
  422. return false;
  423. $sUrl = $oStorage->getFileUrlById($iFileId);
  424. if (!$sUrl)
  425. return false;
  426. $sStyleAdd = '';
  427. if ('center' == $sAlign || 'left' == $sAlign || 'right' == $sAlign)
  428. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  429. return '<div class="bx-page-image-container" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  430. }
  431. /**
  432. * Get content for 'rss' block type.
  433. * @return string
  434. */
  435. protected function _getBlockRss ($aBlock)
  436. {
  437. if (empty($aBlock['content']))
  438. return false;
  439. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  440. $iNum = (int)$iNum;
  441. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  442. }
  443. /**
  444. * Get content for 'menu' block type.
  445. * @return string
  446. */
  447. protected function _getBlockMenu ($aBlock)
  448. {
  449. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  450. return $oMenu ? $oMenu->getCode () : '';
  451. }
  452. /**
  453. * Get content for 'service' block type.
  454. * @return string
  455. */
  456. protected function _getBlockService ($aBlock)
  457. {
  458. return BxDolService::callSerialized($aBlock['content'], $this->_aMarkers);
  459. }
  460. /**
  461. * Get page title.
  462. * @return string
  463. */
  464. protected function _getPageTitle()
  465. {
  466. return $this->_replaceMarkers(_t($this->_aObject['title']));
  467. }
  468. /**
  469. * Get page meta description.
  470. * @return string
  471. */
  472. protected function _getPageMetaDesc()
  473. {
  474. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  475. }
  476. /**
  477. * Get page meta image.
  478. * @return string
  479. */
  480. protected function _getPageMetaImage()
  481. {
  482. return '';
  483. }
  484. /**
  485. * Get page meta keywords.
  486. * @return string
  487. */
  488. protected function _getPageMetaKeywords()
  489. {
  490. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  491. }
  492. /**
  493. * Get page meta robots.
  494. * @return string
  495. */
  496. protected function _getPageMetaRobots()
  497. {
  498. return _t($this->_aObject['meta_robots']);
  499. }
  500. /**
  501. * Get access denied message HTML.
  502. * @return string
  503. */
  504. protected function _getPageAccessDeniedMsg ()
  505. {
  506. return MsgBox(_t('_Access denied'));
  507. }
  508. /**
  509. * Get page cache object.
  510. * @return cache object instance
  511. */
  512. protected function _getPageCacheObject ()
  513. {
  514. if ($this->_oPageCacheObject != null) {
  515. return $this->_oPageCacheObject;
  516. } else {
  517. $sEngine = getParam('sys_page_cache_engine');
  518. $this->_oPageCacheObject = bx_instance ('BxDolCache' . $sEngine);
  519. if (!$this->_oPageCacheObject->isAvailable())
  520. $this->_oPageCacheObject = bx_instance ('BxDolCacheFile');
  521. return $this->_oPageCacheObject;
  522. }
  523. }
  524. /**
  525. * Get page cache key.
  526. * @param $isPrefixOnly return key prefix only.
  527. * @return string
  528. */
  529. protected function _getPageCacheKey ($isPrefixOnly = false)
  530. {
  531. $s = 'page_' . $this->_aObject['object'] . '_';
  532. if ($isPrefixOnly)
  533. return $s;
  534. $s .= $this->_getPageCacheParams ();
  535. $s .= bx_site_hash() . '.php';
  536. return $s;
  537. }
  538. /**
  539. * Additional cache key. In the case of dynamic page.
  540. * For example - profile page, where each profile has own page.
  541. * @return string
  542. */
  543. protected function _getPageCacheParams ()
  544. {
  545. return '';
  546. }
  547. /**
  548. * Clean page cache.
  549. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  550. * @return string
  551. */
  552. protected function cleanCache ($isDelAllWithPagePrefix = false)
  553. {
  554. $oCache = $this->_getPageCacheObject ();
  555. $sKey = $this->_getPageCacheKey($isDelAllWithPagePrefix);
  556. if ($isDelAllWithPagePrefix)
  557. return $oCache->removeAllByPrefix($sKey);
  558. else
  559. return $oCache->delData($sKey);
  560. }
  561. }
  562. /** @} */