BxBasePage.php 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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. {
  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 $a;
  598. }
  599. public function getPageBlocksAPI($aBlocks = [])
  600. {
  601. $bBlocks = !empty($aBlocks) && is_array($aBlocks);
  602. $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'];
  603. $aCells = $this->_oQuery->getPageBlocks(true);
  604. foreach($aCells as $sKey => &$aCell) {
  605. foreach($aCell as $i => $aBlock) {
  606. if(!$this->_isVisibleBlock($aBlock)) {
  607. unset($aCells[$sKey][$i]);
  608. continue;
  609. }
  610. $this->processPageBlock($aCells[$sKey][$i], true);
  611. $aBlock = $aCells[$sKey][$i];
  612. $sSource = '';
  613. if($aBlock['type'] == 'service') {
  614. $aContent = @unserialize($aBlock['content']);
  615. if(isset($aContent['module'], $aContent['method']))
  616. $sSource = $aContent['module'] . ':' . $aContent['method'];
  617. }
  618. else
  619. $sSource = 'system:block_' . $aBlock['id'];
  620. if($bBlocks && !in_array($sSource, $aBlocks)) {
  621. unset($aCells[$sKey][$i]);
  622. continue;
  623. }
  624. $mBlock = $aBlock['content'];
  625. if(($sFunc = '_getBlock' . bx_gen_method_name($aBlock['type'])) && method_exists($this, $sFunc))
  626. $mBlock = $this->$sFunc($aBlock);
  627. if(empty($mBlock)) {
  628. unset($aCells[$sKey][$i]);
  629. continue;
  630. }
  631. $aCells[$sKey][$i] = array_merge($aCells[$sKey][$i], [
  632. 'title' => isset($mBlock['title']) ? $mBlock['title'] : $this->getBlockTitle($aBlock),
  633. 'content' => isset($mBlock['content']) ? $mBlock['content'] : $mBlock,
  634. 'menu' => isset($mBlock['menu']) ? $mBlock['menu'] : '',
  635. 'source' => $sSource
  636. ]);
  637. $aCells[$sKey][$i] = array_diff_key($aCells[$sKey][$i], array_flip($aFieldsUnset));
  638. }
  639. }
  640. return array_map('array_values', $aCells);
  641. }
  642. /**
  643. * Get page code vars
  644. * @return string
  645. */
  646. protected function _getPageCodeVars ()
  647. {
  648. $aHiddenOn = array(
  649. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  650. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  651. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  652. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  653. );
  654. $aVars = array (
  655. 'page_id' => 'bx-page-' . $this->_aObject['uri'],
  656. 'bx_if:show_layout_row_dump' => array(
  657. 'condition' => false,
  658. 'content' => array()
  659. )
  660. );
  661. $aBlocks = $this->_oQuery->getPageBlocks();
  662. foreach ($aBlocks as $sKey => $aCell) {
  663. $sCell = '';
  664. foreach ($aCell as $aBlock) {
  665. $this->processPageBlock($aBlock, false);
  666. $sContentWithBox = $this->_getBlockCodeWithCache($aBlock, $aBlock['async']);
  667. $sClassAdd = '';
  668. if(!empty($aBlock['class']))
  669. $sClassAdd .= ' ' . $aBlock['class'];
  670. if(!empty($aBlock['hidden_on']))
  671. foreach($aHiddenOn as $iHiddenOn => $sHiddenOnClass)
  672. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  673. $sClassAdd .= ' ' . $sHiddenOnClass;
  674. if ($sContentWithBox)
  675. $sCell .= $this->_oTemplate->parseHtmlByName('designbox_container.html', array(
  676. 'class_add' => $sClassAdd,
  677. 'bx_if:show_html_id' => array(
  678. 'condition' => true,
  679. 'content' => array(
  680. 'html_id' => 'bx-page-block-' . $aBlock['id']
  681. ),
  682. ),
  683. 'content' => $sContentWithBox
  684. ));
  685. }
  686. $aVars[$sKey] = $sCell;
  687. }
  688. return $aVars;
  689. }
  690. /**
  691. * Process block values, especially if someting need to be overrided
  692. */
  693. protected function processPageBlock(&$aBlock, $bApi = false)
  694. {
  695. }
  696. /**
  697. * Get page code only.
  698. * @return string
  699. */
  700. protected function _getPageCode ()
  701. {
  702. $aVars = $this->_getPageCodeVars ();
  703. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  704. }
  705. /**
  706. * Get one block code only.
  707. * @return string
  708. */
  709. protected function _getBlockOnlyCode ($iBlockId)
  710. {
  711. if (!($aBlock = $this->_oQuery->getPageBlock((int)$iBlockId)))
  712. return '';
  713. return $this->_getBlockCodeWithCache($aBlock, 0);
  714. }
  715. protected function _getBlockCodeWithCache(&$aBlock, $iAsync = 0)
  716. {
  717. if (bx_is_dbg() || !getParam('sys_pb_cache_enable') || !$aBlock['cache_lifetime'] || $iAsync || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  718. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  719. }
  720. else {
  721. $oCache = $this->_getBlockCacheObject();
  722. $sKey = $this->_getBlockCacheKey(false, $aBlock);
  723. $sKeyCssJs = 'cssjs_' . $sKey;
  724. $mixedRet = $oCache->getData($sKey, $aBlock['cache_lifetime']);
  725. $aRetCssJs = $oCache->getData($sKeyCssJs, $aBlock['cache_lifetime']);
  726. if ($mixedRet !== null && $aRetCssJs !== null) {
  727. BxDolTemplate::getInstance()->collectingStart();
  728. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  729. $sBlockCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  730. $sBlockCode .= $mixedRet;
  731. } else {
  732. BxDolTemplate::getInstance()->collectingStart();
  733. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  734. $aBlockCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  735. $oCache->setData($sKey, $sBlockCode, $aBlock['cache_lifetime']);
  736. $oCache->setData($sKeyCssJs, $aBlockCssJs, $aBlock['cache_lifetime']);
  737. }
  738. }
  739. return $sBlockCode;
  740. }
  741. /**
  742. * Get block code.
  743. * @return string
  744. */
  745. protected function _getBlockCode(&$aBlock, $iAsync = 0)
  746. {
  747. $sContentWithBox = '';
  748. $oFunctions = $this->_oTemplate->getTemplateFunctions();
  749. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  750. $aDbNoTitle = [BX_DB_CONTENT_ONLY, BX_DB_PADDING_CONTENT_ONLY, BX_DB_NO_CAPTION, BX_DB_PADDING_NO_CAPTION];
  751. $sTitle = $this->getBlockTitle($aBlock);
  752. $sHelp = $this->getBlockHelp($aBlock);
  753. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  754. $bBlockVisible = $this->_isVisibleBlock($aBlock);
  755. if ($iAsync && $bBlockVisible) {
  756. $iDesignboxId = $aBlock['designbox_id'];
  757. $sHelpTitle = $sHelpContent = '';
  758. if(!in_array($iDesignboxId, $aDbNoTitle))
  759. $sHelpTitle = $sHelp;
  760. else
  761. $sHelpContent = $sHelp;
  762. $sContent = $this->getBlockAsyncCode($aBlock, $iAsync);
  763. $aParams = array(
  764. $sTitle . $sHelpTitle,
  765. $sContent . $sHelpContent,
  766. $iDesignboxId
  767. );
  768. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  769. }
  770. elseif ($bBlockVisible && method_exists($this, $sFunc)) {
  771. $mixedContent = $this->$sFunc($aBlock);
  772. if(!empty($aBlock['content_empty'])) {
  773. $sEmpty = _t($aBlock['content_empty']);
  774. $sRegExp = '/(<[A-Za-z0-9]+\b.[^<]*[\pPi\pPf]bx\-msg\-box\b.*[\pPi\pPf][^>]*>)(' . _t('_Empty') . ')(<\/[A-Za-z0-9]+>)/su';
  775. if(is_string($mixedContent))
  776. $mixedContent = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent);
  777. else if(is_array($mixedContent) && !empty($mixedContent['content']))
  778. $mixedContent['content'] = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent['content']);
  779. }
  780. $this->_oQuery->setReadOnlyMode(true);
  781. if(is_array($mixedContent) && !empty($mixedContent['content'])) {
  782. $iDesignboxId = isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'];
  783. if(!empty($mixedContent['markers']) && is_array($mixedContent['markers'])) {
  784. $this->addMarkers($mixedContent['markers']);
  785. $sTitle = $this->getBlockTitle($aBlock);
  786. }
  787. $sHelpTitle = $sHelpContent = '';
  788. if(!in_array($iDesignboxId, $aDbNoTitle))
  789. $sHelpTitle = $sHelp;
  790. else
  791. $sHelpContent = $sHelp;
  792. $aParams = array(
  793. (isset($mixedContent['title']) ? $mixedContent['title'] : $sTitle) . $sHelpTitle,
  794. $mixedContent['content'] . $sHelpContent,
  795. $iDesignboxId
  796. );
  797. $mixedMenu = false;
  798. if(isset($mixedContent['menu']))
  799. $mixedMenu = $mixedContent['menu'];
  800. else if(!empty($aBlock['submenu']))
  801. $mixedMenu = $aBlock['submenu'];
  802. $aParams[] = $mixedMenu;
  803. if(isset($mixedContent['buttons']))
  804. $aParams[] = $mixedContent['buttons'];
  805. else if($mixedMenu)
  806. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  807. if(isset($mixedContent['class']))
  808. $aBlock['class'] = $mixedContent['class'];
  809. }
  810. else if(is_string($mixedContent) && !empty($mixedContent)) {
  811. $iDesignboxId = $aBlock['designbox_id'];
  812. $sHelpTitle = $sHelpContent = '';
  813. if(!in_array($iDesignboxId, $aDbNoTitle))
  814. $sHelpTitle = $sHelp;
  815. else
  816. $sHelpContent = $sHelp;
  817. $aParams = array(
  818. $sTitle . $sHelpTitle,
  819. $mixedContent . $sHelpContent,
  820. $iDesignboxId
  821. );
  822. $mixedMenu = !empty($aBlock['submenu']) ? $aBlock['submenu'] : false;
  823. $aParams[] = $mixedMenu;
  824. if($mixedMenu)
  825. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  826. }
  827. if(!empty($aParams))
  828. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  829. }
  830. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  831. return $sContentWithBox;
  832. }
  833. /**
  834. * Add necessary js and css files.
  835. */
  836. protected function _addJsCss()
  837. {
  838. $this->_oTemplate->addJs(array('BxDolPage.js', 'theia-sticky-sidebar/theia-sticky-sidebar.umd.js', 'theia-sticky-sidebar/ResizeSensor.min.js'));
  839. $this->_oTemplate->addCss('page_layouts.css');
  840. }
  841. /**
  842. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  843. */
  844. protected function _addSysTemplateVars ()
  845. {
  846. $oTemplate = BxDolTemplate::getInstance();
  847. $sPageTitle = $this->_getPageTitle();
  848. if ($sPageTitle)
  849. $oTemplate->setPageHeader ($sPageTitle);
  850. $sMetaTitle = $this->_getPageMetaTitle();
  851. if ($sMetaTitle)
  852. $oTemplate->setPageMetaTitle ($sMetaTitle);
  853. $sMetaDesc = $this->_getPageMetaDesc();
  854. if ($sMetaDesc)
  855. $oTemplate->setPageDescription ($sMetaDesc);
  856. $sMetaRobots = $this->_getPageMetaRobots();
  857. if ($sMetaRobots)
  858. $oTemplate->setPageMetaRobots ($sMetaRobots);
  859. $sMetaImage = $this->_getPageMetaImage();
  860. if ($sMetaImage)
  861. $oTemplate->addPageMetaImage($sMetaImage);
  862. $sMetaKeywords = $this->_getPageMetaKeywords();
  863. if ($sMetaKeywords)
  864. $oTemplate->addPageKeywords ($sMetaKeywords);
  865. }
  866. /**
  867. * Select menu from page properties.
  868. */
  869. protected function _selectMenu ()
  870. {
  871. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  872. }
  873. /**
  874. * Set page submenu if it's specified
  875. */
  876. protected function _setSubmenu ($aParams)
  877. {
  878. if(empty($this->_aObject['submenu']) || $this->_aObject['submenu'] == 'disabled')
  879. return;
  880. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  881. if(!$oMenuSubmenu)
  882. return;
  883. $oMenuSubmenu->setObjectSubmenu($this->_aObject['submenu'], $aParams);
  884. }
  885. /**
  886. * Get content for 'raw' block type.
  887. * @return string
  888. */
  889. protected function _getBlockRaw ($aBlock)
  890. {
  891. if (bx_is_api()){
  892. return [bx_api_get_block('raw', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  893. }
  894. $s = '<div class="bx-page-raw-container bx-def-vanilla-html max-w-none">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  895. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  896. $s = bx_process_macros($s);
  897. return $s;
  898. }
  899. /**
  900. * Get content for 'custom' block type.
  901. * @return string
  902. */
  903. protected function _getBlockCustom ($aBlock)
  904. {
  905. if (bx_is_api()){
  906. return [bx_api_get_block('custom', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  907. }
  908. $s = '<div class="bx-page-custom-container">' . 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 'html' block type.
  915. * @return string
  916. */
  917. protected function _getBlockHtml ($aBlock)
  918. {
  919. if (bx_is_api()){
  920. return [bx_api_get_block('html', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  921. }
  922. $s = '<div class="bx-page-html-container bx-def-vanilla-html max-w-none">' . $aBlock['content'] . '</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 'creative' block type.
  929. * @return string
  930. */
  931. protected function _getBlockCreative ($aBlock)
  932. {
  933. if(bx_is_api())
  934. return [bx_api_get_block('creative', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  935. $s = $this->_replaceMarkers($aBlock['content'], ['block_id' => $aBlock['id']]);
  936. $s = bx_process_macros($s);
  937. $bCanManage = $this->isAllowedCreativeManage();
  938. if($bCanManage) {
  939. $this->_oTemplate->addCss([
  940. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'grapesjs/|grapes.min.css',
  941. ]);
  942. $this->_oTemplate->addJs([
  943. 'grapesjs/grapes.min.js',
  944. 'grapesjs/grapesjs-blocks-basic.js',
  945. 'grapesjs/grapesjs-style-bg.js',
  946. 'grapesjs/grapesjs-preset-webpage.min.js',
  947. ]);
  948. }
  949. return $this->_oTemplate->parseHtmlByName('block_creative.html', [
  950. 'block_id' => $aBlock['id'],
  951. 'content' => $s,
  952. 'bx_if:show_controls' => [
  953. 'condition' => $bCanManage,
  954. 'content' => [
  955. 'onclick_edit' => $this->_sJsObjectName . '.creativeEdit(this, ' . $aBlock['id'] . ')',
  956. 'onclick_save' => $this->_sJsObjectName . '.creativeSave(this, ' . $aBlock['id'] . ')'
  957. ]
  958. ]
  959. ]);
  960. }
  961. /**
  962. * Get content for 'bento_grid' block type.
  963. * @return string
  964. */
  965. protected function _getBlockBentoGrid ($aBlock)
  966. {
  967. if(bx_is_api()) {
  968. $iBlockId = (int)$aBlock['id'];
  969. $iContentId = 0;
  970. if(($mContentId = bx_get('id')) !== false)
  971. $iContentId = (int)$mContentId;
  972. $sContentModule = '';
  973. if(!empty($this->_aObject['module']) && !in_array($this->_aObject['module'], ['system', 'custom']))
  974. $sContentModule = $this->_aObject['module'];
  975. $bIsAllowedEdit = false;
  976. if(!empty($sContentModule) && bx_is_srv($sContentModule, 'check_allowed_with_content'))
  977. $bIsAllowedEdit = bx_srv($sContentModule, 'check_allowed_with_content', ['edit', $iContentId]) === CHECK_ACTION_RESULT_ALLOWED;
  978. else
  979. $bIsAllowedEdit = isAdmin();
  980. return [bx_api_get_block('bento_grid', [
  981. 'title' => _t($aBlock['title']),
  982. 'content' => self::getPageBlockData($iBlockId, $iContentId, $sContentModule)
  983. ], ['ext' => [
  984. 'block_id' => $iBlockId,
  985. 'content_id' => $iContentId,
  986. 'content_module' => $sContentModule,
  987. 'is_allowed_edit' => $bIsAllowedEdit
  988. ]])];
  989. }
  990. /**
  991. * Isn't supported for now.
  992. */
  993. return '';
  994. }
  995. /**
  996. * Get content for 'wiki' block type.
  997. * @return string
  998. */
  999. protected function _getBlockWiki ($aBlock)
  1000. {
  1001. $oWiki = BxDolWiki::getObjectInstance($this->_aObject['module']);
  1002. if (!$oWiki) {
  1003. $sContent = _t('_sys_wiki_error_missing_wiki_object', $this->_aObject['module']);
  1004. }
  1005. else {
  1006. $sContent = $oWiki->getBlockContent($aBlock['id'], false, (int)bx_get($aBlock['id'].'rev') ? (int)bx_get($aBlock['id'].'rev') : false);
  1007. }
  1008. $s = '<div id="bx-page-wiki-container-' . $aBlock['id'] . '" class="bx-page-wiki-container markdown-body bx-def-vanilla-html">' . $sContent . '</div>';
  1009. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  1010. $s = bx_process_macros($s);
  1011. return $s;
  1012. }
  1013. /**
  1014. * Get content for 'lang' block type.
  1015. * @return string
  1016. */
  1017. protected function _getBlockLang ($aBlock)
  1018. {
  1019. if (bx_is_api()){
  1020. return [bx_api_get_block('lang', ['title' => _t($aBlock['title']), 'content' => bx_process_macros(_t(trim($aBlock['content'])))])];
  1021. }
  1022. $s = '<div class="bx-page-lang-container bx-def-vanilla-html max-w-none">' . _t(trim($aBlock['content'])) . '</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 'image' block type.
  1029. * @return string
  1030. */
  1031. protected function _getBlockImage ($aBlock)
  1032. {
  1033. if (empty($aBlock['content']))
  1034. return false;
  1035. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  1036. $iFileId = (int)$iFileId;
  1037. if (!$iFileId)
  1038. return false;
  1039. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  1040. if (!$oStorage)
  1041. return false;
  1042. $sUrl = $oStorage->getFileUrlById($iFileId);
  1043. if (!$sUrl)
  1044. return false;
  1045. $sStyleAdd = '';
  1046. $sClassAdd = 'bx-page-image-container';
  1047. $aAlign = ['left' => 'start', 'center' => 'center', 'right' => 'end'];
  1048. if(array_key_exists($sAlign, $aAlign)) {
  1049. $sClassAdd .= ' flex justify-' . $aAlign[$sAlign];
  1050. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  1051. }
  1052. return '<div class="' . $sClassAdd . '" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  1053. }
  1054. /**
  1055. * Get content for 'rss' block type.
  1056. * @return string
  1057. */
  1058. protected function _getBlockRss ($aBlock)
  1059. {
  1060. if (empty($aBlock['content']))
  1061. return false;
  1062. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  1063. $iNum = (int)$iNum;
  1064. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  1065. }
  1066. /**
  1067. * Get content for 'menu' block type.
  1068. * @return string
  1069. */
  1070. protected function _getBlockMenu ($aBlock)
  1071. {
  1072. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  1073. return $oMenu ? $oMenu->getCode () : '';
  1074. }
  1075. /**
  1076. * Get content for 'service' block type.
  1077. * @return string
  1078. */
  1079. protected function _getBlockService ($aBlock)
  1080. {
  1081. $aMarkers = array_merge($this->_aMarkers, [
  1082. 'block_id' => $aBlock['id']
  1083. ]);
  1084. return BxDolService::callSerialized($aBlock['content'], $aMarkers);
  1085. }
  1086. /**
  1087. * Get page title.
  1088. * @return string
  1089. */
  1090. protected function _getPageTitle()
  1091. {
  1092. return $this->_replaceMarkers(_t($this->_aObject['title']));
  1093. }
  1094. /**
  1095. * Get page meta description.
  1096. * @return string
  1097. */
  1098. protected function _getPageMetaDesc()
  1099. {
  1100. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  1101. }
  1102. /**
  1103. * Get page meta title.
  1104. * @return string
  1105. */
  1106. protected function _getPageMetaTitle()
  1107. {
  1108. return $this->_replaceMarkers(_t($this->_aObject['meta_title']));
  1109. }
  1110. /**
  1111. * Get page meta image.
  1112. * @return string
  1113. */
  1114. protected function _getPageMetaImage()
  1115. {
  1116. return '';
  1117. }
  1118. /**
  1119. * Get page meta keywords.
  1120. * @return string
  1121. */
  1122. protected function _getPageMetaKeywords()
  1123. {
  1124. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  1125. }
  1126. /**
  1127. * Get page meta robots.
  1128. * @return string
  1129. */
  1130. protected function _getPageMetaRobots()
  1131. {
  1132. return _t($this->_aObject['meta_robots']);
  1133. }
  1134. /**
  1135. * Get page not found message.
  1136. * @return string
  1137. */
  1138. protected function _getPageNotFoundMsg ($mixedMsg = false)
  1139. {
  1140. $this->_oTemplate->displayPageNotFound();
  1141. exit;
  1142. }
  1143. /**
  1144. * Get access denied message.
  1145. * @return string
  1146. */
  1147. protected function _getPageAccessDeniedMsg ($mixedMsg = false)
  1148. {
  1149. return MsgBox($mixedMsg !== false ? $mixedMsg : _t('_Access denied'));
  1150. }
  1151. /**
  1152. * Get page cache object.
  1153. * @return cache object instance
  1154. */
  1155. protected function _getPageCacheObject ()
  1156. {
  1157. return $this->_getCacheObject ();
  1158. }
  1159. /**
  1160. * Get block cache object.
  1161. * @return cache object instance
  1162. */
  1163. protected function _getBlockCacheObject ()
  1164. {
  1165. return $this->_getCacheObject ('Block', 'pb');
  1166. }
  1167. protected function _getCacheObject ($sSuffixObj = 'Page', $sSuffixParam = 'page')
  1168. {
  1169. $sObj = '_o' . $sSuffixObj . 'CacheObject';
  1170. $sParam = 'sys_' . $sSuffixParam . '_cache_engine';
  1171. if ($this->{$sObj} != null) {
  1172. return $this->{$sObj};
  1173. } else {
  1174. $sEngine = getParam($sParam);
  1175. $this->{$sObj} = bx_instance ('BxDolCache' . $sEngine);
  1176. if (!$this->{$sObj}->isAvailable())
  1177. $this->{$sObj} = bx_instance ('BxDolCacheFile');
  1178. return $this->{$sObj};
  1179. }
  1180. }
  1181. /**
  1182. * Get page cache key.
  1183. * @param $isPrefixOnly return key prefix only.
  1184. * @return string
  1185. */
  1186. protected function _getPageCacheKey ($isPrefixOnly = false)
  1187. {
  1188. $s = 'page_' . $this->_aObject['object'] . '_';
  1189. if ($isPrefixOnly)
  1190. return $s;
  1191. $s .= $this->_getPageCacheParams ();
  1192. $s .= bx_site_hash() . '.php';
  1193. return $s;
  1194. }
  1195. /**
  1196. * Get block cache key.
  1197. * @param $isPrefixOnly return key prefix only.
  1198. * @return string
  1199. */
  1200. protected function _getBlockCacheKey ($isPrefixOnly = false, $aBlock = array())
  1201. {
  1202. $s = $this->_getPageCacheKey(true) . 'block_';
  1203. if ($isPrefixOnly)
  1204. return $s;
  1205. $s .= ($aBlock ? $aBlock['id'] : 0) . '_' . $this->_getPageCacheParams ();
  1206. $s .= bx_site_hash() . '.php';
  1207. return $s;
  1208. }
  1209. /**
  1210. * Additional cache key. In the case of dynamic page.
  1211. * For example - profile page, where each profile has own page.
  1212. * @return string
  1213. */
  1214. protected function _getPageCacheParams ()
  1215. {
  1216. return md5(serialize($_GET) . bx_lang_name());
  1217. }
  1218. /**
  1219. * Clean page cache.
  1220. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  1221. * @return string
  1222. */
  1223. protected function cleanCache ($isDelAllWithPagePrefix = false)
  1224. {
  1225. $a = ['Page', 'Block'];
  1226. $bRet = false;
  1227. foreach ($a as $s) {
  1228. $oCache = $this->{'_get' . $s . 'CacheObject'} ();
  1229. $sKey = $this->{'_get'. $s . 'CacheKey'} ($isDelAllWithPagePrefix);
  1230. if ($isDelAllWithPagePrefix)
  1231. $bRet = $bRet && $oCache->removeAllByPrefix($sKey);
  1232. else
  1233. $bRet = $bRet && $oCache->delData($sKey);
  1234. }
  1235. return $bRet;
  1236. }
  1237. }
  1238. /** @} */