BxBasePage.php 32 KB

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