BxBasePage.php 39 KB

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