BxBasePage.php 40 KB

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