BxBasePage.php 34 KB

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