BxBasePage.php 36 KB

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