BxBasePage.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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. if (isLogged() && $oProfile = BxDolProfile::getInstance()) {
  42. $this->addMarkers([
  43. 'logged_profile_id' => $oProfile->id(),
  44. 'logged_account_id' => $oProfile->getAccountId(),
  45. 'logged_profile_module' => $oProfile->getModule(),
  46. 'logged_display_name' => $oProfile->getDisplayName(),
  47. 'logged_profile_url' => $oProfile->getUrl(),
  48. ]);
  49. }
  50. if (isset($GLOBALS['bxReplaceableMarkersPages'])) {
  51. $this->addMarkers($GLOBALS['bxReplaceableMarkersPages']);
  52. }
  53. }
  54. public function performActionGetHelp ()
  55. {
  56. $iBlockId = (int)bx_get('block_id');
  57. if(empty($iBlockId))
  58. return;
  59. $aBlock = $this->_oQuery->getPageBlock($iBlockId);
  60. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['help']))
  61. return;
  62. echo $this->_oTemplate->parseHtmlByName('designbox_help_popup.html', array(
  63. 'content' => _t($aBlock['help'])
  64. ));
  65. }
  66. public function performActionGetShare ()
  67. {
  68. $aEmbedData = BxDolPage::getEmbedData(bx_get('url'));
  69. if(!isset($aEmbedData['url']))
  70. return;
  71. $sMenu = '';
  72. if(getParam('sys_a2a_enable') != 'on') {
  73. if(($oMenu = BxDolMenu::getObjectInstance('sys_social_sharing')) !== false) {
  74. $oMenu->addMarkers([
  75. 'url' => $aEmbedData['url'],
  76. 'img_url_encoded' => '',
  77. 'title_encoded' => ''
  78. ]);
  79. $sMenu = $oMenu->getCode();
  80. }
  81. }
  82. else
  83. $sMenu = getParam('sys_a2a_code');
  84. echo PopupBox('sys_share_popup', _t('_sys_txt_share_popup_header'), $this->_oTemplate->parseHtmlByName('designbox_share_popup.html', [
  85. 'url' => bx_process_output($aEmbedData['url']),
  86. 'menu' => $sMenu,
  87. 'code' => htmlspecialchars($aEmbedData['html']),
  88. ]), true);
  89. }
  90. public function performActionEmbed ()
  91. {
  92. $aCover = $this->getPageCoverImage();
  93. $sTitle = $this->_getPageTitle();
  94. $this->_oTemplate->getEmbed($this->_oTemplate->parseHtmlByName('embed_card.html', [
  95. 'title' => $sTitle,
  96. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  97. 'bx_if:thumb' => [
  98. 'condition' => $this->isPageCover() && count($aCover) > 0,
  99. 'content' => [
  100. 'title' => $sTitle,
  101. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  102. 'background' => BxDolCover::getInstance(BxDolTemplate::getInstance())->getCoverImageUrl($aCover)
  103. ],
  104. ],
  105. 'bx_if:no_thumb' => [
  106. 'condition' => count($aCover) == 0 || !$this->isPageCover(),
  107. 'content' => [
  108. 'title' => $sTitle,
  109. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  110. ]
  111. ]
  112. ]
  113. ));
  114. }
  115. public function performActionCreativeSave()
  116. {
  117. if(!$this->isAllowedCreativeManage())
  118. return echoJson(['code' => 1]);
  119. $iId = 0;
  120. $sContent = '';
  121. if(($iId = bx_get('b')) === false || ($sContent = bx_get('c')) === false)
  122. return echoJson(['code' => 2]);
  123. $iId = bx_process_input($iId, BX_DATA_INT);
  124. $sContent = bx_process_input($sContent, BX_DATA_HTML);
  125. if(!$this->_oQuery->setPageBlockContent($iId, $sContent))
  126. return echoJson(['code' => 3]);
  127. return echoJson(['code' => 0]);
  128. }
  129. public function isAllowedCreativeManage()
  130. {
  131. return isAdmin();
  132. }
  133. /**
  134. * Very similar to BxBasePage::getCode
  135. * but adds css and js files which are needed for the corect page display
  136. */
  137. public function getCodeDynamic ()
  138. {
  139. // TODO: remake to use use collect* template methods
  140. $oTemplate = BxDolTemplate::getInstance();
  141. // get js&css before the page code is generated
  142. $aCssBefore = $oTemplate->getCss();
  143. $aJsBefore = $oTemplate->getJs();
  144. // generate page code
  145. $sContent = $this->getCode();
  146. // get js&css after the page code is generated
  147. $aCssAfter = $oTemplate->getCss();
  148. $aJsAfter = $oTemplate->getJs();
  149. // compare files which were added before and after page code is generated
  150. $f = function ($a1, $a2) {
  151. return strcasecmp($a1['url'], $a2['url']);
  152. };
  153. $aCssNew = array_udiff($aCssAfter, $aCssBefore, $f);
  154. $aJsNew = array_udiff($aJsAfter, $aJsBefore, $f);
  155. // add newly added js&css files in static mode
  156. $sCss = $sJs = '';
  157. foreach ($aCssNew as $a)
  158. $sCss .= $oTemplate->addCss($a['url'], true);
  159. foreach ($aJsNew as $a)
  160. $sJs .= $oTemplate->addJs($a['url'], true);
  161. return $sJs . $sCss . $sContent;
  162. }
  163. public function getIncludes ()
  164. {
  165. $oTemplate = BxDolTemplate::getInstance();
  166. // generate page code
  167. $this->getCode();
  168. // get js&css after the page code is generated
  169. $aCss = $oTemplate->getCss();
  170. $aJs = $oTemplate->getJs();
  171. // add js&css files in static mode
  172. $sResult = '';
  173. foreach ($aCss as $a)
  174. $sResult .= $oTemplate->addCss($a['url'], true);
  175. foreach ($aJs as $a)
  176. $sResult .= $oTemplate->addJs($a['url'], true);
  177. // add js translations
  178. $sResult .= $oTemplate->getJsTranslation(true);
  179. return $sResult;
  180. }
  181. /**
  182. * Get page code with automatic caching, adding necessary css/js files and system template vars.
  183. * @return string.
  184. */
  185. public function getCode ()
  186. {
  187. if (bx_get('dynamic') && ($iBlockId = (int)bx_get('pageBlock'))) {
  188. if (self::isLockedFromUnauthenticated($this->_aObject['uri']) || !$this->_isVisiblePage($this->_aObject)) {
  189. header('HTTP/1.0 403 Forbidden');
  190. exit;
  191. }
  192. /**
  193. * @hooks
  194. * @hookdef hook-system-page_output_block 'system', 'page_output_block' - hook with page block data to be output
  195. * - $unit_name - equals `system`
  196. * - $action - equals `page_output_block`
  197. * - $object_id - not used
  198. * - $sender_id - not used
  199. * - $extra_params - array of additional params with the following array keys:
  200. * - `page_name` - [string] page object name
  201. * - `page_object` - [object] an instance of page class, @see BxDolPage
  202. * - `page_query` - [object] an instance of page related query class
  203. * - `block_id` - [int] block id
  204. * @hook @ref hook-system-page_output_block
  205. */
  206. bx_alert('system', 'page_output_block', 0, false, [
  207. 'page_name' => $this->_sObject,
  208. 'page_object' => $this,
  209. 'page_query' => $this->_oQuery,
  210. 'block_id' => (int)$iBlockId,
  211. ]);
  212. header( 'Content-type:text/html;charset=utf-8' );
  213. BxDolTemplate::getInstance()->collectingStart();
  214. $s = $this->_getBlockOnlyCode($iBlockId);
  215. $sCss = bx_get('includedCss');
  216. $sJs = bx_get('includedJs');
  217. echo BxDolTemplate::getInstance()->collectingEndGetCode($sCss ? @json_decode($sCss) : [], $sJs ? @json_decode($sJs) : []);
  218. echo $s;
  219. exit;
  220. }
  221. if (($mixedAvailable = $this->_isAvailablePage($this->_aObject)) !== true)
  222. return $this->_getPageNotFoundMsg($mixedAvailable);
  223. if (($mixedVisible = $this->_isVisiblePage($this->_aObject)) !== true)
  224. return $this->_getPageAccessDeniedMsg($mixedVisible);
  225. $this->_addJsCss();
  226. if (!$this->_bSubPage) {
  227. $this->_addSysTemplateVars();
  228. $this->_selectMenu();
  229. $this->_setSubmenu(array());
  230. }
  231. if (bx_is_dbg() || !getParam('sys_page_cache_enable') || !$this->_aObject['cache_lifetime'] || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  232. $sPageCode = $this->_getPageCode();
  233. }
  234. else {
  235. $oCache = $this->_getPageCacheObject();
  236. $sKey = $this->_getPageCacheKey();
  237. $sKeyCssJs = 'cssjs_' . $sKey;
  238. $mixedRet = $oCache->getData($sKey, $this->_aObject['cache_lifetime']);
  239. $aRetCssJs = $oCache->getData($sKeyCssJs, $this->_aObject['cache_lifetime']);
  240. if ($mixedRet !== null && $aRetCssJs !== null) {
  241. BxDolTemplate::getInstance()->collectingStart();
  242. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  243. $sPageCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  244. $sPageCode .= $mixedRet;
  245. } else {
  246. BxDolTemplate::getInstance()->collectingStart();
  247. $sPageCode = $this->_getPageCode();
  248. $aPageCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  249. $oCache->setData($sKey, $sPageCode, $this->_aObject['cache_lifetime']);
  250. $oCache->setData($sKeyCssJs, $aPageCssJs, $this->_aObject['cache_lifetime']);
  251. }
  252. }
  253. /**
  254. * @hooks
  255. * @hookdef hook-system-page_output 'system', 'page_output' - hook with page data to be output
  256. * - $unit_name - equals `system`
  257. * - $action - equals `page_output`
  258. * - $object_id - not used
  259. * - $sender_id - not used
  260. * - $extra_params - array of additional params with the following array keys:
  261. * - `page_name` - [string] page object name
  262. * - `page_object` - [object] an instance of page class, @see BxDolPage
  263. * - `page_query` - [object] an instance of page related query class
  264. * - `page_code` - [string] by ref, final page code to be output, can be overridden in hook processing
  265. * - `sub_page` - [boolean] if the page is used as subpage of some parent page
  266. * @hook @ref hook-system-page_output
  267. */
  268. bx_alert('system', 'page_output', 0, false, [
  269. 'page_name' => $this->_sObject,
  270. 'page_object' => $this,
  271. 'page_query' => $this->_oQuery,
  272. 'page_code' => &$sPageCode,
  273. 'sub_page' => $this->_bSubPage,
  274. ]);
  275. if (!$this->_bSubPage)
  276. $sPageCode .= $this->getJsScript();
  277. return $sPageCode;
  278. }
  279. public function getJsClassName()
  280. {
  281. return $this->_sJsClassName;
  282. }
  283. public function getJsObjectName()
  284. {
  285. return $this->_sJsObjectName;
  286. }
  287. function _wrapInTagJsCode($sCode)
  288. {
  289. return "<script language=\"javascript\">\n<!--\n" . $sCode . "\n-->\n</script>";
  290. }
  291. public function getJsScript()
  292. {
  293. $sJsObjName = $this->getJsObjectName();
  294. $sJsObjClass = $this->getJsClassName();
  295. $sCode = "if(window['" . $sJsObjName . "'] == undefined) var " . $sJsObjName . " = new " . $sJsObjClass . "(" . json_encode(array(
  296. 'sObjName' => $sJsObjName,
  297. 'sObject' => $this->_sObject,
  298. 'sRootUrl' => BX_DOL_URL_ROOT,
  299. 'aHtmlIds' => $this->_aHtmlIds,
  300. 'isStickyColumns' => $this->_bStickyColumns
  301. )) . ");";
  302. return $this->_wrapInTagJsCode($sCode);
  303. }
  304. /**
  305. * Is page cover enabled.
  306. * @return string
  307. */
  308. public function isPageCover()
  309. {
  310. $bResult = false;
  311. switch((int)$this->_aObject['cover']) {
  312. case 1: //--- Enabled for all
  313. $bResult = true;
  314. break;
  315. case 2: //--- Enabled for visitors only
  316. $bResult = !isLogged();
  317. break;
  318. case 3: //--- Enabled for members only
  319. $bResult = isLogged();
  320. break;
  321. }
  322. return $bResult;
  323. }
  324. public function setSubPage($b = true)
  325. {
  326. $this->_bSubPage = $b;
  327. }
  328. public function setPageCover($bCover = true)
  329. {
  330. $this->_aObject['cover'] = (bool)$bCover;
  331. }
  332. public function getPageCoverImage($bTranscoder = true)
  333. {
  334. $iId = (int)$this->_aObject['cover_image'];
  335. if(empty($iId)) {
  336. $iId = (int)getParam('sys_site_cover_common');
  337. if(empty($iId))
  338. return array();
  339. }
  340. $aResult = array(
  341. 'id' => $iId
  342. );
  343. if($bTranscoder)
  344. $aResult['transcoder'] = BX_DOL_TRANSCODER_OBJ_COVER;
  345. else
  346. $aResult['object'] = $this->_sStorage;
  347. return $aResult;
  348. }
  349. public function getPageCoverParams()
  350. {
  351. $aParams = [
  352. 'title' => $this->_getPageTitle(),
  353. 'actions' => '',
  354. 'bx_if:image' => [
  355. 'condition' => false,
  356. 'content' => []
  357. ],
  358. 'bx_if:icon' => [
  359. 'condition' => false,
  360. 'content' => []
  361. ],
  362. ];
  363. if(($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  364. $aCoverParams = $oMenuSubmenu->getPageCoverParams();
  365. if(!empty($aCoverParams) && is_array($aCoverParams))
  366. $aParams = $aCoverParams;
  367. }
  368. if(!empty($this->_aObject['cover_title'])) {
  369. $sCoverTitle = _t($this->_aObject['cover_title']);
  370. if($sCoverTitle && strcmp($sCoverTitle, $this->_aObject['cover_title']) != 0)
  371. $aParams['title'] = $sCoverTitle;
  372. }
  373. return $aParams;
  374. }
  375. /**
  376. * Get block title.
  377. * @return string
  378. */
  379. public function getBlockTitle ($aBlock)
  380. {
  381. return $this->_replaceMarkers(_t($aBlock['title']), array('block_id' => $aBlock['id']));
  382. }
  383. /**
  384. * Get help control if help is available for the block.
  385. * @return string
  386. */
  387. public function getBlockHelp ($aBlock)
  388. {
  389. if(empty($aBlock['help']) || empty(strip_tags(_t($aBlock['help']))))
  390. return '';
  391. return $this->_oTemplate->parseHtmlByName('designbox_help.html', array(
  392. 'js_object' => $this->_sJsObjectName,
  393. 'block_id' => $aBlock['id']
  394. ));
  395. }
  396. /**
  397. * Get code to load block asyncroniously
  398. * @param $aBlock block code
  399. * @param $iAsync if greater than 0 the it defines loading indicator for the block
  400. * @return HTML code string
  401. */
  402. public function getBlockAsyncCode($aBlock, $iAsync)
  403. {
  404. $aContentPlaceholder = $this->_oQuery->getPageBlockContentPlaceholder($iAsync);
  405. if (!$aContentPlaceholder)
  406. return _t('_sys_txt_error_occured');
  407. $oTemplate = $this->_oTemplate;
  408. if ('system' != $aContentPlaceholder['module']) {
  409. $oModule = BxDolModule::getInstance($aContentPlaceholder['module']);
  410. if (!$oModule)
  411. return _t('_sys_txt_error_occured');
  412. $oTemplate = $oModule->_oTemplate;
  413. }
  414. return $oTemplate->parseHtmlByName($aContentPlaceholder['template'], $aBlock);
  415. }
  416. /**
  417. * Get page array with all cells and blocks
  418. */
  419. public function getPageAPI ($aBlocks = [])
  420. {
  421. define('BX_API_PAGE', true);
  422. $aGetParams = bx_get('params');
  423. $bGetParams = $aGetParams !== false;
  424. $sQueryString = '';
  425. if($bGetParams && !empty($aGetParams[2]) && ($_aGetParams = json_decode($aGetParams[2], true)) && is_array($_aGetParams)) {
  426. $sQueryString = http_build_query($_aGetParams);
  427. parse_str($sQueryString, $aQueryString);
  428. $_GET = array_merge($_GET, $aQueryString);
  429. }
  430. $bIsAvailable = $this->_isAvailablePage($this->_aObject);
  431. $bIsVisible = $this->_isVisiblePage($this->_aObject);
  432. //TODO: If page isn't Available or isn't Visible then we should exit with related Error Codes immediately.
  433. $sMetaTitle = $this->_getPageMetaTitle();
  434. $sName = $this->_getPageTitle();
  435. $sModule = $this->getModule();
  436. $a = [
  437. 'id' => $this->_aObject['id'],
  438. 'title' => $sMetaTitle ? $sMetaTitle : $sName,
  439. 'name' => $sName,
  440. 'description' => $this->_getPageMetaDesc(),
  441. 'keywords' => $this->_getPageMetaKeywords(),
  442. 'image' => '',
  443. 'uri' => $this->_aObject['uri'],
  444. 'url' => ($bGetParams && !empty($aGetParams[0]) ? $aGetParams[0] : $this->_aObject['uri']) . ($sQueryString != '' ? '?' . $sQueryString : ''),
  445. 'author' => $this->_aObject['author'],
  446. 'added' => $this->_aObject['added'],
  447. 'module' => $sModule,
  448. 'type' => $this->getType (),
  449. 'layout' => str_replace('.html', '', $this->_aObject['template']),
  450. 'cover_block' => '',
  451. 'menu_top' => '',
  452. 'menu' => '',
  453. 'menu_bottom' => '',
  454. 'menu_add' => '',
  455. 'config' => $this->_aObject['config_api'],
  456. 'elements' => $this->getPageBlocksAPI($aBlocks),
  457. ];
  458. $sMenuTop = getParam('sys_api_menu_top');
  459. if (!empty($sMenuTop) && ($oMenuTop = BxDolMenu::getObjectInstance($sMenuTop)) !== false)
  460. $a['menu_top'] = $oMenuTop->getCodeAPI();
  461. if (($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  462. if(($sSubmenu = $this->getSubMenu()) !== '')
  463. $oMenuSubmenu->setObjectSubmenu($sSubmenu, []);
  464. $a['menu'] = $oMenuSubmenu->getCodeAPI();
  465. if($sModule && $sModule != 'system' && ($oModule = BxDolModule::getInstance($sModule))) {
  466. $CNF = &$oModule->_oConfig->CNF;
  467. $sUrlAdd = '';
  468. if(($sKey = 'URI_ADD_ENTRY') && !empty($CNF[$sKey]))
  469. $sUrlAdd = $CNF[$sKey];
  470. if(!$sUrlAdd && ($sKey = 'URI_EDIT_ENTRY') && !empty($CNF[$sKey]))
  471. $sUrlAdd = str_replace('edit-', 'create-', $CNF[$sKey]);
  472. $a['menu'] = array_merge($a['menu'], [
  473. 'name' => $oModule->getName(),
  474. 'title' => BxDolModule::getTitle($oModule->_oConfig->getUri()),
  475. 'icon' => !empty($CNF['ICON']) ? $CNF['ICON'] : '',
  476. 'add_url' => $sUrlAdd
  477. ]);
  478. }
  479. }
  480. /**
  481. * Profile/Context view page.
  482. */
  483. if(isset($this->_aProfileInfo)) {
  484. $oProfileModule = BxDolModule::getInstance($this->_aProfileInfo['type']);
  485. if($oProfileModule !== null)
  486. $aProfileContentInfo = $oProfileModule->_oDb->getContentInfoById($this->_aProfileInfo['content_id']);
  487. if(!empty($aProfileContentInfo) && is_array($aProfileContentInfo)) {
  488. $CNF = &$oProfileModule->_oConfig->CNF;
  489. /**
  490. * Process page cover with related menus and lists.
  491. */
  492. if(BxDolCover::getInstance($this)->isCover()) {
  493. $aProfileInfo = BxDolProfile::getData($this->_aProfileInfo['id'], [
  494. 'get_avatar' => 'getAvatarBig',
  495. 'with_info' => true
  496. ]);
  497. $oProfile = BxDolProfile::getInstance($this->_aProfileInfo['id']);
  498. $aProfileInfo['online'] = $oProfile->isOnline();
  499. unset($aProfileInfo['info']['profile_email']);
  500. unset($aProfileInfo['info']['profile_last_active']);
  501. unset($aProfileInfo['info']['profile_ip']);
  502. $a['cover_block'] = [
  503. 'profile' => $aProfileInfo,
  504. 'actions_menu' => '',
  505. 'meta_menu' => '',
  506. 'badges' => $oProfile->getBadges(),
  507. 'cover' => $oProfileModule->serviceGetCover($this->_aProfileInfo['content_id']),
  508. 'allow_edit' => $oProfileModule->checkAllowedChangeCover($aProfileContentInfo) === CHECK_ACTION_RESULT_ALLOWED
  509. ];
  510. if(!empty($CNF['OBJECT_MENU_VIEW_ENTRY_META']) && ($oMetaMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_VIEW_ENTRY_META'])) !== false)
  511. $a['cover_block']['meta_menu'] = $oMetaMenu->getCodeAPI();
  512. if(!empty($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) !== false)
  513. $a['cover_block']['members_list'] = $oConnection->getConnectedListAPI($this->_aProfileInfo['id'], false, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS, 10);
  514. if(!empty($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_ALL']) && ($oActionMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_ALL'])) !== false)
  515. $a['cover_block']['actions_menu'] = $oActionMenu->getCodeAPI();
  516. }
  517. /**
  518. * Process page mange menu.
  519. */
  520. if(($oMenuManage = BxDolMenu::getObjectInstance('sys_site_manage')) !== false) {
  521. if(!empty($CNF['FIELD_ID']))
  522. $oMenuManage->setContentId($aProfileContentInfo[$CNF['FIELD_ID']]);
  523. $aObjectManage = [];
  524. if(!empty($CNF['OBJECT_MENU_MANAGE_VIEW_ENTRY']))
  525. $aObjectManage = [$CNF['OBJECT_MENU_MANAGE_VIEW_ENTRY']];
  526. else if(!empty($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE']))
  527. $aObjectManage = [$CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE'], true];
  528. call_user_func_array([$oMenuManage, 'setObjectManage'], $aObjectManage);
  529. $aMenuManage = $oMenuManage->getCodeAPI();
  530. if(!empty($aMenuManage) && is_array($aMenuManage)) {
  531. if(isset($a['cover_block'], $a['cover_block']['actions_menu']) && is_array($a['cover_block']['actions_menu']))
  532. $a['cover_block']['actions_menu']['items'] = array_merge($a['cover_block']['actions_menu']['items'], $aMenuManage['items']);
  533. else
  534. $a['cover_block']['actions_menu'] = $aMenuManage;
  535. }
  536. }
  537. }
  538. }
  539. if(isLogged() && ($o = BxDolProfile::getInstance()) !== false) {
  540. $iLogged = $o->id();
  541. $a['user'] = BxDolProfile::getDataForPage($o);
  542. if(($sContextSwitcher = getParam('sys_api_context_switcher')) && ($sContextConnection = getParam('sys_api_context_connection'))) {
  543. $aCpIds = bx_srv($sContextSwitcher, 'get_participating_profiles', [$iLogged, ($sContextConnection != 'participants' ? $sContextConnection : false)]);
  544. $aContexts = [];
  545. foreach($aCpIds as $iCpId)
  546. $aContexts[] = BxDolProfile::getData($iCpId);
  547. $aInvitedTo = [];
  548. $aInvitations = bx_srv($sContextSwitcher, 'invitations', [$iLogged, true]);
  549. foreach($aInvitations as $aInvitation)
  550. $aInvitedTo[] = BxDolProfile::getData($aInvitation['group_profile_id']);
  551. $aInfo = [];
  552. if(($iId = bx_get('id')) !== false) {
  553. $aInfo = bx_srv($sModule, 'get_info', [(int)$iId, false]);
  554. }
  555. else if(($iPid = bx_get('profile_id')) !== false) {
  556. $aInfo = bx_srv($sModule, 'get_content_info_by_profile_id', [(int)$iPid]);
  557. }
  558. $aCurrent = [];
  559. if($aInfo && ((($sKey = 'allow_view_to') && isset($aInfo[$sKey]) && ($iValue = (int)$aInfo[$sKey])) || (($sKey = 'object_privacy_view') && isset($aInfo[$sKey]) && ($iValue = (int)$aInfo[$sKey])))) {
  560. $mixedContext = false;
  561. if($iValue < 0) {
  562. $aContext = BxDolProfileQuery::getInstance()->getInfoById(abs($iValue));
  563. if($aContext && $aContext['type'] == $sContextSwitcher)
  564. $mixedContext = (int)$aContext['id'];
  565. }
  566. else if($sModule == $sContextSwitcher)
  567. $mixedContext = BxDolProfile::getInstanceByContentAndType((int)$aInfo['id'], $sModule);
  568. if($mixedContext !== false)
  569. $aCurrent = BxDolProfile::getData($mixedContext);
  570. }
  571. $_sSample = '_' . $sContextSwitcher . '_txt_sample_single';
  572. $sSample = _t($_sSample);
  573. if(strcmp($_sSample, $sSample) == 0)
  574. $sSample = _t('_Context');
  575. $a['context'] = [
  576. 'list' => $aContexts,
  577. 'invitations' => $aInvitedTo,
  578. 'current' => $aCurrent,
  579. 'links' => []
  580. ];
  581. if(bx_srv($sContextSwitcher, 'check_allowed', ['add']) === CHECK_ACTION_RESULT_ALLOWED)
  582. $a['context']['links'][] = [
  583. 'title' => _t('_api_create_context', $sSample),
  584. 'icon' => 'Plus',
  585. 'url' => bx_api_get_relative_url(bx_srv($sContextSwitcher, 'get_link_add'))
  586. ];
  587. }
  588. }
  589. if(!$bIsAvailable)
  590. $a['page_status'] = 404;
  591. else if(!$bIsVisible)
  592. $a['page_status'] = 403;
  593. return $a;
  594. }
  595. public function getPageBlocksAPI($aBlocks = [])
  596. {
  597. $bBlocks = !empty($aBlocks) && is_array($aBlocks);
  598. $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'];
  599. $aCells = $this->_oQuery->getPageBlocks(true);
  600. foreach($aCells as $sKey => &$aCell) {
  601. foreach($aCell as $i => $aBlock) {
  602. if(!$this->_isVisibleBlock($aBlock)) {
  603. unset($aCells[$sKey][$i]);
  604. continue;
  605. }
  606. $this->processPageBlock($aCells[$sKey][$i], true);
  607. $aBlock = $aCells[$sKey][$i];
  608. $sSource = '';
  609. if($aBlock['type'] == 'service') {
  610. $aContent = @unserialize($aBlock['content']);
  611. if(isset($aContent['module'], $aContent['method']))
  612. $sSource = $aContent['module'] . ':' . $aContent['method'];
  613. }
  614. else
  615. $sSource = 'system:block_' . $aBlock['id'];
  616. if($bBlocks && !in_array($sSource, $aBlocks)) {
  617. unset($aCells[$sKey][$i]);
  618. continue;
  619. }
  620. $mBlock = $aBlock['content'];
  621. if(($sFunc = '_getBlock' . bx_gen_method_name($aBlock['type'])) && method_exists($this, $sFunc))
  622. $mBlock = $this->$sFunc($aBlock);
  623. if(empty($mBlock)) {
  624. unset($aCells[$sKey][$i]);
  625. continue;
  626. }
  627. $aCells[$sKey][$i] = array_merge($aCells[$sKey][$i], [
  628. 'title' => isset($mBlock['title']) ? $mBlock['title'] : $this->getBlockTitle($aBlock),
  629. 'content' => isset($mBlock['content']) ? $mBlock['content'] : $mBlock,
  630. 'menu' => isset($mBlock['menu']) ? $mBlock['menu'] : '',
  631. 'source' => $sSource
  632. ]);
  633. $aCells[$sKey][$i] = array_diff_key($aCells[$sKey][$i], array_flip($aFieldsUnset));
  634. }
  635. }
  636. return array_map('array_values', $aCells);
  637. }
  638. /**
  639. * Get page code vars
  640. * @return string
  641. */
  642. protected function _getPageCodeVars ()
  643. {
  644. $aHiddenOn = array(
  645. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  646. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  647. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  648. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  649. );
  650. $aVars = array (
  651. 'page_id' => 'bx-page-' . $this->_aObject['uri'],
  652. 'bx_if:show_layout_row_dump' => array(
  653. 'condition' => false,
  654. 'content' => array()
  655. )
  656. );
  657. $aBlocks = $this->_oQuery->getPageBlocks();
  658. foreach ($aBlocks as $sKey => $aCell) {
  659. $sCell = '';
  660. foreach ($aCell as $aBlock) {
  661. $this->processPageBlock($aBlock, false);
  662. $sContentWithBox = $this->_getBlockCodeWithCache($aBlock, $aBlock['async']);
  663. $sClassAdd = '';
  664. if(!empty($aBlock['class']))
  665. $sClassAdd .= ' ' . $aBlock['class'];
  666. if(!empty($aBlock['hidden_on']))
  667. foreach($aHiddenOn as $iHiddenOn => $sHiddenOnClass)
  668. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  669. $sClassAdd .= ' ' . $sHiddenOnClass;
  670. if ($sContentWithBox)
  671. $sCell .= $this->_oTemplate->parseHtmlByName('designbox_container.html', array(
  672. 'class_add' => $sClassAdd,
  673. 'bx_if:show_html_id' => array(
  674. 'condition' => true,
  675. 'content' => array(
  676. 'html_id' => 'bx-page-block-' . $aBlock['id']
  677. ),
  678. ),
  679. 'content' => $sContentWithBox
  680. ));
  681. }
  682. $aVars[$sKey] = $sCell;
  683. }
  684. return $aVars;
  685. }
  686. /**
  687. * Process block values, especially if someting need to be overrided
  688. */
  689. protected function processPageBlock(&$aBlock, $bApi = false)
  690. {
  691. }
  692. /**
  693. * Get page code only.
  694. * @return string
  695. */
  696. protected function _getPageCode ()
  697. {
  698. $aVars = $this->_getPageCodeVars ();
  699. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  700. }
  701. /**
  702. * Get one block code only.
  703. * @return string
  704. */
  705. protected function _getBlockOnlyCode ($iBlockId)
  706. {
  707. if (!($aBlock = $this->_oQuery->getPageBlock((int)$iBlockId)))
  708. return '';
  709. return $this->_getBlockCodeWithCache($aBlock, 0);
  710. }
  711. protected function _getBlockCodeWithCache(&$aBlock, $iAsync = 0)
  712. {
  713. if (bx_is_dbg() || !getParam('sys_pb_cache_enable') || !$aBlock['cache_lifetime'] || $iAsync || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  714. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  715. }
  716. else {
  717. $oCache = $this->_getBlockCacheObject();
  718. $sKey = $this->_getBlockCacheKey(false, $aBlock);
  719. $sKeyCssJs = 'cssjs_' . $sKey;
  720. $mixedRet = $oCache->getData($sKey, $aBlock['cache_lifetime']);
  721. $aRetCssJs = $oCache->getData($sKeyCssJs, $aBlock['cache_lifetime']);
  722. if ($mixedRet !== null && $aRetCssJs !== null) {
  723. BxDolTemplate::getInstance()->collectingStart();
  724. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  725. $sBlockCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  726. $sBlockCode .= $mixedRet;
  727. } else {
  728. BxDolTemplate::getInstance()->collectingStart();
  729. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  730. $aBlockCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  731. $oCache->setData($sKey, $sBlockCode, $aBlock['cache_lifetime']);
  732. $oCache->setData($sKeyCssJs, $aBlockCssJs, $aBlock['cache_lifetime']);
  733. }
  734. }
  735. return $sBlockCode;
  736. }
  737. /**
  738. * Get block code.
  739. * @return string
  740. */
  741. protected function _getBlockCode(&$aBlock, $iAsync = 0)
  742. {
  743. $sContentWithBox = '';
  744. $oFunctions = $this->_oTemplate->getTemplateFunctions();
  745. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  746. $aDbNoTitle = [BX_DB_CONTENT_ONLY, BX_DB_PADDING_CONTENT_ONLY, BX_DB_NO_CAPTION, BX_DB_PADDING_NO_CAPTION];
  747. $sTitle = $this->getBlockTitle($aBlock);
  748. $sHelp = $this->getBlockHelp($aBlock);
  749. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  750. $bBlockVisible = $this->_isVisibleBlock($aBlock);
  751. if ($iAsync && $bBlockVisible) {
  752. $iDesignboxId = $aBlock['designbox_id'];
  753. $sHelpTitle = $sHelpContent = '';
  754. if(!in_array($iDesignboxId, $aDbNoTitle))
  755. $sHelpTitle = $sHelp;
  756. else
  757. $sHelpContent = $sHelp;
  758. $sContent = $this->getBlockAsyncCode($aBlock, $iAsync);
  759. $aParams = array(
  760. $sTitle . $sHelpTitle,
  761. $sContent . $sHelpContent,
  762. $iDesignboxId
  763. );
  764. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  765. }
  766. elseif ($bBlockVisible && method_exists($this, $sFunc)) {
  767. $mixedContent = $this->$sFunc($aBlock);
  768. if(!empty($aBlock['content_empty'])) {
  769. $sEmpty = _t($aBlock['content_empty']);
  770. $sRegExp = '/(<[A-Za-z0-9]+\b.[^<]*[\pPi\pPf]bx\-msg\-box\b.*[\pPi\pPf][^>]*>)(' . _t('_Empty') . ')(<\/[A-Za-z0-9]+>)/su';
  771. if(is_string($mixedContent))
  772. $mixedContent = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent);
  773. else if(is_array($mixedContent) && !empty($mixedContent['content']))
  774. $mixedContent['content'] = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent['content']);
  775. }
  776. $this->_oQuery->setReadOnlyMode(true);
  777. if(is_array($mixedContent) && !empty($mixedContent['content'])) {
  778. $iDesignboxId = isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'];
  779. if(!empty($mixedContent['markers']) && is_array($mixedContent['markers'])) {
  780. $this->addMarkers($mixedContent['markers']);
  781. $sTitle = $this->getBlockTitle($aBlock);
  782. }
  783. $sHelpTitle = $sHelpContent = '';
  784. if(!in_array($iDesignboxId, $aDbNoTitle))
  785. $sHelpTitle = $sHelp;
  786. else
  787. $sHelpContent = $sHelp;
  788. $aParams = array(
  789. (isset($mixedContent['title']) ? $mixedContent['title'] : $sTitle) . $sHelpTitle,
  790. $mixedContent['content'] . $sHelpContent,
  791. $iDesignboxId
  792. );
  793. $mixedMenu = false;
  794. if(isset($mixedContent['menu']))
  795. $mixedMenu = $mixedContent['menu'];
  796. else if(!empty($aBlock['submenu']))
  797. $mixedMenu = $aBlock['submenu'];
  798. $aParams[] = $mixedMenu;
  799. if(isset($mixedContent['buttons']))
  800. $aParams[] = $mixedContent['buttons'];
  801. else if($mixedMenu)
  802. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  803. if(isset($mixedContent['class']))
  804. $aBlock['class'] = $mixedContent['class'];
  805. }
  806. else if(is_string($mixedContent) && !empty($mixedContent)) {
  807. $iDesignboxId = $aBlock['designbox_id'];
  808. $sHelpTitle = $sHelpContent = '';
  809. if(!in_array($iDesignboxId, $aDbNoTitle))
  810. $sHelpTitle = $sHelp;
  811. else
  812. $sHelpContent = $sHelp;
  813. $aParams = array(
  814. $sTitle . $sHelpTitle,
  815. $mixedContent . $sHelpContent,
  816. $iDesignboxId
  817. );
  818. $mixedMenu = !empty($aBlock['submenu']) ? $aBlock['submenu'] : false;
  819. $aParams[] = $mixedMenu;
  820. if($mixedMenu)
  821. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  822. }
  823. if(!empty($aParams))
  824. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  825. }
  826. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  827. return $sContentWithBox;
  828. }
  829. /**
  830. * Add necessary js and css files.
  831. */
  832. protected function _addJsCss()
  833. {
  834. $this->_oTemplate->addJs(array('BxDolPage.js', 'theia-sticky-sidebar/theia-sticky-sidebar.min.js', 'theia-sticky-sidebar/ResizeSensor.min.js'));
  835. $this->_oTemplate->addCss('page_layouts.css');
  836. }
  837. /**
  838. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  839. */
  840. protected function _addSysTemplateVars ()
  841. {
  842. $oTemplate = BxDolTemplate::getInstance();
  843. $sPageTitle = $this->_getPageTitle();
  844. if ($sPageTitle)
  845. $oTemplate->setPageHeader ($sPageTitle);
  846. $sMetaTitle = $this->_getPageMetaTitle();
  847. if ($sMetaTitle)
  848. $oTemplate->setPageMetaTitle ($sMetaTitle);
  849. $sMetaDesc = $this->_getPageMetaDesc();
  850. if ($sMetaDesc)
  851. $oTemplate->setPageDescription ($sMetaDesc);
  852. $sMetaRobots = $this->_getPageMetaRobots();
  853. if ($sMetaRobots)
  854. $oTemplate->setPageMetaRobots ($sMetaRobots);
  855. $sMetaImage = $this->_getPageMetaImage();
  856. if ($sMetaImage)
  857. $oTemplate->addPageMetaImage($sMetaImage);
  858. $sMetaKeywords = $this->_getPageMetaKeywords();
  859. if ($sMetaKeywords)
  860. $oTemplate->addPageKeywords ($sMetaKeywords);
  861. }
  862. /**
  863. * Select menu from page properties.
  864. */
  865. protected function _selectMenu ()
  866. {
  867. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  868. }
  869. /**
  870. * Set page submenu if it's specified
  871. */
  872. protected function _setSubmenu ($aParams)
  873. {
  874. if(empty($this->_aObject['submenu']) || $this->_aObject['submenu'] == 'disabled')
  875. return;
  876. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  877. if(!$oMenuSubmenu)
  878. return;
  879. $oMenuSubmenu->setObjectSubmenu($this->_aObject['submenu'], $aParams);
  880. }
  881. /**
  882. * Get content for 'raw' block type.
  883. * @return string
  884. */
  885. protected function _getBlockRaw ($aBlock)
  886. {
  887. if (bx_is_api()){
  888. return [bx_api_get_block('raw', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  889. }
  890. $s = '<div class="bx-page-raw-container bx-def-vanilla-html max-w-none">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  891. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  892. $s = bx_process_macros($s);
  893. return $s;
  894. }
  895. /**
  896. * Get content for 'custom' block type.
  897. * @return string
  898. */
  899. protected function _getBlockCustom ($aBlock)
  900. {
  901. if (bx_is_api()){
  902. return [bx_api_get_block('custom', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  903. }
  904. $s = '<div class="bx-page-custom-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  905. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  906. $s = bx_process_macros($s);
  907. return $s;
  908. }
  909. /**
  910. * Get content for 'html' block type.
  911. * @return string
  912. */
  913. protected function _getBlockHtml ($aBlock)
  914. {
  915. if (bx_is_api()){
  916. return [bx_api_get_block('html', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  917. }
  918. $s = '<div class="bx-page-html-container bx-def-vanilla-html max-w-none">' . $aBlock['content'] . '</div>';
  919. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  920. $s = bx_process_macros($s);
  921. return $s;
  922. }
  923. /**
  924. * Get content for 'creative' block type.
  925. * @return string
  926. */
  927. protected function _getBlockCreative ($aBlock)
  928. {
  929. if(bx_is_api())
  930. return [bx_api_get_block('creative', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  931. $s = $this->_replaceMarkers($aBlock['content'], ['block_id' => $aBlock['id']]);
  932. $s = bx_process_macros($s);
  933. $bCanManage = $this->isAllowedCreativeManage();
  934. if($bCanManage) {
  935. $this->_oTemplate->addCss([
  936. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'grapesjs/|grapes.min.css',
  937. ]);
  938. $this->_oTemplate->addJs([
  939. 'grapesjs/grapes.min.js',
  940. 'grapesjs/grapesjs-blocks-basic.js',
  941. 'grapesjs/grapesjs-style-bg.js',
  942. 'grapesjs/grapesjs-preset-webpage.min.js',
  943. ]);
  944. }
  945. return $this->_oTemplate->parseHtmlByName('block_creative.html', [
  946. 'block_id' => $aBlock['id'],
  947. 'content' => $s,
  948. 'bx_if:show_controls' => [
  949. 'condition' => $bCanManage,
  950. 'content' => [
  951. 'onclick_edit' => $this->_sJsObjectName . '.creativeEdit(this, ' . $aBlock['id'] . ')',
  952. 'onclick_save' => $this->_sJsObjectName . '.creativeSave(this, ' . $aBlock['id'] . ')'
  953. ]
  954. ]
  955. ]);
  956. }
  957. /**
  958. * Get content for 'bento_grid' block type.
  959. * @return string
  960. */
  961. protected function _getBlockBentoGrid ($aBlock)
  962. {
  963. if(bx_is_api()) {
  964. $iBlockId = (int)$aBlock['id'];
  965. $iContentId = 0;
  966. if(($mContentId = bx_get('id')) !== false)
  967. $iContentId = (int)$mContentId;
  968. $sContentModule = '';
  969. if(!empty($this->_aObject['module']) && !in_array($this->_aObject['module'], ['system', 'custom']))
  970. $sContentModule = $this->_aObject['module'];
  971. $bIsAllowedEdit = false;
  972. if(!empty($sContentModule) && bx_is_srv($sContentModule, 'check_allowed_with_content'))
  973. $bIsAllowedEdit = bx_srv($sContentModule, 'check_allowed_with_content', ['edit', $iContentId]) === CHECK_ACTION_RESULT_ALLOWED;
  974. else
  975. $bIsAllowedEdit = isAdmin();
  976. return [bx_api_get_block('bento_grid', [
  977. 'title' => _t($aBlock['title']),
  978. 'content' => self::getPageBlockData($iBlockId, $iContentId, $sContentModule)
  979. ], ['ext' => [
  980. 'block_id' => $iBlockId,
  981. 'content_id' => $iContentId,
  982. 'content_module' => $sContentModule,
  983. 'is_allowed_edit' => $bIsAllowedEdit
  984. ]])];
  985. }
  986. /**
  987. * Isn't supported for now.
  988. */
  989. return '';
  990. }
  991. /**
  992. * Get content for 'wiki' block type.
  993. * @return string
  994. */
  995. protected function _getBlockWiki ($aBlock)
  996. {
  997. $oWiki = BxDolWiki::getObjectInstance($this->_aObject['module']);
  998. if (!$oWiki) {
  999. $sContent = _t('_sys_wiki_error_missing_wiki_object', $this->_aObject['module']);
  1000. }
  1001. else {
  1002. $sContent = $oWiki->getBlockContent($aBlock['id'], false, (int)bx_get($aBlock['id'].'rev') ? (int)bx_get($aBlock['id'].'rev') : false);
  1003. }
  1004. $s = '<div id="bx-page-wiki-container-' . $aBlock['id'] . '" class="bx-page-wiki-container markdown-body bx-def-vanilla-html">' . $sContent . '</div>';
  1005. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  1006. $s = bx_process_macros($s);
  1007. return $s;
  1008. }
  1009. /**
  1010. * Get content for 'lang' block type.
  1011. * @return string
  1012. */
  1013. protected function _getBlockLang ($aBlock)
  1014. {
  1015. if (bx_is_api()){
  1016. return [bx_api_get_block('lang', ['title' => _t($aBlock['title']), 'content' => bx_process_macros(_t(trim($aBlock['content'])))])];
  1017. }
  1018. $s = '<div class="bx-page-lang-container bx-def-vanilla-html max-w-none">' . _t(trim($aBlock['content'])) . '</div>';
  1019. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  1020. $s = bx_process_macros($s);
  1021. return $s;
  1022. }
  1023. /**
  1024. * Get content for 'image' block type.
  1025. * @return string
  1026. */
  1027. protected function _getBlockImage ($aBlock)
  1028. {
  1029. if (empty($aBlock['content']))
  1030. return false;
  1031. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  1032. $iFileId = (int)$iFileId;
  1033. if (!$iFileId)
  1034. return false;
  1035. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  1036. if (!$oStorage)
  1037. return false;
  1038. $sUrl = $oStorage->getFileUrlById($iFileId);
  1039. if (!$sUrl)
  1040. return false;
  1041. $sStyleAdd = '';
  1042. $sClassAdd = 'bx-page-image-container';
  1043. $aAlign = ['left' => 'start', 'center' => 'center', 'right' => 'end'];
  1044. if(array_key_exists($sAlign, $aAlign)) {
  1045. $sClassAdd .= ' flex justify-' . $aAlign[$sAlign];
  1046. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  1047. }
  1048. return '<div class="' . $sClassAdd . '" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  1049. }
  1050. /**
  1051. * Get content for 'rss' block type.
  1052. * @return string
  1053. */
  1054. protected function _getBlockRss ($aBlock)
  1055. {
  1056. if (empty($aBlock['content']))
  1057. return false;
  1058. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  1059. $iNum = (int)$iNum;
  1060. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  1061. }
  1062. /**
  1063. * Get content for 'menu' block type.
  1064. * @return string
  1065. */
  1066. protected function _getBlockMenu ($aBlock)
  1067. {
  1068. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  1069. return $oMenu ? $oMenu->getCode () : '';
  1070. }
  1071. /**
  1072. * Get content for 'service' block type.
  1073. * @return string
  1074. */
  1075. protected function _getBlockService ($aBlock)
  1076. {
  1077. $aMarkers = array_merge($this->_aMarkers, [
  1078. 'block_id' => $aBlock['id']
  1079. ]);
  1080. return BxDolService::callSerialized($aBlock['content'], $aMarkers);
  1081. }
  1082. /**
  1083. * Get page title.
  1084. * @return string
  1085. */
  1086. protected function _getPageTitle()
  1087. {
  1088. return $this->_replaceMarkers(_t($this->_aObject['title']));
  1089. }
  1090. /**
  1091. * Get page meta description.
  1092. * @return string
  1093. */
  1094. protected function _getPageMetaDesc()
  1095. {
  1096. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  1097. }
  1098. /**
  1099. * Get page meta title.
  1100. * @return string
  1101. */
  1102. protected function _getPageMetaTitle()
  1103. {
  1104. return $this->_replaceMarkers(_t($this->_aObject['meta_title']));
  1105. }
  1106. /**
  1107. * Get page meta image.
  1108. * @return string
  1109. */
  1110. protected function _getPageMetaImage()
  1111. {
  1112. return '';
  1113. }
  1114. /**
  1115. * Get page meta keywords.
  1116. * @return string
  1117. */
  1118. protected function _getPageMetaKeywords()
  1119. {
  1120. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  1121. }
  1122. /**
  1123. * Get page meta robots.
  1124. * @return string
  1125. */
  1126. protected function _getPageMetaRobots()
  1127. {
  1128. return _t($this->_aObject['meta_robots']);
  1129. }
  1130. /**
  1131. * Get page not found message.
  1132. * @return string
  1133. */
  1134. protected function _getPageNotFoundMsg ($mixedMsg = false)
  1135. {
  1136. $this->_oTemplate->displayPageNotFound();
  1137. exit;
  1138. }
  1139. /**
  1140. * Get access denied message.
  1141. * @return string
  1142. */
  1143. protected function _getPageAccessDeniedMsg ($mixedMsg = false)
  1144. {
  1145. return MsgBox($mixedMsg !== false ? $mixedMsg : _t('_Access denied'));
  1146. }
  1147. /**
  1148. * Get page cache object.
  1149. * @return cache object instance
  1150. */
  1151. protected function _getPageCacheObject ()
  1152. {
  1153. return $this->_getCacheObject ();
  1154. }
  1155. /**
  1156. * Get block cache object.
  1157. * @return cache object instance
  1158. */
  1159. protected function _getBlockCacheObject ()
  1160. {
  1161. return $this->_getCacheObject ('Block', 'pb');
  1162. }
  1163. protected function _getCacheObject ($sSuffixObj = 'Page', $sSuffixParam = 'page')
  1164. {
  1165. $sObj = '_o' . $sSuffixObj . 'CacheObject';
  1166. $sParam = 'sys_' . $sSuffixParam . '_cache_engine';
  1167. if ($this->{$sObj} != null) {
  1168. return $this->{$sObj};
  1169. } else {
  1170. $sEngine = getParam($sParam);
  1171. $this->{$sObj} = bx_instance ('BxDolCache' . $sEngine);
  1172. if (!$this->{$sObj}->isAvailable())
  1173. $this->{$sObj} = bx_instance ('BxDolCacheFile');
  1174. return $this->{$sObj};
  1175. }
  1176. }
  1177. /**
  1178. * Get page cache key.
  1179. * @param $isPrefixOnly return key prefix only.
  1180. * @return string
  1181. */
  1182. protected function _getPageCacheKey ($isPrefixOnly = false)
  1183. {
  1184. $s = 'page_' . $this->_aObject['object'] . '_';
  1185. if ($isPrefixOnly)
  1186. return $s;
  1187. $s .= $this->_getPageCacheParams ();
  1188. $s .= bx_site_hash() . '.php';
  1189. return $s;
  1190. }
  1191. /**
  1192. * Get block cache key.
  1193. * @param $isPrefixOnly return key prefix only.
  1194. * @return string
  1195. */
  1196. protected function _getBlockCacheKey ($isPrefixOnly = false, $aBlock = array())
  1197. {
  1198. $s = $this->_getPageCacheKey(true) . 'block_';
  1199. if ($isPrefixOnly)
  1200. return $s;
  1201. $s .= ($aBlock ? $aBlock['id'] : 0) . '_' . $this->_getPageCacheParams ();
  1202. $s .= bx_site_hash() . '.php';
  1203. return $s;
  1204. }
  1205. /**
  1206. * Additional cache key. In the case of dynamic page.
  1207. * For example - profile page, where each profile has own page.
  1208. * @return string
  1209. */
  1210. protected function _getPageCacheParams ()
  1211. {
  1212. return md5(serialize($_GET) . bx_lang_name());
  1213. }
  1214. /**
  1215. * Clean page cache.
  1216. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  1217. * @return string
  1218. */
  1219. protected function cleanCache ($isDelAllWithPagePrefix = false)
  1220. {
  1221. $a = ['Page', 'Block'];
  1222. $bRet = false;
  1223. foreach ($a as $s) {
  1224. $oCache = $this->{'_get' . $s . 'CacheObject'} ();
  1225. $sKey = $this->{'_get'. $s . 'CacheKey'} ($isDelAllWithPagePrefix);
  1226. if ($isDelAllWithPagePrefix)
  1227. $bRet = $bRet && $oCache->removeAllByPrefix($sKey);
  1228. else
  1229. $bRet = $bRet && $oCache->delData($sKey);
  1230. }
  1231. return $bRet;
  1232. }
  1233. }
  1234. /** @} */