BxBasePage.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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 $_sJsClassName = '';
  21. protected $_sJsObjectName = '';
  22. protected $_aHtmlIds = [];
  23. protected $_bStickyColumns = false;
  24. protected $_bSubPage = false;
  25. public function __construct ($aObject, $oTemplate)
  26. {
  27. parent::__construct ($aObject);
  28. if ($oTemplate)
  29. $this->_oTemplate = $oTemplate;
  30. else
  31. $this->_oTemplate = BxDolTemplate::getInstance();
  32. $this->_sStorage = 'sys_images';
  33. $this->_sJsClassName = 'BxDolPage';
  34. $this->_sJsObjectName = 'oBxDolPage';
  35. $this->_bStickyColumns = isset($this->_aObject['sticky_columns']) && $this->_aObject['sticky_columns'] == 1;
  36. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $this->_sObject);
  37. $this->_aHtmlIds = array(
  38. 'help_popup' => $sHtmlId . '-help-popup-',
  39. );
  40. }
  41. public function performActionGetHelp ()
  42. {
  43. $iBlockId = (int)bx_get('block_id');
  44. if(empty($iBlockId))
  45. return;
  46. $aBlock = $this->_oQuery->getPageBlock($iBlockId);
  47. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['help']))
  48. return;
  49. echo $this->_oTemplate->parseHtmlByName('designbox_help_popup.html', array(
  50. 'content' => _t($aBlock['help'])
  51. ));
  52. }
  53. public function performActionGetShare ()
  54. {
  55. $aEmbedData = BxDolPage::getEmbedData(bx_get('url'));
  56. if(!isset($aEmbedData['url']))
  57. return;
  58. $sMenu = '';
  59. if(getParam('sys_a2a_enable') != 'on') {
  60. if(($oMenu = BxDolMenu::getObjectInstance('sys_social_sharing')) !== false) {
  61. $oMenu->addMarkers([
  62. 'url' => $aEmbedData['url'],
  63. 'img_url_encoded' => '',
  64. 'title_encoded' => ''
  65. ]);
  66. $sMenu = $oMenu->getCode();
  67. }
  68. }
  69. else
  70. $sMenu = getParam('sys_a2a_code');
  71. echo PopupBox('sys_share_popup', _t('_sys_txt_share_popup_header'), $this->_oTemplate->parseHtmlByName('designbox_share_popup.html', [
  72. 'url' => bx_process_output($aEmbedData['url']),
  73. 'menu' => $sMenu,
  74. 'code' => htmlspecialchars($aEmbedData['html']),
  75. ]), true);
  76. }
  77. public function performActionEmbed ()
  78. {
  79. $aCover = $this->getPageCoverImage();
  80. $sTitle = $this->_getPageTitle();
  81. $this->_oTemplate->getEmbed($this->_oTemplate->parseHtmlByName('embed_card.html', [
  82. 'title' => $sTitle,
  83. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  84. 'bx_if:thumb' => [
  85. 'condition' => $this->isPageCover() && count($aCover) > 0,
  86. 'content' => [
  87. 'title' => $sTitle,
  88. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  89. 'background' => BxDolCover::getInstance(BxDolTemplate::getInstance())->getCoverImageUrl($aCover)
  90. ],
  91. ],
  92. 'bx_if:no_thumb' => [
  93. 'condition' => count($aCover) == 0 || !$this->isPageCover(),
  94. 'content' => [
  95. 'title' => $sTitle,
  96. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  97. ]
  98. ]
  99. ]
  100. ));
  101. }
  102. /**
  103. * Very similar to BxBasePage::getCode
  104. * but adds css and js files which are needed for the corect page display
  105. */
  106. public function getCodeDynamic ()
  107. {
  108. // TODO: remake to use use collect* template methods
  109. $oTemplate = BxDolTemplate::getInstance();
  110. // get js&css before the page code is generated
  111. $aCssBefore = $oTemplate->getCss();
  112. $aJsBefore = $oTemplate->getJs();
  113. // generate page code
  114. $sContent = $this->getCode();
  115. // get js&css after the page code is generated
  116. $aCssAfter = $oTemplate->getCss();
  117. $aJsAfter = $oTemplate->getJs();
  118. // compare files which were added before and after page code is generated
  119. $f = function ($a1, $a2) {
  120. return strcasecmp($a1['url'], $a2['url']);
  121. };
  122. $aCssNew = array_udiff($aCssAfter, $aCssBefore, $f);
  123. $aJsNew = array_udiff($aJsAfter, $aJsBefore, $f);
  124. // add newly added js&css files in static mode
  125. $sCss = $sJs = '';
  126. foreach ($aCssNew as $a)
  127. $sCss .= $oTemplate->addCss($a['url'], true);
  128. foreach ($aJsNew as $a)
  129. $sJs .= $oTemplate->addJs($a['url'], true);
  130. return $sJs . $sCss . $sContent;
  131. }
  132. /**
  133. * Get page code with automatic caching, adding necessary css/js files and system template vars.
  134. * @return string.
  135. */
  136. public function getCode ()
  137. {
  138. if (bx_get('dynamic') && ($iBlockId = (int)bx_get('pageBlock'))) {
  139. if (self::isLockedFromUnauthenticated($this->_aObject['uri']) || !$this->_isVisiblePage($this->_aObject)) {
  140. header('HTTP/1.0 403 Forbidden');
  141. exit;
  142. }
  143. bx_alert('system', 'page_output_block', 0, false, array(
  144. 'page_name' => $this->_sObject,
  145. 'page_object' => $this,
  146. 'page_query' => $this->_oQuery,
  147. 'block_id' => (int)$iBlockId,
  148. ));
  149. header( 'Content-type:text/html;charset=utf-8' );
  150. BxDolTemplate::getInstance()->collectingStart();
  151. $s = $this->_getBlockOnlyCode($iBlockId);
  152. $sCss = bx_get('includedCss');
  153. $sJs = bx_get('includedJs');
  154. echo BxDolTemplate::getInstance()->collectingEndGetCode($sCss ? @json_decode($sCss) : [], $sJs ? @json_decode($sJs) : []);
  155. echo $s;
  156. exit;
  157. }
  158. if (!$this->_isVisiblePage($this->_aObject))
  159. return $this->_getPageAccessDeniedMsg ();
  160. $this->_addJsCss();
  161. if (!$this->_bSubPage) {
  162. $this->_addSysTemplateVars();
  163. $this->_selectMenu();
  164. $this->_setSubmenu(array());
  165. }
  166. if (!getParam('sys_page_cache_enable') || !$this->_aObject['cache_lifetime'] || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  167. $sPageCode = $this->_getPageCode();
  168. }
  169. else {
  170. $oCache = $this->_getPageCacheObject();
  171. $sKey = $this->_getPageCacheKey();
  172. $sKeyCssJs = 'cssjs_' . $sKey;
  173. $mixedRet = $oCache->getData($sKey, $this->_aObject['cache_lifetime']);
  174. $aRetCssJs = $oCache->getData($sKeyCssJs, $this->_aObject['cache_lifetime']);
  175. if ($mixedRet !== null && $aRetCssJs !== null) {
  176. BxDolTemplate::getInstance()->collectingStart();
  177. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  178. $sPageCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  179. $sPageCode .= $mixedRet;
  180. } else {
  181. BxDolTemplate::getInstance()->collectingStart();
  182. $sPageCode = $this->_getPageCode();
  183. $aPageCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  184. $oCache->setData($sKey, $sPageCode, $this->_aObject['cache_lifetime']);
  185. $oCache->setData($sKeyCssJs, $aPageCssJs, $this->_aObject['cache_lifetime']);
  186. }
  187. }
  188. bx_alert('system', 'page_output', 0, false, array(
  189. 'page_name' => $this->_sObject,
  190. 'page_object' => $this,
  191. 'page_query' => $this->_oQuery,
  192. 'page_code' => &$sPageCode,
  193. 'sub_page' => $this->_bSubPage,
  194. ));
  195. if (!$this->_bSubPage)
  196. $sPageCode .= $this->getJsScript();
  197. return $sPageCode;
  198. }
  199. public function getJsClassName()
  200. {
  201. return $this->_sJsClassName;
  202. }
  203. public function getJsObjectName()
  204. {
  205. return $this->_sJsObjectName;
  206. }
  207. function _wrapInTagJsCode($sCode)
  208. {
  209. return "<script language=\"javascript\">\n<!--\n" . $sCode . "\n-->\n</script>";
  210. }
  211. public function getJsScript()
  212. {
  213. $sJsObjName = $this->getJsObjectName();
  214. $sJsObjClass = $this->getJsClassName();
  215. $sCode = "if(window['" . $sJsObjName . "'] == undefined) var " . $sJsObjName . " = new " . $sJsObjClass . "(" . json_encode(array(
  216. 'sObjName' => $sJsObjName,
  217. 'sObject' => $this->_sObject,
  218. 'sRootUrl' => BX_DOL_URL_ROOT,
  219. 'aHtmlIds' => $this->_aHtmlIds,
  220. 'isStickyColumns' => $this->_bStickyColumns
  221. )) . ");";
  222. return $this->_wrapInTagJsCode($sCode);
  223. }
  224. /**
  225. * Is page cover enabled.
  226. * @return string
  227. */
  228. public function isPageCover()
  229. {
  230. $bResult = false;
  231. switch((int)$this->_aObject['cover']) {
  232. case 1: //--- Enabled for all
  233. $bResult = true;
  234. break;
  235. case 2: //--- Enabled for visitors only
  236. $bResult = !isLogged();
  237. break;
  238. case 3: //--- Enabled for members only
  239. $bResult = isLogged();
  240. break;
  241. }
  242. return $bResult;
  243. }
  244. public function setSubPage($b = true)
  245. {
  246. $this->_bSubPage = $b;
  247. }
  248. public function setPageCover($bCover = true)
  249. {
  250. $this->_aObject['cover'] = (bool)$bCover;
  251. }
  252. public function getPageCoverImage($bTranscoder = true)
  253. {
  254. $iId = (int)$this->_aObject['cover_image'];
  255. if(empty($iId)) {
  256. $iId = (int)getParam('sys_site_cover_common');
  257. if(empty($iId))
  258. return array();
  259. }
  260. $aResult = array(
  261. 'id' => $iId
  262. );
  263. if($bTranscoder)
  264. $aResult['transcoder'] = BX_DOL_TRANSCODER_OBJ_COVER;
  265. else
  266. $aResult['object'] = $this->_sStorage;
  267. return $aResult;
  268. }
  269. public function getPageCoverParams()
  270. {
  271. $aParams = [
  272. 'title' => $this->_getPageTitle(),
  273. 'actions' => '',
  274. 'bx_if:image' => [
  275. 'condition' => false,
  276. 'content' => []
  277. ],
  278. 'bx_if:icon' => [
  279. 'condition' => false,
  280. 'content' => []
  281. ],
  282. ];
  283. if(($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  284. $aCoverParams = $oMenuSubmenu->getPageCoverParams();
  285. if(!empty($aCoverParams) && is_array($aCoverParams))
  286. $aParams = $aCoverParams;
  287. }
  288. if(!empty($this->_aObject['cover_title'])) {
  289. $sCoverTitle = _t($this->_aObject['cover_title']);
  290. if($sCoverTitle && strcmp($sCoverTitle, $this->_aObject['cover_title']) != 0)
  291. $aParams['title'] = $sCoverTitle;
  292. }
  293. return $aParams;
  294. }
  295. /**
  296. * Get block title.
  297. * @return string
  298. */
  299. public function getBlockTitle ($aBlock)
  300. {
  301. return $this->_replaceMarkers(_t($aBlock['title']), array('block_id' => $aBlock['id']));
  302. }
  303. /**
  304. * Get help control if help is available for the block.
  305. * @return string
  306. */
  307. public function getBlockHelp ($aBlock)
  308. {
  309. if(empty($aBlock['help']) || empty(strip_tags(_t($aBlock['help']))))
  310. return '';
  311. return $this->_oTemplate->parseHtmlByName('designbox_help.html', array(
  312. 'js_object' => $this->_sJsObjectName,
  313. 'block_id' => $aBlock['id']
  314. ));
  315. }
  316. /**
  317. * Get code to load block asyncroniously
  318. * @param $aBlock block code
  319. * @param $iAsync if greater than 0 the it defines loading indicator for the block
  320. * @return HTML code string
  321. */
  322. public function getBlockAsyncCode($aBlock, $iAsync)
  323. {
  324. $aContentPlaceholder = $this->_oQuery->getPageBlockContentPlaceholder($iAsync);
  325. if (!$aContentPlaceholder)
  326. return _t('_sys_txt_error_occured');
  327. $oTemplate = $this->_oTemplate;
  328. if ('system' != $aContentPlaceholder['module']) {
  329. $oModule = BxDolModule::getInstance($aContentPlaceholder['module']);
  330. if (!$oModule)
  331. return _t('_sys_txt_error_occured');
  332. $oTemplate = $oModule->_oTemplate;
  333. }
  334. return $oTemplate->parseHtmlByName($aContentPlaceholder['template'], $aBlock);
  335. }
  336. /**
  337. * Get page array with all cells and blocks
  338. */
  339. public function getPageAPI ($aBlocks = [])
  340. {
  341. define('BX_API_PAGE', true);
  342. $query_string = '';
  343. if (isset(bx_get('params')[2]) && bx_get('params')[2] != ''){
  344. $array = json_decode(bx_get('params')[2], true);
  345. $query_string = http_build_query($array);
  346. }
  347. $a = [
  348. 'id' => $this->_aObject['id'],
  349. 'title' => $this->_getPageTitle(),
  350. 'uri' => $this->_aObject['uri'],
  351. 'url' => bx_get('params')[0] . ($query_string != '' ? '?' . $query_string : ''),
  352. 'author' => $this->_aObject['author'],
  353. 'added' => $this->_aObject['added'],
  354. 'module' => $this->getModule(),
  355. 'type' => $this->getType (),
  356. 'layout' => str_replace('.html', '', $this->_aObject['template']),
  357. 'cover_block' => '',
  358. 'menu_top' => '',
  359. 'menu' => '',
  360. 'menu_bottom' => '',
  361. 'menu_add' => '',
  362. 'elements' => $this->getPageBlocksAPI($aBlocks),
  363. ];
  364. if (BxDolCover::getInstance($this)->isCover() && isset($this->_aProfileInfo)) {
  365. $oModule = BxDolModule::getInstance($this->getModule());
  366. $a['cover_block'] = [
  367. 'profile' => BxDolProfile::getData($this->_aProfileInfo['id'], [
  368. 'get_avatar' => 'getAvatarBig',
  369. 'with_info' => true
  370. ]),
  371. 'actions_menu' => '',
  372. 'meta_menu' => '',
  373. 'cover' => $oModule->serviceGetCover($this->_aProfileInfo['content_id'])
  374. ];
  375. $CNF = $oModule->_oConfig->CNF;
  376. if(!empty($CNF['OBJECT_MENU_VIEW_ENTRY_META'])){
  377. $oMetaMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_VIEW_ENTRY_META']);
  378. $a['cover_block']['meta_menu'] = $oMetaMenu->getCodeAPI();
  379. }
  380. if(!empty($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY'])){
  381. $oActionMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_ALL']);
  382. $a['cover_block']['actions_menu'] = $oActionMenu->getCodeAPI();
  383. }
  384. }
  385. $sMenuTop = getParam('sys_api_menu_top');
  386. if (!empty($sMenuTop) && ($oMenuTop = BxDolMenu::getObjectInstance($sMenuTop)) !== false)
  387. $a['menu_top'] = $oMenuTop->getCodeAPI();
  388. if (($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  389. if(($sSubmenu = $this->getSubMenu()) !== '')
  390. $oMenuSubmenu->setObjectSubmenu($sSubmenu, []);
  391. $a['menu'] = $oMenuSubmenu->getCodeAPI();
  392. }
  393. if (isLogged()) {
  394. /* temporatery commented
  395. if(($oMenuAddContent = BxDolMenu::getObjectInstance('sys_add_content')) !== false)
  396. $a['menu_add'] = $oMenuAddContent->getCodeAPI();
  397. */
  398. $o = BxDolProfile::getInstance();
  399. $a['user'] = [
  400. 'id' => $o->id(),
  401. 'email' => $o->getAccountObject()->getEmail(),
  402. 'display_name' => $o->getDisplayName(),
  403. 'url' => bx_api_get_relative_url($o->getUrl()),
  404. 'avatar' => $o->getAvatar(),
  405. 'info' => $o->getInfo(),
  406. 'notifications' => 0,
  407. 'active' => $o->isActive(),
  408. 'status' => $o->getStatus(),
  409. ];
  410. $oInformer = BxDolInformer::getInstance(BxDolTemplate::getInstance());
  411. $sRet = $oInformer ? $oInformer->display() : '';
  412. if ($sRet){
  413. $a['user']['informer'] = $sRet;
  414. }
  415. $sModuleNotifications = 'bx_notifications';
  416. if(BxDolRequest::serviceExists($sModuleNotifications, 'get_unread_notifications_num'))
  417. $a['user']['notifications'] = bx_srv($sModuleNotifications, 'get_unread_notifications_num', [$o->id()]);
  418. }
  419. return $a;
  420. }
  421. public function getPageBlocksAPI($aBlocks = [])
  422. {
  423. $bBlocks = !empty($aBlocks) && is_array($aBlocks);
  424. $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'];
  425. $aCells = $this->_oQuery->getPageBlocks(true);
  426. foreach($aCells as $sKey => &$aCell) {
  427. foreach($aCell as $i => $aBlock) {
  428. if(!$this->_isVisibleBlock($aBlock)) {
  429. unset($aCells[$sKey][$i]);
  430. continue;
  431. }
  432. $this->processPageBlock($aCells[$sKey][$i], true);
  433. $aBlock = $aCells[$sKey][$i];
  434. $sSource = '';
  435. if($aBlock['type'] == 'service') {
  436. $aContent = @unserialize($aBlock['content']);
  437. if(isset($aContent['module'], $aContent['method']))
  438. $sSource = $aContent['module'] . ':' . $aContent['method'];
  439. }
  440. if($bBlocks && !in_array($sSource, $aBlocks)) {
  441. unset($aCells[$sKey][$i]);
  442. continue;
  443. }
  444. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  445. $mBlock = method_exists($this, $sFunc) ? $this->$sFunc($aBlock) : $aBlock['content'];
  446. $aCells[$sKey][$i] = array_merge($aCells[$sKey][$i], [
  447. 'title' => isset($mBlock['title']) ? $mBlock['title'] : $this->getBlockTitle($aBlock),
  448. 'content' => isset($mBlock['content']) ? $mBlock['content'] : $mBlock,
  449. 'menu' => isset($mBlock['menu']) ? $mBlock['menu'] : '',
  450. 'source' => $sSource
  451. ]);
  452. $aCells[$sKey][$i] = array_diff_key($aCells[$sKey][$i], array_flip($aFieldsUnset));
  453. }
  454. }
  455. return array_map('array_values', $aCells);
  456. }
  457. /**
  458. * Get page code vars
  459. * @return string
  460. */
  461. protected function _getPageCodeVars ()
  462. {
  463. $aHiddenOn = array(
  464. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  465. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  466. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  467. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  468. );
  469. $aVars = array (
  470. 'page_id' => 'bx-page-' . $this->_aObject['uri'],
  471. 'bx_if:show_layout_row_dump' => array(
  472. 'condition' => false,
  473. 'content' => array()
  474. )
  475. );
  476. $aBlocks = $this->_oQuery->getPageBlocks();
  477. foreach ($aBlocks as $sKey => $aCell) {
  478. $sCell = '';
  479. foreach ($aCell as $aBlock) {
  480. $this->processPageBlock($aBlock, false);
  481. $sContentWithBox = $this->_getBlockCodeWithCache($aBlock, $aBlock['async']);
  482. $sClassAdd = '';
  483. if(!empty($aBlock['class']))
  484. $sClassAdd .= ' ' . $aBlock['class'];
  485. if(!empty($aBlock['hidden_on']))
  486. foreach($aHiddenOn as $iHiddenOn => $sHiddenOnClass)
  487. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  488. $sClassAdd .= ' ' . $sHiddenOnClass;
  489. if ($sContentWithBox)
  490. $sCell .= $this->_oTemplate->parseHtmlByName('designbox_container.html', array(
  491. 'class_add' => $sClassAdd,
  492. 'bx_if:show_html_id' => array(
  493. 'condition' => true,
  494. 'content' => array(
  495. 'html_id' => 'bx-page-block-' . $aBlock['id']
  496. ),
  497. ),
  498. 'content' => $sContentWithBox
  499. ));
  500. }
  501. $aVars[$sKey] = $sCell;
  502. }
  503. return $aVars;
  504. }
  505. /**
  506. * Process block values, especially if someting need to be overrided
  507. */
  508. protected function processPageBlock(&$aBlock, $bApi = false)
  509. {
  510. }
  511. /**
  512. * Get page code only.
  513. * @return string
  514. */
  515. protected function _getPageCode ()
  516. {
  517. $aVars = $this->_getPageCodeVars ();
  518. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  519. }
  520. /**
  521. * Get one block code only.
  522. * @return string
  523. */
  524. protected function _getBlockOnlyCode ($iBlockId)
  525. {
  526. if (!($aBlock = $this->_oQuery->getPageBlock((int)$iBlockId)))
  527. return '';
  528. return $this->_getBlockCodeWithCache($aBlock, 0);
  529. }
  530. protected function _getBlockCodeWithCache(&$aBlock, $iAsync = 0)
  531. {
  532. if (!getParam('sys_pb_cache_enable') || !$aBlock['cache_lifetime'] || $iAsync || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  533. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  534. }
  535. else {
  536. $oCache = $this->_getBlockCacheObject();
  537. $sKey = $this->_getBlockCacheKey(false, $aBlock);
  538. $sKeyCssJs = 'cssjs_' . $sKey;
  539. $mixedRet = $oCache->getData($sKey, $aBlock['cache_lifetime']);
  540. $aRetCssJs = $oCache->getData($sKeyCssJs, $aBlock['cache_lifetime']);
  541. if ($mixedRet !== null && $aRetCssJs !== null) {
  542. BxDolTemplate::getInstance()->collectingStart();
  543. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  544. $sBlockCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  545. $sBlockCode .= $mixedRet;
  546. } else {
  547. BxDolTemplate::getInstance()->collectingStart();
  548. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  549. $aBlockCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  550. $oCache->setData($sKey, $sBlockCode, $aBlock['cache_lifetime']);
  551. $oCache->setData($sKeyCssJs, $aBlockCssJs, $aBlock['cache_lifetime']);
  552. }
  553. }
  554. return $sBlockCode;
  555. }
  556. /**
  557. * Get block code.
  558. * @return string
  559. */
  560. protected function _getBlockCode(&$aBlock, $iAsync = 0)
  561. {
  562. $sContentWithBox = '';
  563. $oFunctions = $this->_oTemplate->getTemplateFunctions();
  564. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  565. $aDbNoTitle = [BX_DB_CONTENT_ONLY, BX_DB_PADDING_CONTENT_ONLY, BX_DB_NO_CAPTION, BX_DB_PADDING_NO_CAPTION];
  566. $sTitle = $this->getBlockTitle($aBlock);
  567. $sHelp = $this->getBlockHelp($aBlock);
  568. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  569. $bBlockVisible = $this->_isVisibleBlock($aBlock);
  570. if ($iAsync && $bBlockVisible) {
  571. $iDesignboxId = $aBlock['designbox_id'];
  572. $sHelpTitle = $sHelpContent = '';
  573. if(!in_array($iDesignboxId, $aDbNoTitle))
  574. $sHelpTitle = $sHelp;
  575. else
  576. $sHelpContent = $sHelp;
  577. $sContent = $this->getBlockAsyncCode($aBlock, $iAsync);
  578. $aParams = array(
  579. $sTitle . $sHelpTitle,
  580. $sContent . $sHelpContent,
  581. $iDesignboxId
  582. );
  583. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  584. }
  585. elseif ($bBlockVisible && method_exists($this, $sFunc)) {
  586. $mixedContent = $this->$sFunc($aBlock);
  587. if(!empty($aBlock['content_empty'])) {
  588. $sEmpty = _t($aBlock['content_empty']);
  589. $sRegExp = '/(<[A-Za-z0-9]+\b.[^<]*[\pPi\pPf]bx\-msg\-box\b.*[\pPi\pPf][^>]*>)(' . _t('_Empty') . ')(<\/[A-Za-z0-9]+>)/su';
  590. if(is_string($mixedContent))
  591. $mixedContent = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent);
  592. else if(is_array($mixedContent) && !empty($mixedContent['content']))
  593. $mixedContent['content'] = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent['content']);
  594. }
  595. $this->_oQuery->setReadOnlyMode(true);
  596. if(is_array($mixedContent) && !empty($mixedContent['content'])) {
  597. $iDesignboxId = isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'];
  598. $sHelpTitle = $sHelpContent = '';
  599. if(!in_array($iDesignboxId, $aDbNoTitle))
  600. $sHelpTitle = $sHelp;
  601. else
  602. $sHelpContent = $sHelp;
  603. $aParams = array(
  604. (isset($mixedContent['title']) ? $mixedContent['title'] : $sTitle) . $sHelpTitle,
  605. $mixedContent['content'] . $sHelpContent,
  606. $iDesignboxId
  607. );
  608. $mixedMenu = false;
  609. if(isset($mixedContent['menu']))
  610. $mixedMenu = $mixedContent['menu'];
  611. else if(!empty($aBlock['submenu']))
  612. $mixedMenu = $aBlock['submenu'];
  613. $aParams[] = $mixedMenu;
  614. if(isset($mixedContent['buttons']))
  615. $aParams[] = $mixedContent['buttons'];
  616. else if($mixedMenu)
  617. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  618. if(isset($mixedContent['class']))
  619. $aBlock['class'] = $mixedContent['class'];
  620. }
  621. else if(is_string($mixedContent) && !empty($mixedContent)) {
  622. $iDesignboxId = $aBlock['designbox_id'];
  623. $sHelpTitle = $sHelpContent = '';
  624. if(!in_array($iDesignboxId, $aDbNoTitle))
  625. $sHelpTitle = $sHelp;
  626. else
  627. $sHelpContent = $sHelp;
  628. $aParams = array(
  629. $sTitle . $sHelpTitle,
  630. $mixedContent . $sHelpContent,
  631. $iDesignboxId
  632. );
  633. $mixedMenu = !empty($aBlock['submenu']) ? $aBlock['submenu'] : false;
  634. $aParams[] = $mixedMenu;
  635. if($mixedMenu)
  636. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  637. }
  638. if(!empty($aParams))
  639. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  640. }
  641. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  642. return $sContentWithBox;
  643. }
  644. /**
  645. * Add necessary js and css files.
  646. */
  647. protected function _addJsCss()
  648. {
  649. $this->_oTemplate->addJs(array('BxDolPage.js', 'theia-sticky-sidebar/theia-sticky-sidebar.min.js', 'theia-sticky-sidebar/ResizeSensor.min.js'));
  650. $this->_oTemplate->addCss('page_layouts.css');
  651. }
  652. /**
  653. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  654. */
  655. protected function _addSysTemplateVars ()
  656. {
  657. $oTemplate = BxDolTemplate::getInstance();
  658. $sPageTitle = $this->_getPageTitle();
  659. if ($sPageTitle)
  660. $oTemplate->setPageHeader ($sPageTitle);
  661. $sMetaDesc = $this->_getPageMetaDesc();
  662. if ($sMetaDesc)
  663. $oTemplate->setPageDescription ($sMetaDesc);
  664. $sMetaRobots = $this->_getPageMetaRobots();
  665. if ($sMetaRobots)
  666. $oTemplate->setPageMetaRobots ($sMetaRobots);
  667. $sMetaImage = $this->_getPageMetaImage();
  668. if ($sMetaImage)
  669. $oTemplate->addPageMetaImage($sMetaImage);
  670. $sMetaKeywords = $this->_getPageMetaKeywords();
  671. if ($sMetaKeywords)
  672. $oTemplate->addPageKeywords ($sMetaKeywords);
  673. }
  674. /**
  675. * Select menu from page properties.
  676. */
  677. protected function _selectMenu ()
  678. {
  679. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  680. }
  681. /**
  682. * Set page submenu if it's specified
  683. */
  684. protected function _setSubmenu ($aParams)
  685. {
  686. if(empty($this->_aObject['submenu']) || $this->_aObject['submenu'] == 'disabled')
  687. return;
  688. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  689. if(!$oMenuSubmenu)
  690. return;
  691. $oMenuSubmenu->setObjectSubmenu($this->_aObject['submenu'], $aParams);
  692. }
  693. /**
  694. * Get content for 'raw' block type.
  695. * @return string
  696. */
  697. protected function _getBlockRaw ($aBlock)
  698. {
  699. $s = '<div class="bx-page-raw-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  700. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  701. $s = bx_process_macros($s);
  702. return $s;
  703. }
  704. /**
  705. * Get content for 'custom' block type.
  706. * @return string
  707. */
  708. protected function _getBlockCustom ($aBlock)
  709. {
  710. $s = '<div class="bx-page-custom-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  711. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  712. $s = bx_process_macros($s);
  713. return $s;
  714. }
  715. /**
  716. * Get content for 'html' block type.
  717. * @return string
  718. */
  719. protected function _getBlockHtml ($aBlock)
  720. {
  721. $s = '<div class="bx-page-html-container bx-def-vanilla-html max-w-none">' . $aBlock['content'] . '</div>';
  722. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  723. $s = bx_process_macros($s);
  724. return $s;
  725. }
  726. /**
  727. * Get content for 'wiki' block type.
  728. * @return string
  729. */
  730. protected function _getBlockWiki ($aBlock)
  731. {
  732. $oWiki = BxDolWiki::getObjectInstance($this->_aObject['module']);
  733. if (!$oWiki) {
  734. $sContent = _t('_sys_wiki_error_missing_wiki_object', $this->_aObject['module']);
  735. }
  736. else {
  737. $sContent = $oWiki->getBlockContent($aBlock['id'], false, (int)bx_get($aBlock['id'].'rev') ? (int)bx_get($aBlock['id'].'rev') : false);
  738. }
  739. $s = '<div id="bx-page-wiki-container-' . $aBlock['id'] . '" class="bx-page-wiki-container markdown-body bx-def-vanilla-html">' . $sContent . '</div>';
  740. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  741. $s = bx_process_macros($s);
  742. return $s;
  743. }
  744. /**
  745. * Get content for 'lang' block type.
  746. * @return string
  747. */
  748. protected function _getBlockLang ($aBlock)
  749. {
  750. $s = '<div class="bx-page-lang-container bx-def-vanilla-html max-w-none">' . _t(trim($aBlock['content'])) . '</div>';
  751. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  752. $s = bx_process_macros($s);
  753. return $s;
  754. }
  755. /**
  756. * Get content for 'image' block type.
  757. * @return string
  758. */
  759. protected function _getBlockImage ($aBlock)
  760. {
  761. if (empty($aBlock['content']))
  762. return false;
  763. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  764. $iFileId = (int)$iFileId;
  765. if (!$iFileId)
  766. return false;
  767. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  768. if (!$oStorage)
  769. return false;
  770. $sUrl = $oStorage->getFileUrlById($iFileId);
  771. if (!$sUrl)
  772. return false;
  773. $sStyleAdd = '';
  774. if ('center' == $sAlign || 'left' == $sAlign || 'right' == $sAlign)
  775. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  776. return '<div class="bx-page-image-container" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  777. }
  778. /**
  779. * Get content for 'rss' block type.
  780. * @return string
  781. */
  782. protected function _getBlockRss ($aBlock)
  783. {
  784. if (empty($aBlock['content']))
  785. return false;
  786. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  787. $iNum = (int)$iNum;
  788. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  789. }
  790. /**
  791. * Get content for 'menu' block type.
  792. * @return string
  793. */
  794. protected function _getBlockMenu ($aBlock)
  795. {
  796. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  797. return $oMenu ? $oMenu->getCode () : '';
  798. }
  799. /**
  800. * Get content for 'service' block type.
  801. * @return string
  802. */
  803. protected function _getBlockService ($aBlock)
  804. {
  805. $aMarkers = array_merge($this->_aMarkers, [
  806. 'block_id' => $aBlock['id']
  807. ]);
  808. return BxDolService::callSerialized($aBlock['content'], $aMarkers);
  809. }
  810. /**
  811. * Get page title.
  812. * @return string
  813. */
  814. protected function _getPageTitle()
  815. {
  816. return $this->_replaceMarkers(_t($this->_aObject['title']));
  817. }
  818. /**
  819. * Get page meta description.
  820. * @return string
  821. */
  822. protected function _getPageMetaDesc()
  823. {
  824. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  825. }
  826. /**
  827. * Get page meta image.
  828. * @return string
  829. */
  830. protected function _getPageMetaImage()
  831. {
  832. return '';
  833. }
  834. /**
  835. * Get page meta keywords.
  836. * @return string
  837. */
  838. protected function _getPageMetaKeywords()
  839. {
  840. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  841. }
  842. /**
  843. * Get page meta robots.
  844. * @return string
  845. */
  846. protected function _getPageMetaRobots()
  847. {
  848. return _t($this->_aObject['meta_robots']);
  849. }
  850. /**
  851. * Get access denied message HTML.
  852. * @return string
  853. */
  854. protected function _getPageAccessDeniedMsg ()
  855. {
  856. return MsgBox(_t('_Access denied'));
  857. }
  858. /**
  859. * Get page cache object.
  860. * @return cache object instance
  861. */
  862. protected function _getPageCacheObject ()
  863. {
  864. return $this->_getCacheObject ();
  865. }
  866. /**
  867. * Get block cache object.
  868. * @return cache object instance
  869. */
  870. protected function _getBlockCacheObject ()
  871. {
  872. return $this->_getCacheObject ('Block', 'pb');
  873. }
  874. protected function _getCacheObject ($sSuffixObj = 'Page', $sSuffixParam = 'page')
  875. {
  876. $sObj = '_o' . $sSuffixObj . 'CacheObject';
  877. $sParam = 'sys_' . $sSuffixParam . '_cache_engine';
  878. if ($this->{$sObj} != null) {
  879. return $this->{$sObj};
  880. } else {
  881. $sEngine = getParam($sParam);
  882. $this->{$sObj} = bx_instance ('BxDolCache' . $sEngine);
  883. if (!$this->{$sObj}->isAvailable())
  884. $this->{$sObj} = bx_instance ('BxDolCacheFile');
  885. return $this->{$sObj};
  886. }
  887. }
  888. /**
  889. * Get page cache key.
  890. * @param $isPrefixOnly return key prefix only.
  891. * @return string
  892. */
  893. protected function _getPageCacheKey ($isPrefixOnly = false)
  894. {
  895. $s = 'page_' . $this->_aObject['object'] . '_';
  896. if ($isPrefixOnly)
  897. return $s;
  898. $s .= $this->_getPageCacheParams ();
  899. $s .= bx_site_hash() . '.php';
  900. return $s;
  901. }
  902. /**
  903. * Get block cache key.
  904. * @param $isPrefixOnly return key prefix only.
  905. * @return string
  906. */
  907. protected function _getBlockCacheKey ($isPrefixOnly = false, $aBlock = array())
  908. {
  909. $s = $this->_getPageCacheKey(true) . 'block_';
  910. if ($isPrefixOnly)
  911. return $s;
  912. $s .= ($aBlock ? $aBlock['id'] : 0) . '_' . $this->_getPageCacheParams ();
  913. $s .= bx_site_hash() . '.php';
  914. return $s;
  915. }
  916. /**
  917. * Additional cache key. In the case of dynamic page.
  918. * For example - profile page, where each profile has own page.
  919. * @return string
  920. */
  921. protected function _getPageCacheParams ()
  922. {
  923. return md5(serialize($_GET) . bx_lang_name());
  924. }
  925. /**
  926. * Clean page cache.
  927. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  928. * @return string
  929. */
  930. protected function cleanCache ($isDelAllWithPagePrefix = false)
  931. {
  932. $a = ['Page', 'Block'];
  933. $bRet = false;
  934. foreach ($a as $s) {
  935. $oCache = $this->{'_get' . $s . 'CacheObject'} ();
  936. $sKey = $this->{'_get'. $s . 'CacheKey'} ($isDelAllWithPagePrefix);
  937. if ($isDelAllWithPagePrefix)
  938. $bRet = $bRet && $oCache->removeAllByPrefix($sKey);
  939. else
  940. $bRet = $bRet && $oCache->delData($sKey);
  941. }
  942. return $bRet;
  943. }
  944. }
  945. /** @} */