BxBasePage.php 36 KB

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