BxBasePage.php 34 KB

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