BxBasePage.php 38 KB

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