BxBasePage.php 53 KB

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