BxBasePage.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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 getPageAPI ()
  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->getPageBlocksAPI(),
  343. 'menu_top' => '',
  344. 'menu' => '',
  345. 'menu_bottom' => ''
  346. ];
  347. if (($oMenuTop = BxDolMenu::getObjectInstance('sys_site')) !== false)
  348. $a['menu_top'] = $oMenuTop->getCodeAPI();
  349. if (($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  350. if(($sSubmenu = $this->getSubMenu()) !== '')
  351. $oMenuSubmenu->setObjectSubmenu($sSubmenu, []);
  352. $a['menu'] = $oMenuSubmenu->getCodeAPI();
  353. }
  354. if (isLogged()) {
  355. $o = BxDolProfile::getInstance();
  356. $a['user'] = [
  357. 'id' => $o->id(),
  358. 'display_name' => $o->getDisplayName(),
  359. 'url' => $o->getUrl(),
  360. 'avatar' => $o->getAvatar(),
  361. 'info' => $o->getInfo(),
  362. 'active' => $o->isActive(),
  363. 'status' => $o->getStatus(),
  364. ];
  365. }
  366. return $a;
  367. }
  368. public function getPageBlocksAPI()
  369. {
  370. $aFieldsUnset = ['cell_id', 'active', 'copyable', 'deletable', 'object', 'text', 'text_updated', 'title_system', 'visible_for_levels'];
  371. $aCells = $this->_oQuery->getPageBlocks();
  372. foreach($aCells as $sKey => &$aCell) {
  373. foreach($aCell as $i => $aBlock) {
  374. if(!$this->_isVisibleBlock($aBlock) || ($aCells[$sKey][$i]['hidden_on'] > 0 && ((int)$aCells[$sKey][$i]['hidden_on'] & 8) > 0)) {
  375. unset($aCells[$sKey][$i]);
  376. continue;
  377. }
  378. $this->processPageBlock($aCells[$sKey][$i], true);
  379. $aBlock = $aCells[$sKey][$i];
  380. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  381. $mBlock = method_exists($this, $sFunc) ? $this->$sFunc($aBlock) : $aBlock['content'];
  382. $aCells[$sKey][$i]['title'] = isset($mBlock['title']) ? $mBlock['title'] : $this->getBlockTitle($aBlock);
  383. $aCells[$sKey][$i]['content'] = isset($mBlock['content']) ? $mBlock['content'] : $mBlock;
  384. $aCells[$sKey][$i]['menu'] = isset($mBlock['menu']) ? $mBlock['menu'] : '';
  385. $aCells[$sKey][$i] = array_diff_key($aCells[$sKey][$i], array_flip($aFieldsUnset));
  386. }
  387. }
  388. return array_map('array_values', $aCells);
  389. }
  390. /**
  391. * Get page code vars
  392. * @return string
  393. */
  394. protected function _getPageCodeVars ()
  395. {
  396. $aHiddenOn = array(
  397. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  398. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  399. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  400. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  401. );
  402. $aVars = array (
  403. 'page_id' => 'bx-page-' . $this->_aObject['uri'],
  404. 'bx_if:show_layout_row_dump' => array(
  405. 'condition' => false,
  406. 'content' => array()
  407. )
  408. );
  409. $aBlocks = $this->_oQuery->getPageBlocks();
  410. foreach ($aBlocks as $sKey => $aCell) {
  411. $sCell = '';
  412. foreach ($aCell as $aBlock) {
  413. $this->processPageBlock($aBlock, false);
  414. $sContentWithBox = $this->_getBlockCodeWithCache($aBlock, $aBlock['async']);
  415. $sClassAdd = '';
  416. if(!empty($aBlock['class']))
  417. $sClassAdd .= ' ' . $aBlock['class'];
  418. if(!empty($aBlock['hidden_on']))
  419. foreach($aHiddenOn as $iHiddenOn => $sHiddenOnClass)
  420. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  421. $sClassAdd .= ' ' . $sHiddenOnClass;
  422. if ($sContentWithBox)
  423. $sCell .= $this->_oTemplate->parseHtmlByName('designbox_container.html', array(
  424. 'class_add' => $sClassAdd,
  425. 'bx_if:show_html_id' => array(
  426. 'condition' => true,
  427. 'content' => array(
  428. 'html_id' => 'bx-page-block-' . $aBlock['id']
  429. ),
  430. ),
  431. 'content' => $sContentWithBox
  432. ));
  433. }
  434. $aVars[$sKey] = $sCell;
  435. }
  436. return $aVars;
  437. }
  438. /**
  439. * Process block values, especially if someting need to be overrided
  440. */
  441. protected function processPageBlock(&$aBlock, $bApi = false)
  442. {
  443. }
  444. /**
  445. * Get page code only.
  446. * @return string
  447. */
  448. protected function _getPageCode ()
  449. {
  450. $aVars = $this->_getPageCodeVars ();
  451. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  452. }
  453. /**
  454. * Get one block code only.
  455. * @return string
  456. */
  457. protected function _getBlockOnlyCode ($iBlockId)
  458. {
  459. if (!($aBlock = $this->_oQuery->getPageBlock((int)$iBlockId)))
  460. return '';
  461. return $this->_getBlockCodeWithCache($aBlock, 0);
  462. }
  463. protected function _getBlockCodeWithCache(&$aBlock, $iAsync = 0)
  464. {
  465. if (!getParam('sys_pb_cache_enable') || !$aBlock['cache_lifetime'] || $iAsync || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  466. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  467. }
  468. else {
  469. $oCache = $this->_getBlockCacheObject();
  470. $sKey = $this->_getBlockCacheKey(false, $aBlock);
  471. $sKeyCssJs = 'cssjs_' . $sKey;
  472. $mixedRet = $oCache->getData($sKey, $aBlock['cache_lifetime']);
  473. $aRetCssJs = $oCache->getData($sKeyCssJs, $aBlock['cache_lifetime']);
  474. if ($mixedRet !== null && $aRetCssJs !== null) {
  475. BxDolTemplate::getInstance()->collectingStart();
  476. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  477. $sBlockCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  478. $sBlockCode .= $mixedRet;
  479. } else {
  480. BxDolTemplate::getInstance()->collectingStart();
  481. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  482. $aBlockCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  483. $oCache->setData($sKey, $sBlockCode, $aBlock['cache_lifetime']);
  484. $oCache->setData($sKeyCssJs, $aBlockCssJs, $aBlock['cache_lifetime']);
  485. }
  486. }
  487. return $sBlockCode;
  488. }
  489. /**
  490. * Get block code.
  491. * @return string
  492. */
  493. protected function _getBlockCode(&$aBlock, $iAsync = 0)
  494. {
  495. $sContentWithBox = '';
  496. $oFunctions = $this->_oTemplate->getTemplateFunctions();
  497. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  498. $aDbNoTitle = [BX_DB_CONTENT_ONLY, BX_DB_PADDING_CONTENT_ONLY, BX_DB_NO_CAPTION, BX_DB_PADDING_NO_CAPTION];
  499. $sTitle = $this->getBlockTitle($aBlock);
  500. $sHelp = $this->getBlockHelp($aBlock);
  501. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  502. $bBlockVisible = $this->_isVisibleBlock($aBlock);
  503. if ($iAsync && $bBlockVisible) {
  504. $iDesignboxId = $aBlock['designbox_id'];
  505. $sHelpTitle = $sHelpContent = '';
  506. if(!in_array($iDesignboxId, $aDbNoTitle))
  507. $sHelpTitle = $sHelp;
  508. else
  509. $sHelpContent = $sHelp;
  510. $sContent = $this->getBlockAsyncCode($aBlock, $iAsync);
  511. $aParams = array(
  512. $sTitle . $sHelpTitle,
  513. $sContent . $sHelpContent,
  514. $iDesignboxId
  515. );
  516. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  517. }
  518. elseif ($bBlockVisible && method_exists($this, $sFunc)) {
  519. $mixedContent = $this->$sFunc($aBlock);
  520. $this->_oQuery->setReadOnlyMode(true);
  521. if(is_array($mixedContent) && !empty($mixedContent['content'])) {
  522. $iDesignboxId = isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'];
  523. $sHelpTitle = $sHelpContent = '';
  524. if(!in_array($iDesignboxId, $aDbNoTitle))
  525. $sHelpTitle = $sHelp;
  526. else
  527. $sHelpContent = $sHelp;
  528. $aParams = array(
  529. (isset($mixedContent['title']) ? $mixedContent['title'] : $sTitle) . $sHelpTitle,
  530. $mixedContent['content'] . $sHelpContent,
  531. $iDesignboxId
  532. );
  533. $mixedMenu = false;
  534. if(isset($mixedContent['menu']))
  535. $mixedMenu = $mixedContent['menu'];
  536. else if(!empty($aBlock['submenu']))
  537. $mixedMenu = $aBlock['submenu'];
  538. $aParams[] = $mixedMenu;
  539. if(isset($mixedContent['buttons']))
  540. $aParams[] = $mixedContent['buttons'];
  541. else if($mixedMenu)
  542. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  543. if(isset($mixedContent['class']))
  544. $aBlock['class'] = $mixedContent['class'];
  545. }
  546. else if(is_string($mixedContent) && !empty($mixedContent)) {
  547. $iDesignboxId = $aBlock['designbox_id'];
  548. $sHelpTitle = $sHelpContent = '';
  549. if(!in_array($iDesignboxId, $aDbNoTitle))
  550. $sHelpTitle = $sHelp;
  551. else
  552. $sHelpContent = $sHelp;
  553. $aParams = array(
  554. $sTitle . $sHelpTitle,
  555. $mixedContent . $sHelpContent,
  556. $iDesignboxId
  557. );
  558. $mixedMenu = !empty($aBlock['submenu']) ? $aBlock['submenu'] : false;
  559. $aParams[] = $mixedMenu;
  560. if($mixedMenu)
  561. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  562. }
  563. if(!empty($aParams))
  564. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  565. }
  566. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  567. return $sContentWithBox;
  568. }
  569. /**
  570. * Add necessary js and css files.
  571. */
  572. protected function _addJsCss()
  573. {
  574. $this->_oTemplate->addJs(array('BxDolPage.js', 'theia-sticky-sidebar/theia-sticky-sidebar.min.js', 'theia-sticky-sidebar/ResizeSensor.min.js'));
  575. $this->_oTemplate->addCss('page_layouts.css');
  576. }
  577. /**
  578. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  579. */
  580. protected function _addSysTemplateVars ()
  581. {
  582. $oTemplate = BxDolTemplate::getInstance();
  583. $sPageTitle = $this->_getPageTitle();
  584. if ($sPageTitle)
  585. $oTemplate->setPageHeader ($sPageTitle);
  586. $sMetaDesc = $this->_getPageMetaDesc();
  587. if ($sMetaDesc)
  588. $oTemplate->setPageDescription ($sMetaDesc);
  589. $sMetaRobots = $this->_getPageMetaRobots();
  590. if ($sMetaRobots)
  591. $oTemplate->setPageMetaRobots ($sMetaRobots);
  592. $sMetaImage = $this->_getPageMetaImage();
  593. if ($sMetaImage)
  594. $oTemplate->addPageMetaImage($sMetaImage);
  595. $sMetaKeywords = $this->_getPageMetaKeywords();
  596. if ($sMetaKeywords)
  597. $oTemplate->addPageKeywords ($sMetaKeywords);
  598. }
  599. /**
  600. * Select menu from page properties.
  601. */
  602. protected function _selectMenu ()
  603. {
  604. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  605. }
  606. /**
  607. * Set page submenu if it's specified
  608. */
  609. protected function _setSubmenu ($aParams)
  610. {
  611. if(empty($this->_aObject['submenu']) || $this->_aObject['submenu'] == 'disabled')
  612. return;
  613. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  614. if(!$oMenuSubmenu)
  615. return;
  616. $oMenuSubmenu->setObjectSubmenu($this->_aObject['submenu'], $aParams);
  617. }
  618. /**
  619. * Get content for 'raw' block type.
  620. * @return string
  621. */
  622. protected function _getBlockRaw ($aBlock)
  623. {
  624. $s = '<div class="bx-page-raw-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  625. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  626. $s = bx_process_macros($s);
  627. return $s;
  628. }
  629. /**
  630. * Get content for 'custom' block type.
  631. * @return string
  632. */
  633. protected function _getBlockCustom ($aBlock)
  634. {
  635. $s = '<div class="bx-page-custom-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  636. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  637. $s = bx_process_macros($s);
  638. return $s;
  639. }
  640. /**
  641. * Get content for 'html' block type.
  642. * @return string
  643. */
  644. protected function _getBlockHtml ($aBlock)
  645. {
  646. $s = '<div class="bx-page-html-container">' . $aBlock['content'] . '</div>';
  647. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  648. $s = bx_process_macros($s);
  649. return $s;
  650. }
  651. /**
  652. * Get content for 'wiki' block type.
  653. * @return string
  654. */
  655. protected function _getBlockWiki ($aBlock)
  656. {
  657. $oWiki = BxDolWiki::getObjectInstance($this->_aObject['module']);
  658. if (!$oWiki) {
  659. $sContent = _t('_sys_wiki_error_missing_wiki_object', $this->_aObject['module']);
  660. }
  661. else {
  662. $sContent = $oWiki->getBlockContent($aBlock['id'], false, (int)bx_get($aBlock['id'].'rev') ? (int)bx_get($aBlock['id'].'rev') : false);
  663. }
  664. $s = '<div id="bx-page-wiki-container-' . $aBlock['id'] . '" class="bx-page-wiki-container markdown-body bx-def-vanilla-html">' . $sContent . '</div>';
  665. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  666. $s = bx_process_macros($s);
  667. return $s;
  668. }
  669. /**
  670. * Get content for 'lang' block type.
  671. * @return string
  672. */
  673. protected function _getBlockLang ($aBlock)
  674. {
  675. $s = '<div class="bx-page-lang-container">' . _t(trim($aBlock['content'])) . '</div>';
  676. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  677. $s = bx_process_macros($s);
  678. return $s;
  679. }
  680. /**
  681. * Get content for 'image' block type.
  682. * @return string
  683. */
  684. protected function _getBlockImage ($aBlock)
  685. {
  686. if (empty($aBlock['content']))
  687. return false;
  688. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  689. $iFileId = (int)$iFileId;
  690. if (!$iFileId)
  691. return false;
  692. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  693. if (!$oStorage)
  694. return false;
  695. $sUrl = $oStorage->getFileUrlById($iFileId);
  696. if (!$sUrl)
  697. return false;
  698. $sStyleAdd = '';
  699. if ('center' == $sAlign || 'left' == $sAlign || 'right' == $sAlign)
  700. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  701. return '<div class="bx-page-image-container" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  702. }
  703. /**
  704. * Get content for 'rss' block type.
  705. * @return string
  706. */
  707. protected function _getBlockRss ($aBlock)
  708. {
  709. if (empty($aBlock['content']))
  710. return false;
  711. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  712. $iNum = (int)$iNum;
  713. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  714. }
  715. /**
  716. * Get content for 'menu' block type.
  717. * @return string
  718. */
  719. protected function _getBlockMenu ($aBlock)
  720. {
  721. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  722. return $oMenu ? $oMenu->getCode () : '';
  723. }
  724. /**
  725. * Get content for 'service' block type.
  726. * @return string
  727. */
  728. protected function _getBlockService ($aBlock)
  729. {
  730. return BxDolService::callSerialized($aBlock['content'], $this->_aMarkers);
  731. }
  732. /**
  733. * Get page title.
  734. * @return string
  735. */
  736. protected function _getPageTitle()
  737. {
  738. return $this->_replaceMarkers(_t($this->_aObject['title']));
  739. }
  740. /**
  741. * Get page meta description.
  742. * @return string
  743. */
  744. protected function _getPageMetaDesc()
  745. {
  746. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  747. }
  748. /**
  749. * Get page meta image.
  750. * @return string
  751. */
  752. protected function _getPageMetaImage()
  753. {
  754. return '';
  755. }
  756. /**
  757. * Get page meta keywords.
  758. * @return string
  759. */
  760. protected function _getPageMetaKeywords()
  761. {
  762. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  763. }
  764. /**
  765. * Get page meta robots.
  766. * @return string
  767. */
  768. protected function _getPageMetaRobots()
  769. {
  770. return _t($this->_aObject['meta_robots']);
  771. }
  772. /**
  773. * Get access denied message HTML.
  774. * @return string
  775. */
  776. protected function _getPageAccessDeniedMsg ()
  777. {
  778. return MsgBox(_t('_Access denied'));
  779. }
  780. /**
  781. * Get page cache object.
  782. * @return cache object instance
  783. */
  784. protected function _getPageCacheObject ()
  785. {
  786. return $this->_getCacheObject ();
  787. }
  788. /**
  789. * Get block cache object.
  790. * @return cache object instance
  791. */
  792. protected function _getBlockCacheObject ()
  793. {
  794. return $this->_getCacheObject ('Block', 'pb');
  795. }
  796. protected function _getCacheObject ($sSuffixObj = 'Page', $sSuffixParam = 'page')
  797. {
  798. $sObj = '_o' . $sSuffixObj . 'CacheObject';
  799. $sParam = 'sys_' . $sSuffixParam . '_cache_engine';
  800. if ($this->{$sObj} != null) {
  801. return $this->{$sObj};
  802. } else {
  803. $sEngine = getParam($sParam);
  804. $this->{$sObj} = bx_instance ('BxDolCache' . $sEngine);
  805. if (!$this->{$sObj}->isAvailable())
  806. $this->{$sObj} = bx_instance ('BxDolCacheFile');
  807. return $this->{$sObj};
  808. }
  809. }
  810. /**
  811. * Get page cache key.
  812. * @param $isPrefixOnly return key prefix only.
  813. * @return string
  814. */
  815. protected function _getPageCacheKey ($isPrefixOnly = false)
  816. {
  817. $s = 'page_' . $this->_aObject['object'] . '_';
  818. if ($isPrefixOnly)
  819. return $s;
  820. $s .= $this->_getPageCacheParams ();
  821. $s .= bx_site_hash() . '.php';
  822. return $s;
  823. }
  824. /**
  825. * Get block cache key.
  826. * @param $isPrefixOnly return key prefix only.
  827. * @return string
  828. */
  829. protected function _getBlockCacheKey ($isPrefixOnly = false, $aBlock = array())
  830. {
  831. $s = $this->_getPageCacheKey(true) . 'block_';
  832. if ($isPrefixOnly)
  833. return $s;
  834. $s .= ($aBlock ? $aBlock['id'] : 0) . '_' . $this->_getPageCacheParams ();
  835. $s .= bx_site_hash() . '.php';
  836. return $s;
  837. }
  838. /**
  839. * Additional cache key. In the case of dynamic page.
  840. * For example - profile page, where each profile has own page.
  841. * @return string
  842. */
  843. protected function _getPageCacheParams ()
  844. {
  845. return md5(serialize($_GET) . bx_lang_name());
  846. }
  847. /**
  848. * Clean page cache.
  849. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  850. * @return string
  851. */
  852. protected function cleanCache ($isDelAllWithPagePrefix = false)
  853. {
  854. $a = ['Page', 'Block'];
  855. $bRet = false;
  856. foreach ($a as $s) {
  857. $oCache = $this->{'_get' . $s . 'CacheObject'} ();
  858. $sKey = $this->{'_get'. $s . 'CacheKey'} ($isDelAllWithPagePrefix);
  859. if ($isDelAllWithPagePrefix)
  860. $bRet = $bRet && $oCache->removeAllByPrefix($sKey);
  861. else
  862. $bRet = $bRet && $oCache->delData($sKey);
  863. }
  864. return $bRet;
  865. }
  866. }
  867. /** @} */