BxBasePage.php 55 KB

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