BxBasePage.php 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  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. 'page_code_ref' => &$sPageCode,
  275. 'sub_page' => $this->_bSubPage,
  276. ]);
  277. if (!$this->_bSubPage)
  278. $sPageCode .= $this->getJsScript();
  279. return $sPageCode;
  280. }
  281. public function getJsClassName()
  282. {
  283. return $this->_sJsClassName;
  284. }
  285. public function getJsObjectName()
  286. {
  287. return $this->_sJsObjectName;
  288. }
  289. function _wrapInTagJsCode($sCode)
  290. {
  291. return "<script language=\"javascript\">\n<!--\n" . $sCode . "\n-->\n</script>";
  292. }
  293. public function getJsScript()
  294. {
  295. $sJsObjName = $this->getJsObjectName();
  296. $sJsObjClass = $this->getJsClassName();
  297. $sCode = "if(window['" . $sJsObjName . "'] == undefined) var " . $sJsObjName . " = new " . $sJsObjClass . "(" . json_encode(array(
  298. 'sObjName' => $sJsObjName,
  299. 'sObject' => $this->_sObject,
  300. 'sRootUrl' => BX_DOL_URL_ROOT,
  301. 'aHtmlIds' => $this->_aHtmlIds,
  302. 'isStickyColumns' => $this->_bStickyColumns
  303. )) . ");";
  304. return $this->_wrapInTagJsCode($sCode);
  305. }
  306. /**
  307. * Is page cover enabled.
  308. * @return string
  309. */
  310. public function isPageCover()
  311. {
  312. $bResult = false;
  313. switch((int)$this->_aObject['cover']) {
  314. case 1: //--- Enabled for all
  315. $bResult = true;
  316. break;
  317. case 2: //--- Enabled for visitors only
  318. $bResult = !isLogged();
  319. break;
  320. case 3: //--- Enabled for members only
  321. $bResult = isLogged();
  322. break;
  323. }
  324. return $bResult;
  325. }
  326. public function setSubPage($b = true)
  327. {
  328. $this->_bSubPage = $b;
  329. }
  330. public function setPageCover($bCover = true)
  331. {
  332. $this->_aObject['cover'] = (bool)$bCover;
  333. }
  334. public function getPageCoverImage($bTranscoder = true)
  335. {
  336. $iId = (int)$this->_aObject['cover_image'];
  337. if(empty($iId)) {
  338. $iId = (int)getParam('sys_site_cover_common');
  339. if(empty($iId))
  340. return array();
  341. }
  342. $aResult = array(
  343. 'id' => $iId
  344. );
  345. if($bTranscoder)
  346. $aResult['transcoder'] = BX_DOL_TRANSCODER_OBJ_COVER;
  347. else
  348. $aResult['object'] = $this->_sStorage;
  349. return $aResult;
  350. }
  351. public function getPageCoverParams()
  352. {
  353. $aParams = [
  354. 'title' => $this->_getPageTitle(),
  355. 'actions' => '',
  356. 'bx_if:image' => [
  357. 'condition' => false,
  358. 'content' => []
  359. ],
  360. 'bx_if:icon' => [
  361. 'condition' => false,
  362. 'content' => []
  363. ],
  364. ];
  365. if(($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  366. $aCoverParams = $oMenuSubmenu->getPageCoverParams();
  367. if(!empty($aCoverParams) && is_array($aCoverParams))
  368. $aParams = $aCoverParams;
  369. }
  370. if(!empty($this->_aObject['cover_title'])) {
  371. $sCoverTitle = _t($this->_aObject['cover_title']);
  372. if($sCoverTitle && strcmp($sCoverTitle, $this->_aObject['cover_title']) != 0)
  373. $aParams['title'] = $sCoverTitle;
  374. }
  375. return $aParams;
  376. }
  377. /**
  378. * Get block title.
  379. * @return string
  380. */
  381. public function getBlockTitle ($aBlock)
  382. {
  383. return $this->_replaceMarkers(_t($aBlock['title']), array('block_id' => $aBlock['id']));
  384. }
  385. /**
  386. * Get block description.
  387. * @return string
  388. */
  389. public function getBlockDescription ($aBlock)
  390. {
  391. return $this->_replaceMarkers(_t($aBlock['description']), ['block_id' => $aBlock['id']]);
  392. }
  393. /**
  394. * Get block icon.
  395. * @return string
  396. */
  397. public function getBlockIcon ($aBlock)
  398. {
  399. return $aBlock['icon'] ?? '';
  400. }
  401. /**
  402. * Get help control if help is available for the block.
  403. * @return string
  404. */
  405. public function getBlockHelp ($aBlock)
  406. {
  407. if(empty($aBlock['help']) || empty(strip_tags(_t($aBlock['help']))))
  408. return '';
  409. return $this->_oTemplate->parseHtmlByName('designbox_help.html', array(
  410. 'js_object' => $this->_sJsObjectName,
  411. 'block_id' => $aBlock['id']
  412. ));
  413. }
  414. /**
  415. * Get code to load block asyncroniously
  416. * @param $aBlock block code
  417. * @param $iAsync if greater than 0 the it defines loading indicator for the block
  418. * @return HTML code string
  419. */
  420. public function getBlockAsyncCode($aBlock, $iAsync)
  421. {
  422. $aContentPlaceholder = $this->_oQuery->getPageBlockContentPlaceholder($iAsync);
  423. if (!$aContentPlaceholder)
  424. return _t('_sys_txt_error_occured');
  425. $oTemplate = $this->_oTemplate;
  426. if ('system' != $aContentPlaceholder['module']) {
  427. $oModule = BxDolModule::getInstance($aContentPlaceholder['module']);
  428. if (!$oModule)
  429. return _t('_sys_txt_error_occured');
  430. $oTemplate = $oModule->_oTemplate;
  431. }
  432. return $oTemplate->parseHtmlByName($aContentPlaceholder['template'], $aBlock);
  433. }
  434. /**
  435. * Get page array with all cells and blocks
  436. */
  437. public function getPageAPI ($aBlocks = [])
  438. {
  439. define('BX_API_PAGE', true);
  440. $aGetParams = bx_get('params');
  441. $bGetParams = $aGetParams !== false;
  442. $sQueryString = '';
  443. if($bGetParams && !empty($aGetParams[2]) && ($_aGetParams = json_decode($aGetParams[2], true)) && is_array($_aGetParams)) {
  444. $sQueryString = http_build_query($_aGetParams);
  445. parse_str($sQueryString, $aQueryString);
  446. $_GET = array_merge($_GET, $aQueryString);
  447. }
  448. $bLogged = isLogged();
  449. $bIsAvailable = $this->_isAvailablePage($this->_aObject);
  450. $bIsVisible = $this->_isVisiblePage($this->_aObject);
  451. //TODO: If page isn't Available or isn't Visible then we should exit with related Error Codes immediately.
  452. $sMetaTitle = $this->_getPageMetaTitle();
  453. $sName = $this->_getPageTitle();
  454. $sUri = $this->_aObject['uri'];
  455. $sUrl = ($bGetParams && !empty($aGetParams[0]) ? $aGetParams[0] : $sUri) . ($sQueryString != '' ? '?' . $sQueryString : '');
  456. $sModule = $this->getModule();
  457. $aElements = $this->getPageBlocksAPI($aBlocks);
  458. if(!$bLogged && !in_array($sUri, ['home', 'login', 'create-account']) && ($sAutoProfile = getParam('sys_account_default_profile_type'))) {
  459. $sRedirect = getParam($sAutoProfile . '_redirect_aadd');
  460. if($sRedirect == 'last') {
  461. $aAutoProfileConfig = bx_srv($sAutoProfile, 'module_config');
  462. if($sUri != ($aAutoProfileConfig['URI_ADD_ENTRY'] ?? ''))
  463. BxDolSession::getInstance()->setValue('custom-referrer', $sUrl);
  464. }
  465. }
  466. $a = [
  467. 'id' => $this->_aObject['id'],
  468. 'title' => $sMetaTitle ? $sMetaTitle : $sName,
  469. 'name' => $sName,
  470. 'description' => $this->_getPageMetaDesc(),
  471. 'keywords' => $this->_getPageMetaKeywords(),
  472. 'image' => '',
  473. 'uri' => $sUri,
  474. 'url' => $sUrl,
  475. 'author' => $this->_aObject['author'],
  476. 'added' => $this->_aObject['added'],
  477. 'module' => $sModule,
  478. 'type' => $this->getType (),
  479. 'layout' => !$bLogged && $sUri == 'home' && ($sReplacement = getParam('sys_api_root_page_guest')) == 'splash' ? $sReplacement : str_replace('.html', '', $this->_aObject['template']),
  480. 'layout_parsed' => !is_numeric(trim(current(array_keys($aElements)), 'cell_')),
  481. 'cover_block' => '',
  482. 'cover' => $this->_aObject['cover'],
  483. 'menu_top' => '',
  484. 'menu' => '',
  485. 'menu_bottom' => '',
  486. 'menu_add' => '',
  487. 'config' => $this->_aObject['config_api'],
  488. 'elements' => $aElements
  489. ];
  490. $sMenuTop = getParam('sys_api_menu_top');
  491. if (!empty($sMenuTop) && ($oMenuTop = BxDolMenu::getObjectInstance($sMenuTop)) !== false)
  492. $a['menu_top'] = $oMenuTop->getCodeAPI();
  493. if (($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  494. if(($sSubmenu = $this->getSubMenu()) !== '')
  495. $oMenuSubmenu->setObjectSubmenu($sSubmenu, []);
  496. $a['menu'] = $oMenuSubmenu->getCodeAPI();
  497. if($sModule && $sModule != 'system' && (bx_srv('system', 'is_module_content', [$sModule]) || bx_srv('system', 'is_module_context', [$sModule])) && ($oModule = BxDolModule::getInstance($sModule))) {
  498. $CNF = &$oModule->_oConfig->CNF;
  499. $aMenuAdd = [
  500. 'name' => $oModule->getName(),
  501. 'title' => BxDolModule::getTitle($oModule->_oConfig->getUri()),
  502. 'icon' => !empty($CNF['ICON']) ? $CNF['ICON'] : '',
  503. ];
  504. $sUrlAdd = '';
  505. if(($sKey = 'URI_ADD_ENTRY') && !empty($CNF[$sKey]))
  506. $sUrlAdd = $CNF[$sKey];
  507. if(!$sUrlAdd && ($sKey = 'URI_EDIT_ENTRY') && !empty($CNF[$sKey]))
  508. $sUrlAdd = str_replace('edit-', 'create-', $CNF[$sKey]);
  509. if($sUrlAdd)
  510. $aMenuAdd['add_url'] = $sUrlAdd;
  511. $a['menu'] = array_merge($a['menu'], $aMenuAdd);
  512. }
  513. }
  514. /**
  515. * Profile/Context view page.
  516. */
  517. if(isset($this->_aProfileInfo)) {
  518. $oProfileModule = BxDolModule::getInstance($this->_aProfileInfo['type']);
  519. if($oProfileModule !== null)
  520. $aProfileContentInfo = $oProfileModule->_oDb->getContentInfoById($this->_aProfileInfo['content_id']);
  521. if(!empty($aProfileContentInfo) && is_array($aProfileContentInfo)) {
  522. $CNF = &$oProfileModule->_oConfig->CNF;
  523. /**
  524. * Process page cover with related menus and lists.
  525. */
  526. if(BxDolCover::getInstance($this)->isCover()) {
  527. $aProfileInfo = BxDolProfile::getData($this->_aProfileInfo['id'], [
  528. 'get_avatar' => 'getAvatarBig',
  529. 'with_info' => true
  530. ]);
  531. $oProfile = BxDolProfile::getInstance($this->_aProfileInfo['id']);
  532. $aProfileInfo['online'] = $oProfile->isOnline();
  533. unset($aProfileInfo['info']['profile_email']);
  534. unset($aProfileInfo['info']['profile_last_active']);
  535. unset($aProfileInfo['info']['profile_ip']);
  536. $a['cover_block'] = [
  537. 'profile' => $aProfileInfo,
  538. 'actions_menu' => '',
  539. 'meta_menu' => '',
  540. 'badges' => $oProfile->getBadges(),
  541. 'cover' => $oProfileModule->serviceGetCover($this->_aProfileInfo['content_id']),
  542. 'allow_edit' => $oProfileModule->checkAllowedChangeCover($aProfileContentInfo) === CHECK_ACTION_RESULT_ALLOWED
  543. ];
  544. if(!empty($CNF['OBJECT_MENU_VIEW_ENTRY_META']) && ($oMetaMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_VIEW_ENTRY_META'])) !== false)
  545. $a['cover_block']['meta_menu'] = $oMetaMenu->getCodeAPI();
  546. if(!empty($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) !== false)
  547. $a['cover_block']['members_list'] = $oConnection->getConnectedListAPI($this->_aProfileInfo['id'], false, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS, 10);
  548. if(!empty($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_ALL']) && ($oActionMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_ALL'])) !== false)
  549. $a['cover_block']['actions_menu'] = $oActionMenu->getCodeAPI();
  550. }
  551. /**
  552. * Process page mange menu.
  553. */
  554. if(($oMenuManage = BxDolMenu::getObjectInstance('sys_site_manage')) !== false) {
  555. if(!empty($CNF['FIELD_ID']))
  556. $oMenuManage->setContentId($aProfileContentInfo[$CNF['FIELD_ID']]);
  557. $aObjectManage = [];
  558. if(!empty($CNF['OBJECT_MENU_MANAGE_VIEW_ENTRY']))
  559. $aObjectManage = [$CNF['OBJECT_MENU_MANAGE_VIEW_ENTRY']];
  560. else if(!empty($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE']))
  561. $aObjectManage = [$CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE'], true];
  562. call_user_func_array([$oMenuManage, 'setObjectManage'], $aObjectManage);
  563. $aMenuManage = $oMenuManage->getCodeAPI();
  564. if(!empty($aMenuManage) && is_array($aMenuManage)) {
  565. if(isset($a['cover_block'], $a['cover_block']['actions_menu']) && is_array($a['cover_block']['actions_menu']))
  566. $a['cover_block']['actions_menu']['items'] = array_merge($a['cover_block']['actions_menu']['items'], $aMenuManage['items']);
  567. else
  568. $a['cover_block']['actions_menu'] = $aMenuManage;
  569. }
  570. }
  571. }
  572. }
  573. if($bLogged && ($o = BxDolProfile::getInstance()) !== false) {
  574. $iLogged = $o->id();
  575. $a['user'] = BxDolProfile::getDataForPage($o);
  576. if(($sContextSwitcher = getParam('sys_api_context_switcher')) && ($sContextConnection = getParam('sys_api_context_connection'))) {
  577. $aCpIds = bx_srv($sContextSwitcher, 'get_participating_profiles', [$iLogged, ($sContextConnection != 'participants' ? $sContextConnection : false)]);
  578. $aContexts = [];
  579. foreach($aCpIds as $iCpId) {
  580. $aProfileData = BxDolProfile::getData($iCpId);
  581. if($aProfileData['display_name'] === false && $aProfileData['url'] === false )
  582. continue;
  583. $aContexts[] = $aProfileData;
  584. }
  585. $aInvitedTo = [];
  586. if(($aInvitations = bx_srv($sContextSwitcher, 'invitations', [$iLogged, true])) && is_array($aInvitations))
  587. foreach($aInvitations as $aInvitation)
  588. $aInvitedTo[] = BxDolProfile::getData($aInvitation['group_profile_id']);
  589. $aCurrent = [];
  590. if(($iId = bx_get('id')) !== false) {
  591. $aInfo = bx_srv($sModule, 'get_info', [(int)$iId, false]);
  592. if($aInfo && (($iValue = (int)($aInfo['allow_view_to'] ?? 0)) || ($iValue = (int)($aInfo['object_privacy_view'] ?? 0)))) {
  593. $mixedContext = false;
  594. if($iValue < 0) {
  595. $aContext = BxDolProfileQuery::getInstance()->getInfoById(abs($iValue));
  596. if($aContext && $aContext['type'] == $sContextSwitcher)
  597. $mixedContext = (int)$aContext['id'];
  598. }
  599. else if($sModule == $sContextSwitcher)
  600. $mixedContext = BxDolProfile::getInstanceByContentAndType((int)$aInfo['id'], $sModule);
  601. if($mixedContext !== false)
  602. $aCurrent = BxDolProfile::getData($mixedContext);
  603. }
  604. }
  605. else if(($iContextPid = bx_get('profile_id')) !== false && ($oContext = BxDolProfile::getInstance($iContextPid)) !== false)
  606. $aCurrent = BxDolProfile::getData($oContext);
  607. $_sSample = '_' . $sContextSwitcher . '_txt_sample_single';
  608. $sSample = _t($_sSample);
  609. if(strcmp($_sSample, $sSample) == 0)
  610. $sSample = _t('_Context');
  611. $a['context'] = [
  612. 'list' => $aContexts,
  613. 'invitations' => $aInvitedTo,
  614. 'current' => $aCurrent,
  615. 'links' => []
  616. ];
  617. if(bx_srv($sContextSwitcher, 'check_allowed', ['add']) === CHECK_ACTION_RESULT_ALLOWED)
  618. $a['context']['links'][] = [
  619. 'title' => _t('_api_create_context', $sSample),
  620. 'icon' => 'Plus',
  621. 'url' => bx_api_get_relative_url(bx_srv($sContextSwitcher, 'get_link_add'))
  622. ];
  623. }
  624. }
  625. if($bIsAvailable !== true)
  626. $a['page_status'] = 404;
  627. else if($bIsVisible !== true)
  628. $a['page_status'] = 403;
  629. return array_merge($a, [
  630. 'version' => ($sVersion = bx_get_ver()) && $sVersion == BX_DOL_VERSION ? $sVersion : 'invalid',
  631. 'ts' => time()
  632. ]);
  633. }
  634. public function getPageContentAPI($aBlocks = [])
  635. {
  636. $sModule = $this->getModule();
  637. $sName = $this->_getPageTitle();
  638. $sMetaTitle = $this->_getPageMetaTitle();
  639. return [
  640. 'module' => $sModule,
  641. 'title' => $sMetaTitle ? $sMetaTitle : $sName,
  642. 'elements' => $this->getPageBlocksAPI($aBlocks),
  643. ];
  644. }
  645. public function getPageBlocksAPI($aBlocks = [])
  646. {
  647. $aHiddenOn = [
  648. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'phone',
  649. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'tablet',
  650. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'desktop',
  651. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'mobile-app'
  652. ];
  653. $aFieldsUnset = ['object', 'cell_id', 'title_system', 'class', 'submenu', 'tabs', 'async', 'visible_for_levels', 'type', 'text', 'text_updated', 'cache_lifetime', 'active', 'active_api', 'copyable', 'deletable', 'order'];
  654. $bBlocks = !empty($aBlocks) && is_array($aBlocks);
  655. $aCells = $this->_oQuery->getPageBlocks(true);
  656. foreach($aCells as $sKey => &$aCell) {
  657. foreach($aCell as $i => &$aBlock) {
  658. if(!$this->_isVisibleBlock($aBlock)) {
  659. unset($aCells[$sKey][$i]);
  660. continue;
  661. }
  662. $this->processPageBlock($aCells[$sKey][$i], true);
  663. $aBlock = $aCells[$sKey][$i];
  664. $sSource = '';
  665. if($aBlock['type'] == 'service') {
  666. $aContent = @unserialize($aBlock['content']);
  667. if(isset($aContent['module'], $aContent['method']))
  668. $sSource = $aContent['module'] . ':' . $aContent['method'];
  669. }
  670. else if($aBlock['type'] == 'custom')
  671. $sSource = $aBlock['content'];
  672. else
  673. $sSource = 'system:block_' . $aBlock['id'];
  674. if($bBlocks && !in_array($sSource, $aBlocks)) {
  675. unset($aCells[$sKey][$i]);
  676. continue;
  677. }
  678. if(($sK = 'config_api') && !empty($aBlock[$sK])) {
  679. $aConfigApi = json_decode($aBlock[$sK], true);
  680. if(!empty($aConfigApi) && is_array($aConfigApi))
  681. $aBlock[$sK] = $aConfigApi;
  682. }
  683. if(($sK = 'help') && !empty($aBlock[$sK]))
  684. $aBlock[$sK] = _t($aBlock[$sK]);
  685. $mBlock = $aBlock['content'];
  686. if(($sFunc = '_getBlock' . bx_gen_method_name($aBlock['type'])) && method_exists($this, $sFunc))
  687. $mBlock = $this->$sFunc($aBlock);
  688. if(empty($mBlock)) {
  689. unset($aCells[$sKey][$i]);
  690. continue;
  691. }
  692. $sIcon = '';
  693. if(($sIcon = $this->getBlockIcon($aBlock))) {
  694. list($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_oTemplate->getTemplateFunctions()->getIcon($sIcon);
  695. if($sIcon)
  696. $sIcon = BxDolIconset::getObjectInstance()->getIcon($sIcon);
  697. else if($sIconHtml)
  698. $sIcon = $sIconHtml;
  699. }
  700. $aCells[$sKey][$i] = array_merge($aCells[$sKey][$i], [
  701. 'title' => isset($mBlock['title']) ? $mBlock['title'] : $this->getBlockTitle($aBlock),
  702. 'description' => isset($mBlock['description']) ? $mBlock['description'] : $this->getBlockDescription($aBlock),
  703. 'icon' => $sIcon,
  704. 'content' => isset($mBlock['content']) ? $mBlock['content'] : $mBlock,
  705. 'menu' => isset($mBlock['menu']) ? $mBlock['menu'] : '',
  706. 'source' => $sSource
  707. ]);
  708. if(($sK = 'hidden_on') && !empty($aBlock[$sK])) {
  709. $aHoResults = [];
  710. foreach($aHiddenOn as $iHiddenOn => $sHiddenOn)
  711. if((int)$aBlock[$sK] & $iHiddenOn)
  712. $aHoResults[] = $sHiddenOn;
  713. $aCells[$sKey][$i][$sK] = $aHoResults;
  714. }
  715. else
  716. $aCells[$sKey][$i][$sK] = false;
  717. $aCells[$sKey][$i] = array_diff_key($aCells[$sKey][$i], array_flip($aFieldsUnset));
  718. }
  719. }
  720. return array_map('array_values', $aCells);
  721. }
  722. /**
  723. * Get page code vars
  724. * @return string
  725. */
  726. protected function _getPageCodeVars ()
  727. {
  728. $aHiddenOn = array(
  729. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  730. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  731. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  732. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  733. );
  734. $aVars = array (
  735. 'page_id' => 'bx-page-' . $this->_aObject['uri'],
  736. 'bx_if:show_layout_row_dump' => array(
  737. 'condition' => false,
  738. 'content' => array()
  739. )
  740. );
  741. $aBlocks = $this->_oQuery->getPageBlocks();
  742. foreach ($aBlocks as $sKey => $aCell) {
  743. $sCell = '';
  744. foreach ($aCell as $aBlock) {
  745. $this->processPageBlock($aBlock, false);
  746. $sContentWithBox = $this->_getBlockCodeWithCache($aBlock, $aBlock['async']);
  747. $sClassAdd = '';
  748. if(!empty($aBlock['class']))
  749. $sClassAdd .= ' ' . $aBlock['class'];
  750. if(!empty($aBlock['hidden_on']))
  751. foreach($aHiddenOn as $iHiddenOn => $sHiddenOnClass)
  752. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  753. $sClassAdd .= ' ' . $sHiddenOnClass;
  754. if ($sContentWithBox)
  755. $sCell .= $this->_oTemplate->parseHtmlByName('designbox_container.html', array(
  756. 'class_add' => $sClassAdd,
  757. 'bx_if:show_html_id' => array(
  758. 'condition' => true,
  759. 'content' => array(
  760. 'html_id' => 'bx-page-block-' . $aBlock['id']
  761. ),
  762. ),
  763. 'content' => $sContentWithBox
  764. ));
  765. }
  766. $aVars[$sKey] = $sCell;
  767. }
  768. return $aVars;
  769. }
  770. /**
  771. * Process block values, especially if someting need to be overrided
  772. */
  773. protected function processPageBlock(&$aBlock, $bApi = false)
  774. {
  775. }
  776. /**
  777. * Get page code only.
  778. * @return string
  779. */
  780. protected function _getPageCode ()
  781. {
  782. $aVars = $this->_getPageCodeVars ();
  783. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  784. }
  785. /**
  786. * Get one block code only.
  787. * @return string
  788. */
  789. protected function _getBlockOnlyCode ($iBlockId)
  790. {
  791. if (!($aBlock = $this->_oQuery->getPageBlock((int)$iBlockId)))
  792. return '';
  793. return $this->_getBlockCodeWithCache($aBlock, 0);
  794. }
  795. protected function _getBlockCodeWithCache(&$aBlock, $iAsync = 0)
  796. {
  797. if (bx_is_dbg() || !getParam('sys_pb_cache_enable') || !$aBlock['cache_lifetime'] || $iAsync || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  798. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  799. }
  800. else {
  801. $oCache = $this->_getBlockCacheObject();
  802. $sKey = $this->_getBlockCacheKey(false, $aBlock);
  803. $sKeyCssJs = 'cssjs_' . $sKey;
  804. $mixedRet = $oCache->getData($sKey, $aBlock['cache_lifetime']);
  805. $aRetCssJs = $oCache->getData($sKeyCssJs, $aBlock['cache_lifetime']);
  806. if ($mixedRet !== null && $aRetCssJs !== null) {
  807. BxDolTemplate::getInstance()->collectingStart();
  808. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  809. $sBlockCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  810. $sBlockCode .= $mixedRet;
  811. } else {
  812. BxDolTemplate::getInstance()->collectingStart();
  813. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  814. $aBlockCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  815. $oCache->setData($sKey, $sBlockCode, $aBlock['cache_lifetime']);
  816. $oCache->setData($sKeyCssJs, $aBlockCssJs, $aBlock['cache_lifetime']);
  817. }
  818. }
  819. return $sBlockCode;
  820. }
  821. /**
  822. * Get block code.
  823. * @return string
  824. */
  825. protected function _getBlockCode(&$aBlock, $iAsync = 0)
  826. {
  827. $sContentWithBox = '';
  828. $oFunctions = $this->_oTemplate->getTemplateFunctions();
  829. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  830. $aDbNoTitle = [BX_DB_CONTENT_ONLY, BX_DB_PADDING_CONTENT_ONLY, BX_DB_NO_CAPTION, BX_DB_PADDING_NO_CAPTION];
  831. $sTitle = $this->getBlockTitle($aBlock);
  832. $sDescription = $this->getBlockDescription($aBlock);
  833. $sIcon = $this->getBlockIcon($aBlock);
  834. $sHelp = $this->getBlockHelp($aBlock);
  835. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  836. $bBlockVisible = $this->_isVisibleBlock($aBlock);
  837. if ($iAsync && $bBlockVisible) {
  838. $iDesignboxId = $aBlock['designbox_id'];
  839. $sHelpTitle = $sHelpContent = '';
  840. if(!in_array($iDesignboxId, $aDbNoTitle))
  841. $sHelpTitle = $sHelp;
  842. else
  843. $sHelpContent = $sHelp;
  844. $sContent = $this->getBlockAsyncCode($aBlock, $iAsync);
  845. $aParams = [
  846. ($sBlTitle = $sTitle . $sHelpTitle) && ($sDescription || $sIcon) ? [$sBlTitle, $sDescription, $sIcon] : $sBlTitle,
  847. $sContent . $sHelpContent,
  848. $iDesignboxId
  849. ];
  850. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  851. }
  852. elseif ($bBlockVisible && method_exists($this, $sFunc)) {
  853. $mixedContent = $this->$sFunc($aBlock);
  854. if(!empty($aBlock['content_empty'])) {
  855. $sEmpty = _t($aBlock['content_empty']);
  856. $sRegExp = '/(<[A-Za-z0-9]+\b.[^<]*[\pPi\pPf]bx\-msg\-box\b.*[\pPi\pPf][^>]*>)(' . _t('_Empty') . ')(<\/[A-Za-z0-9]+>)/su';
  857. if(is_string($mixedContent))
  858. $mixedContent = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent);
  859. else if(is_array($mixedContent) && !empty($mixedContent['content']))
  860. $mixedContent['content'] = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent['content']);
  861. }
  862. $this->_oQuery->setReadOnlyMode(true);
  863. if(is_array($mixedContent) && !empty($mixedContent['content'])) {
  864. $iDesignboxId = isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'];
  865. if(!empty($mixedContent['markers']) && is_array($mixedContent['markers'])) {
  866. $this->addMarkers($mixedContent['markers']);
  867. $sTitle = $this->getBlockTitle($aBlock);
  868. $sDescription = $this->getBlockDescription($aBlock);
  869. }
  870. $sHelpTitle = $sHelpContent = '';
  871. if(!in_array($iDesignboxId, $aDbNoTitle))
  872. $sHelpTitle = $sHelp;
  873. else
  874. $sHelpContent = $sHelp;
  875. $aParams = [
  876. ($sBlTitle = ($mixedContent['title'] ?? $sTitle) . $sHelpTitle) && ($sDescription || $sIcon) ? [$sBlTitle, $sDescription, $sIcon] : $sBlTitle,
  877. $mixedContent['content'] . $sHelpContent,
  878. $iDesignboxId
  879. ];
  880. $mixedMenu = false;
  881. if(isset($mixedContent['menu']))
  882. $mixedMenu = $mixedContent['menu'];
  883. else if(!empty($aBlock['submenu']))
  884. $mixedMenu = $aBlock['submenu'];
  885. $aParams[] = $mixedMenu;
  886. if(isset($mixedContent['buttons']))
  887. $aParams[] = $mixedContent['buttons'];
  888. else if($mixedMenu)
  889. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  890. if(isset($mixedContent['class']))
  891. $aBlock['class'] = $mixedContent['class'];
  892. }
  893. else if(is_string($mixedContent) && !empty($mixedContent)) {
  894. $iDesignboxId = $aBlock['designbox_id'];
  895. $sHelpTitle = $sHelpContent = '';
  896. if(!in_array($iDesignboxId, $aDbNoTitle))
  897. $sHelpTitle = $sHelp;
  898. else
  899. $sHelpContent = $sHelp;
  900. $aParams = array(
  901. ($sBlTitle = $sTitle . $sHelpTitle) && ($sDescription || $sIcon) ? [$sBlTitle, $sDescription, $sIcon] : $sBlTitle,
  902. $mixedContent . $sHelpContent,
  903. $iDesignboxId
  904. );
  905. $mixedMenu = !empty($aBlock['submenu']) ? $aBlock['submenu'] : false;
  906. $aParams[] = $mixedMenu;
  907. if($mixedMenu)
  908. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  909. }
  910. if(!empty($aParams))
  911. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  912. }
  913. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  914. return $sContentWithBox;
  915. }
  916. /**
  917. * Add necessary js and css files.
  918. */
  919. protected function _addJsCss()
  920. {
  921. $this->_oTemplate->addJs(array('BxDolPage.js', 'theia-sticky-sidebar/theia-sticky-sidebar.umd.js', 'theia-sticky-sidebar/ResizeSensor.min.js'));
  922. $this->_oTemplate->addCss('page_layouts.css');
  923. }
  924. /**
  925. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  926. */
  927. protected function _addSysTemplateVars ()
  928. {
  929. $oTemplate = BxDolTemplate::getInstance();
  930. $sPageTitle = $this->_getPageTitle();
  931. if ($sPageTitle)
  932. $oTemplate->setPageHeader ($sPageTitle);
  933. $sMetaTitle = $this->_getPageMetaTitle();
  934. if ($sMetaTitle)
  935. $oTemplate->setPageMetaTitle ($sMetaTitle);
  936. $sMetaDesc = $this->_getPageMetaDesc();
  937. if ($sMetaDesc)
  938. $oTemplate->setPageDescription ($sMetaDesc);
  939. $sMetaRobots = $this->_getPageMetaRobots();
  940. if ($sMetaRobots)
  941. $oTemplate->setPageMetaRobots ($sMetaRobots);
  942. $sMetaImage = $this->_getPageMetaImage();
  943. if ($sMetaImage)
  944. $oTemplate->addPageMetaImage($sMetaImage);
  945. $sMetaKeywords = $this->_getPageMetaKeywords();
  946. if ($sMetaKeywords)
  947. $oTemplate->addPageKeywords ($sMetaKeywords);
  948. }
  949. /**
  950. * Select menu from page properties.
  951. */
  952. protected function _selectMenu ()
  953. {
  954. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  955. }
  956. /**
  957. * Set page submenu if it's specified
  958. */
  959. protected function _setSubmenu ($aParams)
  960. {
  961. if(empty($this->_aObject['submenu']) || $this->_aObject['submenu'] == 'disabled')
  962. return;
  963. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  964. if(!$oMenuSubmenu)
  965. return;
  966. $oMenuSubmenu->setObjectSubmenu($this->_aObject['submenu'], $aParams);
  967. }
  968. /**
  969. * Get content for 'raw' block type.
  970. * @return string
  971. */
  972. protected function _getBlockRaw ($aBlock)
  973. {
  974. if (bx_is_api()){
  975. return [bx_api_get_block('raw', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  976. }
  977. $s = '<div class="bx-page-raw-container bx-def-vanilla-html max-w-none">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  978. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  979. $s = bx_process_macros($s);
  980. return $s;
  981. }
  982. /**
  983. * Get content for 'custom' block type.
  984. * @return string
  985. */
  986. protected function _getBlockCustom ($aBlock)
  987. {
  988. if (bx_is_api()){
  989. return [bx_api_get_block('custom', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  990. }
  991. $s = '<div class="bx-page-custom-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  992. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  993. $s = bx_process_macros($s);
  994. return $s;
  995. }
  996. /**
  997. * Get content for 'html' block type.
  998. * @return string
  999. */
  1000. protected function _getBlockHtml ($aBlock)
  1001. {
  1002. if (bx_is_api()){
  1003. return [bx_api_get_block('html', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  1004. }
  1005. $s = '<div class="bx-page-html-container bx-def-vanilla-html max-w-none">' . $aBlock['content'] . '</div>';
  1006. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  1007. $s = bx_process_macros($s);
  1008. return $s;
  1009. }
  1010. /**
  1011. * Get content for 'creative' block type.
  1012. * @return string
  1013. */
  1014. protected function _getBlockCreative ($aBlock)
  1015. {
  1016. if(bx_is_api())
  1017. return [bx_api_get_block('creative', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  1018. $s = $this->_replaceMarkers($aBlock['content'], ['block_id' => $aBlock['id']]);
  1019. $s = bx_process_macros($s);
  1020. $bCanManage = $this->isAllowedCreativeManage();
  1021. if($bCanManage) {
  1022. $this->_oTemplate->addCss([
  1023. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'grapesjs/|grapes.min.css',
  1024. ]);
  1025. $this->_oTemplate->addJs([
  1026. 'grapesjs/grapes.min.js',
  1027. 'grapesjs/grapesjs-blocks-basic.js',
  1028. 'grapesjs/grapesjs-style-bg.js',
  1029. 'grapesjs/grapesjs-preset-webpage.min.js',
  1030. ]);
  1031. }
  1032. return $this->_oTemplate->parseHtmlByName('block_creative.html', [
  1033. 'block_id' => $aBlock['id'],
  1034. 'content' => $s,
  1035. 'bx_if:show_controls' => [
  1036. 'condition' => $bCanManage,
  1037. 'content' => [
  1038. 'onclick_edit' => $this->_sJsObjectName . '.creativeEdit(this, ' . $aBlock['id'] . ')',
  1039. 'onclick_save' => $this->_sJsObjectName . '.creativeSave(this, ' . $aBlock['id'] . ')'
  1040. ]
  1041. ]
  1042. ]);
  1043. }
  1044. /**
  1045. * Get content for 'bento_grid' block type.
  1046. * @return string
  1047. */
  1048. protected function _getBlockBentoGrid ($aBlock)
  1049. {
  1050. if(bx_is_api()) {
  1051. $iBlockId = (int)$aBlock['id'];
  1052. $iContentId = 0;
  1053. if(($mContentId = bx_get('id')) !== false)
  1054. $iContentId = (int)$mContentId;
  1055. $sContentModule = '';
  1056. if(!empty($this->_aObject['module']) && !in_array($this->_aObject['module'], ['system', 'custom']))
  1057. $sContentModule = $this->_aObject['module'];
  1058. $bIsAllowedEdit = false;
  1059. if(!empty($sContentModule) && bx_is_srv($sContentModule, 'check_allowed_with_content'))
  1060. $bIsAllowedEdit = bx_srv($sContentModule, 'check_allowed_with_content', ['edit', $iContentId]) === CHECK_ACTION_RESULT_ALLOWED;
  1061. else
  1062. $bIsAllowedEdit = isAdmin();
  1063. return [bx_api_get_block('bento_grid', [
  1064. 'title' => _t($aBlock['title']),
  1065. 'content' => self::getPageBlockData($iBlockId, $iContentId, $sContentModule)
  1066. ], ['ext' => [
  1067. 'block_id' => $iBlockId,
  1068. 'content_id' => $iContentId,
  1069. 'content_module' => $sContentModule,
  1070. 'is_allowed_edit' => $bIsAllowedEdit
  1071. ]])];
  1072. }
  1073. /**
  1074. * Isn't supported for now.
  1075. */
  1076. return '';
  1077. }
  1078. /**
  1079. * Get content for 'wiki' block type.
  1080. * @return string
  1081. */
  1082. protected function _getBlockWiki ($aBlock)
  1083. {
  1084. $oWiki = BxDolWiki::getObjectInstance($this->_aObject['module']);
  1085. if (!$oWiki) {
  1086. $sContent = ($sMsg = _t('_sys_wiki_error_missing_wiki_object', $this->_aObject['module'])) && $this->_bIsApi ? ['content' => $sMsg] : $sMsg;
  1087. }
  1088. else {
  1089. $sContent = $oWiki->getBlockContent($aBlock['id'], false, ($iRevision = bx_get($aBlock['id'] . 'rev')) !== false ? (int)$iRevision : false);
  1090. }
  1091. if($this->_bIsApi)
  1092. return [bx_api_get_block('html', array_merge([
  1093. 'title' => _t($aBlock['title']),
  1094. 'content' => ''
  1095. ], $sContent))];
  1096. $s = '<div id="bx-page-wiki-container-' . $aBlock['id'] . '" class="bx-page-wiki-container markdown-body bx-def-vanilla-html">' . $sContent . '</div>';
  1097. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  1098. $s = bx_process_macros($s);
  1099. return $s;
  1100. }
  1101. /**
  1102. * Get content for 'lang' block type.
  1103. * @return string
  1104. */
  1105. protected function _getBlockLang ($aBlock)
  1106. {
  1107. if (bx_is_api()){
  1108. return [bx_api_get_block('lang', ['title' => _t($aBlock['title']), 'content' => bx_process_macros(_t(trim($aBlock['content'])))])];
  1109. }
  1110. $s = '<div class="bx-page-lang-container bx-def-vanilla-html max-w-none">' . _t(trim($aBlock['content'])) . '</div>';
  1111. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  1112. $s = bx_process_macros($s);
  1113. return $s;
  1114. }
  1115. /**
  1116. * Get content for 'image' block type.
  1117. * @return string
  1118. */
  1119. protected function _getBlockImage ($aBlock)
  1120. {
  1121. if (empty($aBlock['content']))
  1122. return false;
  1123. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  1124. $iFileId = (int)$iFileId;
  1125. if (!$iFileId)
  1126. return false;
  1127. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  1128. if (!$oStorage)
  1129. return false;
  1130. $sUrl = $oStorage->getFileUrlById($iFileId);
  1131. if (!$sUrl)
  1132. return false;
  1133. $sStyleAdd = '';
  1134. $sClassAdd = 'bx-page-image-container';
  1135. $aAlign = ['left' => 'start', 'center' => 'center', 'right' => 'end'];
  1136. if(array_key_exists($sAlign, $aAlign)) {
  1137. $sClassAdd .= ' flex justify-' . $aAlign[$sAlign];
  1138. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  1139. }
  1140. return '<div class="' . $sClassAdd . '" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  1141. }
  1142. /**
  1143. * Get content for 'rss' block type.
  1144. * @return string
  1145. */
  1146. protected function _getBlockRss ($aBlock)
  1147. {
  1148. if (empty($aBlock['content']))
  1149. return false;
  1150. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  1151. $iNum = (int)$iNum;
  1152. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  1153. }
  1154. /**
  1155. * Get content for 'menu' block type.
  1156. * @return string
  1157. */
  1158. protected function _getBlockMenu ($aBlock)
  1159. {
  1160. $bIsApi = bx_is_api();
  1161. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  1162. if(!$oMenu)
  1163. return $bIsApi ? [] : '';
  1164. if($bIsApi)
  1165. return [bx_api_get_block('menu' . ($this->_sObject == 'sys_sub_wiki_pages_list' ? '_wiki' : ''), ['title' => _t($aBlock['title']), 'content' => $oMenu->getCodeAPI()])];
  1166. return $oMenu ? $oMenu->getCode() : '';
  1167. }
  1168. /**
  1169. * Get content for 'service' block type.
  1170. * @return string
  1171. */
  1172. protected function _getBlockService ($aBlock)
  1173. {
  1174. self::setBlockProcessing($aBlock);
  1175. $sContent = $aBlock['content'];
  1176. // get_mockup_block blocks are stored with empty params, so BxDolService's memory cache would
  1177. // hand every such block on the page the same tree; key the call by block id instead.
  1178. // Read and rewritten through the service-call codec: new blocks are stored as JSON, and
  1179. // old serialized ones must never be unserialized with objects allowed.
  1180. $aCall = BxDolService::decodeServiceCall($sContent);
  1181. if (is_array($aCall) && ($aCall['method'] ?? '') === 'get_mockup_block')
  1182. $sContent = BxDolService::getSerializedService($aCall['module'], $aCall['method'], [(int)$aBlock['id']], $aCall['class'] ?? '');
  1183. $aResult = BxDolService::callSerialized($sContent, array_merge($this->_aMarkers, [
  1184. 'block_id' => $aBlock['id']
  1185. ]));
  1186. self::unsetBlockProcessing();
  1187. return $aResult;
  1188. }
  1189. /**
  1190. * Get page title.
  1191. * @return string
  1192. */
  1193. protected function _getPageTitle()
  1194. {
  1195. return $this->_replaceMarkers(_t($this->_aObject['title']));
  1196. }
  1197. /**
  1198. * Get page meta description.
  1199. * @return string
  1200. */
  1201. protected function _getPageMetaDesc()
  1202. {
  1203. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  1204. }
  1205. /**
  1206. * Get page meta title.
  1207. * @return string
  1208. */
  1209. protected function _getPageMetaTitle()
  1210. {
  1211. return $this->_replaceMarkers(_t($this->_aObject['meta_title']));
  1212. }
  1213. /**
  1214. * Get page meta image.
  1215. * @return string
  1216. */
  1217. protected function _getPageMetaImage()
  1218. {
  1219. return '';
  1220. }
  1221. /**
  1222. * Get page meta keywords.
  1223. * @return string
  1224. */
  1225. protected function _getPageMetaKeywords()
  1226. {
  1227. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  1228. }
  1229. /**
  1230. * Get page meta robots.
  1231. * @return string
  1232. */
  1233. protected function _getPageMetaRobots()
  1234. {
  1235. return _t($this->_aObject['meta_robots']);
  1236. }
  1237. /**
  1238. * Get page not found message.
  1239. * @return string
  1240. */
  1241. protected function _getPageNotFoundMsg ($mixedMsg = false)
  1242. {
  1243. $this->_oTemplate->displayPageNotFound();
  1244. exit;
  1245. }
  1246. /**
  1247. * Get access denied message.
  1248. * @return string
  1249. */
  1250. protected function _getPageAccessDeniedMsg ($mixedMsg = false)
  1251. {
  1252. return MsgBox($mixedMsg !== false ? $mixedMsg : _t('_Access denied'));
  1253. }
  1254. /**
  1255. * Get page cache object.
  1256. * @return cache object instance
  1257. */
  1258. protected function _getPageCacheObject ()
  1259. {
  1260. return $this->_getCacheObject ();
  1261. }
  1262. /**
  1263. * Get block cache object.
  1264. * @return cache object instance
  1265. */
  1266. protected function _getBlockCacheObject ()
  1267. {
  1268. return $this->_getCacheObject ('Block', 'pb');
  1269. }
  1270. protected function _getCacheObject ($sSuffixObj = 'Page', $sSuffixParam = 'page')
  1271. {
  1272. $sObj = '_o' . $sSuffixObj . 'CacheObject';
  1273. $sParam = 'sys_' . $sSuffixParam . '_cache_engine';
  1274. if ($this->{$sObj} != null) {
  1275. return $this->{$sObj};
  1276. } else {
  1277. $sEngine = getParam($sParam);
  1278. $this->{$sObj} = bx_instance ('BxDolCache' . $sEngine);
  1279. if (!$this->{$sObj}->isAvailable())
  1280. $this->{$sObj} = bx_instance ('BxDolCacheFile');
  1281. return $this->{$sObj};
  1282. }
  1283. }
  1284. /**
  1285. * Get page cache key.
  1286. * @param $isPrefixOnly return key prefix only.
  1287. * @return string
  1288. */
  1289. protected function _getPageCacheKey ($isPrefixOnly = false)
  1290. {
  1291. $s = 'page_' . $this->_aObject['object'] . '_';
  1292. if ($isPrefixOnly)
  1293. return $s;
  1294. $s .= $this->_getPageCacheParams ();
  1295. $s .= bx_site_hash() . '.php';
  1296. return $s;
  1297. }
  1298. /**
  1299. * Get block cache key.
  1300. * @param $isPrefixOnly return key prefix only.
  1301. * @return string
  1302. */
  1303. protected function _getBlockCacheKey ($isPrefixOnly = false, $aBlock = array())
  1304. {
  1305. $s = $this->_getPageCacheKey(true) . 'block_';
  1306. if ($isPrefixOnly)
  1307. return $s;
  1308. $s .= ($aBlock ? $aBlock['id'] : 0) . '_' . $this->_getPageCacheParams ();
  1309. $s .= bx_site_hash() . '.php';
  1310. return $s;
  1311. }
  1312. /**
  1313. * Additional cache key. In the case of dynamic page.
  1314. * For example - profile page, where each profile has own page.
  1315. * @return string
  1316. */
  1317. protected function _getPageCacheParams ()
  1318. {
  1319. return md5(serialize($_GET) . bx_lang_name());
  1320. }
  1321. /**
  1322. * Clean page cache.
  1323. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  1324. * @return string
  1325. */
  1326. protected function cleanCache ($isDelAllWithPagePrefix = false)
  1327. {
  1328. $a = ['Page', 'Block'];
  1329. $bRet = false;
  1330. foreach ($a as $s) {
  1331. $oCache = $this->{'_get' . $s . 'CacheObject'} ();
  1332. $sKey = $this->{'_get'. $s . 'CacheKey'} ($isDelAllWithPagePrefix);
  1333. if ($isDelAllWithPagePrefix)
  1334. $bRet = $bRet && $oCache->removeAllByPrefix($sKey);
  1335. else
  1336. $bRet = $bRet && $oCache->delData($sKey);
  1337. }
  1338. return $bRet;
  1339. }
  1340. }
  1341. /** @} */