BxBasePage.php 37 KB

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