BxBasePage.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. protected $_oBlockCacheObject = null;
  20. protected $_bStickyColumns = false;
  21. public function __construct ($aObject, $oTemplate)
  22. {
  23. parent::__construct ($aObject);
  24. if ($oTemplate)
  25. $this->_oTemplate = $oTemplate;
  26. else
  27. $this->_oTemplate = BxDolTemplate::getInstance();
  28. $this->_sStorage = 'sys_images';
  29. $this->_sJsClassName = 'BxDolPage';
  30. $this->_sJsObjectName = 'oBxDolPage';
  31. $this->_bStickyColumns = isset($this->_aObject['sticky_columns']) && $this->_aObject['sticky_columns'] == 1;
  32. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $this->_sObject);
  33. $this->_aHtmlIds = array(
  34. 'help_popup' => $sHtmlId . '-help-popup-',
  35. );
  36. }
  37. public function performActionGetHelp ()
  38. {
  39. $iBlockId = (int)bx_get('block_id');
  40. if(empty($iBlockId))
  41. return;
  42. $aBlock = $this->_oQuery->getPageBlock($iBlockId);
  43. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['help']))
  44. return;
  45. echo $this->_oTemplate->parseHtmlByName('designbox_help_popup.html', array(
  46. 'content' => _t($aBlock['help'])
  47. ));
  48. }
  49. public function performActionEmbed ()
  50. {
  51. $aCover = $this->getPageCoverImage();
  52. $this->_oTemplate->getEmbed($this->_oTemplate->parseHtmlByName('embed_card.html', [
  53. 'title' => _t($this->_aObject['title']),
  54. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  55. 'bx_if:thumb' => [
  56. 'condition' => $this->isPageCover() && count($aCover) > 0,
  57. 'content' => [
  58. 'title' => _t($this->_aObject['title']),
  59. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  60. 'background' => BxDolCover::getInstance(BxDolTemplate::getInstance())->getCoverImageUrl($aCover)
  61. ],
  62. ],
  63. 'bx_if:no_thumb' => [
  64. 'condition' => count($aCover) == 0 || !$this->isPageCover(),
  65. 'content' => [
  66. 'title' => _t($this->_aObject['title']),
  67. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  68. ]
  69. ]
  70. ]
  71. ));
  72. }
  73. /**
  74. * Very similar to BxBasePage::getCode
  75. * but adds css and js files which are needed for the corect page display
  76. */
  77. public function getCodeDynamic ()
  78. {
  79. // TODO: remake to use use collect* template methods
  80. $oTemplate = BxDolTemplate::getInstance();
  81. // get js&css before the page code is generated
  82. $aCssBefore = $oTemplate->getCss();
  83. $aJsBefore = $oTemplate->getJs();
  84. // generate page code
  85. $sContent = $this->getCode();
  86. // get js&css after the page code is generated
  87. $aCssAfter = $oTemplate->getCss();
  88. $aJsAfter = $oTemplate->getJs();
  89. // compare files which were added before and after page code is generated
  90. $f = function ($a1, $a2) {
  91. return strcasecmp($a1['url'], $a2['url']);
  92. };
  93. $aCssNew = array_udiff($aCssAfter, $aCssBefore, $f);
  94. $aJsNew = array_udiff($aJsAfter, $aJsBefore, $f);
  95. // add newly added js&css files in static mode
  96. $sCss = $sJs = '';
  97. foreach ($aCssNew as $a)
  98. $sCss .= $oTemplate->addCss($a['url'], true);
  99. foreach ($aJsNew as $a)
  100. $sJs .= $oTemplate->addJs($a['url'], true);
  101. return $sJs . $sCss . $sContent;
  102. }
  103. /**
  104. * Get page code with automatic caching, adding necessary css/js files and system template vars.
  105. * @return string.
  106. */
  107. public function getCode ()
  108. {
  109. if (bx_get('dynamic') && ($iBlockId = (int)bx_get('pageBlock'))) {
  110. if (!$this->_isVisiblePage($this->_aObject)) {
  111. header('HTTP/1.0 403 Forbidden');
  112. exit;
  113. }
  114. bx_alert('system', 'page_output_block', 0, false, array(
  115. 'page_name' => $this->_sObject,
  116. 'page_object' => $this,
  117. 'page_query' => $this->_oQuery,
  118. 'block_id' => (int)$iBlockId,
  119. ));
  120. header( 'Content-type:text/html;charset=utf-8' );
  121. BxDolTemplate::getInstance()->collectingStart();
  122. $s = $this->_getBlockOnlyCode($iBlockId);
  123. $sCss = bx_get('includedCss');
  124. $sJs = bx_get('includedJs');
  125. echo BxDolTemplate::getInstance()->collectingEndGetCode($sCss ? json_decode($sCss) : array(), $sJs ? json_decode($sJs) : array());
  126. echo $s;
  127. exit;
  128. }
  129. if (!$this->_isVisiblePage($this->_aObject))
  130. return $this->_getPageAccessDeniedMsg ();
  131. $this->_addJsCss();
  132. $this->_addSysTemplateVars();
  133. $this->_selectMenu();
  134. $this->_setSubmenu(array());
  135. if (!getParam('sys_page_cache_enable') || !$this->_aObject['cache_lifetime']) {
  136. $sPageCode = $this->_getPageCode();
  137. }
  138. else {
  139. $oCache = $this->_getPageCacheObject();
  140. $sKey = $this->_getPageCacheKey();
  141. $sKeyCssJs = 'cssjs_' . $sKey;
  142. $mixedRet = $oCache->getData($sKey, $this->_aObject['cache_lifetime']);
  143. $aRetCssJs = $oCache->getData($sKeyCssJs, $this->_aObject['cache_lifetime']);
  144. if ($mixedRet !== null && $aRetCssJs !== null) {
  145. BxDolTemplate::getInstance()->collectingStart();
  146. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  147. $sPageCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  148. $sPageCode .= $mixedRet;
  149. } else {
  150. BxDolTemplate::getInstance()->collectingStart();
  151. $sPageCode = $this->_getPageCode();
  152. $aPageCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  153. $oCache->setData($sKey, $sPageCode, $this->_aObject['cache_lifetime']);
  154. $oCache->setData($sKeyCssJs, $aPageCssJs, $this->_aObject['cache_lifetime']);
  155. }
  156. }
  157. bx_alert('system', 'page_output', 0, false, array(
  158. 'page_name' => $this->_sObject,
  159. 'page_object' => $this,
  160. 'page_query' => $this->_oQuery,
  161. 'page_code' => &$sPageCode,
  162. ));
  163. $sPageCode .= $this->getJsScript();
  164. return $sPageCode;
  165. }
  166. public function getJsClassName()
  167. {
  168. return $this->_sJsClassName;
  169. }
  170. public function getJsObjectName()
  171. {
  172. return $this->_sJsObjectName;
  173. }
  174. function _wrapInTagJsCode($sCode)
  175. {
  176. return "<script language=\"javascript\">\n<!--\n" . $sCode . "\n-->\n</script>";
  177. }
  178. public function getJsScript()
  179. {
  180. $sJsObjName = $this->getJsObjectName();
  181. $sJsObjClass = $this->getJsClassName();
  182. $sCode = "if(window['" . $sJsObjName . "'] == undefined) var " . $sJsObjName . " = new " . $sJsObjClass . "(" . json_encode(array(
  183. 'sObjName' => $sJsObjName,
  184. 'sObject' => $this->_sObject,
  185. 'sRootUrl' => BX_DOL_URL_ROOT,
  186. 'aHtmlIds' => $this->_aHtmlIds,
  187. 'isStickyColumns' => $this->_bStickyColumns
  188. )) . ");";
  189. return $this->_wrapInTagJsCode($sCode);
  190. }
  191. /**
  192. * Is page cover enabled.
  193. * @return string
  194. */
  195. public function isPageCover()
  196. {
  197. $bResult = false;
  198. switch((int)$this->_aObject['cover']) {
  199. case 1: //--- Enabled for all
  200. $bResult = true;
  201. break;
  202. case 2: //--- Enabled for visitors only
  203. $bResult = !isLogged();
  204. break;
  205. case 3: //--- Enabled for members only
  206. $bResult = isLogged();
  207. break;
  208. }
  209. return $bResult;
  210. }
  211. public function setPageCover($bCover = true)
  212. {
  213. $this->_aObject['cover'] = (bool)$bCover;
  214. }
  215. public function getPageCoverImage($bTranscoder = true)
  216. {
  217. $iId = (int)$this->_aObject['cover_image'];
  218. if(empty($iId)) {
  219. $iId = (int)getParam('sys_site_cover_common');
  220. if(empty($iId))
  221. return array();
  222. }
  223. $aResult = array(
  224. 'id' => $iId
  225. );
  226. if($bTranscoder)
  227. $aResult['transcoder'] = BX_DOL_TRANSCODER_OBJ_COVER;
  228. else
  229. $aResult['object'] = $this->_sStorage;
  230. return $aResult;
  231. }
  232. public function getPageCoverParams()
  233. {
  234. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  235. if($oMenuSubmenu) {
  236. $aParams = $oMenuSubmenu->getPageCoverParams();
  237. if(!empty($aParams) && is_array($aParams))
  238. return $aParams;
  239. }
  240. return array (
  241. 'title' => $this->_getPageTitle(),
  242. 'actions' => '',
  243. 'bx_if:image' => array (
  244. 'condition' => false,
  245. 'content' => array(),
  246. ),
  247. 'bx_if:icon' => array (
  248. 'condition' => false,
  249. 'content' => array(),
  250. ),
  251. );
  252. }
  253. /**
  254. * Get block title.
  255. * @return string
  256. */
  257. public function getBlockTitle ($aBlock)
  258. {
  259. return $this->_replaceMarkers(_t($aBlock['title']), array('block_id' => $aBlock['id']));
  260. }
  261. /**
  262. * Get help control if help is available for the block.
  263. * @return string
  264. */
  265. public function getBlockHelp ($aBlock)
  266. {
  267. if(empty($aBlock['help']))
  268. return '';
  269. return $this->_oTemplate->parseHtmlByName('designbox_help.html', array(
  270. 'js_object' => $this->_sJsObjectName,
  271. 'block_id' => $aBlock['id']
  272. ));
  273. }
  274. /**
  275. * Get code to load block asyncroniously
  276. * @param $aBlock block code
  277. * @param $iAsync if greater than 0 the it defines loading indicator for the block
  278. * @return HTML code string
  279. */
  280. public function getBlockAsyncCode($aBlock, $iAsync)
  281. {
  282. $aContentPlaceholder = $this->_oQuery->getPageBlockContentPlaceholder($iAsync);
  283. if (!$aContentPlaceholder)
  284. return _t('_sys_txt_error_occured');
  285. $oTemplate = $this->_oTemplate;
  286. if ('system' != $aContentPlaceholder['module']) {
  287. $oModule = BxDolModule::getInstance($aContentPlaceholder['module']);
  288. if (!$oModule)
  289. return _t('_sys_txt_error_occured');
  290. $oTemplate = $oModule->_oTemplate;
  291. }
  292. return $oTemplate->parseHtmlByName($aContentPlaceholder['template'], $aBlock);
  293. }
  294. /**
  295. * Get page array with all cells and blocks
  296. */
  297. public function getPage ()
  298. {
  299. return array(
  300. 'id' => $this->_aObject['id'],
  301. 'title' => $this->_getPageTitle(),
  302. 'uri' => $this->_aObject['uri'],
  303. 'author' => $this->_aObject['author'],
  304. 'added' => $this->_aObject['added'],
  305. 'module' => $this->getModule (),
  306. 'type' => $this->getType (),
  307. 'layout' => $this->_aObject['layout_id'],
  308. 'elements' => $this->getPageBlocks (),
  309. );
  310. }
  311. public function getPageBlocks ()
  312. {
  313. $aFieldsUnset = array('cell_id', 'active', 'copyable', 'deletable', 'object', 'text', 'text_updated', 'title_system', 'visible_for_levels');
  314. $aCells = $this->_oQuery->getPageBlocks();
  315. foreach ($aCells as $sKey => $aCell) {
  316. foreach ($aCell as $i => $aBlock) {
  317. if (!$this->_isVisibleBlock($aBlock))
  318. unset($aCells[$sKey][$i]);
  319. $this->processPageBlock($aCells[$sKey][$i], true);
  320. $aBlock = $aCells[$sKey][$i];
  321. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  322. $aCells[$sKey][$i]['content'] = method_exists($this, $sFunc) ? $this->$sFunc($aBlock) : $aBlock['content'];
  323. $aCells[$sKey][$i]['title'] = $this->getBlockTitle($aBlock);
  324. foreach ($aFieldsUnset as $s)
  325. unset($aCells[$sKey][$i][$s]);
  326. }
  327. }
  328. return $aCells;
  329. }
  330. /**
  331. * Get page code vars
  332. * @return string
  333. */
  334. protected function _getPageCodeVars ()
  335. {
  336. $aHiddenOn = array(
  337. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  338. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  339. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  340. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  341. );
  342. $aVars = array (
  343. 'page_id' => 'bx-page-' . $this->_aObject['uri'],
  344. 'bx_if:show_layout_row_dump' => array(
  345. 'condition' => false,
  346. 'content' => array()
  347. )
  348. );
  349. $aBlocks = $this->_oQuery->getPageBlocks();
  350. foreach ($aBlocks as $sKey => $aCell) {
  351. $sCell = '';
  352. foreach ($aCell as $aBlock) {
  353. $this->processPageBlock($aBlock, false);
  354. $sContentWithBox = $this->_getBlockCodeWithCache($aBlock, $aBlock['async']);
  355. $sHiddenOn = '';
  356. if(!empty($aBlock['hidden_on']))
  357. foreach($aHiddenOn as $iHiddenOn => $sClass)
  358. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  359. $sHiddenOn .= ' ' . $sClass;
  360. if ($sContentWithBox)
  361. $sCell .= $this->_oTemplate->parseHtmlByName('designbox_container.html', array(
  362. 'class_add' => $sHiddenOn,
  363. 'bx_if:show_html_id' => array(
  364. 'condition' => true,
  365. 'content' => array(
  366. 'html_id' => 'bx-page-block-' . $aBlock['id']
  367. ),
  368. ),
  369. 'content' => $sContentWithBox
  370. ));
  371. }
  372. $aVars[$sKey] = $sCell;
  373. }
  374. return $aVars;
  375. }
  376. /**
  377. * Process block values, especially if someting need to be overrided
  378. */
  379. protected function processPageBlock(&$aBlock, $bApi = false)
  380. {
  381. }
  382. /**
  383. * Get page code only.
  384. * @return string
  385. */
  386. protected function _getPageCode ()
  387. {
  388. $aVars = $this->_getPageCodeVars ();
  389. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  390. }
  391. /**
  392. * Get one block code only.
  393. * @return string
  394. */
  395. protected function _getBlockOnlyCode ($iBlockId)
  396. {
  397. if (!($aBlock = $this->_oQuery->getPageBlock((int)$iBlockId)))
  398. return '';
  399. return $this->_getBlockCodeWithCache($aBlock, 0);
  400. }
  401. protected function _getBlockCodeWithCache($aBlock, $iAsync = 0)
  402. {
  403. if (!getParam('sys_pb_cache_enable') || !$aBlock['cache_lifetime'] || $iAsync) {
  404. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  405. }
  406. else {
  407. $oCache = $this->_getBlockCacheObject();
  408. $sKey = $this->_getBlockCacheKey(false, $aBlock);
  409. $sKeyCssJs = 'cssjs_' . $sKey;
  410. $mixedRet = $oCache->getData($sKey, $aBlock['cache_lifetime']);
  411. $aRetCssJs = $oCache->getData($sKeyCssJs, $aBlock['cache_lifetime']);
  412. if ($mixedRet !== null && $aRetCssJs !== null) {
  413. BxDolTemplate::getInstance()->collectingStart();
  414. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  415. $sBlockCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  416. $sBlockCode .= $mixedRet;
  417. } else {
  418. BxDolTemplate::getInstance()->collectingStart();
  419. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  420. $aBlockCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  421. $oCache->setData($sKey, $sBlockCode, $aBlock['cache_lifetime']);
  422. $oCache->setData($sKeyCssJs, $aBlockCssJs, $aBlock['cache_lifetime']);
  423. }
  424. }
  425. return $sBlockCode;
  426. }
  427. /**
  428. * Get block code.
  429. * @return string
  430. */
  431. protected function _getBlockCode($aBlock, $iAsync = 0)
  432. {
  433. $sContentWithBox = '';
  434. $oFunctions = $this->_oTemplate->getTemplateFunctions();
  435. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  436. $aDbNoTitle = [BX_DB_CONTENT_ONLY, BX_DB_PADDING_CONTENT_ONLY, BX_DB_NO_CAPTION, BX_DB_PADDING_NO_CAPTION];
  437. $sTitle = $this->getBlockTitle($aBlock);
  438. $sHelp = $this->getBlockHelp($aBlock);
  439. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  440. $bBlockVisible = $this->_isVisibleBlock($aBlock);
  441. if ($iAsync && $bBlockVisible) {
  442. $iDesignboxId = $aBlock['designbox_id'];
  443. $sHelpTitle = $sHelpContent = '';
  444. if(!in_array($iDesignboxId, $aDbNoTitle))
  445. $sHelpTitle = $sHelp;
  446. else
  447. $sHelpContent = $sHelp;
  448. $sContent = $this->getBlockAsyncCode($aBlock, $iAsync);
  449. $aParams = array(
  450. $sTitle . $sHelpTitle,
  451. $sContent . $sHelpContent,
  452. $iDesignboxId
  453. );
  454. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  455. }
  456. elseif ($bBlockVisible && method_exists($this, $sFunc)) {
  457. $mixedContent = $this->$sFunc($aBlock);
  458. $this->_oQuery->setReadOnlyMode(true);
  459. if(is_array($mixedContent) && !empty($mixedContent['content'])) {
  460. $iDesignboxId = isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'];
  461. $sHelpTitle = $sHelpContent = '';
  462. if(!in_array($iDesignboxId, $aDbNoTitle))
  463. $sHelpTitle = $sHelp;
  464. else
  465. $sHelpContent = $sHelp;
  466. $aParams = array(
  467. (isset($mixedContent['title']) ? $mixedContent['title'] : $sTitle) . $sHelpTitle,
  468. $mixedContent['content'] . $sHelpContent,
  469. $iDesignboxId
  470. );
  471. $mixedMenu = false;
  472. if(isset($mixedContent['menu']))
  473. $mixedMenu = $mixedContent['menu'];
  474. else if(!empty($aBlock['submenu']))
  475. $mixedMenu = $aBlock['submenu'];
  476. $aParams[] = $mixedMenu;
  477. if(isset($mixedContent['buttons']))
  478. $aParams[] = $mixedContent['buttons'];
  479. else if($mixedMenu)
  480. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  481. }
  482. else if(is_string($mixedContent) && !empty($mixedContent)) {
  483. $iDesignboxId = $aBlock['designbox_id'];
  484. $sHelpTitle = $sHelpContent = '';
  485. if(!in_array($iDesignboxId, $aDbNoTitle))
  486. $sHelpTitle = $sHelp;
  487. else
  488. $sHelpContent = $sHelp;
  489. $aParams = array(
  490. $sTitle . $sHelpTitle,
  491. $mixedContent . $sHelpContent,
  492. $iDesignboxId
  493. );
  494. $mixedMenu = !empty($aBlock['submenu']) ? $aBlock['submenu'] : false;
  495. $aParams[] = $mixedMenu;
  496. if($mixedMenu)
  497. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  498. }
  499. if(!empty($aParams))
  500. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  501. }
  502. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  503. return $sContentWithBox;
  504. }
  505. /**
  506. * Add necessary js and css files.
  507. */
  508. protected function _addJsCss()
  509. {
  510. $this->_oTemplate->addJs(array('BxDolPage.js', 'theia-sticky-sidebar/ResizeSensor.min.js', 'theia-sticky-sidebar/theia-sticky-sidebar.min.js'));
  511. $this->_oTemplate->addCss('page_layouts.css');
  512. }
  513. /**
  514. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  515. */
  516. protected function _addSysTemplateVars ()
  517. {
  518. $oTemplate = BxDolTemplate::getInstance();
  519. $sPageTitle = $this->_getPageTitle();
  520. if ($sPageTitle)
  521. $oTemplate->setPageHeader ($sPageTitle);
  522. $sMetaDesc = $this->_getPageMetaDesc();
  523. if ($sMetaDesc)
  524. $oTemplate->setPageDescription ($sMetaDesc);
  525. $sMetaRobots = $this->_getPageMetaRobots();
  526. if ($sMetaRobots)
  527. $oTemplate->setPageMetaRobots ($sMetaRobots);
  528. $sMetaImage = $this->_getPageMetaImage();
  529. if ($sMetaImage)
  530. $oTemplate->addPageMetaImage($sMetaImage);
  531. $sMetaKeywords = $this->_getPageMetaKeywords();
  532. if ($sMetaKeywords)
  533. $oTemplate->addPageKeywords ($sMetaKeywords);
  534. }
  535. /**
  536. * Select menu from page properties.
  537. */
  538. protected function _selectMenu ()
  539. {
  540. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  541. }
  542. /**
  543. * Set page submenu if it's specified
  544. */
  545. protected function _setSubmenu ($aParams)
  546. {
  547. if(empty($this->_aObject['submenu']) || $this->_aObject['submenu'] == 'disabled')
  548. return;
  549. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  550. if(!$oMenuSubmenu)
  551. return;
  552. $oMenuSubmenu->setObjectSubmenu($this->_aObject['submenu'], $aParams);
  553. }
  554. /**
  555. * Get content for 'raw' block type.
  556. * @return string
  557. */
  558. protected function _getBlockRaw ($aBlock)
  559. {
  560. $s = '<div class="bx-page-raw-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  561. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  562. $s = bx_process_macros($s);
  563. return $s;
  564. }
  565. /**
  566. * Get content for 'custom' block type.
  567. * @return string
  568. */
  569. protected function _getBlockCustom ($aBlock)
  570. {
  571. $s = '<div class="bx-page-custom-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  572. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  573. $s = bx_process_macros($s);
  574. return $s;
  575. }
  576. /**
  577. * Get content for 'html' block type.
  578. * @return string
  579. */
  580. protected function _getBlockHtml ($aBlock)
  581. {
  582. $s = '<div class="bx-page-html-container">' . $aBlock['content'] . '</div>';
  583. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  584. $s = bx_process_macros($s);
  585. return $s;
  586. }
  587. /**
  588. * Get content for 'wiki' block type.
  589. * @return string
  590. */
  591. protected function _getBlockWiki ($aBlock)
  592. {
  593. $oWiki = BxDolWiki::getObjectInstance($this->_aObject['module']);
  594. if (!$oWiki) {
  595. $sContent = _t('_sys_wiki_error_missing_wiki_object', $this->_aObject['module']);
  596. }
  597. else {
  598. $sContent = $oWiki->getBlockContent($aBlock['id'], false, (int)bx_get($aBlock['id'].'rev') ? (int)bx_get($aBlock['id'].'rev') : false);
  599. }
  600. $s = '<div id="bx-page-wiki-container-' . $aBlock['id'] . '" class="bx-page-wiki-container markdown-body">' . $sContent . '</div>';
  601. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  602. $s = bx_process_macros($s);
  603. return $s;
  604. }
  605. /**
  606. * Get content for 'lang' block type.
  607. * @return string
  608. */
  609. protected function _getBlockLang ($aBlock)
  610. {
  611. $s = '<div class="bx-page-lang-container">' . _t(trim($aBlock['content'])) . '</div>';
  612. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  613. $s = bx_process_macros($s);
  614. return $s;
  615. }
  616. /**
  617. * Get content for 'image' block type.
  618. * @return string
  619. */
  620. protected function _getBlockImage ($aBlock)
  621. {
  622. if (empty($aBlock['content']))
  623. return false;
  624. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  625. $iFileId = (int)$iFileId;
  626. if (!$iFileId)
  627. return false;
  628. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  629. if (!$oStorage)
  630. return false;
  631. $sUrl = $oStorage->getFileUrlById($iFileId);
  632. if (!$sUrl)
  633. return false;
  634. $sStyleAdd = '';
  635. if ('center' == $sAlign || 'left' == $sAlign || 'right' == $sAlign)
  636. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  637. return '<div class="bx-page-image-container" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  638. }
  639. /**
  640. * Get content for 'rss' block type.
  641. * @return string
  642. */
  643. protected function _getBlockRss ($aBlock)
  644. {
  645. if (empty($aBlock['content']))
  646. return false;
  647. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  648. $iNum = (int)$iNum;
  649. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  650. }
  651. /**
  652. * Get content for 'menu' block type.
  653. * @return string
  654. */
  655. protected function _getBlockMenu ($aBlock)
  656. {
  657. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  658. return $oMenu ? $oMenu->getCode () : '';
  659. }
  660. /**
  661. * Get content for 'service' block type.
  662. * @return string
  663. */
  664. protected function _getBlockService ($aBlock)
  665. {
  666. return BxDolService::callSerialized($aBlock['content'], $this->_aMarkers);
  667. }
  668. /**
  669. * Get page title.
  670. * @return string
  671. */
  672. protected function _getPageTitle()
  673. {
  674. return $this->_replaceMarkers(_t($this->_aObject['title']));
  675. }
  676. /**
  677. * Get page meta description.
  678. * @return string
  679. */
  680. protected function _getPageMetaDesc()
  681. {
  682. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  683. }
  684. /**
  685. * Get page meta image.
  686. * @return string
  687. */
  688. protected function _getPageMetaImage()
  689. {
  690. return '';
  691. }
  692. /**
  693. * Get page meta keywords.
  694. * @return string
  695. */
  696. protected function _getPageMetaKeywords()
  697. {
  698. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  699. }
  700. /**
  701. * Get page meta robots.
  702. * @return string
  703. */
  704. protected function _getPageMetaRobots()
  705. {
  706. return _t($this->_aObject['meta_robots']);
  707. }
  708. /**
  709. * Get access denied message HTML.
  710. * @return string
  711. */
  712. protected function _getPageAccessDeniedMsg ()
  713. {
  714. return MsgBox(_t('_Access denied'));
  715. }
  716. /**
  717. * Get page cache object.
  718. * @return cache object instance
  719. */
  720. protected function _getPageCacheObject ()
  721. {
  722. return $this->_getCacheObject ();
  723. }
  724. /**
  725. * Get block cache object.
  726. * @return cache object instance
  727. */
  728. protected function _getBlockCacheObject ()
  729. {
  730. return $this->_getCacheObject ('Block', 'pb');
  731. }
  732. protected function _getCacheObject ($sSuffixObj = 'Page', $sSuffixParam = 'page')
  733. {
  734. $sObj = '_o' . $sSuffixObj . 'CacheObject';
  735. $sParam = 'sys_' . $sSuffixParam . '_cache_engine';
  736. if ($this->{$sObj} != null) {
  737. return $this->{$sObj};
  738. } else {
  739. $sEngine = getParam($sParam);
  740. $this->{$sObj} = bx_instance ('BxDolCache' . $sEngine);
  741. if (!$this->{$sObj}->isAvailable())
  742. $this->{$sObj} = bx_instance ('BxDolCacheFile');
  743. return $this->{$sObj};
  744. }
  745. }
  746. /**
  747. * Get page cache key.
  748. * @param $isPrefixOnly return key prefix only.
  749. * @return string
  750. */
  751. protected function _getPageCacheKey ($isPrefixOnly = false)
  752. {
  753. $s = 'page_' . $this->_aObject['object'] . '_';
  754. if ($isPrefixOnly)
  755. return $s;
  756. $s .= $this->_getPageCacheParams ();
  757. $s .= bx_site_hash() . '.php';
  758. return $s;
  759. }
  760. /**
  761. * Get block cache key.
  762. * @param $isPrefixOnly return key prefix only.
  763. * @return string
  764. */
  765. protected function _getBlockCacheKey ($isPrefixOnly = false, $aBlock = array())
  766. {
  767. $s = $this->_getPageCacheKey(true) . 'block_';
  768. if ($isPrefixOnly)
  769. return $s;
  770. $s .= ($aBlock ? $aBlock['id'] : 0) . '_' . $this->_getPageCacheParams ();
  771. $s .= bx_site_hash() . '.php';
  772. return $s;
  773. }
  774. /**
  775. * Additional cache key. In the case of dynamic page.
  776. * For example - profile page, where each profile has own page.
  777. * @return string
  778. */
  779. protected function _getPageCacheParams ()
  780. {
  781. return '';
  782. }
  783. /**
  784. * Clean page cache.
  785. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  786. * @return string
  787. */
  788. protected function cleanCache ($isDelAllWithPagePrefix = false)
  789. {
  790. $a = ['Page', 'Block'];
  791. $bRet = false;
  792. foreach ($a as $s) {
  793. $oCache = $this->{'_get' . $s . 'CacheObject'} ();
  794. $sKey = $this->{'_get'. $s . 'CacheKey'} ($isDelAllWithPagePrefix);
  795. if ($isDelAllWithPagePrefix)
  796. $bRet = $bRet && $oCache->removeAllByPrefix($sKey);
  797. else
  798. $bRet = $bRet && $oCache->delData($sKey);
  799. }
  800. return $bRet;
  801. }
  802. }
  803. /** @} */