BxBasePage.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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. }
  41. public function performActionGetHelp ()
  42. {
  43. $iBlockId = (int)bx_get('block_id');
  44. if(empty($iBlockId))
  45. return;
  46. $aBlock = $this->_oQuery->getPageBlock($iBlockId);
  47. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['help']))
  48. return;
  49. echo $this->_oTemplate->parseHtmlByName('designbox_help_popup.html', array(
  50. 'content' => _t($aBlock['help'])
  51. ));
  52. }
  53. public function performActionGetShare ()
  54. {
  55. $aEmbedData = BxDolPage::getEmbedData(bx_get('url'));
  56. if(!isset($aEmbedData['url']))
  57. return;
  58. $sMenu = '';
  59. if(getParam('sys_a2a_enable') != 'on') {
  60. if(($oMenu = BxDolMenu::getObjectInstance('sys_social_sharing')) !== false) {
  61. $oMenu->addMarkers([
  62. 'url' => $aEmbedData['url'],
  63. 'img_url_encoded' => '',
  64. 'title_encoded' => ''
  65. ]);
  66. $sMenu = $oMenu->getCode();
  67. }
  68. }
  69. else
  70. $sMenu = getParam('sys_a2a_code');
  71. echo PopupBox('sys_share_popup', _t('_sys_txt_share_popup_header'), $this->_oTemplate->parseHtmlByName('designbox_share_popup.html', [
  72. 'url' => bx_process_output($aEmbedData['url']),
  73. 'menu' => $sMenu,
  74. 'code' => htmlspecialchars($aEmbedData['html']),
  75. ]), true);
  76. }
  77. public function performActionEmbed ()
  78. {
  79. $aCover = $this->getPageCoverImage();
  80. $sTitle = $this->_getPageTitle();
  81. $this->_oTemplate->getEmbed($this->_oTemplate->parseHtmlByName('embed_card.html', [
  82. 'title' => $sTitle,
  83. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  84. 'bx_if:thumb' => [
  85. 'condition' => $this->isPageCover() && count($aCover) > 0,
  86. 'content' => [
  87. 'title' => $sTitle,
  88. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  89. 'background' => BxDolCover::getInstance(BxDolTemplate::getInstance())->getCoverImageUrl($aCover)
  90. ],
  91. ],
  92. 'bx_if:no_thumb' => [
  93. 'condition' => count($aCover) == 0 || !$this->isPageCover(),
  94. 'content' => [
  95. 'title' => $sTitle,
  96. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  97. ]
  98. ]
  99. ]
  100. ));
  101. }
  102. /**
  103. * Very similar to BxBasePage::getCode
  104. * but adds css and js files which are needed for the corect page display
  105. */
  106. public function getCodeDynamic ()
  107. {
  108. // TODO: remake to use use collect* template methods
  109. $oTemplate = BxDolTemplate::getInstance();
  110. // get js&css before the page code is generated
  111. $aCssBefore = $oTemplate->getCss();
  112. $aJsBefore = $oTemplate->getJs();
  113. // generate page code
  114. $sContent = $this->getCode();
  115. // get js&css after the page code is generated
  116. $aCssAfter = $oTemplate->getCss();
  117. $aJsAfter = $oTemplate->getJs();
  118. // compare files which were added before and after page code is generated
  119. $f = function ($a1, $a2) {
  120. return strcasecmp($a1['url'], $a2['url']);
  121. };
  122. $aCssNew = array_udiff($aCssAfter, $aCssBefore, $f);
  123. $aJsNew = array_udiff($aJsAfter, $aJsBefore, $f);
  124. // add newly added js&css files in static mode
  125. $sCss = $sJs = '';
  126. foreach ($aCssNew as $a)
  127. $sCss .= $oTemplate->addCss($a['url'], true);
  128. foreach ($aJsNew as $a)
  129. $sJs .= $oTemplate->addJs($a['url'], true);
  130. return $sJs . $sCss . $sContent;
  131. }
  132. /**
  133. * Get page code with automatic caching, adding necessary css/js files and system template vars.
  134. * @return string.
  135. */
  136. public function getCode ()
  137. {
  138. if (bx_get('dynamic') && ($iBlockId = (int)bx_get('pageBlock'))) {
  139. if (self::isLockedFromUnauthenticated($this->_aObject['uri']) || !$this->_isVisiblePage($this->_aObject)) {
  140. header('HTTP/1.0 403 Forbidden');
  141. exit;
  142. }
  143. bx_alert('system', 'page_output_block', 0, false, array(
  144. 'page_name' => $this->_sObject,
  145. 'page_object' => $this,
  146. 'page_query' => $this->_oQuery,
  147. 'block_id' => (int)$iBlockId,
  148. ));
  149. header( 'Content-type:text/html;charset=utf-8' );
  150. BxDolTemplate::getInstance()->collectingStart();
  151. $s = $this->_getBlockOnlyCode($iBlockId);
  152. $sCss = bx_get('includedCss');
  153. $sJs = bx_get('includedJs');
  154. echo BxDolTemplate::getInstance()->collectingEndGetCode($sCss ? @json_decode($sCss) : [], $sJs ? @json_decode($sJs) : []);
  155. echo $s;
  156. exit;
  157. }
  158. if (!$this->_isVisiblePage($this->_aObject))
  159. return $this->_getPageAccessDeniedMsg ();
  160. $this->_addJsCss();
  161. if (!$this->_bSubPage) {
  162. $this->_addSysTemplateVars();
  163. $this->_selectMenu();
  164. $this->_setSubmenu(array());
  165. }
  166. if (bx_is_dbg() || !getParam('sys_page_cache_enable') || !$this->_aObject['cache_lifetime'] || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  167. $sPageCode = $this->_getPageCode();
  168. }
  169. else {
  170. $oCache = $this->_getPageCacheObject();
  171. $sKey = $this->_getPageCacheKey();
  172. $sKeyCssJs = 'cssjs_' . $sKey;
  173. $mixedRet = $oCache->getData($sKey, $this->_aObject['cache_lifetime']);
  174. $aRetCssJs = $oCache->getData($sKeyCssJs, $this->_aObject['cache_lifetime']);
  175. if ($mixedRet !== null && $aRetCssJs !== null) {
  176. BxDolTemplate::getInstance()->collectingStart();
  177. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  178. $sPageCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  179. $sPageCode .= $mixedRet;
  180. } else {
  181. BxDolTemplate::getInstance()->collectingStart();
  182. $sPageCode = $this->_getPageCode();
  183. $aPageCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  184. $oCache->setData($sKey, $sPageCode, $this->_aObject['cache_lifetime']);
  185. $oCache->setData($sKeyCssJs, $aPageCssJs, $this->_aObject['cache_lifetime']);
  186. }
  187. }
  188. bx_alert('system', 'page_output', 0, false, array(
  189. 'page_name' => $this->_sObject,
  190. 'page_object' => $this,
  191. 'page_query' => $this->_oQuery,
  192. 'page_code' => &$sPageCode,
  193. 'sub_page' => $this->_bSubPage,
  194. ));
  195. if (!$this->_bSubPage)
  196. $sPageCode .= $this->getJsScript();
  197. return $sPageCode;
  198. }
  199. public function getJsClassName()
  200. {
  201. return $this->_sJsClassName;
  202. }
  203. public function getJsObjectName()
  204. {
  205. return $this->_sJsObjectName;
  206. }
  207. function _wrapInTagJsCode($sCode)
  208. {
  209. return "<script language=\"javascript\">\n<!--\n" . $sCode . "\n-->\n</script>";
  210. }
  211. public function getJsScript()
  212. {
  213. $sJsObjName = $this->getJsObjectName();
  214. $sJsObjClass = $this->getJsClassName();
  215. $sCode = "if(window['" . $sJsObjName . "'] == undefined) var " . $sJsObjName . " = new " . $sJsObjClass . "(" . json_encode(array(
  216. 'sObjName' => $sJsObjName,
  217. 'sObject' => $this->_sObject,
  218. 'sRootUrl' => BX_DOL_URL_ROOT,
  219. 'aHtmlIds' => $this->_aHtmlIds,
  220. 'isStickyColumns' => $this->_bStickyColumns
  221. )) . ");";
  222. return $this->_wrapInTagJsCode($sCode);
  223. }
  224. /**
  225. * Is page cover enabled.
  226. * @return string
  227. */
  228. public function isPageCover()
  229. {
  230. $bResult = false;
  231. switch((int)$this->_aObject['cover']) {
  232. case 1: //--- Enabled for all
  233. $bResult = true;
  234. break;
  235. case 2: //--- Enabled for visitors only
  236. $bResult = !isLogged();
  237. break;
  238. case 3: //--- Enabled for members only
  239. $bResult = isLogged();
  240. break;
  241. }
  242. return $bResult;
  243. }
  244. public function setSubPage($b = true)
  245. {
  246. $this->_bSubPage = $b;
  247. }
  248. public function setPageCover($bCover = true)
  249. {
  250. $this->_aObject['cover'] = (bool)$bCover;
  251. }
  252. public function getPageCoverImage($bTranscoder = true)
  253. {
  254. $iId = (int)$this->_aObject['cover_image'];
  255. if(empty($iId)) {
  256. $iId = (int)getParam('sys_site_cover_common');
  257. if(empty($iId))
  258. return array();
  259. }
  260. $aResult = array(
  261. 'id' => $iId
  262. );
  263. if($bTranscoder)
  264. $aResult['transcoder'] = BX_DOL_TRANSCODER_OBJ_COVER;
  265. else
  266. $aResult['object'] = $this->_sStorage;
  267. return $aResult;
  268. }
  269. public function getPageCoverParams()
  270. {
  271. $aParams = [
  272. 'title' => $this->_getPageTitle(),
  273. 'actions' => '',
  274. 'bx_if:image' => [
  275. 'condition' => false,
  276. 'content' => []
  277. ],
  278. 'bx_if:icon' => [
  279. 'condition' => false,
  280. 'content' => []
  281. ],
  282. ];
  283. if(($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  284. $aCoverParams = $oMenuSubmenu->getPageCoverParams();
  285. if(!empty($aCoverParams) && is_array($aCoverParams))
  286. $aParams = $aCoverParams;
  287. }
  288. if(!empty($this->_aObject['cover_title'])) {
  289. $sCoverTitle = _t($this->_aObject['cover_title']);
  290. if($sCoverTitle && strcmp($sCoverTitle, $this->_aObject['cover_title']) != 0)
  291. $aParams['title'] = $sCoverTitle;
  292. }
  293. return $aParams;
  294. }
  295. /**
  296. * Get block title.
  297. * @return string
  298. */
  299. public function getBlockTitle ($aBlock)
  300. {
  301. return $this->_replaceMarkers(_t($aBlock['title']), array('block_id' => $aBlock['id']));
  302. }
  303. /**
  304. * Get help control if help is available for the block.
  305. * @return string
  306. */
  307. public function getBlockHelp ($aBlock)
  308. {
  309. if(empty($aBlock['help']) || empty(strip_tags(_t($aBlock['help']))))
  310. return '';
  311. return $this->_oTemplate->parseHtmlByName('designbox_help.html', array(
  312. 'js_object' => $this->_sJsObjectName,
  313. 'block_id' => $aBlock['id']
  314. ));
  315. }
  316. /**
  317. * Get code to load block asyncroniously
  318. * @param $aBlock block code
  319. * @param $iAsync if greater than 0 the it defines loading indicator for the block
  320. * @return HTML code string
  321. */
  322. public function getBlockAsyncCode($aBlock, $iAsync)
  323. {
  324. $aContentPlaceholder = $this->_oQuery->getPageBlockContentPlaceholder($iAsync);
  325. if (!$aContentPlaceholder)
  326. return _t('_sys_txt_error_occured');
  327. $oTemplate = $this->_oTemplate;
  328. if ('system' != $aContentPlaceholder['module']) {
  329. $oModule = BxDolModule::getInstance($aContentPlaceholder['module']);
  330. if (!$oModule)
  331. return _t('_sys_txt_error_occured');
  332. $oTemplate = $oModule->_oTemplate;
  333. }
  334. return $oTemplate->parseHtmlByName($aContentPlaceholder['template'], $aBlock);
  335. }
  336. /**
  337. * Get page array with all cells and blocks
  338. */
  339. public function getPageAPI ($aBlocks = [])
  340. {
  341. define('BX_API_PAGE', true);
  342. $query_string = '';
  343. if (isset(bx_get('params')[2]) && bx_get('params')[2] != ''){
  344. $array = json_decode(bx_get('params')[2], true);
  345. $query_string = http_build_query($array);
  346. }
  347. $a = [
  348. 'id' => $this->_aObject['id'],
  349. 'title' => $this->_getPageTitle(),
  350. 'uri' => $this->_aObject['uri'],
  351. 'url' => bx_get('params')[0] . ($query_string != '' ? '?' . $query_string : ''),
  352. 'author' => $this->_aObject['author'],
  353. 'added' => $this->_aObject['added'],
  354. 'module' => $this->getModule(),
  355. 'type' => $this->getType (),
  356. 'layout' => str_replace('.html', '', $this->_aObject['template']),
  357. 'cover_block' => '',
  358. 'menu_top' => '',
  359. 'menu' => '',
  360. 'menu_bottom' => '',
  361. 'menu_add' => '',
  362. 'elements' => $this->getPageBlocksAPI($aBlocks),
  363. ];
  364. if (BxDolCover::getInstance($this)->isCover() && isset($this->_aProfileInfo)) {
  365. $oModule = BxDolModule::getInstance($this->getModule());
  366. $a['cover_block'] = [
  367. 'profile' => BxDolProfile::getData($this->_aProfileInfo['id'], [
  368. 'get_avatar' => 'getAvatarBig',
  369. 'with_info' => true
  370. ]),
  371. 'actions_menu' => '',
  372. 'meta_menu' => '',
  373. 'cover' => $oModule->serviceGetCover($this->_aProfileInfo['content_id']),
  374. 'allow_edit' => $oModule->checkAllowedChangeCover($this->_aProfileInfo['id']) === CHECK_ACTION_RESULT_ALLOWED
  375. ];
  376. $CNF = $oModule->_oConfig->CNF;
  377. if(!empty($CNF['OBJECT_MENU_VIEW_ENTRY_META'])){
  378. $oMetaMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_VIEW_ENTRY_META']);
  379. $a['cover_block']['meta_menu'] = $oMetaMenu->getCodeAPI();
  380. }
  381. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  382. if ($oConnection){
  383. $a['cover_block']['members_list'] = $oConnection->getConnectedListAPI($this->_aProfileInfo['id'], false, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS, 10);
  384. }
  385. if(!empty($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY'])){
  386. $oActionMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_ALL']);
  387. $a['cover_block']['actions_menu'] = $oActionMenu->getCodeAPI();
  388. }
  389. }
  390. $sMenuTop = getParam('sys_api_menu_top');
  391. if (!empty($sMenuTop) && ($oMenuTop = BxDolMenu::getObjectInstance($sMenuTop)) !== false)
  392. $a['menu_top'] = $oMenuTop->getCodeAPI();
  393. if (($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  394. if(($sSubmenu = $this->getSubMenu()) !== '')
  395. $oMenuSubmenu->setObjectSubmenu($sSubmenu, []);
  396. $a['menu'] = $oMenuSubmenu->getCodeAPI();
  397. }
  398. if (isLogged()) {
  399. /* temporatery commented
  400. if(($oMenuAddContent = BxDolMenu::getObjectInstance('sys_add_content')) !== false)
  401. $a['menu_add'] = $oMenuAddContent->getCodeAPI();
  402. */
  403. $o = BxDolProfile::getInstance();
  404. $a['user'] = [
  405. 'id' => $o->id(),
  406. 'email' => $o->getAccountObject()->getEmail(),
  407. 'display_name' => $o->getDisplayName(),
  408. 'url' => bx_api_get_relative_url($o->getUrl()),
  409. 'avatar' => $o->getAvatar(),
  410. 'info' => $o->getInfo(),
  411. 'notifications' => 0,
  412. 'cart' => 0,
  413. 'active' => $o->isActive(),
  414. 'status' => $o->getStatus(),
  415. ];
  416. $oInformer = BxDolInformer::getInstance(BxDolTemplate::getInstance());
  417. $sRet = $oInformer ? $oInformer->display() : '';
  418. if ($sRet){
  419. $a['user']['informer'] = $sRet;
  420. }
  421. $sModuleNotifications = 'bx_notifications';
  422. if(BxDolRequest::serviceExists($sModuleNotifications, 'get_unread_notifications_num'))
  423. $a['user']['notifications'] = bx_srv($sModuleNotifications, 'get_unread_notifications_num', [$o->id()]);
  424. $oPayments = BxDolPayments::getInstance();
  425. if($oPayments->isActive())
  426. $a['user']['cart'] = $oPayments->getCartItemsCount();
  427. }
  428. return $a;
  429. }
  430. public function getPageBlocksAPI($aBlocks = [])
  431. {
  432. $bBlocks = !empty($aBlocks) && is_array($aBlocks);
  433. $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'];
  434. $aCells = $this->_oQuery->getPageBlocks(true);
  435. foreach($aCells as $sKey => &$aCell) {
  436. foreach($aCell as $i => $aBlock) {
  437. if(!$this->_isVisibleBlock($aBlock)) {
  438. unset($aCells[$sKey][$i]);
  439. continue;
  440. }
  441. $this->processPageBlock($aCells[$sKey][$i], true);
  442. $aBlock = $aCells[$sKey][$i];
  443. $sSource = '';
  444. if($aBlock['type'] == 'service') {
  445. $aContent = @unserialize($aBlock['content']);
  446. if(isset($aContent['module'], $aContent['method']))
  447. $sSource = $aContent['module'] . ':' . $aContent['method'];
  448. }
  449. else{
  450. $sSource = 'system:block_' . $aBlock['id'];
  451. }
  452. if($bBlocks && !in_array($sSource, $aBlocks)) {
  453. unset($aCells[$sKey][$i]);
  454. continue;
  455. }
  456. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  457. $mBlock = method_exists($this, $sFunc) ? $this->$sFunc($aBlock) : $aBlock['content'];
  458. $aCells[$sKey][$i] = array_merge($aCells[$sKey][$i], [
  459. 'title' => isset($mBlock['title']) ? $mBlock['title'] : $this->getBlockTitle($aBlock),
  460. 'content' => isset($mBlock['content']) ? $mBlock['content'] : $mBlock,
  461. 'menu' => isset($mBlock['menu']) ? $mBlock['menu'] : '',
  462. 'source' => $sSource
  463. ]);
  464. $aCells[$sKey][$i] = array_diff_key($aCells[$sKey][$i], array_flip($aFieldsUnset));
  465. }
  466. }
  467. return array_map('array_values', $aCells);
  468. }
  469. /**
  470. * Get page code vars
  471. * @return string
  472. */
  473. protected function _getPageCodeVars ()
  474. {
  475. $aHiddenOn = array(
  476. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  477. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  478. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  479. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  480. );
  481. $aVars = array (
  482. 'page_id' => 'bx-page-' . $this->_aObject['uri'],
  483. 'bx_if:show_layout_row_dump' => array(
  484. 'condition' => false,
  485. 'content' => array()
  486. )
  487. );
  488. $aBlocks = $this->_oQuery->getPageBlocks();
  489. foreach ($aBlocks as $sKey => $aCell) {
  490. $sCell = '';
  491. foreach ($aCell as $aBlock) {
  492. $this->processPageBlock($aBlock, false);
  493. $sContentWithBox = $this->_getBlockCodeWithCache($aBlock, $aBlock['async']);
  494. $sClassAdd = '';
  495. if(!empty($aBlock['class']))
  496. $sClassAdd .= ' ' . $aBlock['class'];
  497. if(!empty($aBlock['hidden_on']))
  498. foreach($aHiddenOn as $iHiddenOn => $sHiddenOnClass)
  499. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  500. $sClassAdd .= ' ' . $sHiddenOnClass;
  501. if ($sContentWithBox)
  502. $sCell .= $this->_oTemplate->parseHtmlByName('designbox_container.html', array(
  503. 'class_add' => $sClassAdd,
  504. 'bx_if:show_html_id' => array(
  505. 'condition' => true,
  506. 'content' => array(
  507. 'html_id' => 'bx-page-block-' . $aBlock['id']
  508. ),
  509. ),
  510. 'content' => $sContentWithBox
  511. ));
  512. }
  513. $aVars[$sKey] = $sCell;
  514. }
  515. return $aVars;
  516. }
  517. /**
  518. * Process block values, especially if someting need to be overrided
  519. */
  520. protected function processPageBlock(&$aBlock, $bApi = false)
  521. {
  522. }
  523. /**
  524. * Get page code only.
  525. * @return string
  526. */
  527. protected function _getPageCode ()
  528. {
  529. $aVars = $this->_getPageCodeVars ();
  530. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  531. }
  532. /**
  533. * Get one block code only.
  534. * @return string
  535. */
  536. protected function _getBlockOnlyCode ($iBlockId)
  537. {
  538. if (!($aBlock = $this->_oQuery->getPageBlock((int)$iBlockId)))
  539. return '';
  540. return $this->_getBlockCodeWithCache($aBlock, 0);
  541. }
  542. protected function _getBlockCodeWithCache(&$aBlock, $iAsync = 0)
  543. {
  544. if (bx_is_dbg() || !getParam('sys_pb_cache_enable') || !$aBlock['cache_lifetime'] || $iAsync || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  545. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  546. }
  547. else {
  548. $oCache = $this->_getBlockCacheObject();
  549. $sKey = $this->_getBlockCacheKey(false, $aBlock);
  550. $sKeyCssJs = 'cssjs_' . $sKey;
  551. $mixedRet = $oCache->getData($sKey, $aBlock['cache_lifetime']);
  552. $aRetCssJs = $oCache->getData($sKeyCssJs, $aBlock['cache_lifetime']);
  553. if ($mixedRet !== null && $aRetCssJs !== null) {
  554. BxDolTemplate::getInstance()->collectingStart();
  555. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  556. $sBlockCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  557. $sBlockCode .= $mixedRet;
  558. } else {
  559. BxDolTemplate::getInstance()->collectingStart();
  560. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  561. $aBlockCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  562. $oCache->setData($sKey, $sBlockCode, $aBlock['cache_lifetime']);
  563. $oCache->setData($sKeyCssJs, $aBlockCssJs, $aBlock['cache_lifetime']);
  564. }
  565. }
  566. return $sBlockCode;
  567. }
  568. /**
  569. * Get block code.
  570. * @return string
  571. */
  572. protected function _getBlockCode(&$aBlock, $iAsync = 0)
  573. {
  574. $sContentWithBox = '';
  575. $oFunctions = $this->_oTemplate->getTemplateFunctions();
  576. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  577. $aDbNoTitle = [BX_DB_CONTENT_ONLY, BX_DB_PADDING_CONTENT_ONLY, BX_DB_NO_CAPTION, BX_DB_PADDING_NO_CAPTION];
  578. $sTitle = $this->getBlockTitle($aBlock);
  579. $sHelp = $this->getBlockHelp($aBlock);
  580. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  581. $bBlockVisible = $this->_isVisibleBlock($aBlock);
  582. if ($iAsync && $bBlockVisible) {
  583. $iDesignboxId = $aBlock['designbox_id'];
  584. $sHelpTitle = $sHelpContent = '';
  585. if(!in_array($iDesignboxId, $aDbNoTitle))
  586. $sHelpTitle = $sHelp;
  587. else
  588. $sHelpContent = $sHelp;
  589. $sContent = $this->getBlockAsyncCode($aBlock, $iAsync);
  590. $aParams = array(
  591. $sTitle . $sHelpTitle,
  592. $sContent . $sHelpContent,
  593. $iDesignboxId
  594. );
  595. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  596. }
  597. elseif ($bBlockVisible && method_exists($this, $sFunc)) {
  598. $mixedContent = $this->$sFunc($aBlock);
  599. if(!empty($aBlock['content_empty'])) {
  600. $sEmpty = _t($aBlock['content_empty']);
  601. $sRegExp = '/(<[A-Za-z0-9]+\b.[^<]*[\pPi\pPf]bx\-msg\-box\b.*[\pPi\pPf][^>]*>)(' . _t('_Empty') . ')(<\/[A-Za-z0-9]+>)/su';
  602. if(is_string($mixedContent))
  603. $mixedContent = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent);
  604. else if(is_array($mixedContent) && !empty($mixedContent['content']))
  605. $mixedContent['content'] = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent['content']);
  606. }
  607. $this->_oQuery->setReadOnlyMode(true);
  608. if(is_array($mixedContent) && !empty($mixedContent['content'])) {
  609. $iDesignboxId = isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'];
  610. $sHelpTitle = $sHelpContent = '';
  611. if(!in_array($iDesignboxId, $aDbNoTitle))
  612. $sHelpTitle = $sHelp;
  613. else
  614. $sHelpContent = $sHelp;
  615. $aParams = array(
  616. (isset($mixedContent['title']) ? $mixedContent['title'] : $sTitle) . $sHelpTitle,
  617. $mixedContent['content'] . $sHelpContent,
  618. $iDesignboxId
  619. );
  620. $mixedMenu = false;
  621. if(isset($mixedContent['menu']))
  622. $mixedMenu = $mixedContent['menu'];
  623. else if(!empty($aBlock['submenu']))
  624. $mixedMenu = $aBlock['submenu'];
  625. $aParams[] = $mixedMenu;
  626. if(isset($mixedContent['buttons']))
  627. $aParams[] = $mixedContent['buttons'];
  628. else if($mixedMenu)
  629. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  630. if(isset($mixedContent['class']))
  631. $aBlock['class'] = $mixedContent['class'];
  632. }
  633. else if(is_string($mixedContent) && !empty($mixedContent)) {
  634. $iDesignboxId = $aBlock['designbox_id'];
  635. $sHelpTitle = $sHelpContent = '';
  636. if(!in_array($iDesignboxId, $aDbNoTitle))
  637. $sHelpTitle = $sHelp;
  638. else
  639. $sHelpContent = $sHelp;
  640. $aParams = array(
  641. $sTitle . $sHelpTitle,
  642. $mixedContent . $sHelpContent,
  643. $iDesignboxId
  644. );
  645. $mixedMenu = !empty($aBlock['submenu']) ? $aBlock['submenu'] : false;
  646. $aParams[] = $mixedMenu;
  647. if($mixedMenu)
  648. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  649. }
  650. if(!empty($aParams))
  651. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  652. }
  653. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  654. return $sContentWithBox;
  655. }
  656. /**
  657. * Add necessary js and css files.
  658. */
  659. protected function _addJsCss()
  660. {
  661. $this->_oTemplate->addJs(array('BxDolPage.js', 'theia-sticky-sidebar/theia-sticky-sidebar.min.js', 'theia-sticky-sidebar/ResizeSensor.min.js'));
  662. $this->_oTemplate->addCss('page_layouts.css');
  663. }
  664. /**
  665. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  666. */
  667. protected function _addSysTemplateVars ()
  668. {
  669. $oTemplate = BxDolTemplate::getInstance();
  670. $sPageTitle = $this->_getPageTitle();
  671. if ($sPageTitle)
  672. $oTemplate->setPageHeader ($sPageTitle);
  673. $sMetaDesc = $this->_getPageMetaDesc();
  674. if ($sMetaDesc)
  675. $oTemplate->setPageDescription ($sMetaDesc);
  676. $sMetaRobots = $this->_getPageMetaRobots();
  677. if ($sMetaRobots)
  678. $oTemplate->setPageMetaRobots ($sMetaRobots);
  679. $sMetaImage = $this->_getPageMetaImage();
  680. if ($sMetaImage)
  681. $oTemplate->addPageMetaImage($sMetaImage);
  682. $sMetaKeywords = $this->_getPageMetaKeywords();
  683. if ($sMetaKeywords)
  684. $oTemplate->addPageKeywords ($sMetaKeywords);
  685. }
  686. /**
  687. * Select menu from page properties.
  688. */
  689. protected function _selectMenu ()
  690. {
  691. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  692. }
  693. /**
  694. * Set page submenu if it's specified
  695. */
  696. protected function _setSubmenu ($aParams)
  697. {
  698. if(empty($this->_aObject['submenu']) || $this->_aObject['submenu'] == 'disabled')
  699. return;
  700. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  701. if(!$oMenuSubmenu)
  702. return;
  703. $oMenuSubmenu->setObjectSubmenu($this->_aObject['submenu'], $aParams);
  704. }
  705. /**
  706. * Get content for 'raw' block type.
  707. * @return string
  708. */
  709. protected function _getBlockRaw ($aBlock)
  710. {
  711. if (bx_is_api()){
  712. return [bx_api_get_block('raw', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  713. }
  714. $s = '<div class="bx-page-raw-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  715. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  716. $s = bx_process_macros($s);
  717. return $s;
  718. }
  719. /**
  720. * Get content for 'custom' block type.
  721. * @return string
  722. */
  723. protected function _getBlockCustom ($aBlock)
  724. {
  725. if (bx_is_api()){
  726. return [bx_api_get_block('custom', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  727. }
  728. $s = '<div class="bx-page-custom-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  729. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  730. $s = bx_process_macros($s);
  731. return $s;
  732. }
  733. /**
  734. * Get content for 'html' block type.
  735. * @return string
  736. */
  737. protected function _getBlockHtml ($aBlock)
  738. {
  739. if (bx_is_api()){
  740. return [bx_api_get_block('html', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  741. }
  742. $s = '<div class="bx-page-html-container bx-def-vanilla-html max-w-none">' . $aBlock['content'] . '</div>';
  743. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  744. $s = bx_process_macros($s);
  745. return $s;
  746. }
  747. /**
  748. * Get content for 'wiki' block type.
  749. * @return string
  750. */
  751. protected function _getBlockWiki ($aBlock)
  752. {
  753. $oWiki = BxDolWiki::getObjectInstance($this->_aObject['module']);
  754. if (!$oWiki) {
  755. $sContent = _t('_sys_wiki_error_missing_wiki_object', $this->_aObject['module']);
  756. }
  757. else {
  758. $sContent = $oWiki->getBlockContent($aBlock['id'], false, (int)bx_get($aBlock['id'].'rev') ? (int)bx_get($aBlock['id'].'rev') : false);
  759. }
  760. $s = '<div id="bx-page-wiki-container-' . $aBlock['id'] . '" class="bx-page-wiki-container markdown-body bx-def-vanilla-html">' . $sContent . '</div>';
  761. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  762. $s = bx_process_macros($s);
  763. return $s;
  764. }
  765. /**
  766. * Get content for 'lang' block type.
  767. * @return string
  768. */
  769. protected function _getBlockLang ($aBlock)
  770. {
  771. if (bx_is_api()){
  772. return [bx_api_get_block('lang', ['title' => _t($aBlock['title']), 'content' => bx_process_macros(_t(trim($aBlock['content'])))])];
  773. }
  774. $s = '<div class="bx-page-lang-container">' . _t(trim($aBlock['content'])) . '</div>';
  775. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  776. $s = bx_process_macros($s);
  777. return $s;
  778. }
  779. /**
  780. * Get content for 'image' block type.
  781. * @return string
  782. */
  783. protected function _getBlockImage ($aBlock)
  784. {
  785. if (empty($aBlock['content']))
  786. return false;
  787. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  788. $iFileId = (int)$iFileId;
  789. if (!$iFileId)
  790. return false;
  791. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  792. if (!$oStorage)
  793. return false;
  794. $sUrl = $oStorage->getFileUrlById($iFileId);
  795. if (!$sUrl)
  796. return false;
  797. $sStyleAdd = '';
  798. if ('center' == $sAlign || 'left' == $sAlign || 'right' == $sAlign)
  799. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  800. return '<div class="bx-page-image-container" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  801. }
  802. /**
  803. * Get content for 'rss' block type.
  804. * @return string
  805. */
  806. protected function _getBlockRss ($aBlock)
  807. {
  808. if (empty($aBlock['content']))
  809. return false;
  810. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  811. $iNum = (int)$iNum;
  812. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  813. }
  814. /**
  815. * Get content for 'menu' block type.
  816. * @return string
  817. */
  818. protected function _getBlockMenu ($aBlock)
  819. {
  820. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  821. return $oMenu ? $oMenu->getCode () : '';
  822. }
  823. /**
  824. * Get content for 'service' block type.
  825. * @return string
  826. */
  827. protected function _getBlockService ($aBlock)
  828. {
  829. $aMarkers = array_merge($this->_aMarkers, [
  830. 'block_id' => $aBlock['id']
  831. ]);
  832. return BxDolService::callSerialized($aBlock['content'], $aMarkers);
  833. }
  834. /**
  835. * Get page title.
  836. * @return string
  837. */
  838. protected function _getPageTitle()
  839. {
  840. return $this->_replaceMarkers(_t($this->_aObject['title']));
  841. }
  842. /**
  843. * Get page meta description.
  844. * @return string
  845. */
  846. protected function _getPageMetaDesc()
  847. {
  848. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  849. }
  850. /**
  851. * Get page meta image.
  852. * @return string
  853. */
  854. protected function _getPageMetaImage()
  855. {
  856. return '';
  857. }
  858. /**
  859. * Get page meta keywords.
  860. * @return string
  861. */
  862. protected function _getPageMetaKeywords()
  863. {
  864. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  865. }
  866. /**
  867. * Get page meta robots.
  868. * @return string
  869. */
  870. protected function _getPageMetaRobots()
  871. {
  872. return _t($this->_aObject['meta_robots']);
  873. }
  874. /**
  875. * Get access denied message HTML.
  876. * @return string
  877. */
  878. protected function _getPageAccessDeniedMsg ()
  879. {
  880. return MsgBox(_t('_Access denied'));
  881. }
  882. /**
  883. * Get page cache object.
  884. * @return cache object instance
  885. */
  886. protected function _getPageCacheObject ()
  887. {
  888. return $this->_getCacheObject ();
  889. }
  890. /**
  891. * Get block cache object.
  892. * @return cache object instance
  893. */
  894. protected function _getBlockCacheObject ()
  895. {
  896. return $this->_getCacheObject ('Block', 'pb');
  897. }
  898. protected function _getCacheObject ($sSuffixObj = 'Page', $sSuffixParam = 'page')
  899. {
  900. $sObj = '_o' . $sSuffixObj . 'CacheObject';
  901. $sParam = 'sys_' . $sSuffixParam . '_cache_engine';
  902. if ($this->{$sObj} != null) {
  903. return $this->{$sObj};
  904. } else {
  905. $sEngine = getParam($sParam);
  906. $this->{$sObj} = bx_instance ('BxDolCache' . $sEngine);
  907. if (!$this->{$sObj}->isAvailable())
  908. $this->{$sObj} = bx_instance ('BxDolCacheFile');
  909. return $this->{$sObj};
  910. }
  911. }
  912. /**
  913. * Get page cache key.
  914. * @param $isPrefixOnly return key prefix only.
  915. * @return string
  916. */
  917. protected function _getPageCacheKey ($isPrefixOnly = false)
  918. {
  919. $s = 'page_' . $this->_aObject['object'] . '_';
  920. if ($isPrefixOnly)
  921. return $s;
  922. $s .= $this->_getPageCacheParams ();
  923. $s .= bx_site_hash() . '.php';
  924. return $s;
  925. }
  926. /**
  927. * Get block cache key.
  928. * @param $isPrefixOnly return key prefix only.
  929. * @return string
  930. */
  931. protected function _getBlockCacheKey ($isPrefixOnly = false, $aBlock = array())
  932. {
  933. $s = $this->_getPageCacheKey(true) . 'block_';
  934. if ($isPrefixOnly)
  935. return $s;
  936. $s .= ($aBlock ? $aBlock['id'] : 0) . '_' . $this->_getPageCacheParams ();
  937. $s .= bx_site_hash() . '.php';
  938. return $s;
  939. }
  940. /**
  941. * Additional cache key. In the case of dynamic page.
  942. * For example - profile page, where each profile has own page.
  943. * @return string
  944. */
  945. protected function _getPageCacheParams ()
  946. {
  947. return md5(serialize($_GET) . bx_lang_name());
  948. }
  949. /**
  950. * Clean page cache.
  951. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  952. * @return string
  953. */
  954. protected function cleanCache ($isDelAllWithPagePrefix = false)
  955. {
  956. $a = ['Page', 'Block'];
  957. $bRet = false;
  958. foreach ($a as $s) {
  959. $oCache = $this->{'_get' . $s . 'CacheObject'} ();
  960. $sKey = $this->{'_get'. $s . 'CacheKey'} ($isDelAllWithPagePrefix);
  961. if ($isDelAllWithPagePrefix)
  962. $bRet = $bRet && $oCache->removeAllByPrefix($sKey);
  963. else
  964. $bRet = $bRet && $oCache->delData($sKey);
  965. }
  966. return $bRet;
  967. }
  968. }
  969. /** @} */