BxBasePage.php 43 KB

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