BxBasePage.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  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, $_GET);
  384. }
  385. $sMetaTitle = $this->_getPageMetaTitle();
  386. $sName = $this->_getPageTitle();
  387. $a = [
  388. 'id' => $this->_aObject['id'],
  389. 'title' => $sMetaTitle ? $sMetaTitle : $sName,
  390. 'name' => $sName,
  391. 'uri' => $this->_aObject['uri'],
  392. 'url' => ((bx_get('params')[0] ? bx_get('params')[0] : $this->_aObject['uri'] ) . ($query_string != '' ? '?' . $query_string : '')),
  393. 'author' => $this->_aObject['author'],
  394. 'added' => $this->_aObject['added'],
  395. 'module' => $this->getModule(),
  396. 'type' => $this->getType (),
  397. 'layout' => str_replace('.html', '', $this->_aObject['template']),
  398. 'cover_block' => '',
  399. 'menu_top' => '',
  400. 'menu' => '',
  401. 'menu_bottom' => '',
  402. 'menu_add' => '',
  403. 'elements' => $this->getPageBlocksAPI($aBlocks),
  404. ];
  405. if (BxDolCover::getInstance($this)->isCover() && isset($this->_aProfileInfo)) {
  406. $oModule = BxDolModule::getInstance($this->getModule());
  407. $aContentInfo = bx_srv($this->getModule(), 'get_info', [$this->_aProfileInfo['content_id'], false]);
  408. $a['cover_block'] = [
  409. 'profile' => BxDolProfile::getData($this->_aProfileInfo['id'], [
  410. 'get_avatar' => 'getAvatarBig',
  411. 'with_info' => true
  412. ]),
  413. 'actions_menu' => '',
  414. 'meta_menu' => '',
  415. 'badges' => BxDolProfile::getInstance($this->_aProfileInfo['id'])->getBadges(),
  416. 'cover' => $oModule->serviceGetCover($this->_aProfileInfo['content_id']),
  417. 'allow_edit' => $oModule->checkAllowedChangeCover($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED
  418. ];
  419. $CNF = $oModule->_oConfig->CNF;
  420. if(!empty($CNF['OBJECT_MENU_VIEW_ENTRY_META'])){
  421. $oMetaMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_VIEW_ENTRY_META']);
  422. $a['cover_block']['meta_menu'] = $oMetaMenu->getCodeAPI();
  423. }
  424. if(!empty($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) !== false)
  425. $a['cover_block']['members_list'] = $oConnection->getConnectedListAPI($this->_aProfileInfo['id'], false, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS, 10);
  426. if(!empty($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY'])){
  427. $oActionMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_ALL']);
  428. $a['cover_block']['actions_menu'] = $oActionMenu->getCodeAPI();
  429. }
  430. }
  431. $sMenuTop = getParam('sys_api_menu_top');
  432. if (!empty($sMenuTop) && ($oMenuTop = BxDolMenu::getObjectInstance($sMenuTop)) !== false)
  433. $a['menu_top'] = $oMenuTop->getCodeAPI();
  434. if (($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  435. if(($sSubmenu = $this->getSubMenu()) !== '')
  436. $oMenuSubmenu->setObjectSubmenu($sSubmenu, []);
  437. $a['menu'] = $oMenuSubmenu->getCodeAPI();
  438. }
  439. if (isLogged()) {
  440. /* temporatery commented
  441. if(($oMenuAddContent = BxDolMenu::getObjectInstance('sys_add_content')) !== false)
  442. $a['menu_add'] = $oMenuAddContent->getCodeAPI();
  443. */
  444. $o = BxDolProfile::getInstance();
  445. $a['user'] = BxDolProfile::getDataForPage($o);
  446. $oInformer = BxDolInformer::getInstance(BxDolTemplate::getInstance());
  447. $sRet = $oInformer ? $oInformer->display() : '';
  448. if ($sRet){
  449. $a['user']['informer'] = $sRet;
  450. }
  451. $sModuleNotifications = 'bx_notifications';
  452. if(BxDolRequest::serviceExists($sModuleNotifications, 'get_unread_notifications_num'))
  453. $a['user']['notifications'] = bx_srv($sModuleNotifications, 'get_unread_notifications_num', [$o->id()]);
  454. $oPayments = BxDolPayments::getInstance();
  455. if($oPayments->isActive())
  456. $a['user']['cart'] = $oPayments->getCartItemsCount();
  457. }
  458. $aExtras = [
  459. 'page' => $this,
  460. 'blocks' => $aBlocks,
  461. 'data' => &$a,
  462. ];
  463. if(!$this->_isAvailablePage($this->_aObject))
  464. $a['page_status'] = 404;
  465. else if(!$this->_isVisiblePage($this->_aObject))
  466. $a['page_status'] = 403;
  467. bx_alert('system', 'get_page_api', 0, 0, $aExtras);
  468. return $a;
  469. }
  470. public function getPageBlocksAPI($aBlocks = [])
  471. {
  472. $bBlocks = !empty($aBlocks) && is_array($aBlocks);
  473. $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'];
  474. $aCells = $this->_oQuery->getPageBlocks(true);
  475. foreach($aCells as $sKey => &$aCell) {
  476. foreach($aCell as $i => $aBlock) {
  477. if(!$this->_isVisibleBlock($aBlock)) {
  478. unset($aCells[$sKey][$i]);
  479. continue;
  480. }
  481. $this->processPageBlock($aCells[$sKey][$i], true);
  482. $aBlock = $aCells[$sKey][$i];
  483. $sSource = '';
  484. if($aBlock['type'] == 'service') {
  485. $aContent = @unserialize($aBlock['content']);
  486. if(isset($aContent['module'], $aContent['method']))
  487. $sSource = $aContent['module'] . ':' . $aContent['method'];
  488. }
  489. else{
  490. $sSource = 'system:block_' . $aBlock['id'];
  491. }
  492. if($bBlocks && !in_array($sSource, $aBlocks)) {
  493. unset($aCells[$sKey][$i]);
  494. continue;
  495. }
  496. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  497. $mBlock = method_exists($this, $sFunc) ? $this->$sFunc($aBlock) : $aBlock['content'];
  498. $aCells[$sKey][$i] = array_merge($aCells[$sKey][$i], [
  499. 'title' => isset($mBlock['title']) ? $mBlock['title'] : $this->getBlockTitle($aBlock),
  500. 'content' => isset($mBlock['content']) ? $mBlock['content'] : $mBlock,
  501. 'menu' => isset($mBlock['menu']) ? $mBlock['menu'] : '',
  502. 'source' => $sSource
  503. ]);
  504. $aCells[$sKey][$i] = array_diff_key($aCells[$sKey][$i], array_flip($aFieldsUnset));
  505. }
  506. }
  507. return array_map('array_values', $aCells);
  508. }
  509. /**
  510. * Get page code vars
  511. * @return string
  512. */
  513. protected function _getPageCodeVars ()
  514. {
  515. $aHiddenOn = array(
  516. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  517. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  518. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  519. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  520. );
  521. $aVars = array (
  522. 'page_id' => 'bx-page-' . $this->_aObject['uri'],
  523. 'bx_if:show_layout_row_dump' => array(
  524. 'condition' => false,
  525. 'content' => array()
  526. )
  527. );
  528. $aBlocks = $this->_oQuery->getPageBlocks();
  529. foreach ($aBlocks as $sKey => $aCell) {
  530. $sCell = '';
  531. foreach ($aCell as $aBlock) {
  532. $this->processPageBlock($aBlock, false);
  533. $sContentWithBox = $this->_getBlockCodeWithCache($aBlock, $aBlock['async']);
  534. $sClassAdd = '';
  535. if(!empty($aBlock['class']))
  536. $sClassAdd .= ' ' . $aBlock['class'];
  537. if(!empty($aBlock['hidden_on']))
  538. foreach($aHiddenOn as $iHiddenOn => $sHiddenOnClass)
  539. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  540. $sClassAdd .= ' ' . $sHiddenOnClass;
  541. if ($sContentWithBox)
  542. $sCell .= $this->_oTemplate->parseHtmlByName('designbox_container.html', array(
  543. 'class_add' => $sClassAdd,
  544. 'bx_if:show_html_id' => array(
  545. 'condition' => true,
  546. 'content' => array(
  547. 'html_id' => 'bx-page-block-' . $aBlock['id']
  548. ),
  549. ),
  550. 'content' => $sContentWithBox
  551. ));
  552. }
  553. $aVars[$sKey] = $sCell;
  554. }
  555. return $aVars;
  556. }
  557. /**
  558. * Process block values, especially if someting need to be overrided
  559. */
  560. protected function processPageBlock(&$aBlock, $bApi = false)
  561. {
  562. }
  563. /**
  564. * Get page code only.
  565. * @return string
  566. */
  567. protected function _getPageCode ()
  568. {
  569. $aVars = $this->_getPageCodeVars ();
  570. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  571. }
  572. /**
  573. * Get one block code only.
  574. * @return string
  575. */
  576. protected function _getBlockOnlyCode ($iBlockId)
  577. {
  578. if (!($aBlock = $this->_oQuery->getPageBlock((int)$iBlockId)))
  579. return '';
  580. return $this->_getBlockCodeWithCache($aBlock, 0);
  581. }
  582. protected function _getBlockCodeWithCache(&$aBlock, $iAsync = 0)
  583. {
  584. if (bx_is_dbg() || !getParam('sys_pb_cache_enable') || !$aBlock['cache_lifetime'] || $iAsync || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  585. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  586. }
  587. else {
  588. $oCache = $this->_getBlockCacheObject();
  589. $sKey = $this->_getBlockCacheKey(false, $aBlock);
  590. $sKeyCssJs = 'cssjs_' . $sKey;
  591. $mixedRet = $oCache->getData($sKey, $aBlock['cache_lifetime']);
  592. $aRetCssJs = $oCache->getData($sKeyCssJs, $aBlock['cache_lifetime']);
  593. if ($mixedRet !== null && $aRetCssJs !== null) {
  594. BxDolTemplate::getInstance()->collectingStart();
  595. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  596. $sBlockCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  597. $sBlockCode .= $mixedRet;
  598. } else {
  599. BxDolTemplate::getInstance()->collectingStart();
  600. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  601. $aBlockCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  602. $oCache->setData($sKey, $sBlockCode, $aBlock['cache_lifetime']);
  603. $oCache->setData($sKeyCssJs, $aBlockCssJs, $aBlock['cache_lifetime']);
  604. }
  605. }
  606. return $sBlockCode;
  607. }
  608. /**
  609. * Get block code.
  610. * @return string
  611. */
  612. protected function _getBlockCode(&$aBlock, $iAsync = 0)
  613. {
  614. $sContentWithBox = '';
  615. $oFunctions = $this->_oTemplate->getTemplateFunctions();
  616. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  617. $aDbNoTitle = [BX_DB_CONTENT_ONLY, BX_DB_PADDING_CONTENT_ONLY, BX_DB_NO_CAPTION, BX_DB_PADDING_NO_CAPTION];
  618. $sTitle = $this->getBlockTitle($aBlock);
  619. $sHelp = $this->getBlockHelp($aBlock);
  620. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  621. $bBlockVisible = $this->_isVisibleBlock($aBlock);
  622. if ($iAsync && $bBlockVisible) {
  623. $iDesignboxId = $aBlock['designbox_id'];
  624. $sHelpTitle = $sHelpContent = '';
  625. if(!in_array($iDesignboxId, $aDbNoTitle))
  626. $sHelpTitle = $sHelp;
  627. else
  628. $sHelpContent = $sHelp;
  629. $sContent = $this->getBlockAsyncCode($aBlock, $iAsync);
  630. $aParams = array(
  631. $sTitle . $sHelpTitle,
  632. $sContent . $sHelpContent,
  633. $iDesignboxId
  634. );
  635. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  636. }
  637. elseif ($bBlockVisible && method_exists($this, $sFunc)) {
  638. $mixedContent = $this->$sFunc($aBlock);
  639. if(!empty($aBlock['content_empty'])) {
  640. $sEmpty = _t($aBlock['content_empty']);
  641. $sRegExp = '/(<[A-Za-z0-9]+\b.[^<]*[\pPi\pPf]bx\-msg\-box\b.*[\pPi\pPf][^>]*>)(' . _t('_Empty') . ')(<\/[A-Za-z0-9]+>)/su';
  642. if(is_string($mixedContent))
  643. $mixedContent = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent);
  644. else if(is_array($mixedContent) && !empty($mixedContent['content']))
  645. $mixedContent['content'] = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent['content']);
  646. }
  647. $this->_oQuery->setReadOnlyMode(true);
  648. if(is_array($mixedContent) && !empty($mixedContent['content'])) {
  649. $iDesignboxId = isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'];
  650. $sHelpTitle = $sHelpContent = '';
  651. if(!in_array($iDesignboxId, $aDbNoTitle))
  652. $sHelpTitle = $sHelp;
  653. else
  654. $sHelpContent = $sHelp;
  655. $aParams = array(
  656. (isset($mixedContent['title']) ? $mixedContent['title'] : $sTitle) . $sHelpTitle,
  657. $mixedContent['content'] . $sHelpContent,
  658. $iDesignboxId
  659. );
  660. $mixedMenu = false;
  661. if(isset($mixedContent['menu']))
  662. $mixedMenu = $mixedContent['menu'];
  663. else if(!empty($aBlock['submenu']))
  664. $mixedMenu = $aBlock['submenu'];
  665. $aParams[] = $mixedMenu;
  666. if(isset($mixedContent['buttons']))
  667. $aParams[] = $mixedContent['buttons'];
  668. else if($mixedMenu)
  669. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  670. if(isset($mixedContent['class']))
  671. $aBlock['class'] = $mixedContent['class'];
  672. }
  673. else if(is_string($mixedContent) && !empty($mixedContent)) {
  674. $iDesignboxId = $aBlock['designbox_id'];
  675. $sHelpTitle = $sHelpContent = '';
  676. if(!in_array($iDesignboxId, $aDbNoTitle))
  677. $sHelpTitle = $sHelp;
  678. else
  679. $sHelpContent = $sHelp;
  680. $aParams = array(
  681. $sTitle . $sHelpTitle,
  682. $mixedContent . $sHelpContent,
  683. $iDesignboxId
  684. );
  685. $mixedMenu = !empty($aBlock['submenu']) ? $aBlock['submenu'] : false;
  686. $aParams[] = $mixedMenu;
  687. if($mixedMenu)
  688. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  689. }
  690. if(!empty($aParams))
  691. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  692. }
  693. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  694. return $sContentWithBox;
  695. }
  696. /**
  697. * Add necessary js and css files.
  698. */
  699. protected function _addJsCss()
  700. {
  701. $this->_oTemplate->addJs(array('BxDolPage.js', 'theia-sticky-sidebar/theia-sticky-sidebar.min.js', 'theia-sticky-sidebar/ResizeSensor.min.js'));
  702. $this->_oTemplate->addCss('page_layouts.css');
  703. }
  704. /**
  705. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  706. */
  707. protected function _addSysTemplateVars ()
  708. {
  709. $oTemplate = BxDolTemplate::getInstance();
  710. $sPageTitle = $this->_getPageTitle();
  711. if ($sPageTitle)
  712. $oTemplate->setPageHeader ($sPageTitle);
  713. $sMetaTitle = $this->_getPageMetaTitle();
  714. if ($sMetaTitle)
  715. $oTemplate->setPageMetaTitle ($sMetaTitle);
  716. $sMetaDesc = $this->_getPageMetaDesc();
  717. if ($sMetaDesc)
  718. $oTemplate->setPageDescription ($sMetaDesc);
  719. $sMetaRobots = $this->_getPageMetaRobots();
  720. if ($sMetaRobots)
  721. $oTemplate->setPageMetaRobots ($sMetaRobots);
  722. $sMetaImage = $this->_getPageMetaImage();
  723. if ($sMetaImage)
  724. $oTemplate->addPageMetaImage($sMetaImage);
  725. $sMetaKeywords = $this->_getPageMetaKeywords();
  726. if ($sMetaKeywords)
  727. $oTemplate->addPageKeywords ($sMetaKeywords);
  728. }
  729. /**
  730. * Select menu from page properties.
  731. */
  732. protected function _selectMenu ()
  733. {
  734. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  735. }
  736. /**
  737. * Set page submenu if it's specified
  738. */
  739. protected function _setSubmenu ($aParams)
  740. {
  741. if(empty($this->_aObject['submenu']) || $this->_aObject['submenu'] == 'disabled')
  742. return;
  743. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  744. if(!$oMenuSubmenu)
  745. return;
  746. $oMenuSubmenu->setObjectSubmenu($this->_aObject['submenu'], $aParams);
  747. }
  748. /**
  749. * Get content for 'raw' block type.
  750. * @return string
  751. */
  752. protected function _getBlockRaw ($aBlock)
  753. {
  754. if (bx_is_api()){
  755. return [bx_api_get_block('raw', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  756. }
  757. $s = '<div class="bx-page-raw-container bx-def-vanilla-html max-w-none">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  758. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  759. $s = bx_process_macros($s);
  760. return $s;
  761. }
  762. /**
  763. * Get content for 'custom' block type.
  764. * @return string
  765. */
  766. protected function _getBlockCustom ($aBlock)
  767. {
  768. if (bx_is_api()){
  769. return [bx_api_get_block('custom', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  770. }
  771. $s = '<div class="bx-page-custom-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  772. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  773. $s = bx_process_macros($s);
  774. return $s;
  775. }
  776. /**
  777. * Get content for 'html' block type.
  778. * @return string
  779. */
  780. protected function _getBlockHtml ($aBlock)
  781. {
  782. if (bx_is_api()){
  783. return [bx_api_get_block('html', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  784. }
  785. $s = '<div class="bx-page-html-container bx-def-vanilla-html max-w-none">' . $aBlock['content'] . '</div>';
  786. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  787. $s = bx_process_macros($s);
  788. return $s;
  789. }
  790. /**
  791. * Get content for 'creative' block type.
  792. * @return string
  793. */
  794. protected function _getBlockCreative ($aBlock)
  795. {
  796. if(bx_is_api())
  797. return [bx_api_get_block('creative', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  798. $s = $this->_replaceMarkers($aBlock['content'], ['block_id' => $aBlock['id']]);
  799. $s = bx_process_macros($s);
  800. $bCanManage = $this->isAllowedCreativeManage();
  801. if($bCanManage) {
  802. $this->_oTemplate->addCss([
  803. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'grapesjs/|grapes.min.css',
  804. ]);
  805. $this->_oTemplate->addJs([
  806. 'grapesjs/grapes.min.js',
  807. 'grapesjs/grapesjs-blocks-basic.js',
  808. 'grapesjs/grapesjs-style-bg.js',
  809. 'grapesjs/grapesjs-preset-webpage.min.js',
  810. ]);
  811. }
  812. return $this->_oTemplate->parseHtmlByName('block_creative.html', [
  813. 'block_id' => $aBlock['id'],
  814. 'content' => $s,
  815. 'bx_if:show_controls' => [
  816. 'condition' => $bCanManage,
  817. 'content' => [
  818. 'onclick_edit' => $this->_sJsObjectName . '.creativeEdit(this, ' . $aBlock['id'] . ')',
  819. 'onclick_save' => $this->_sJsObjectName . '.creativeSave(this, ' . $aBlock['id'] . ')'
  820. ]
  821. ]
  822. ]);
  823. }
  824. /**
  825. * Get content for 'wiki' block type.
  826. * @return string
  827. */
  828. protected function _getBlockWiki ($aBlock)
  829. {
  830. $oWiki = BxDolWiki::getObjectInstance($this->_aObject['module']);
  831. if (!$oWiki) {
  832. $sContent = _t('_sys_wiki_error_missing_wiki_object', $this->_aObject['module']);
  833. }
  834. else {
  835. $sContent = $oWiki->getBlockContent($aBlock['id'], false, (int)bx_get($aBlock['id'].'rev') ? (int)bx_get($aBlock['id'].'rev') : false);
  836. }
  837. $s = '<div id="bx-page-wiki-container-' . $aBlock['id'] . '" class="bx-page-wiki-container markdown-body bx-def-vanilla-html">' . $sContent . '</div>';
  838. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  839. $s = bx_process_macros($s);
  840. return $s;
  841. }
  842. /**
  843. * Get content for 'lang' block type.
  844. * @return string
  845. */
  846. protected function _getBlockLang ($aBlock)
  847. {
  848. if (bx_is_api()){
  849. return [bx_api_get_block('lang', ['title' => _t($aBlock['title']), 'content' => bx_process_macros(_t(trim($aBlock['content'])))])];
  850. }
  851. $s = '<div class="bx-page-lang-container bx-def-vanilla-html max-w-none">' . _t(trim($aBlock['content'])) . '</div>';
  852. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  853. $s = bx_process_macros($s);
  854. return $s;
  855. }
  856. /**
  857. * Get content for 'image' block type.
  858. * @return string
  859. */
  860. protected function _getBlockImage ($aBlock)
  861. {
  862. if (empty($aBlock['content']))
  863. return false;
  864. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  865. $iFileId = (int)$iFileId;
  866. if (!$iFileId)
  867. return false;
  868. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  869. if (!$oStorage)
  870. return false;
  871. $sUrl = $oStorage->getFileUrlById($iFileId);
  872. if (!$sUrl)
  873. return false;
  874. $sStyleAdd = '';
  875. $sClassAdd = 'bx-page-image-container';
  876. $aAlign = ['left' => 'start', 'center' => 'center', 'right' => 'end'];
  877. if(array_key_exists($sAlign, $aAlign)) {
  878. $sClassAdd .= ' flex justify-' . $aAlign[$sAlign];
  879. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  880. }
  881. return '<div class="' . $sClassAdd . '" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  882. }
  883. /**
  884. * Get content for 'rss' block type.
  885. * @return string
  886. */
  887. protected function _getBlockRss ($aBlock)
  888. {
  889. if (empty($aBlock['content']))
  890. return false;
  891. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  892. $iNum = (int)$iNum;
  893. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  894. }
  895. /**
  896. * Get content for 'menu' block type.
  897. * @return string
  898. */
  899. protected function _getBlockMenu ($aBlock)
  900. {
  901. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  902. return $oMenu ? $oMenu->getCode () : '';
  903. }
  904. /**
  905. * Get content for 'service' block type.
  906. * @return string
  907. */
  908. protected function _getBlockService ($aBlock)
  909. {
  910. $aMarkers = array_merge($this->_aMarkers, [
  911. 'block_id' => $aBlock['id']
  912. ]);
  913. return BxDolService::callSerialized($aBlock['content'], $aMarkers);
  914. }
  915. /**
  916. * Get page title.
  917. * @return string
  918. */
  919. protected function _getPageTitle()
  920. {
  921. return $this->_replaceMarkers(_t($this->_aObject['title']));
  922. }
  923. /**
  924. * Get page meta description.
  925. * @return string
  926. */
  927. protected function _getPageMetaDesc()
  928. {
  929. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  930. }
  931. /**
  932. * Get page meta title.
  933. * @return string
  934. */
  935. protected function _getPageMetaTitle()
  936. {
  937. return $this->_replaceMarkers(_t($this->_aObject['meta_title']));
  938. }
  939. /**
  940. * Get page meta image.
  941. * @return string
  942. */
  943. protected function _getPageMetaImage()
  944. {
  945. return '';
  946. }
  947. /**
  948. * Get page meta keywords.
  949. * @return string
  950. */
  951. protected function _getPageMetaKeywords()
  952. {
  953. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  954. }
  955. /**
  956. * Get page meta robots.
  957. * @return string
  958. */
  959. protected function _getPageMetaRobots()
  960. {
  961. return _t($this->_aObject['meta_robots']);
  962. }
  963. /**
  964. * Get page not found message.
  965. * @return string
  966. */
  967. protected function _getPageNotFoundMsg ($mixedMsg = false)
  968. {
  969. $this->_oTemplate->displayPageNotFound();
  970. exit;
  971. }
  972. /**
  973. * Get access denied message.
  974. * @return string
  975. */
  976. protected function _getPageAccessDeniedMsg ($mixedMsg = false)
  977. {
  978. return MsgBox($mixedMsg !== false ? $mixedMsg : _t('_Access denied'));
  979. }
  980. /**
  981. * Get page cache object.
  982. * @return cache object instance
  983. */
  984. protected function _getPageCacheObject ()
  985. {
  986. return $this->_getCacheObject ();
  987. }
  988. /**
  989. * Get block cache object.
  990. * @return cache object instance
  991. */
  992. protected function _getBlockCacheObject ()
  993. {
  994. return $this->_getCacheObject ('Block', 'pb');
  995. }
  996. protected function _getCacheObject ($sSuffixObj = 'Page', $sSuffixParam = 'page')
  997. {
  998. $sObj = '_o' . $sSuffixObj . 'CacheObject';
  999. $sParam = 'sys_' . $sSuffixParam . '_cache_engine';
  1000. if ($this->{$sObj} != null) {
  1001. return $this->{$sObj};
  1002. } else {
  1003. $sEngine = getParam($sParam);
  1004. $this->{$sObj} = bx_instance ('BxDolCache' . $sEngine);
  1005. if (!$this->{$sObj}->isAvailable())
  1006. $this->{$sObj} = bx_instance ('BxDolCacheFile');
  1007. return $this->{$sObj};
  1008. }
  1009. }
  1010. /**
  1011. * Get page cache key.
  1012. * @param $isPrefixOnly return key prefix only.
  1013. * @return string
  1014. */
  1015. protected function _getPageCacheKey ($isPrefixOnly = false)
  1016. {
  1017. $s = 'page_' . $this->_aObject['object'] . '_';
  1018. if ($isPrefixOnly)
  1019. return $s;
  1020. $s .= $this->_getPageCacheParams ();
  1021. $s .= bx_site_hash() . '.php';
  1022. return $s;
  1023. }
  1024. /**
  1025. * Get block cache key.
  1026. * @param $isPrefixOnly return key prefix only.
  1027. * @return string
  1028. */
  1029. protected function _getBlockCacheKey ($isPrefixOnly = false, $aBlock = array())
  1030. {
  1031. $s = $this->_getPageCacheKey(true) . 'block_';
  1032. if ($isPrefixOnly)
  1033. return $s;
  1034. $s .= ($aBlock ? $aBlock['id'] : 0) . '_' . $this->_getPageCacheParams ();
  1035. $s .= bx_site_hash() . '.php';
  1036. return $s;
  1037. }
  1038. /**
  1039. * Additional cache key. In the case of dynamic page.
  1040. * For example - profile page, where each profile has own page.
  1041. * @return string
  1042. */
  1043. protected function _getPageCacheParams ()
  1044. {
  1045. return md5(serialize($_GET) . bx_lang_name());
  1046. }
  1047. /**
  1048. * Clean page cache.
  1049. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  1050. * @return string
  1051. */
  1052. protected function cleanCache ($isDelAllWithPagePrefix = false)
  1053. {
  1054. $a = ['Page', 'Block'];
  1055. $bRet = false;
  1056. foreach ($a as $s) {
  1057. $oCache = $this->{'_get' . $s . 'CacheObject'} ();
  1058. $sKey = $this->{'_get'. $s . 'CacheKey'} ($isDelAllWithPagePrefix);
  1059. if ($isDelAllWithPagePrefix)
  1060. $bRet = $bRet && $oCache->removeAllByPrefix($sKey);
  1061. else
  1062. $bRet = $bRet && $oCache->delData($sKey);
  1063. }
  1064. return $bRet;
  1065. }
  1066. }
  1067. /** @} */