BxBasePage.php 36 KB

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