BxBasePage.php 36 KB

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