BxBasePage.php 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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. 'layout_parsed' => true,
  452. 'cover_block' => '',
  453. 'cover' => $this->_aObject['cover'],
  454. 'menu_top' => '',
  455. 'menu' => '',
  456. 'menu_bottom' => '',
  457. 'menu_add' => '',
  458. 'config' => $this->_aObject['config_api'],
  459. 'elements' => $this->getPageBlocksAPI($aBlocks),
  460. ];
  461. $sMenuTop = getParam('sys_api_menu_top');
  462. if (!empty($sMenuTop) && ($oMenuTop = BxDolMenu::getObjectInstance($sMenuTop)) !== false)
  463. $a['menu_top'] = $oMenuTop->getCodeAPI();
  464. if (($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  465. if(($sSubmenu = $this->getSubMenu()) !== '')
  466. $oMenuSubmenu->setObjectSubmenu($sSubmenu, []);
  467. $a['menu'] = $oMenuSubmenu->getCodeAPI();
  468. if($sModule && $sModule != 'system' && (bx_srv('system', 'is_module_content', [$sModule]) || bx_srv('system', 'is_module_context', [$sModule])) && ($oModule = BxDolModule::getInstance($sModule))) {
  469. $CNF = &$oModule->_oConfig->CNF;
  470. $aMenuAdd = [
  471. 'name' => $oModule->getName(),
  472. 'title' => BxDolModule::getTitle($oModule->_oConfig->getUri()),
  473. 'icon' => !empty($CNF['ICON']) ? $CNF['ICON'] : '',
  474. ];
  475. $sUrlAdd = '';
  476. if(($sKey = 'URI_ADD_ENTRY') && !empty($CNF[$sKey]))
  477. $sUrlAdd = $CNF[$sKey];
  478. if(!$sUrlAdd && ($sKey = 'URI_EDIT_ENTRY') && !empty($CNF[$sKey]))
  479. $sUrlAdd = str_replace('edit-', 'create-', $CNF[$sKey]);
  480. if($sUrlAdd)
  481. $aMenuAdd['add_url'] = $sUrlAdd;
  482. $a['menu'] = array_merge($a['menu'], $aMenuAdd);
  483. }
  484. }
  485. /**
  486. * Profile/Context view page.
  487. */
  488. if(isset($this->_aProfileInfo)) {
  489. $oProfileModule = BxDolModule::getInstance($this->_aProfileInfo['type']);
  490. if($oProfileModule !== null)
  491. $aProfileContentInfo = $oProfileModule->_oDb->getContentInfoById($this->_aProfileInfo['content_id']);
  492. if(!empty($aProfileContentInfo) && is_array($aProfileContentInfo)) {
  493. $CNF = &$oProfileModule->_oConfig->CNF;
  494. /**
  495. * Process page cover with related menus and lists.
  496. */
  497. if(BxDolCover::getInstance($this)->isCover()) {
  498. $aProfileInfo = BxDolProfile::getData($this->_aProfileInfo['id'], [
  499. 'get_avatar' => 'getAvatarBig',
  500. 'with_info' => true
  501. ]);
  502. $oProfile = BxDolProfile::getInstance($this->_aProfileInfo['id']);
  503. $aProfileInfo['online'] = $oProfile->isOnline();
  504. unset($aProfileInfo['info']['profile_email']);
  505. unset($aProfileInfo['info']['profile_last_active']);
  506. unset($aProfileInfo['info']['profile_ip']);
  507. $a['cover_block'] = [
  508. 'profile' => $aProfileInfo,
  509. 'actions_menu' => '',
  510. 'meta_menu' => '',
  511. 'badges' => $oProfile->getBadges(),
  512. 'cover' => $oProfileModule->serviceGetCover($this->_aProfileInfo['content_id']),
  513. 'allow_edit' => $oProfileModule->checkAllowedChangeCover($aProfileContentInfo) === CHECK_ACTION_RESULT_ALLOWED
  514. ];
  515. if(!empty($CNF['OBJECT_MENU_VIEW_ENTRY_META']) && ($oMetaMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_VIEW_ENTRY_META'])) !== false)
  516. $a['cover_block']['meta_menu'] = $oMetaMenu->getCodeAPI();
  517. if(!empty($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) !== false)
  518. $a['cover_block']['members_list'] = $oConnection->getConnectedListAPI($this->_aProfileInfo['id'], false, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS, 10);
  519. if(!empty($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_ALL']) && ($oActionMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_ALL'])) !== false)
  520. $a['cover_block']['actions_menu'] = $oActionMenu->getCodeAPI();
  521. }
  522. /**
  523. * Process page mange menu.
  524. */
  525. if(($oMenuManage = BxDolMenu::getObjectInstance('sys_site_manage')) !== false) {
  526. if(!empty($CNF['FIELD_ID']))
  527. $oMenuManage->setContentId($aProfileContentInfo[$CNF['FIELD_ID']]);
  528. $aObjectManage = [];
  529. if(!empty($CNF['OBJECT_MENU_MANAGE_VIEW_ENTRY']))
  530. $aObjectManage = [$CNF['OBJECT_MENU_MANAGE_VIEW_ENTRY']];
  531. else if(!empty($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE']))
  532. $aObjectManage = [$CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE'], true];
  533. call_user_func_array([$oMenuManage, 'setObjectManage'], $aObjectManage);
  534. $aMenuManage = $oMenuManage->getCodeAPI();
  535. if(!empty($aMenuManage) && is_array($aMenuManage)) {
  536. if(isset($a['cover_block'], $a['cover_block']['actions_menu']) && is_array($a['cover_block']['actions_menu']))
  537. $a['cover_block']['actions_menu']['items'] = array_merge($a['cover_block']['actions_menu']['items'], $aMenuManage['items']);
  538. else
  539. $a['cover_block']['actions_menu'] = $aMenuManage;
  540. }
  541. }
  542. }
  543. }
  544. if(isLogged() && ($o = BxDolProfile::getInstance()) !== false) {
  545. $iLogged = $o->id();
  546. $a['user'] = BxDolProfile::getDataForPage($o);
  547. if(($sContextSwitcher = getParam('sys_api_context_switcher')) && ($sContextConnection = getParam('sys_api_context_connection'))) {
  548. $aCpIds = bx_srv($sContextSwitcher, 'get_participating_profiles', [$iLogged, ($sContextConnection != 'participants' ? $sContextConnection : false)]);
  549. $aContexts = [];
  550. foreach($aCpIds as $iCpId)
  551. $aContexts[] = BxDolProfile::getData($iCpId);
  552. $aInvitedTo = [];
  553. if(($aInvitations = bx_srv($sContextSwitcher, 'invitations', [$iLogged, true])) && is_array($aInvitations))
  554. foreach($aInvitations as $aInvitation)
  555. $aInvitedTo[] = BxDolProfile::getData($aInvitation['group_profile_id']);
  556. $aInfo = [];
  557. if(($iId = bx_get('id')) !== false) {
  558. $aInfo = bx_srv($sModule, 'get_info', [(int)$iId, false]);
  559. }
  560. else if(($iPid = bx_get('profile_id')) !== false) {
  561. $aInfo = bx_srv($sModule, 'get_content_info_by_profile_id', [(int)$iPid]);
  562. }
  563. $aCurrent = [];
  564. if($aInfo && ((($sKey = 'allow_view_to') && isset($aInfo[$sKey]) && ($iValue = (int)$aInfo[$sKey])) || (($sKey = 'object_privacy_view') && isset($aInfo[$sKey]) && ($iValue = (int)$aInfo[$sKey])))) {
  565. $mixedContext = false;
  566. if($iValue < 0) {
  567. $aContext = BxDolProfileQuery::getInstance()->getInfoById(abs($iValue));
  568. if($aContext && $aContext['type'] == $sContextSwitcher)
  569. $mixedContext = (int)$aContext['id'];
  570. }
  571. else if($sModule == $sContextSwitcher)
  572. $mixedContext = BxDolProfile::getInstanceByContentAndType((int)$aInfo['id'], $sModule);
  573. if($mixedContext !== false)
  574. $aCurrent = BxDolProfile::getData($mixedContext);
  575. }
  576. $_sSample = '_' . $sContextSwitcher . '_txt_sample_single';
  577. $sSample = _t($_sSample);
  578. if(strcmp($_sSample, $sSample) == 0)
  579. $sSample = _t('_Context');
  580. $a['context'] = [
  581. 'list' => $aContexts,
  582. 'invitations' => $aInvitedTo,
  583. 'current' => $aCurrent,
  584. 'links' => []
  585. ];
  586. if(bx_srv($sContextSwitcher, 'check_allowed', ['add']) === CHECK_ACTION_RESULT_ALLOWED)
  587. $a['context']['links'][] = [
  588. 'title' => _t('_api_create_context', $sSample),
  589. 'icon' => 'Plus',
  590. 'url' => bx_api_get_relative_url(bx_srv($sContextSwitcher, 'get_link_add'))
  591. ];
  592. }
  593. }
  594. if(!$bIsAvailable)
  595. $a['page_status'] = 404;
  596. else if(!$bIsVisible)
  597. $a['page_status'] = 403;
  598. return array_merge($a, [
  599. 'version' => ($sVersion = bx_get_ver()) && $sVersion == BX_DOL_VERSION ? $sVersion : 'invalid',
  600. 'ts' => time()
  601. ]);
  602. }
  603. public function getPageContentAPI($aBlocks = [])
  604. {
  605. $sModule = $this->getModule();
  606. $sName = $this->_getPageTitle();
  607. $sMetaTitle = $this->_getPageMetaTitle();
  608. return [
  609. 'module' => $sModule,
  610. 'title' => $sMetaTitle ? $sMetaTitle : $sName,
  611. 'elements' => $this->getPageBlocksAPI($aBlocks),
  612. ];
  613. }
  614. public function getPageBlocksAPI($aBlocks = [])
  615. {
  616. $bBlocks = !empty($aBlocks) && is_array($aBlocks);
  617. $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'];
  618. $aCells = $this->_oQuery->getPageBlocks(true);
  619. foreach($aCells as $sKey => &$aCell) {
  620. foreach($aCell as $i => &$aBlock) {
  621. if(!$this->_isVisibleBlock($aBlock)) {
  622. unset($aCells[$sKey][$i]);
  623. continue;
  624. }
  625. $this->processPageBlock($aCells[$sKey][$i], true);
  626. $aBlock = $aCells[$sKey][$i];
  627. $sSource = '';
  628. if($aBlock['type'] == 'service') {
  629. $aContent = @unserialize($aBlock['content']);
  630. if(isset($aContent['module'], $aContent['method']))
  631. $sSource = $aContent['module'] . ':' . $aContent['method'];
  632. }
  633. else
  634. $sSource = 'system:block_' . $aBlock['id'];
  635. if($bBlocks && !in_array($sSource, $aBlocks)) {
  636. unset($aCells[$sKey][$i]);
  637. continue;
  638. }
  639. if(($sK = 'config_api') && !empty($aBlock[$sK])) {
  640. $aConfigApi = json_decode($aBlock[$sK], true);
  641. if(!empty($aConfigApi) && is_array($aConfigApi))
  642. $aBlock[$sK] = $aConfigApi;
  643. }
  644. $mBlock = $aBlock['content'];
  645. if(($sFunc = '_getBlock' . bx_gen_method_name($aBlock['type'])) && method_exists($this, $sFunc))
  646. $mBlock = $this->$sFunc($aBlock);
  647. if(empty($mBlock)) {
  648. unset($aCells[$sKey][$i]);
  649. continue;
  650. }
  651. $aCells[$sKey][$i] = array_merge($aCells[$sKey][$i], [
  652. 'title' => isset($mBlock['title']) ? $mBlock['title'] : $this->getBlockTitle($aBlock),
  653. 'content' => isset($mBlock['content']) ? $mBlock['content'] : $mBlock,
  654. 'menu' => isset($mBlock['menu']) ? $mBlock['menu'] : '',
  655. 'source' => $sSource
  656. ]);
  657. $aCells[$sKey][$i] = array_diff_key($aCells[$sKey][$i], array_flip($aFieldsUnset));
  658. }
  659. }
  660. return array_map('array_values', $aCells);
  661. }
  662. /**
  663. * Get page code vars
  664. * @return string
  665. */
  666. protected function _getPageCodeVars ()
  667. {
  668. $aHiddenOn = array(
  669. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  670. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  671. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  672. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  673. );
  674. $aVars = array (
  675. 'page_id' => 'bx-page-' . $this->_aObject['uri'],
  676. 'bx_if:show_layout_row_dump' => array(
  677. 'condition' => false,
  678. 'content' => array()
  679. )
  680. );
  681. $aBlocks = $this->_oQuery->getPageBlocks();
  682. foreach ($aBlocks as $sKey => $aCell) {
  683. $sCell = '';
  684. foreach ($aCell as $aBlock) {
  685. $this->processPageBlock($aBlock, false);
  686. $sContentWithBox = $this->_getBlockCodeWithCache($aBlock, $aBlock['async']);
  687. $sClassAdd = '';
  688. if(!empty($aBlock['class']))
  689. $sClassAdd .= ' ' . $aBlock['class'];
  690. if(!empty($aBlock['hidden_on']))
  691. foreach($aHiddenOn as $iHiddenOn => $sHiddenOnClass)
  692. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  693. $sClassAdd .= ' ' . $sHiddenOnClass;
  694. if ($sContentWithBox)
  695. $sCell .= $this->_oTemplate->parseHtmlByName('designbox_container.html', array(
  696. 'class_add' => $sClassAdd,
  697. 'bx_if:show_html_id' => array(
  698. 'condition' => true,
  699. 'content' => array(
  700. 'html_id' => 'bx-page-block-' . $aBlock['id']
  701. ),
  702. ),
  703. 'content' => $sContentWithBox
  704. ));
  705. }
  706. $aVars[$sKey] = $sCell;
  707. }
  708. return $aVars;
  709. }
  710. /**
  711. * Process block values, especially if someting need to be overrided
  712. */
  713. protected function processPageBlock(&$aBlock, $bApi = false)
  714. {
  715. }
  716. /**
  717. * Get page code only.
  718. * @return string
  719. */
  720. protected function _getPageCode ()
  721. {
  722. $aVars = $this->_getPageCodeVars ();
  723. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  724. }
  725. /**
  726. * Get one block code only.
  727. * @return string
  728. */
  729. protected function _getBlockOnlyCode ($iBlockId)
  730. {
  731. if (!($aBlock = $this->_oQuery->getPageBlock((int)$iBlockId)))
  732. return '';
  733. return $this->_getBlockCodeWithCache($aBlock, 0);
  734. }
  735. protected function _getBlockCodeWithCache(&$aBlock, $iAsync = 0)
  736. {
  737. if (bx_is_dbg() || !getParam('sys_pb_cache_enable') || !$aBlock['cache_lifetime'] || $iAsync || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  738. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  739. }
  740. else {
  741. $oCache = $this->_getBlockCacheObject();
  742. $sKey = $this->_getBlockCacheKey(false, $aBlock);
  743. $sKeyCssJs = 'cssjs_' . $sKey;
  744. $mixedRet = $oCache->getData($sKey, $aBlock['cache_lifetime']);
  745. $aRetCssJs = $oCache->getData($sKeyCssJs, $aBlock['cache_lifetime']);
  746. if ($mixedRet !== null && $aRetCssJs !== null) {
  747. BxDolTemplate::getInstance()->collectingStart();
  748. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  749. $sBlockCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  750. $sBlockCode .= $mixedRet;
  751. } else {
  752. BxDolTemplate::getInstance()->collectingStart();
  753. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  754. $aBlockCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  755. $oCache->setData($sKey, $sBlockCode, $aBlock['cache_lifetime']);
  756. $oCache->setData($sKeyCssJs, $aBlockCssJs, $aBlock['cache_lifetime']);
  757. }
  758. }
  759. return $sBlockCode;
  760. }
  761. /**
  762. * Get block code.
  763. * @return string
  764. */
  765. protected function _getBlockCode(&$aBlock, $iAsync = 0)
  766. {
  767. $sContentWithBox = '';
  768. $oFunctions = $this->_oTemplate->getTemplateFunctions();
  769. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  770. $aDbNoTitle = [BX_DB_CONTENT_ONLY, BX_DB_PADDING_CONTENT_ONLY, BX_DB_NO_CAPTION, BX_DB_PADDING_NO_CAPTION];
  771. $sTitle = $this->getBlockTitle($aBlock);
  772. $sHelp = $this->getBlockHelp($aBlock);
  773. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  774. $bBlockVisible = $this->_isVisibleBlock($aBlock);
  775. if ($iAsync && $bBlockVisible) {
  776. $iDesignboxId = $aBlock['designbox_id'];
  777. $sHelpTitle = $sHelpContent = '';
  778. if(!in_array($iDesignboxId, $aDbNoTitle))
  779. $sHelpTitle = $sHelp;
  780. else
  781. $sHelpContent = $sHelp;
  782. $sContent = $this->getBlockAsyncCode($aBlock, $iAsync);
  783. $aParams = array(
  784. $sTitle . $sHelpTitle,
  785. $sContent . $sHelpContent,
  786. $iDesignboxId
  787. );
  788. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  789. }
  790. elseif ($bBlockVisible && method_exists($this, $sFunc)) {
  791. $mixedContent = $this->$sFunc($aBlock);
  792. if(!empty($aBlock['content_empty'])) {
  793. $sEmpty = _t($aBlock['content_empty']);
  794. $sRegExp = '/(<[A-Za-z0-9]+\b.[^<]*[\pPi\pPf]bx\-msg\-box\b.*[\pPi\pPf][^>]*>)(' . _t('_Empty') . ')(<\/[A-Za-z0-9]+>)/su';
  795. if(is_string($mixedContent))
  796. $mixedContent = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent);
  797. else if(is_array($mixedContent) && !empty($mixedContent['content']))
  798. $mixedContent['content'] = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent['content']);
  799. }
  800. $this->_oQuery->setReadOnlyMode(true);
  801. if(is_array($mixedContent) && !empty($mixedContent['content'])) {
  802. $iDesignboxId = isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'];
  803. if(!empty($mixedContent['markers']) && is_array($mixedContent['markers'])) {
  804. $this->addMarkers($mixedContent['markers']);
  805. $sTitle = $this->getBlockTitle($aBlock);
  806. }
  807. $sHelpTitle = $sHelpContent = '';
  808. if(!in_array($iDesignboxId, $aDbNoTitle))
  809. $sHelpTitle = $sHelp;
  810. else
  811. $sHelpContent = $sHelp;
  812. $aParams = array(
  813. (isset($mixedContent['title']) ? $mixedContent['title'] : $sTitle) . $sHelpTitle,
  814. $mixedContent['content'] . $sHelpContent,
  815. $iDesignboxId
  816. );
  817. $mixedMenu = false;
  818. if(isset($mixedContent['menu']))
  819. $mixedMenu = $mixedContent['menu'];
  820. else if(!empty($aBlock['submenu']))
  821. $mixedMenu = $aBlock['submenu'];
  822. $aParams[] = $mixedMenu;
  823. if(isset($mixedContent['buttons']))
  824. $aParams[] = $mixedContent['buttons'];
  825. else if($mixedMenu)
  826. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  827. if(isset($mixedContent['class']))
  828. $aBlock['class'] = $mixedContent['class'];
  829. }
  830. else if(is_string($mixedContent) && !empty($mixedContent)) {
  831. $iDesignboxId = $aBlock['designbox_id'];
  832. $sHelpTitle = $sHelpContent = '';
  833. if(!in_array($iDesignboxId, $aDbNoTitle))
  834. $sHelpTitle = $sHelp;
  835. else
  836. $sHelpContent = $sHelp;
  837. $aParams = array(
  838. $sTitle . $sHelpTitle,
  839. $mixedContent . $sHelpContent,
  840. $iDesignboxId
  841. );
  842. $mixedMenu = !empty($aBlock['submenu']) ? $aBlock['submenu'] : false;
  843. $aParams[] = $mixedMenu;
  844. if($mixedMenu)
  845. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  846. }
  847. if(!empty($aParams))
  848. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  849. }
  850. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  851. return $sContentWithBox;
  852. }
  853. /**
  854. * Add necessary js and css files.
  855. */
  856. protected function _addJsCss()
  857. {
  858. $this->_oTemplate->addJs(array('BxDolPage.js', 'theia-sticky-sidebar/theia-sticky-sidebar.umd.js', 'theia-sticky-sidebar/ResizeSensor.min.js'));
  859. $this->_oTemplate->addCss('page_layouts.css');
  860. }
  861. /**
  862. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  863. */
  864. protected function _addSysTemplateVars ()
  865. {
  866. $oTemplate = BxDolTemplate::getInstance();
  867. $sPageTitle = $this->_getPageTitle();
  868. if ($sPageTitle)
  869. $oTemplate->setPageHeader ($sPageTitle);
  870. $sMetaTitle = $this->_getPageMetaTitle();
  871. if ($sMetaTitle)
  872. $oTemplate->setPageMetaTitle ($sMetaTitle);
  873. $sMetaDesc = $this->_getPageMetaDesc();
  874. if ($sMetaDesc)
  875. $oTemplate->setPageDescription ($sMetaDesc);
  876. $sMetaRobots = $this->_getPageMetaRobots();
  877. if ($sMetaRobots)
  878. $oTemplate->setPageMetaRobots ($sMetaRobots);
  879. $sMetaImage = $this->_getPageMetaImage();
  880. if ($sMetaImage)
  881. $oTemplate->addPageMetaImage($sMetaImage);
  882. $sMetaKeywords = $this->_getPageMetaKeywords();
  883. if ($sMetaKeywords)
  884. $oTemplate->addPageKeywords ($sMetaKeywords);
  885. }
  886. /**
  887. * Select menu from page properties.
  888. */
  889. protected function _selectMenu ()
  890. {
  891. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  892. }
  893. /**
  894. * Set page submenu if it's specified
  895. */
  896. protected function _setSubmenu ($aParams)
  897. {
  898. if(empty($this->_aObject['submenu']) || $this->_aObject['submenu'] == 'disabled')
  899. return;
  900. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  901. if(!$oMenuSubmenu)
  902. return;
  903. $oMenuSubmenu->setObjectSubmenu($this->_aObject['submenu'], $aParams);
  904. }
  905. /**
  906. * Get content for 'raw' block type.
  907. * @return string
  908. */
  909. protected function _getBlockRaw ($aBlock)
  910. {
  911. if (bx_is_api()){
  912. return [bx_api_get_block('raw', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  913. }
  914. $s = '<div class="bx-page-raw-container bx-def-vanilla-html max-w-none">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  915. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  916. $s = bx_process_macros($s);
  917. return $s;
  918. }
  919. /**
  920. * Get content for 'custom' block type.
  921. * @return string
  922. */
  923. protected function _getBlockCustom ($aBlock)
  924. {
  925. if (bx_is_api()){
  926. return [bx_api_get_block('custom', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  927. }
  928. $s = '<div class="bx-page-custom-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  929. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  930. $s = bx_process_macros($s);
  931. return $s;
  932. }
  933. /**
  934. * Get content for 'html' block type.
  935. * @return string
  936. */
  937. protected function _getBlockHtml ($aBlock)
  938. {
  939. if (bx_is_api()){
  940. return [bx_api_get_block('html', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  941. }
  942. $s = '<div class="bx-page-html-container bx-def-vanilla-html max-w-none">' . $aBlock['content'] . '</div>';
  943. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  944. $s = bx_process_macros($s);
  945. return $s;
  946. }
  947. /**
  948. * Get content for 'creative' block type.
  949. * @return string
  950. */
  951. protected function _getBlockCreative ($aBlock)
  952. {
  953. if(bx_is_api())
  954. return [bx_api_get_block('creative', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  955. $s = $this->_replaceMarkers($aBlock['content'], ['block_id' => $aBlock['id']]);
  956. $s = bx_process_macros($s);
  957. $bCanManage = $this->isAllowedCreativeManage();
  958. if($bCanManage) {
  959. $this->_oTemplate->addCss([
  960. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'grapesjs/|grapes.min.css',
  961. ]);
  962. $this->_oTemplate->addJs([
  963. 'grapesjs/grapes.min.js',
  964. 'grapesjs/grapesjs-blocks-basic.js',
  965. 'grapesjs/grapesjs-style-bg.js',
  966. 'grapesjs/grapesjs-preset-webpage.min.js',
  967. ]);
  968. }
  969. return $this->_oTemplate->parseHtmlByName('block_creative.html', [
  970. 'block_id' => $aBlock['id'],
  971. 'content' => $s,
  972. 'bx_if:show_controls' => [
  973. 'condition' => $bCanManage,
  974. 'content' => [
  975. 'onclick_edit' => $this->_sJsObjectName . '.creativeEdit(this, ' . $aBlock['id'] . ')',
  976. 'onclick_save' => $this->_sJsObjectName . '.creativeSave(this, ' . $aBlock['id'] . ')'
  977. ]
  978. ]
  979. ]);
  980. }
  981. /**
  982. * Get content for 'bento_grid' block type.
  983. * @return string
  984. */
  985. protected function _getBlockBentoGrid ($aBlock)
  986. {
  987. if(bx_is_api()) {
  988. $iBlockId = (int)$aBlock['id'];
  989. $iContentId = 0;
  990. if(($mContentId = bx_get('id')) !== false)
  991. $iContentId = (int)$mContentId;
  992. $sContentModule = '';
  993. if(!empty($this->_aObject['module']) && !in_array($this->_aObject['module'], ['system', 'custom']))
  994. $sContentModule = $this->_aObject['module'];
  995. $bIsAllowedEdit = false;
  996. if(!empty($sContentModule) && bx_is_srv($sContentModule, 'check_allowed_with_content'))
  997. $bIsAllowedEdit = bx_srv($sContentModule, 'check_allowed_with_content', ['edit', $iContentId]) === CHECK_ACTION_RESULT_ALLOWED;
  998. else
  999. $bIsAllowedEdit = isAdmin();
  1000. return [bx_api_get_block('bento_grid', [
  1001. 'title' => _t($aBlock['title']),
  1002. 'content' => self::getPageBlockData($iBlockId, $iContentId, $sContentModule)
  1003. ], ['ext' => [
  1004. 'block_id' => $iBlockId,
  1005. 'content_id' => $iContentId,
  1006. 'content_module' => $sContentModule,
  1007. 'is_allowed_edit' => $bIsAllowedEdit
  1008. ]])];
  1009. }
  1010. /**
  1011. * Isn't supported for now.
  1012. */
  1013. return '';
  1014. }
  1015. /**
  1016. * Get content for 'wiki' block type.
  1017. * @return string
  1018. */
  1019. protected function _getBlockWiki ($aBlock)
  1020. {
  1021. $oWiki = BxDolWiki::getObjectInstance($this->_aObject['module']);
  1022. if (!$oWiki) {
  1023. $sContent = _t('_sys_wiki_error_missing_wiki_object', $this->_aObject['module']);
  1024. }
  1025. else {
  1026. $sContent = $oWiki->getBlockContent($aBlock['id'], false, (int)bx_get($aBlock['id'].'rev') ? (int)bx_get($aBlock['id'].'rev') : false);
  1027. }
  1028. $s = '<div id="bx-page-wiki-container-' . $aBlock['id'] . '" class="bx-page-wiki-container markdown-body bx-def-vanilla-html">' . $sContent . '</div>';
  1029. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  1030. $s = bx_process_macros($s);
  1031. return $s;
  1032. }
  1033. /**
  1034. * Get content for 'lang' block type.
  1035. * @return string
  1036. */
  1037. protected function _getBlockLang ($aBlock)
  1038. {
  1039. if (bx_is_api()){
  1040. return [bx_api_get_block('lang', ['title' => _t($aBlock['title']), 'content' => bx_process_macros(_t(trim($aBlock['content'])))])];
  1041. }
  1042. $s = '<div class="bx-page-lang-container bx-def-vanilla-html max-w-none">' . _t(trim($aBlock['content'])) . '</div>';
  1043. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  1044. $s = bx_process_macros($s);
  1045. return $s;
  1046. }
  1047. /**
  1048. * Get content for 'image' block type.
  1049. * @return string
  1050. */
  1051. protected function _getBlockImage ($aBlock)
  1052. {
  1053. if (empty($aBlock['content']))
  1054. return false;
  1055. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  1056. $iFileId = (int)$iFileId;
  1057. if (!$iFileId)
  1058. return false;
  1059. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  1060. if (!$oStorage)
  1061. return false;
  1062. $sUrl = $oStorage->getFileUrlById($iFileId);
  1063. if (!$sUrl)
  1064. return false;
  1065. $sStyleAdd = '';
  1066. $sClassAdd = 'bx-page-image-container';
  1067. $aAlign = ['left' => 'start', 'center' => 'center', 'right' => 'end'];
  1068. if(array_key_exists($sAlign, $aAlign)) {
  1069. $sClassAdd .= ' flex justify-' . $aAlign[$sAlign];
  1070. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  1071. }
  1072. return '<div class="' . $sClassAdd . '" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  1073. }
  1074. /**
  1075. * Get content for 'rss' block type.
  1076. * @return string
  1077. */
  1078. protected function _getBlockRss ($aBlock)
  1079. {
  1080. if (empty($aBlock['content']))
  1081. return false;
  1082. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  1083. $iNum = (int)$iNum;
  1084. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  1085. }
  1086. /**
  1087. * Get content for 'menu' block type.
  1088. * @return string
  1089. */
  1090. protected function _getBlockMenu ($aBlock)
  1091. {
  1092. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  1093. return $oMenu ? $oMenu->getCode () : '';
  1094. }
  1095. /**
  1096. * Get content for 'service' block type.
  1097. * @return string
  1098. */
  1099. protected function _getBlockService ($aBlock)
  1100. {
  1101. self::setBlockProcessing($aBlock);
  1102. $aResult = BxDolService::callSerialized($aBlock['content'], array_merge($this->_aMarkers, [
  1103. 'block_id' => $aBlock['id']
  1104. ]));
  1105. self::unsetBlockProcessing();
  1106. return $aResult;
  1107. }
  1108. /**
  1109. * Get page title.
  1110. * @return string
  1111. */
  1112. protected function _getPageTitle()
  1113. {
  1114. return $this->_replaceMarkers(_t($this->_aObject['title']));
  1115. }
  1116. /**
  1117. * Get page meta description.
  1118. * @return string
  1119. */
  1120. protected function _getPageMetaDesc()
  1121. {
  1122. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  1123. }
  1124. /**
  1125. * Get page meta title.
  1126. * @return string
  1127. */
  1128. protected function _getPageMetaTitle()
  1129. {
  1130. return $this->_replaceMarkers(_t($this->_aObject['meta_title']));
  1131. }
  1132. /**
  1133. * Get page meta image.
  1134. * @return string
  1135. */
  1136. protected function _getPageMetaImage()
  1137. {
  1138. return '';
  1139. }
  1140. /**
  1141. * Get page meta keywords.
  1142. * @return string
  1143. */
  1144. protected function _getPageMetaKeywords()
  1145. {
  1146. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  1147. }
  1148. /**
  1149. * Get page meta robots.
  1150. * @return string
  1151. */
  1152. protected function _getPageMetaRobots()
  1153. {
  1154. return _t($this->_aObject['meta_robots']);
  1155. }
  1156. /**
  1157. * Get page not found message.
  1158. * @return string
  1159. */
  1160. protected function _getPageNotFoundMsg ($mixedMsg = false)
  1161. {
  1162. $this->_oTemplate->displayPageNotFound();
  1163. exit;
  1164. }
  1165. /**
  1166. * Get access denied message.
  1167. * @return string
  1168. */
  1169. protected function _getPageAccessDeniedMsg ($mixedMsg = false)
  1170. {
  1171. return MsgBox($mixedMsg !== false ? $mixedMsg : _t('_Access denied'));
  1172. }
  1173. /**
  1174. * Get page cache object.
  1175. * @return cache object instance
  1176. */
  1177. protected function _getPageCacheObject ()
  1178. {
  1179. return $this->_getCacheObject ();
  1180. }
  1181. /**
  1182. * Get block cache object.
  1183. * @return cache object instance
  1184. */
  1185. protected function _getBlockCacheObject ()
  1186. {
  1187. return $this->_getCacheObject ('Block', 'pb');
  1188. }
  1189. protected function _getCacheObject ($sSuffixObj = 'Page', $sSuffixParam = 'page')
  1190. {
  1191. $sObj = '_o' . $sSuffixObj . 'CacheObject';
  1192. $sParam = 'sys_' . $sSuffixParam . '_cache_engine';
  1193. if ($this->{$sObj} != null) {
  1194. return $this->{$sObj};
  1195. } else {
  1196. $sEngine = getParam($sParam);
  1197. $this->{$sObj} = bx_instance ('BxDolCache' . $sEngine);
  1198. if (!$this->{$sObj}->isAvailable())
  1199. $this->{$sObj} = bx_instance ('BxDolCacheFile');
  1200. return $this->{$sObj};
  1201. }
  1202. }
  1203. /**
  1204. * Get page cache key.
  1205. * @param $isPrefixOnly return key prefix only.
  1206. * @return string
  1207. */
  1208. protected function _getPageCacheKey ($isPrefixOnly = false)
  1209. {
  1210. $s = 'page_' . $this->_aObject['object'] . '_';
  1211. if ($isPrefixOnly)
  1212. return $s;
  1213. $s .= $this->_getPageCacheParams ();
  1214. $s .= bx_site_hash() . '.php';
  1215. return $s;
  1216. }
  1217. /**
  1218. * Get block cache key.
  1219. * @param $isPrefixOnly return key prefix only.
  1220. * @return string
  1221. */
  1222. protected function _getBlockCacheKey ($isPrefixOnly = false, $aBlock = array())
  1223. {
  1224. $s = $this->_getPageCacheKey(true) . 'block_';
  1225. if ($isPrefixOnly)
  1226. return $s;
  1227. $s .= ($aBlock ? $aBlock['id'] : 0) . '_' . $this->_getPageCacheParams ();
  1228. $s .= bx_site_hash() . '.php';
  1229. return $s;
  1230. }
  1231. /**
  1232. * Additional cache key. In the case of dynamic page.
  1233. * For example - profile page, where each profile has own page.
  1234. * @return string
  1235. */
  1236. protected function _getPageCacheParams ()
  1237. {
  1238. return md5(serialize($_GET) . bx_lang_name());
  1239. }
  1240. /**
  1241. * Clean page cache.
  1242. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  1243. * @return string
  1244. */
  1245. protected function cleanCache ($isDelAllWithPagePrefix = false)
  1246. {
  1247. $a = ['Page', 'Block'];
  1248. $bRet = false;
  1249. foreach ($a as $s) {
  1250. $oCache = $this->{'_get' . $s . 'CacheObject'} ();
  1251. $sKey = $this->{'_get'. $s . 'CacheKey'} ($isDelAllWithPagePrefix);
  1252. if ($isDelAllWithPagePrefix)
  1253. $bRet = $bRet && $oCache->removeAllByPrefix($sKey);
  1254. else
  1255. $bRet = $bRet && $oCache->delData($sKey);
  1256. }
  1257. return $bRet;
  1258. }
  1259. }
  1260. /** @} */