BxBasePage.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  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_account_id' => $oProfile->getAccountId(),
  45. 'logged_profile_module' => $oProfile->getModule(),
  46. 'logged_display_name' => $oProfile->getDisplayName(),
  47. 'logged_profile_url' => $oProfile->getUrl(),
  48. ]);
  49. }
  50. if (isset($GLOBALS['bxReplaceableMarkersPages'])) {
  51. $this->addMarkers($GLOBALS['bxReplaceableMarkersPages']);
  52. }
  53. }
  54. public function performActionGetHelp ()
  55. {
  56. $iBlockId = (int)bx_get('block_id');
  57. if(empty($iBlockId))
  58. return;
  59. $aBlock = $this->_oQuery->getPageBlock($iBlockId);
  60. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['help']))
  61. return;
  62. echo $this->_oTemplate->parseHtmlByName('designbox_help_popup.html', array(
  63. 'content' => _t($aBlock['help'])
  64. ));
  65. }
  66. public function performActionGetShare ()
  67. {
  68. $aEmbedData = BxDolPage::getEmbedData(bx_get('url'));
  69. if(!isset($aEmbedData['url']))
  70. return;
  71. $sMenu = '';
  72. if(getParam('sys_a2a_enable') != 'on') {
  73. if(($oMenu = BxDolMenu::getObjectInstance('sys_social_sharing')) !== false) {
  74. $oMenu->addMarkers([
  75. 'url' => $aEmbedData['url'],
  76. 'img_url_encoded' => '',
  77. 'title_encoded' => ''
  78. ]);
  79. $sMenu = $oMenu->getCode();
  80. }
  81. }
  82. else
  83. $sMenu = getParam('sys_a2a_code');
  84. echo PopupBox('sys_share_popup', _t('_sys_txt_share_popup_header'), $this->_oTemplate->parseHtmlByName('designbox_share_popup.html', [
  85. 'url' => bx_process_output($aEmbedData['url']),
  86. 'menu' => $sMenu,
  87. 'code' => htmlspecialchars($aEmbedData['html']),
  88. ]), true);
  89. }
  90. public function performActionEmbed ()
  91. {
  92. $aCover = $this->getPageCoverImage();
  93. $sTitle = $this->_getPageTitle();
  94. $this->_oTemplate->getEmbed($this->_oTemplate->parseHtmlByName('embed_card.html', [
  95. 'title' => $sTitle,
  96. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  97. 'bx_if:thumb' => [
  98. 'condition' => $this->isPageCover() && count($aCover) > 0,
  99. 'content' => [
  100. 'title' => $sTitle,
  101. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  102. 'background' => BxDolCover::getInstance(BxDolTemplate::getInstance())->getCoverImageUrl($aCover)
  103. ],
  104. ],
  105. 'bx_if:no_thumb' => [
  106. 'condition' => count($aCover) == 0 || !$this->isPageCover(),
  107. 'content' => [
  108. 'title' => $sTitle,
  109. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  110. ]
  111. ]
  112. ]
  113. ));
  114. }
  115. public function performActionCreativeSave()
  116. {
  117. if(!$this->isAllowedCreativeManage())
  118. return echoJson(['code' => 1]);
  119. $iId = 0;
  120. $sContent = '';
  121. if(($iId = bx_get('b')) === false || ($sContent = bx_get('c')) === false)
  122. return echoJson(['code' => 2]);
  123. $iId = bx_process_input($iId, BX_DATA_INT);
  124. $sContent = bx_process_input($sContent, BX_DATA_HTML);
  125. if(!$this->_oQuery->setPageBlockContent($iId, $sContent))
  126. return echoJson(['code' => 3]);
  127. return echoJson(['code' => 0]);
  128. }
  129. public function isAllowedCreativeManage()
  130. {
  131. return isAdmin();
  132. }
  133. /**
  134. * Very similar to BxBasePage::getCode
  135. * but adds css and js files which are needed for the corect page display
  136. */
  137. public function getCodeDynamic ()
  138. {
  139. // TODO: remake to use use collect* template methods
  140. $oTemplate = BxDolTemplate::getInstance();
  141. // get js&css before the page code is generated
  142. $aCssBefore = $oTemplate->getCss();
  143. $aJsBefore = $oTemplate->getJs();
  144. // generate page code
  145. $sContent = $this->getCode();
  146. // get js&css after the page code is generated
  147. $aCssAfter = $oTemplate->getCss();
  148. $aJsAfter = $oTemplate->getJs();
  149. // compare files which were added before and after page code is generated
  150. $f = function ($a1, $a2) {
  151. return strcasecmp($a1['url'], $a2['url']);
  152. };
  153. $aCssNew = array_udiff($aCssAfter, $aCssBefore, $f);
  154. $aJsNew = array_udiff($aJsAfter, $aJsBefore, $f);
  155. // add newly added js&css files in static mode
  156. $sCss = $sJs = '';
  157. foreach ($aCssNew as $a)
  158. $sCss .= $oTemplate->addCss($a['url'], true);
  159. foreach ($aJsNew as $a)
  160. $sJs .= $oTemplate->addJs($a['url'], true);
  161. return $sJs . $sCss . $sContent;
  162. }
  163. public function getIncludes ()
  164. {
  165. $oTemplate = BxDolTemplate::getInstance();
  166. // generate page code
  167. $this->getCode();
  168. // get js&css after the page code is generated
  169. $aCss = $oTemplate->getCss();
  170. $aJs = $oTemplate->getJs();
  171. // add js&css files in static mode
  172. $sResult = '';
  173. foreach ($aCss as $a)
  174. $sResult .= $oTemplate->addCss($a['url'], true);
  175. foreach ($aJs as $a)
  176. $sResult .= $oTemplate->addJs($a['url'], true);
  177. // add js translations
  178. $sResult .= $oTemplate->getJsTranslation(true);
  179. return $sResult;
  180. }
  181. /**
  182. * Get page code with automatic caching, adding necessary css/js files and system template vars.
  183. * @return string.
  184. */
  185. public function getCode ()
  186. {
  187. if (bx_get('dynamic') && ($iBlockId = (int)bx_get('pageBlock'))) {
  188. if (self::isLockedFromUnauthenticated($this->_aObject['uri']) || !$this->_isVisiblePage($this->_aObject)) {
  189. header('HTTP/1.0 403 Forbidden');
  190. exit;
  191. }
  192. /**
  193. * @hooks
  194. * @hookdef hook-system-page_output_block 'system', 'page_output_block' - hook with page block data to be output
  195. * - $unit_name - equals `system`
  196. * - $action - equals `page_output_block`
  197. * - $object_id - not used
  198. * - $sender_id - not used
  199. * - $extra_params - array of additional params with the following array keys:
  200. * - `page_name` - [string] page object name
  201. * - `page_object` - [object] an instance of page class, @see BxDolPage
  202. * - `page_query` - [object] an instance of page related query class
  203. * - `block_id` - [int] block id
  204. * @hook @ref hook-system-page_output_block
  205. */
  206. bx_alert('system', 'page_output_block', 0, false, [
  207. 'page_name' => $this->_sObject,
  208. 'page_object' => $this,
  209. 'page_query' => $this->_oQuery,
  210. 'block_id' => (int)$iBlockId,
  211. ]);
  212. header( 'Content-type:text/html;charset=utf-8' );
  213. BxDolTemplate::getInstance()->collectingStart();
  214. $s = $this->_getBlockOnlyCode($iBlockId);
  215. $sCss = bx_get('includedCss');
  216. $sJs = bx_get('includedJs');
  217. echo BxDolTemplate::getInstance()->collectingEndGetCode($sCss ? @json_decode($sCss) : [], $sJs ? @json_decode($sJs) : []);
  218. echo $s;
  219. exit;
  220. }
  221. if (($mixedAvailable = $this->_isAvailablePage($this->_aObject)) !== true)
  222. return $this->_getPageNotFoundMsg($mixedAvailable);
  223. if (($mixedVisible = $this->_isVisiblePage($this->_aObject)) !== true)
  224. return $this->_getPageAccessDeniedMsg($mixedVisible);
  225. $this->_addJsCss();
  226. if (!$this->_bSubPage) {
  227. $this->_addSysTemplateVars();
  228. $this->_selectMenu();
  229. $this->_setSubmenu(array());
  230. }
  231. if (bx_is_dbg() || !getParam('sys_page_cache_enable') || !$this->_aObject['cache_lifetime'] || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  232. $sPageCode = $this->_getPageCode();
  233. }
  234. else {
  235. $oCache = $this->_getPageCacheObject();
  236. $sKey = $this->_getPageCacheKey();
  237. $sKeyCssJs = 'cssjs_' . $sKey;
  238. $mixedRet = $oCache->getData($sKey, $this->_aObject['cache_lifetime']);
  239. $aRetCssJs = $oCache->getData($sKeyCssJs, $this->_aObject['cache_lifetime']);
  240. if ($mixedRet !== null && $aRetCssJs !== null) {
  241. BxDolTemplate::getInstance()->collectingStart();
  242. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  243. $sPageCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  244. $sPageCode .= $mixedRet;
  245. } else {
  246. BxDolTemplate::getInstance()->collectingStart();
  247. $sPageCode = $this->_getPageCode();
  248. $aPageCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  249. $oCache->setData($sKey, $sPageCode, $this->_aObject['cache_lifetime']);
  250. $oCache->setData($sKeyCssJs, $aPageCssJs, $this->_aObject['cache_lifetime']);
  251. }
  252. }
  253. /**
  254. * @hooks
  255. * @hookdef hook-system-page_output 'system', 'page_output' - hook with page data to be output
  256. * - $unit_name - equals `system`
  257. * - $action - equals `page_output`
  258. * - $object_id - not used
  259. * - $sender_id - not used
  260. * - $extra_params - array of additional params with the following array keys:
  261. * - `page_name` - [string] page object name
  262. * - `page_object` - [object] an instance of page class, @see BxDolPage
  263. * - `page_query` - [object] an instance of page related query class
  264. * - `page_code` - [string] by ref, final page code to be output, can be overridden in hook processing
  265. * - `sub_page` - [boolean] if the page is used as subpage of some parent page
  266. * @hook @ref hook-system-page_output
  267. */
  268. bx_alert('system', 'page_output', 0, false, [
  269. 'page_name' => $this->_sObject,
  270. 'page_object' => $this,
  271. 'page_query' => $this->_oQuery,
  272. 'page_code' => &$sPageCode,
  273. 'sub_page' => $this->_bSubPage,
  274. ]);
  275. if (!$this->_bSubPage)
  276. $sPageCode .= $this->getJsScript();
  277. return $sPageCode;
  278. }
  279. public function getJsClassName()
  280. {
  281. return $this->_sJsClassName;
  282. }
  283. public function getJsObjectName()
  284. {
  285. return $this->_sJsObjectName;
  286. }
  287. function _wrapInTagJsCode($sCode)
  288. {
  289. return "<script language=\"javascript\">\n<!--\n" . $sCode . "\n-->\n</script>";
  290. }
  291. public function getJsScript()
  292. {
  293. $sJsObjName = $this->getJsObjectName();
  294. $sJsObjClass = $this->getJsClassName();
  295. $sCode = "if(window['" . $sJsObjName . "'] == undefined) var " . $sJsObjName . " = new " . $sJsObjClass . "(" . json_encode(array(
  296. 'sObjName' => $sJsObjName,
  297. 'sObject' => $this->_sObject,
  298. 'sRootUrl' => BX_DOL_URL_ROOT,
  299. 'aHtmlIds' => $this->_aHtmlIds,
  300. 'isStickyColumns' => $this->_bStickyColumns
  301. )) . ");";
  302. return $this->_wrapInTagJsCode($sCode);
  303. }
  304. /**
  305. * Is page cover enabled.
  306. * @return string
  307. */
  308. public function isPageCover()
  309. {
  310. $bResult = false;
  311. switch((int)$this->_aObject['cover']) {
  312. case 1: //--- Enabled for all
  313. $bResult = true;
  314. break;
  315. case 2: //--- Enabled for visitors only
  316. $bResult = !isLogged();
  317. break;
  318. case 3: //--- Enabled for members only
  319. $bResult = isLogged();
  320. break;
  321. }
  322. return $bResult;
  323. }
  324. public function setSubPage($b = true)
  325. {
  326. $this->_bSubPage = $b;
  327. }
  328. public function setPageCover($bCover = true)
  329. {
  330. $this->_aObject['cover'] = (bool)$bCover;
  331. }
  332. public function getPageCoverImage($bTranscoder = true)
  333. {
  334. $iId = (int)$this->_aObject['cover_image'];
  335. if(empty($iId)) {
  336. $iId = (int)getParam('sys_site_cover_common');
  337. if(empty($iId))
  338. return array();
  339. }
  340. $aResult = array(
  341. 'id' => $iId
  342. );
  343. if($bTranscoder)
  344. $aResult['transcoder'] = BX_DOL_TRANSCODER_OBJ_COVER;
  345. else
  346. $aResult['object'] = $this->_sStorage;
  347. return $aResult;
  348. }
  349. public function getPageCoverParams()
  350. {
  351. $aParams = [
  352. 'title' => $this->_getPageTitle(),
  353. 'actions' => '',
  354. 'bx_if:image' => [
  355. 'condition' => false,
  356. 'content' => []
  357. ],
  358. 'bx_if:icon' => [
  359. 'condition' => false,
  360. 'content' => []
  361. ],
  362. ];
  363. if(($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  364. $aCoverParams = $oMenuSubmenu->getPageCoverParams();
  365. if(!empty($aCoverParams) && is_array($aCoverParams))
  366. $aParams = $aCoverParams;
  367. }
  368. if(!empty($this->_aObject['cover_title'])) {
  369. $sCoverTitle = _t($this->_aObject['cover_title']);
  370. if($sCoverTitle && strcmp($sCoverTitle, $this->_aObject['cover_title']) != 0)
  371. $aParams['title'] = $sCoverTitle;
  372. }
  373. return $aParams;
  374. }
  375. /**
  376. * Get block title.
  377. * @return string
  378. */
  379. public function getBlockTitle ($aBlock)
  380. {
  381. return $this->_replaceMarkers(_t($aBlock['title']), array('block_id' => $aBlock['id']));
  382. }
  383. /**
  384. * Get help control if help is available for the block.
  385. * @return string
  386. */
  387. public function getBlockHelp ($aBlock)
  388. {
  389. if(empty($aBlock['help']) || empty(strip_tags(_t($aBlock['help']))))
  390. return '';
  391. return $this->_oTemplate->parseHtmlByName('designbox_help.html', array(
  392. 'js_object' => $this->_sJsObjectName,
  393. 'block_id' => $aBlock['id']
  394. ));
  395. }
  396. /**
  397. * Get code to load block asyncroniously
  398. * @param $aBlock block code
  399. * @param $iAsync if greater than 0 the it defines loading indicator for the block
  400. * @return HTML code string
  401. */
  402. public function getBlockAsyncCode($aBlock, $iAsync)
  403. {
  404. $aContentPlaceholder = $this->_oQuery->getPageBlockContentPlaceholder($iAsync);
  405. if (!$aContentPlaceholder)
  406. return _t('_sys_txt_error_occured');
  407. $oTemplate = $this->_oTemplate;
  408. if ('system' != $aContentPlaceholder['module']) {
  409. $oModule = BxDolModule::getInstance($aContentPlaceholder['module']);
  410. if (!$oModule)
  411. return _t('_sys_txt_error_occured');
  412. $oTemplate = $oModule->_oTemplate;
  413. }
  414. return $oTemplate->parseHtmlByName($aContentPlaceholder['template'], $aBlock);
  415. }
  416. /**
  417. * Get page array with all cells and blocks
  418. */
  419. public function getPageAPI ($aBlocks = [])
  420. {
  421. define('BX_API_PAGE', true);
  422. $aGetParams = bx_get('params');
  423. $bGetParams = $aGetParams !== false;
  424. $sQueryString = '';
  425. if($bGetParams && !empty($aGetParams[2]) && ($_aGetParams = json_decode($aGetParams[2], true)) && is_array($_aGetParams)) {
  426. $sQueryString = http_build_query($_aGetParams);
  427. parse_str($sQueryString, $aQueryString);
  428. $_GET = array_merge($_GET, $aQueryString);
  429. }
  430. $bIsAvailable = $this->_isAvailablePage($this->_aObject);
  431. $bIsVisible = $this->_isVisiblePage($this->_aObject);
  432. //TODO: If page isn't Available or isn't Visible then we should exit with related Error Codes immediately.
  433. $sMetaTitle = $this->_getPageMetaTitle();
  434. $sName = $this->_getPageTitle();
  435. $sModule = $this->getModule();
  436. $a = [
  437. 'id' => $this->_aObject['id'],
  438. 'title' => $sMetaTitle ? $sMetaTitle : $sName,
  439. 'name' => $sName,
  440. 'description' => $this->_getPageMetaDesc(),
  441. 'keywords' => $this->_getPageMetaKeywords(),
  442. 'image' => '',
  443. 'uri' => $this->_aObject['uri'],
  444. 'url' => ($bGetParams && !empty($aGetParams[0]) ? $aGetParams[0] : $this->_aObject['uri']) . ($sQueryString != '' ? '?' . $sQueryString : ''),
  445. 'author' => $this->_aObject['author'],
  446. 'added' => $this->_aObject['added'],
  447. 'module' => $sModule,
  448. 'type' => $this->getType (),
  449. 'layout' => str_replace('.html', '', $this->_aObject['template']),
  450. 'cover_block' => '',
  451. 'cover' => $this->_aObject['cover'],
  452. 'menu_top' => '',
  453. 'menu' => '',
  454. 'menu_bottom' => '',
  455. 'menu_add' => '',
  456. 'config' => $this->_aObject['config_api'],
  457. 'elements' => $this->getPageBlocksAPI($aBlocks),
  458. ];
  459. $sMenuTop = getParam('sys_api_menu_top');
  460. if (!empty($sMenuTop) && ($oMenuTop = BxDolMenu::getObjectInstance($sMenuTop)) !== false)
  461. $a['menu_top'] = $oMenuTop->getCodeAPI();
  462. if (($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  463. if(($sSubmenu = $this->getSubMenu()) !== '')
  464. $oMenuSubmenu->setObjectSubmenu($sSubmenu, []);
  465. $a['menu'] = $oMenuSubmenu->getCodeAPI();
  466. if($sModule && $sModule != 'system' && ($oModule = BxDolModule::getInstance($sModule))) {
  467. $CNF = &$oModule->_oConfig->CNF;
  468. $sUrlAdd = '';
  469. if(($sKey = 'URI_ADD_ENTRY') && !empty($CNF[$sKey]))
  470. $sUrlAdd = $CNF[$sKey];
  471. if(!$sUrlAdd && ($sKey = 'URI_EDIT_ENTRY') && !empty($CNF[$sKey]))
  472. $sUrlAdd = str_replace('edit-', 'create-', $CNF[$sKey]);
  473. $a['menu'] = array_merge($a['menu'], [
  474. 'name' => $oModule->getName(),
  475. 'title' => BxDolModule::getTitle($oModule->_oConfig->getUri()),
  476. 'icon' => !empty($CNF['ICON']) ? $CNF['ICON'] : '',
  477. 'add_url' => $sUrlAdd
  478. ]);
  479. }
  480. }
  481. /**
  482. * Profile/Context view page.
  483. */
  484. if(isset($this->_aProfileInfo)) {
  485. $oProfileModule = BxDolModule::getInstance($this->_aProfileInfo['type']);
  486. if($oProfileModule !== null)
  487. $aProfileContentInfo = $oProfileModule->_oDb->getContentInfoById($this->_aProfileInfo['content_id']);
  488. if(!empty($aProfileContentInfo) && is_array($aProfileContentInfo)) {
  489. $CNF = &$oProfileModule->_oConfig->CNF;
  490. /**
  491. * Process page cover with related menus and lists.
  492. */
  493. if(BxDolCover::getInstance($this)->isCover()) {
  494. $aProfileInfo = BxDolProfile::getData($this->_aProfileInfo['id'], [
  495. 'get_avatar' => 'getAvatarBig',
  496. 'with_info' => true
  497. ]);
  498. $oProfile = BxDolProfile::getInstance($this->_aProfileInfo['id']);
  499. $aProfileInfo['online'] = $oProfile->isOnline();
  500. unset($aProfileInfo['info']['profile_email']);
  501. unset($aProfileInfo['info']['profile_last_active']);
  502. unset($aProfileInfo['info']['profile_ip']);
  503. $a['cover_block'] = [
  504. 'profile' => $aProfileInfo,
  505. 'actions_menu' => '',
  506. 'meta_menu' => '',
  507. 'badges' => $oProfile->getBadges(),
  508. 'cover' => $oProfileModule->serviceGetCover($this->_aProfileInfo['content_id']),
  509. 'allow_edit' => $oProfileModule->checkAllowedChangeCover($aProfileContentInfo) === CHECK_ACTION_RESULT_ALLOWED
  510. ];
  511. if(!empty($CNF['OBJECT_MENU_VIEW_ENTRY_META']) && ($oMetaMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_VIEW_ENTRY_META'])) !== false)
  512. $a['cover_block']['meta_menu'] = $oMetaMenu->getCodeAPI();
  513. if(!empty($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) !== false)
  514. $a['cover_block']['members_list'] = $oConnection->getConnectedListAPI($this->_aProfileInfo['id'], false, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS, 10);
  515. if(!empty($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_ALL']) && ($oActionMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_ALL'])) !== false)
  516. $a['cover_block']['actions_menu'] = $oActionMenu->getCodeAPI();
  517. }
  518. /**
  519. * Process page mange menu.
  520. */
  521. if(($oMenuManage = BxDolMenu::getObjectInstance('sys_site_manage')) !== false) {
  522. if(!empty($CNF['FIELD_ID']))
  523. $oMenuManage->setContentId($aProfileContentInfo[$CNF['FIELD_ID']]);
  524. $aObjectManage = [];
  525. if(!empty($CNF['OBJECT_MENU_MANAGE_VIEW_ENTRY']))
  526. $aObjectManage = [$CNF['OBJECT_MENU_MANAGE_VIEW_ENTRY']];
  527. else if(!empty($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE']))
  528. $aObjectManage = [$CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE'], true];
  529. call_user_func_array([$oMenuManage, 'setObjectManage'], $aObjectManage);
  530. $aMenuManage = $oMenuManage->getCodeAPI();
  531. if(!empty($aMenuManage) && is_array($aMenuManage)) {
  532. if(isset($a['cover_block'], $a['cover_block']['actions_menu']) && is_array($a['cover_block']['actions_menu']))
  533. $a['cover_block']['actions_menu']['items'] = array_merge($a['cover_block']['actions_menu']['items'], $aMenuManage['items']);
  534. else
  535. $a['cover_block']['actions_menu'] = $aMenuManage;
  536. }
  537. }
  538. }
  539. }
  540. if(isLogged() && ($o = BxDolProfile::getInstance()) !== false) {
  541. $iLogged = $o->id();
  542. $a['user'] = BxDolProfile::getDataForPage($o);
  543. if(($sContextSwitcher = getParam('sys_api_context_switcher')) && ($sContextConnection = getParam('sys_api_context_connection'))) {
  544. $aCpIds = bx_srv($sContextSwitcher, 'get_participating_profiles', [$iLogged, ($sContextConnection != 'participants' ? $sContextConnection : false)]);
  545. $aContexts = [];
  546. foreach($aCpIds as $iCpId)
  547. $aContexts[] = BxDolProfile::getData($iCpId);
  548. $aInvitedTo = [];
  549. $aInvitations = bx_srv($sContextSwitcher, 'invitations', [$iLogged, true]);
  550. foreach($aInvitations as $aInvitation)
  551. $aInvitedTo[] = BxDolProfile::getData($aInvitation['group_profile_id']);
  552. $aInfo = [];
  553. if(($iId = bx_get('id')) !== false) {
  554. $aInfo = bx_srv($sModule, 'get_info', [(int)$iId, false]);
  555. }
  556. else if(($iPid = bx_get('profile_id')) !== false) {
  557. $aInfo = bx_srv($sModule, 'get_content_info_by_profile_id', [(int)$iPid]);
  558. }
  559. $aCurrent = [];
  560. if($aInfo && ((($sKey = 'allow_view_to') && isset($aInfo[$sKey]) && ($iValue = (int)$aInfo[$sKey])) || (($sKey = 'object_privacy_view') && isset($aInfo[$sKey]) && ($iValue = (int)$aInfo[$sKey])))) {
  561. $mixedContext = false;
  562. if($iValue < 0) {
  563. $aContext = BxDolProfileQuery::getInstance()->getInfoById(abs($iValue));
  564. if($aContext && $aContext['type'] == $sContextSwitcher)
  565. $mixedContext = (int)$aContext['id'];
  566. }
  567. else if($sModule == $sContextSwitcher)
  568. $mixedContext = BxDolProfile::getInstanceByContentAndType((int)$aInfo['id'], $sModule);
  569. if($mixedContext !== false)
  570. $aCurrent = BxDolProfile::getData($mixedContext);
  571. }
  572. $_sSample = '_' . $sContextSwitcher . '_txt_sample_single';
  573. $sSample = _t($_sSample);
  574. if(strcmp($_sSample, $sSample) == 0)
  575. $sSample = _t('_Context');
  576. $a['context'] = [
  577. 'list' => $aContexts,
  578. 'invitations' => $aInvitedTo,
  579. 'current' => $aCurrent,
  580. 'links' => []
  581. ];
  582. if(bx_srv($sContextSwitcher, 'check_allowed', ['add']) === CHECK_ACTION_RESULT_ALLOWED)
  583. $a['context']['links'][] = [
  584. 'title' => _t('_api_create_context', $sSample),
  585. 'icon' => 'Plus',
  586. 'url' => bx_api_get_relative_url(bx_srv($sContextSwitcher, 'get_link_add'))
  587. ];
  588. }
  589. }
  590. if(!$bIsAvailable)
  591. $a['page_status'] = 404;
  592. else if(!$bIsVisible)
  593. $a['page_status'] = 403;
  594. return $a;
  595. }
  596. public function getPageBlocksAPI($aBlocks = [])
  597. {
  598. $bBlocks = !empty($aBlocks) && is_array($aBlocks);
  599. $aFieldsUnset = ['object', 'cell_id', 'title_system', 'class', 'submenu', 'tabs', 'async', 'visible_for_levels', 'hidden_on', 'type', 'text', 'text_updated', 'help', 'cache_lifetime', 'active', 'active_api', 'copyable', 'deletable', 'order'];
  600. $aCells = $this->_oQuery->getPageBlocks(true);
  601. foreach($aCells as $sKey => &$aCell) {
  602. foreach($aCell as $i => $aBlock) {
  603. if(!$this->_isVisibleBlock($aBlock)) {
  604. unset($aCells[$sKey][$i]);
  605. continue;
  606. }
  607. $this->processPageBlock($aCells[$sKey][$i], true);
  608. $aBlock = $aCells[$sKey][$i];
  609. $sSource = '';
  610. if($aBlock['type'] == 'service') {
  611. $aContent = @unserialize($aBlock['content']);
  612. if(isset($aContent['module'], $aContent['method']))
  613. $sSource = $aContent['module'] . ':' . $aContent['method'];
  614. }
  615. else
  616. $sSource = 'system:block_' . $aBlock['id'];
  617. if($bBlocks && !in_array($sSource, $aBlocks)) {
  618. unset($aCells[$sKey][$i]);
  619. continue;
  620. }
  621. $mBlock = $aBlock['content'];
  622. if(($sFunc = '_getBlock' . bx_gen_method_name($aBlock['type'])) && method_exists($this, $sFunc))
  623. $mBlock = $this->$sFunc($aBlock);
  624. if(empty($mBlock)) {
  625. unset($aCells[$sKey][$i]);
  626. continue;
  627. }
  628. $aCells[$sKey][$i] = array_merge($aCells[$sKey][$i], [
  629. 'title' => isset($mBlock['title']) ? $mBlock['title'] : $this->getBlockTitle($aBlock),
  630. 'content' => isset($mBlock['content']) ? $mBlock['content'] : $mBlock,
  631. 'menu' => isset($mBlock['menu']) ? $mBlock['menu'] : '',
  632. 'source' => $sSource
  633. ]);
  634. $aCells[$sKey][$i] = array_diff_key($aCells[$sKey][$i], array_flip($aFieldsUnset));
  635. }
  636. }
  637. return array_map('array_values', $aCells);
  638. }
  639. /**
  640. * Get page code vars
  641. * @return string
  642. */
  643. protected function _getPageCodeVars ()
  644. {
  645. $aHiddenOn = array(
  646. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  647. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  648. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  649. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  650. );
  651. $aVars = array (
  652. 'page_id' => 'bx-page-' . $this->_aObject['uri'],
  653. 'bx_if:show_layout_row_dump' => array(
  654. 'condition' => false,
  655. 'content' => array()
  656. )
  657. );
  658. $aBlocks = $this->_oQuery->getPageBlocks();
  659. foreach ($aBlocks as $sKey => $aCell) {
  660. $sCell = '';
  661. foreach ($aCell as $aBlock) {
  662. $this->processPageBlock($aBlock, false);
  663. $sContentWithBox = $this->_getBlockCodeWithCache($aBlock, $aBlock['async']);
  664. $sClassAdd = '';
  665. if(!empty($aBlock['class']))
  666. $sClassAdd .= ' ' . $aBlock['class'];
  667. if(!empty($aBlock['hidden_on']))
  668. foreach($aHiddenOn as $iHiddenOn => $sHiddenOnClass)
  669. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  670. $sClassAdd .= ' ' . $sHiddenOnClass;
  671. if ($sContentWithBox)
  672. $sCell .= $this->_oTemplate->parseHtmlByName('designbox_container.html', array(
  673. 'class_add' => $sClassAdd,
  674. 'bx_if:show_html_id' => array(
  675. 'condition' => true,
  676. 'content' => array(
  677. 'html_id' => 'bx-page-block-' . $aBlock['id']
  678. ),
  679. ),
  680. 'content' => $sContentWithBox
  681. ));
  682. }
  683. $aVars[$sKey] = $sCell;
  684. }
  685. return $aVars;
  686. }
  687. /**
  688. * Process block values, especially if someting need to be overrided
  689. */
  690. protected function processPageBlock(&$aBlock, $bApi = false)
  691. {
  692. }
  693. /**
  694. * Get page code only.
  695. * @return string
  696. */
  697. protected function _getPageCode ()
  698. {
  699. $aVars = $this->_getPageCodeVars ();
  700. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  701. }
  702. /**
  703. * Get one block code only.
  704. * @return string
  705. */
  706. protected function _getBlockOnlyCode ($iBlockId)
  707. {
  708. if (!($aBlock = $this->_oQuery->getPageBlock((int)$iBlockId)))
  709. return '';
  710. return $this->_getBlockCodeWithCache($aBlock, 0);
  711. }
  712. protected function _getBlockCodeWithCache(&$aBlock, $iAsync = 0)
  713. {
  714. if (bx_is_dbg() || !getParam('sys_pb_cache_enable') || !$aBlock['cache_lifetime'] || $iAsync || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  715. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  716. }
  717. else {
  718. $oCache = $this->_getBlockCacheObject();
  719. $sKey = $this->_getBlockCacheKey(false, $aBlock);
  720. $sKeyCssJs = 'cssjs_' . $sKey;
  721. $mixedRet = $oCache->getData($sKey, $aBlock['cache_lifetime']);
  722. $aRetCssJs = $oCache->getData($sKeyCssJs, $aBlock['cache_lifetime']);
  723. if ($mixedRet !== null && $aRetCssJs !== null) {
  724. BxDolTemplate::getInstance()->collectingStart();
  725. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  726. $sBlockCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  727. $sBlockCode .= $mixedRet;
  728. } else {
  729. BxDolTemplate::getInstance()->collectingStart();
  730. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  731. $aBlockCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  732. $oCache->setData($sKey, $sBlockCode, $aBlock['cache_lifetime']);
  733. $oCache->setData($sKeyCssJs, $aBlockCssJs, $aBlock['cache_lifetime']);
  734. }
  735. }
  736. return $sBlockCode;
  737. }
  738. /**
  739. * Get block code.
  740. * @return string
  741. */
  742. protected function _getBlockCode(&$aBlock, $iAsync = 0)
  743. {
  744. $sContentWithBox = '';
  745. $oFunctions = $this->_oTemplate->getTemplateFunctions();
  746. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  747. $aDbNoTitle = [BX_DB_CONTENT_ONLY, BX_DB_PADDING_CONTENT_ONLY, BX_DB_NO_CAPTION, BX_DB_PADDING_NO_CAPTION];
  748. $sTitle = $this->getBlockTitle($aBlock);
  749. $sHelp = $this->getBlockHelp($aBlock);
  750. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  751. $bBlockVisible = $this->_isVisibleBlock($aBlock);
  752. if ($iAsync && $bBlockVisible) {
  753. $iDesignboxId = $aBlock['designbox_id'];
  754. $sHelpTitle = $sHelpContent = '';
  755. if(!in_array($iDesignboxId, $aDbNoTitle))
  756. $sHelpTitle = $sHelp;
  757. else
  758. $sHelpContent = $sHelp;
  759. $sContent = $this->getBlockAsyncCode($aBlock, $iAsync);
  760. $aParams = array(
  761. $sTitle . $sHelpTitle,
  762. $sContent . $sHelpContent,
  763. $iDesignboxId
  764. );
  765. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  766. }
  767. elseif ($bBlockVisible && method_exists($this, $sFunc)) {
  768. $mixedContent = $this->$sFunc($aBlock);
  769. if(!empty($aBlock['content_empty'])) {
  770. $sEmpty = _t($aBlock['content_empty']);
  771. $sRegExp = '/(<[A-Za-z0-9]+\b.[^<]*[\pPi\pPf]bx\-msg\-box\b.*[\pPi\pPf][^>]*>)(' . _t('_Empty') . ')(<\/[A-Za-z0-9]+>)/su';
  772. if(is_string($mixedContent))
  773. $mixedContent = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent);
  774. else if(is_array($mixedContent) && !empty($mixedContent['content']))
  775. $mixedContent['content'] = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent['content']);
  776. }
  777. $this->_oQuery->setReadOnlyMode(true);
  778. if(is_array($mixedContent) && !empty($mixedContent['content'])) {
  779. $iDesignboxId = isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'];
  780. if(!empty($mixedContent['markers']) && is_array($mixedContent['markers'])) {
  781. $this->addMarkers($mixedContent['markers']);
  782. $sTitle = $this->getBlockTitle($aBlock);
  783. }
  784. $sHelpTitle = $sHelpContent = '';
  785. if(!in_array($iDesignboxId, $aDbNoTitle))
  786. $sHelpTitle = $sHelp;
  787. else
  788. $sHelpContent = $sHelp;
  789. $aParams = array(
  790. (isset($mixedContent['title']) ? $mixedContent['title'] : $sTitle) . $sHelpTitle,
  791. $mixedContent['content'] . $sHelpContent,
  792. $iDesignboxId
  793. );
  794. $mixedMenu = false;
  795. if(isset($mixedContent['menu']))
  796. $mixedMenu = $mixedContent['menu'];
  797. else if(!empty($aBlock['submenu']))
  798. $mixedMenu = $aBlock['submenu'];
  799. $aParams[] = $mixedMenu;
  800. if(isset($mixedContent['buttons']))
  801. $aParams[] = $mixedContent['buttons'];
  802. else if($mixedMenu)
  803. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  804. if(isset($mixedContent['class']))
  805. $aBlock['class'] = $mixedContent['class'];
  806. }
  807. else if(is_string($mixedContent) && !empty($mixedContent)) {
  808. $iDesignboxId = $aBlock['designbox_id'];
  809. $sHelpTitle = $sHelpContent = '';
  810. if(!in_array($iDesignboxId, $aDbNoTitle))
  811. $sHelpTitle = $sHelp;
  812. else
  813. $sHelpContent = $sHelp;
  814. $aParams = array(
  815. $sTitle . $sHelpTitle,
  816. $mixedContent . $sHelpContent,
  817. $iDesignboxId
  818. );
  819. $mixedMenu = !empty($aBlock['submenu']) ? $aBlock['submenu'] : false;
  820. $aParams[] = $mixedMenu;
  821. if($mixedMenu)
  822. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  823. }
  824. if(!empty($aParams))
  825. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  826. }
  827. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  828. return $sContentWithBox;
  829. }
  830. /**
  831. * Add necessary js and css files.
  832. */
  833. protected function _addJsCss()
  834. {
  835. $this->_oTemplate->addJs(array('BxDolPage.js', 'theia-sticky-sidebar/theia-sticky-sidebar.umd.js', 'theia-sticky-sidebar/ResizeSensor.min.js'));
  836. $this->_oTemplate->addCss('page_layouts.css');
  837. }
  838. /**
  839. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  840. */
  841. protected function _addSysTemplateVars ()
  842. {
  843. $oTemplate = BxDolTemplate::getInstance();
  844. $sPageTitle = $this->_getPageTitle();
  845. if ($sPageTitle)
  846. $oTemplate->setPageHeader ($sPageTitle);
  847. $sMetaTitle = $this->_getPageMetaTitle();
  848. if ($sMetaTitle)
  849. $oTemplate->setPageMetaTitle ($sMetaTitle);
  850. $sMetaDesc = $this->_getPageMetaDesc();
  851. if ($sMetaDesc)
  852. $oTemplate->setPageDescription ($sMetaDesc);
  853. $sMetaRobots = $this->_getPageMetaRobots();
  854. if ($sMetaRobots)
  855. $oTemplate->setPageMetaRobots ($sMetaRobots);
  856. $sMetaImage = $this->_getPageMetaImage();
  857. if ($sMetaImage)
  858. $oTemplate->addPageMetaImage($sMetaImage);
  859. $sMetaKeywords = $this->_getPageMetaKeywords();
  860. if ($sMetaKeywords)
  861. $oTemplate->addPageKeywords ($sMetaKeywords);
  862. }
  863. /**
  864. * Select menu from page properties.
  865. */
  866. protected function _selectMenu ()
  867. {
  868. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  869. }
  870. /**
  871. * Set page submenu if it's specified
  872. */
  873. protected function _setSubmenu ($aParams)
  874. {
  875. if(empty($this->_aObject['submenu']) || $this->_aObject['submenu'] == 'disabled')
  876. return;
  877. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  878. if(!$oMenuSubmenu)
  879. return;
  880. $oMenuSubmenu->setObjectSubmenu($this->_aObject['submenu'], $aParams);
  881. }
  882. /**
  883. * Get content for 'raw' block type.
  884. * @return string
  885. */
  886. protected function _getBlockRaw ($aBlock)
  887. {
  888. if (bx_is_api()){
  889. return [bx_api_get_block('raw', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  890. }
  891. $s = '<div class="bx-page-raw-container bx-def-vanilla-html max-w-none">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  892. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  893. $s = bx_process_macros($s);
  894. return $s;
  895. }
  896. /**
  897. * Get content for 'custom' block type.
  898. * @return string
  899. */
  900. protected function _getBlockCustom ($aBlock)
  901. {
  902. if (bx_is_api()){
  903. return [bx_api_get_block('custom', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  904. }
  905. $s = '<div class="bx-page-custom-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  906. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  907. $s = bx_process_macros($s);
  908. return $s;
  909. }
  910. /**
  911. * Get content for 'html' block type.
  912. * @return string
  913. */
  914. protected function _getBlockHtml ($aBlock)
  915. {
  916. if (bx_is_api()){
  917. return [bx_api_get_block('html', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  918. }
  919. $s = '<div class="bx-page-html-container bx-def-vanilla-html max-w-none">' . $aBlock['content'] . '</div>';
  920. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  921. $s = bx_process_macros($s);
  922. return $s;
  923. }
  924. /**
  925. * Get content for 'creative' block type.
  926. * @return string
  927. */
  928. protected function _getBlockCreative ($aBlock)
  929. {
  930. if(bx_is_api())
  931. return [bx_api_get_block('creative', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  932. $s = $this->_replaceMarkers($aBlock['content'], ['block_id' => $aBlock['id']]);
  933. $s = bx_process_macros($s);
  934. $bCanManage = $this->isAllowedCreativeManage();
  935. if($bCanManage) {
  936. $this->_oTemplate->addCss([
  937. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'grapesjs/|grapes.min.css',
  938. ]);
  939. $this->_oTemplate->addJs([
  940. 'grapesjs/grapes.min.js',
  941. 'grapesjs/grapesjs-blocks-basic.js',
  942. 'grapesjs/grapesjs-style-bg.js',
  943. 'grapesjs/grapesjs-preset-webpage.min.js',
  944. ]);
  945. }
  946. return $this->_oTemplate->parseHtmlByName('block_creative.html', [
  947. 'block_id' => $aBlock['id'],
  948. 'content' => $s,
  949. 'bx_if:show_controls' => [
  950. 'condition' => $bCanManage,
  951. 'content' => [
  952. 'onclick_edit' => $this->_sJsObjectName . '.creativeEdit(this, ' . $aBlock['id'] . ')',
  953. 'onclick_save' => $this->_sJsObjectName . '.creativeSave(this, ' . $aBlock['id'] . ')'
  954. ]
  955. ]
  956. ]);
  957. }
  958. /**
  959. * Get content for 'bento_grid' block type.
  960. * @return string
  961. */
  962. protected function _getBlockBentoGrid ($aBlock)
  963. {
  964. if(bx_is_api()) {
  965. $iBlockId = (int)$aBlock['id'];
  966. $iContentId = 0;
  967. if(($mContentId = bx_get('id')) !== false)
  968. $iContentId = (int)$mContentId;
  969. $sContentModule = '';
  970. if(!empty($this->_aObject['module']) && !in_array($this->_aObject['module'], ['system', 'custom']))
  971. $sContentModule = $this->_aObject['module'];
  972. $bIsAllowedEdit = false;
  973. if(!empty($sContentModule) && bx_is_srv($sContentModule, 'check_allowed_with_content'))
  974. $bIsAllowedEdit = bx_srv($sContentModule, 'check_allowed_with_content', ['edit', $iContentId]) === CHECK_ACTION_RESULT_ALLOWED;
  975. else
  976. $bIsAllowedEdit = isAdmin();
  977. return [bx_api_get_block('bento_grid', [
  978. 'title' => _t($aBlock['title']),
  979. 'content' => self::getPageBlockData($iBlockId, $iContentId, $sContentModule)
  980. ], ['ext' => [
  981. 'block_id' => $iBlockId,
  982. 'content_id' => $iContentId,
  983. 'content_module' => $sContentModule,
  984. 'is_allowed_edit' => $bIsAllowedEdit
  985. ]])];
  986. }
  987. /**
  988. * Isn't supported for now.
  989. */
  990. return '';
  991. }
  992. /**
  993. * Get content for 'wiki' block type.
  994. * @return string
  995. */
  996. protected function _getBlockWiki ($aBlock)
  997. {
  998. $oWiki = BxDolWiki::getObjectInstance($this->_aObject['module']);
  999. if (!$oWiki) {
  1000. $sContent = _t('_sys_wiki_error_missing_wiki_object', $this->_aObject['module']);
  1001. }
  1002. else {
  1003. $sContent = $oWiki->getBlockContent($aBlock['id'], false, (int)bx_get($aBlock['id'].'rev') ? (int)bx_get($aBlock['id'].'rev') : false);
  1004. }
  1005. $s = '<div id="bx-page-wiki-container-' . $aBlock['id'] . '" class="bx-page-wiki-container markdown-body bx-def-vanilla-html">' . $sContent . '</div>';
  1006. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  1007. $s = bx_process_macros($s);
  1008. return $s;
  1009. }
  1010. /**
  1011. * Get content for 'lang' block type.
  1012. * @return string
  1013. */
  1014. protected function _getBlockLang ($aBlock)
  1015. {
  1016. if (bx_is_api()){
  1017. return [bx_api_get_block('lang', ['title' => _t($aBlock['title']), 'content' => bx_process_macros(_t(trim($aBlock['content'])))])];
  1018. }
  1019. $s = '<div class="bx-page-lang-container bx-def-vanilla-html max-w-none">' . _t(trim($aBlock['content'])) . '</div>';
  1020. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  1021. $s = bx_process_macros($s);
  1022. return $s;
  1023. }
  1024. /**
  1025. * Get content for 'image' block type.
  1026. * @return string
  1027. */
  1028. protected function _getBlockImage ($aBlock)
  1029. {
  1030. if (empty($aBlock['content']))
  1031. return false;
  1032. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  1033. $iFileId = (int)$iFileId;
  1034. if (!$iFileId)
  1035. return false;
  1036. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  1037. if (!$oStorage)
  1038. return false;
  1039. $sUrl = $oStorage->getFileUrlById($iFileId);
  1040. if (!$sUrl)
  1041. return false;
  1042. $sStyleAdd = '';
  1043. $sClassAdd = 'bx-page-image-container';
  1044. $aAlign = ['left' => 'start', 'center' => 'center', 'right' => 'end'];
  1045. if(array_key_exists($sAlign, $aAlign)) {
  1046. $sClassAdd .= ' flex justify-' . $aAlign[$sAlign];
  1047. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  1048. }
  1049. return '<div class="' . $sClassAdd . '" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  1050. }
  1051. /**
  1052. * Get content for 'rss' block type.
  1053. * @return string
  1054. */
  1055. protected function _getBlockRss ($aBlock)
  1056. {
  1057. if (empty($aBlock['content']))
  1058. return false;
  1059. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  1060. $iNum = (int)$iNum;
  1061. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  1062. }
  1063. /**
  1064. * Get content for 'menu' block type.
  1065. * @return string
  1066. */
  1067. protected function _getBlockMenu ($aBlock)
  1068. {
  1069. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  1070. return $oMenu ? $oMenu->getCode () : '';
  1071. }
  1072. /**
  1073. * Get content for 'service' block type.
  1074. * @return string
  1075. */
  1076. protected function _getBlockService ($aBlock)
  1077. {
  1078. $aMarkers = array_merge($this->_aMarkers, [
  1079. 'block_id' => $aBlock['id']
  1080. ]);
  1081. return BxDolService::callSerialized($aBlock['content'], $aMarkers);
  1082. }
  1083. /**
  1084. * Get page title.
  1085. * @return string
  1086. */
  1087. protected function _getPageTitle()
  1088. {
  1089. return $this->_replaceMarkers(_t($this->_aObject['title']));
  1090. }
  1091. /**
  1092. * Get page meta description.
  1093. * @return string
  1094. */
  1095. protected function _getPageMetaDesc()
  1096. {
  1097. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  1098. }
  1099. /**
  1100. * Get page meta title.
  1101. * @return string
  1102. */
  1103. protected function _getPageMetaTitle()
  1104. {
  1105. return $this->_replaceMarkers(_t($this->_aObject['meta_title']));
  1106. }
  1107. /**
  1108. * Get page meta image.
  1109. * @return string
  1110. */
  1111. protected function _getPageMetaImage()
  1112. {
  1113. return '';
  1114. }
  1115. /**
  1116. * Get page meta keywords.
  1117. * @return string
  1118. */
  1119. protected function _getPageMetaKeywords()
  1120. {
  1121. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  1122. }
  1123. /**
  1124. * Get page meta robots.
  1125. * @return string
  1126. */
  1127. protected function _getPageMetaRobots()
  1128. {
  1129. return _t($this->_aObject['meta_robots']);
  1130. }
  1131. /**
  1132. * Get page not found message.
  1133. * @return string
  1134. */
  1135. protected function _getPageNotFoundMsg ($mixedMsg = false)
  1136. {
  1137. $this->_oTemplate->displayPageNotFound();
  1138. exit;
  1139. }
  1140. /**
  1141. * Get access denied message.
  1142. * @return string
  1143. */
  1144. protected function _getPageAccessDeniedMsg ($mixedMsg = false)
  1145. {
  1146. return MsgBox($mixedMsg !== false ? $mixedMsg : _t('_Access denied'));
  1147. }
  1148. /**
  1149. * Get page cache object.
  1150. * @return cache object instance
  1151. */
  1152. protected function _getPageCacheObject ()
  1153. {
  1154. return $this->_getCacheObject ();
  1155. }
  1156. /**
  1157. * Get block cache object.
  1158. * @return cache object instance
  1159. */
  1160. protected function _getBlockCacheObject ()
  1161. {
  1162. return $this->_getCacheObject ('Block', 'pb');
  1163. }
  1164. protected function _getCacheObject ($sSuffixObj = 'Page', $sSuffixParam = 'page')
  1165. {
  1166. $sObj = '_o' . $sSuffixObj . 'CacheObject';
  1167. $sParam = 'sys_' . $sSuffixParam . '_cache_engine';
  1168. if ($this->{$sObj} != null) {
  1169. return $this->{$sObj};
  1170. } else {
  1171. $sEngine = getParam($sParam);
  1172. $this->{$sObj} = bx_instance ('BxDolCache' . $sEngine);
  1173. if (!$this->{$sObj}->isAvailable())
  1174. $this->{$sObj} = bx_instance ('BxDolCacheFile');
  1175. return $this->{$sObj};
  1176. }
  1177. }
  1178. /**
  1179. * Get page cache key.
  1180. * @param $isPrefixOnly return key prefix only.
  1181. * @return string
  1182. */
  1183. protected function _getPageCacheKey ($isPrefixOnly = false)
  1184. {
  1185. $s = 'page_' . $this->_aObject['object'] . '_';
  1186. if ($isPrefixOnly)
  1187. return $s;
  1188. $s .= $this->_getPageCacheParams ();
  1189. $s .= bx_site_hash() . '.php';
  1190. return $s;
  1191. }
  1192. /**
  1193. * Get block cache key.
  1194. * @param $isPrefixOnly return key prefix only.
  1195. * @return string
  1196. */
  1197. protected function _getBlockCacheKey ($isPrefixOnly = false, $aBlock = array())
  1198. {
  1199. $s = $this->_getPageCacheKey(true) . 'block_';
  1200. if ($isPrefixOnly)
  1201. return $s;
  1202. $s .= ($aBlock ? $aBlock['id'] : 0) . '_' . $this->_getPageCacheParams ();
  1203. $s .= bx_site_hash() . '.php';
  1204. return $s;
  1205. }
  1206. /**
  1207. * Additional cache key. In the case of dynamic page.
  1208. * For example - profile page, where each profile has own page.
  1209. * @return string
  1210. */
  1211. protected function _getPageCacheParams ()
  1212. {
  1213. return md5(serialize($_GET) . bx_lang_name());
  1214. }
  1215. /**
  1216. * Clean page cache.
  1217. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  1218. * @return string
  1219. */
  1220. protected function cleanCache ($isDelAllWithPagePrefix = false)
  1221. {
  1222. $a = ['Page', 'Block'];
  1223. $bRet = false;
  1224. foreach ($a as $s) {
  1225. $oCache = $this->{'_get' . $s . 'CacheObject'} ();
  1226. $sKey = $this->{'_get'. $s . 'CacheKey'} ($isDelAllWithPagePrefix);
  1227. if ($isDelAllWithPagePrefix)
  1228. $bRet = $bRet && $oCache->removeAllByPrefix($sKey);
  1229. else
  1230. $bRet = $bRet && $oCache->delData($sKey);
  1231. }
  1232. return $bRet;
  1233. }
  1234. }
  1235. /** @} */