BxBasePage.php 35 KB

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