BxBasePage.php 54 KB

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