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. parse_str($query_string, $_GET);
  347. }
  348. $a = [
  349. 'id' => $this->_aObject['id'],
  350. 'title' => $this->_getPageTitle(),
  351. 'uri' => $this->_aObject['uri'],
  352. 'url' => bx_get('params')[0] . ($query_string != '' ? '?' . $query_string : ''),
  353. 'author' => $this->_aObject['author'],
  354. 'added' => $this->_aObject['added'],
  355. 'module' => $this->getModule(),
  356. 'type' => $this->getType (),
  357. 'layout' => str_replace('.html', '', $this->_aObject['template']),
  358. 'cover_block' => '',
  359. 'menu_top' => '',
  360. 'menu' => '',
  361. 'menu_bottom' => '',
  362. 'menu_add' => '',
  363. 'elements' => $this->getPageBlocksAPI($aBlocks),
  364. ];
  365. if (BxDolCover::getInstance($this)->isCover() && isset($this->_aProfileInfo)) {
  366. $oModule = BxDolModule::getInstance($this->getModule());
  367. $a['cover_block'] = [
  368. 'profile' => BxDolProfile::getData($this->_aProfileInfo['id'], [
  369. 'get_avatar' => 'getAvatarBig',
  370. 'with_info' => true
  371. ]),
  372. 'actions_menu' => '',
  373. 'meta_menu' => '',
  374. 'cover' => $oModule->serviceGetCover($this->_aProfileInfo['content_id']),
  375. 'allow_edit' => $oModule->checkAllowedChangeCover($this->_aProfileInfo['id']) === CHECK_ACTION_RESULT_ALLOWED
  376. ];
  377. $CNF = $oModule->_oConfig->CNF;
  378. if(!empty($CNF['OBJECT_MENU_VIEW_ENTRY_META'])){
  379. $oMetaMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_VIEW_ENTRY_META']);
  380. $a['cover_block']['meta_menu'] = $oMetaMenu->getCodeAPI();
  381. }
  382. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  383. if ($oConnection){
  384. $a['cover_block']['members_list'] = $oConnection->getConnectedListAPI($this->_aProfileInfo['id'], false, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS, 10);
  385. }
  386. if(!empty($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY'])){
  387. $oActionMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_ALL']);
  388. $a['cover_block']['actions_menu'] = $oActionMenu->getCodeAPI();
  389. }
  390. }
  391. $sMenuTop = getParam('sys_api_menu_top');
  392. if (!empty($sMenuTop) && ($oMenuTop = BxDolMenu::getObjectInstance($sMenuTop)) !== false)
  393. $a['menu_top'] = $oMenuTop->getCodeAPI();
  394. if (($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  395. if(($sSubmenu = $this->getSubMenu()) !== '')
  396. $oMenuSubmenu->setObjectSubmenu($sSubmenu, []);
  397. $a['menu'] = $oMenuSubmenu->getCodeAPI();
  398. }
  399. if (isLogged()) {
  400. /* temporatery commented
  401. if(($oMenuAddContent = BxDolMenu::getObjectInstance('sys_add_content')) !== false)
  402. $a['menu_add'] = $oMenuAddContent->getCodeAPI();
  403. */
  404. $o = BxDolProfile::getInstance();
  405. $a['user'] = [
  406. 'id' => $o->id(),
  407. 'email' => $o->getAccountObject()->getEmail(),
  408. 'display_name' => $o->getDisplayName(),
  409. 'url' => bx_api_get_relative_url($o->getUrl()),
  410. 'avatar' => $o->getAvatar(),
  411. 'info' => $o->getInfo(),
  412. 'notifications' => 0,
  413. 'cart' => 0,
  414. 'active' => $o->isActive(),
  415. 'status' => $o->getStatus(),
  416. ];
  417. $oInformer = BxDolInformer::getInstance(BxDolTemplate::getInstance());
  418. $sRet = $oInformer ? $oInformer->display() : '';
  419. if ($sRet){
  420. $a['user']['informer'] = $sRet;
  421. }
  422. $sModuleNotifications = 'bx_notifications';
  423. if(BxDolRequest::serviceExists($sModuleNotifications, 'get_unread_notifications_num'))
  424. $a['user']['notifications'] = bx_srv($sModuleNotifications, 'get_unread_notifications_num', [$o->id()]);
  425. $oPayments = BxDolPayments::getInstance();
  426. if($oPayments->isActive())
  427. $a['user']['cart'] = $oPayments->getCartItemsCount();
  428. }
  429. return $a;
  430. }
  431. public function getPageBlocksAPI($aBlocks = [])
  432. {
  433. $bBlocks = !empty($aBlocks) && is_array($aBlocks);
  434. $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'];
  435. $aCells = $this->_oQuery->getPageBlocks(true);
  436. foreach($aCells as $sKey => &$aCell) {
  437. foreach($aCell as $i => $aBlock) {
  438. if(!$this->_isVisibleBlock($aBlock)) {
  439. unset($aCells[$sKey][$i]);
  440. continue;
  441. }
  442. $this->processPageBlock($aCells[$sKey][$i], true);
  443. $aBlock = $aCells[$sKey][$i];
  444. $sSource = '';
  445. if($aBlock['type'] == 'service') {
  446. $aContent = @unserialize($aBlock['content']);
  447. if(isset($aContent['module'], $aContent['method']))
  448. $sSource = $aContent['module'] . ':' . $aContent['method'];
  449. }
  450. else{
  451. $sSource = 'system:block_' . $aBlock['id'];
  452. }
  453. if($bBlocks && !in_array($sSource, $aBlocks)) {
  454. unset($aCells[$sKey][$i]);
  455. continue;
  456. }
  457. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  458. $mBlock = method_exists($this, $sFunc) ? $this->$sFunc($aBlock) : $aBlock['content'];
  459. $aCells[$sKey][$i] = array_merge($aCells[$sKey][$i], [
  460. 'title' => isset($mBlock['title']) ? $mBlock['title'] : $this->getBlockTitle($aBlock),
  461. 'content' => isset($mBlock['content']) ? $mBlock['content'] : $mBlock,
  462. 'menu' => isset($mBlock['menu']) ? $mBlock['menu'] : '',
  463. 'source' => $sSource
  464. ]);
  465. $aCells[$sKey][$i] = array_diff_key($aCells[$sKey][$i], array_flip($aFieldsUnset));
  466. }
  467. }
  468. return array_map('array_values', $aCells);
  469. }
  470. /**
  471. * Get page code vars
  472. * @return string
  473. */
  474. protected function _getPageCodeVars ()
  475. {
  476. $aHiddenOn = array(
  477. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  478. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  479. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  480. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  481. );
  482. $aVars = array (
  483. 'page_id' => 'bx-page-' . $this->_aObject['uri'],
  484. 'bx_if:show_layout_row_dump' => array(
  485. 'condition' => false,
  486. 'content' => array()
  487. )
  488. );
  489. $aBlocks = $this->_oQuery->getPageBlocks();
  490. foreach ($aBlocks as $sKey => $aCell) {
  491. $sCell = '';
  492. foreach ($aCell as $aBlock) {
  493. $this->processPageBlock($aBlock, false);
  494. $sContentWithBox = $this->_getBlockCodeWithCache($aBlock, $aBlock['async']);
  495. $sClassAdd = '';
  496. if(!empty($aBlock['class']))
  497. $sClassAdd .= ' ' . $aBlock['class'];
  498. if(!empty($aBlock['hidden_on']))
  499. foreach($aHiddenOn as $iHiddenOn => $sHiddenOnClass)
  500. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  501. $sClassAdd .= ' ' . $sHiddenOnClass;
  502. if ($sContentWithBox)
  503. $sCell .= $this->_oTemplate->parseHtmlByName('designbox_container.html', array(
  504. 'class_add' => $sClassAdd,
  505. 'bx_if:show_html_id' => array(
  506. 'condition' => true,
  507. 'content' => array(
  508. 'html_id' => 'bx-page-block-' . $aBlock['id']
  509. ),
  510. ),
  511. 'content' => $sContentWithBox
  512. ));
  513. }
  514. $aVars[$sKey] = $sCell;
  515. }
  516. return $aVars;
  517. }
  518. /**
  519. * Process block values, especially if someting need to be overrided
  520. */
  521. protected function processPageBlock(&$aBlock, $bApi = false)
  522. {
  523. }
  524. /**
  525. * Get page code only.
  526. * @return string
  527. */
  528. protected function _getPageCode ()
  529. {
  530. $aVars = $this->_getPageCodeVars ();
  531. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  532. }
  533. /**
  534. * Get one block code only.
  535. * @return string
  536. */
  537. protected function _getBlockOnlyCode ($iBlockId)
  538. {
  539. if (!($aBlock = $this->_oQuery->getPageBlock((int)$iBlockId)))
  540. return '';
  541. return $this->_getBlockCodeWithCache($aBlock, 0);
  542. }
  543. protected function _getBlockCodeWithCache(&$aBlock, $iAsync = 0)
  544. {
  545. if (bx_is_dbg() || !getParam('sys_pb_cache_enable') || !$aBlock['cache_lifetime'] || $iAsync || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  546. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  547. }
  548. else {
  549. $oCache = $this->_getBlockCacheObject();
  550. $sKey = $this->_getBlockCacheKey(false, $aBlock);
  551. $sKeyCssJs = 'cssjs_' . $sKey;
  552. $mixedRet = $oCache->getData($sKey, $aBlock['cache_lifetime']);
  553. $aRetCssJs = $oCache->getData($sKeyCssJs, $aBlock['cache_lifetime']);
  554. if ($mixedRet !== null && $aRetCssJs !== null) {
  555. BxDolTemplate::getInstance()->collectingStart();
  556. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  557. $sBlockCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  558. $sBlockCode .= $mixedRet;
  559. } else {
  560. BxDolTemplate::getInstance()->collectingStart();
  561. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  562. $aBlockCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  563. $oCache->setData($sKey, $sBlockCode, $aBlock['cache_lifetime']);
  564. $oCache->setData($sKeyCssJs, $aBlockCssJs, $aBlock['cache_lifetime']);
  565. }
  566. }
  567. return $sBlockCode;
  568. }
  569. /**
  570. * Get block code.
  571. * @return string
  572. */
  573. protected function _getBlockCode(&$aBlock, $iAsync = 0)
  574. {
  575. $sContentWithBox = '';
  576. $oFunctions = $this->_oTemplate->getTemplateFunctions();
  577. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  578. $aDbNoTitle = [BX_DB_CONTENT_ONLY, BX_DB_PADDING_CONTENT_ONLY, BX_DB_NO_CAPTION, BX_DB_PADDING_NO_CAPTION];
  579. $sTitle = $this->getBlockTitle($aBlock);
  580. $sHelp = $this->getBlockHelp($aBlock);
  581. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  582. $bBlockVisible = $this->_isVisibleBlock($aBlock);
  583. if ($iAsync && $bBlockVisible) {
  584. $iDesignboxId = $aBlock['designbox_id'];
  585. $sHelpTitle = $sHelpContent = '';
  586. if(!in_array($iDesignboxId, $aDbNoTitle))
  587. $sHelpTitle = $sHelp;
  588. else
  589. $sHelpContent = $sHelp;
  590. $sContent = $this->getBlockAsyncCode($aBlock, $iAsync);
  591. $aParams = array(
  592. $sTitle . $sHelpTitle,
  593. $sContent . $sHelpContent,
  594. $iDesignboxId
  595. );
  596. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  597. }
  598. elseif ($bBlockVisible && method_exists($this, $sFunc)) {
  599. $mixedContent = $this->$sFunc($aBlock);
  600. if(!empty($aBlock['content_empty'])) {
  601. $sEmpty = _t($aBlock['content_empty']);
  602. $sRegExp = '/(<[A-Za-z0-9]+\b.[^<]*[\pPi\pPf]bx\-msg\-box\b.*[\pPi\pPf][^>]*>)(' . _t('_Empty') . ')(<\/[A-Za-z0-9]+>)/su';
  603. if(is_string($mixedContent))
  604. $mixedContent = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent);
  605. else if(is_array($mixedContent) && !empty($mixedContent['content']))
  606. $mixedContent['content'] = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent['content']);
  607. }
  608. $this->_oQuery->setReadOnlyMode(true);
  609. if(is_array($mixedContent) && !empty($mixedContent['content'])) {
  610. $iDesignboxId = isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'];
  611. $sHelpTitle = $sHelpContent = '';
  612. if(!in_array($iDesignboxId, $aDbNoTitle))
  613. $sHelpTitle = $sHelp;
  614. else
  615. $sHelpContent = $sHelp;
  616. $aParams = array(
  617. (isset($mixedContent['title']) ? $mixedContent['title'] : $sTitle) . $sHelpTitle,
  618. $mixedContent['content'] . $sHelpContent,
  619. $iDesignboxId
  620. );
  621. $mixedMenu = false;
  622. if(isset($mixedContent['menu']))
  623. $mixedMenu = $mixedContent['menu'];
  624. else if(!empty($aBlock['submenu']))
  625. $mixedMenu = $aBlock['submenu'];
  626. $aParams[] = $mixedMenu;
  627. if(isset($mixedContent['buttons']))
  628. $aParams[] = $mixedContent['buttons'];
  629. else if($mixedMenu)
  630. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  631. if(isset($mixedContent['class']))
  632. $aBlock['class'] = $mixedContent['class'];
  633. }
  634. else if(is_string($mixedContent) && !empty($mixedContent)) {
  635. $iDesignboxId = $aBlock['designbox_id'];
  636. $sHelpTitle = $sHelpContent = '';
  637. if(!in_array($iDesignboxId, $aDbNoTitle))
  638. $sHelpTitle = $sHelp;
  639. else
  640. $sHelpContent = $sHelp;
  641. $aParams = array(
  642. $sTitle . $sHelpTitle,
  643. $mixedContent . $sHelpContent,
  644. $iDesignboxId
  645. );
  646. $mixedMenu = !empty($aBlock['submenu']) ? $aBlock['submenu'] : false;
  647. $aParams[] = $mixedMenu;
  648. if($mixedMenu)
  649. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  650. }
  651. if(!empty($aParams))
  652. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  653. }
  654. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  655. return $sContentWithBox;
  656. }
  657. /**
  658. * Add necessary js and css files.
  659. */
  660. protected function _addJsCss()
  661. {
  662. $this->_oTemplate->addJs(array('BxDolPage.js', 'theia-sticky-sidebar/theia-sticky-sidebar.min.js', 'theia-sticky-sidebar/ResizeSensor.min.js'));
  663. $this->_oTemplate->addCss('page_layouts.css');
  664. }
  665. /**
  666. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  667. */
  668. protected function _addSysTemplateVars ()
  669. {
  670. $oTemplate = BxDolTemplate::getInstance();
  671. $sPageTitle = $this->_getPageTitle();
  672. if ($sPageTitle)
  673. $oTemplate->setPageHeader ($sPageTitle);
  674. $sMetaDesc = $this->_getPageMetaDesc();
  675. if ($sMetaDesc)
  676. $oTemplate->setPageDescription ($sMetaDesc);
  677. $sMetaRobots = $this->_getPageMetaRobots();
  678. if ($sMetaRobots)
  679. $oTemplate->setPageMetaRobots ($sMetaRobots);
  680. $sMetaImage = $this->_getPageMetaImage();
  681. if ($sMetaImage)
  682. $oTemplate->addPageMetaImage($sMetaImage);
  683. $sMetaKeywords = $this->_getPageMetaKeywords();
  684. if ($sMetaKeywords)
  685. $oTemplate->addPageKeywords ($sMetaKeywords);
  686. }
  687. /**
  688. * Select menu from page properties.
  689. */
  690. protected function _selectMenu ()
  691. {
  692. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  693. }
  694. /**
  695. * Set page submenu if it's specified
  696. */
  697. protected function _setSubmenu ($aParams)
  698. {
  699. if(empty($this->_aObject['submenu']) || $this->_aObject['submenu'] == 'disabled')
  700. return;
  701. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  702. if(!$oMenuSubmenu)
  703. return;
  704. $oMenuSubmenu->setObjectSubmenu($this->_aObject['submenu'], $aParams);
  705. }
  706. /**
  707. * Get content for 'raw' block type.
  708. * @return string
  709. */
  710. protected function _getBlockRaw ($aBlock)
  711. {
  712. if (bx_is_api()){
  713. return [bx_api_get_block('raw', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  714. }
  715. $s = '<div class="bx-page-raw-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  716. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  717. $s = bx_process_macros($s);
  718. return $s;
  719. }
  720. /**
  721. * Get content for 'custom' block type.
  722. * @return string
  723. */
  724. protected function _getBlockCustom ($aBlock)
  725. {
  726. if (bx_is_api()){
  727. return [bx_api_get_block('custom', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  728. }
  729. $s = '<div class="bx-page-custom-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  730. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  731. $s = bx_process_macros($s);
  732. return $s;
  733. }
  734. /**
  735. * Get content for 'html' block type.
  736. * @return string
  737. */
  738. protected function _getBlockHtml ($aBlock)
  739. {
  740. if (bx_is_api()){
  741. return [bx_api_get_block('html', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  742. }
  743. $s = '<div class="bx-page-html-container bx-def-vanilla-html max-w-none">' . $aBlock['content'] . '</div>';
  744. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  745. $s = bx_process_macros($s);
  746. return $s;
  747. }
  748. /**
  749. * Get content for 'wiki' block type.
  750. * @return string
  751. */
  752. protected function _getBlockWiki ($aBlock)
  753. {
  754. $oWiki = BxDolWiki::getObjectInstance($this->_aObject['module']);
  755. if (!$oWiki) {
  756. $sContent = _t('_sys_wiki_error_missing_wiki_object', $this->_aObject['module']);
  757. }
  758. else {
  759. $sContent = $oWiki->getBlockContent($aBlock['id'], false, (int)bx_get($aBlock['id'].'rev') ? (int)bx_get($aBlock['id'].'rev') : false);
  760. }
  761. $s = '<div id="bx-page-wiki-container-' . $aBlock['id'] . '" class="bx-page-wiki-container markdown-body bx-def-vanilla-html">' . $sContent . '</div>';
  762. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  763. $s = bx_process_macros($s);
  764. return $s;
  765. }
  766. /**
  767. * Get content for 'lang' block type.
  768. * @return string
  769. */
  770. protected function _getBlockLang ($aBlock)
  771. {
  772. if (bx_is_api()){
  773. return [bx_api_get_block('lang', ['title' => _t($aBlock['title']), 'content' => bx_process_macros(_t(trim($aBlock['content'])))])];
  774. }
  775. $s = '<div class="bx-page-lang-container">' . _t(trim($aBlock['content'])) . '</div>';
  776. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  777. $s = bx_process_macros($s);
  778. return $s;
  779. }
  780. /**
  781. * Get content for 'image' block type.
  782. * @return string
  783. */
  784. protected function _getBlockImage ($aBlock)
  785. {
  786. if (empty($aBlock['content']))
  787. return false;
  788. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  789. $iFileId = (int)$iFileId;
  790. if (!$iFileId)
  791. return false;
  792. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  793. if (!$oStorage)
  794. return false;
  795. $sUrl = $oStorage->getFileUrlById($iFileId);
  796. if (!$sUrl)
  797. return false;
  798. $sStyleAdd = '';
  799. if ('center' == $sAlign || 'left' == $sAlign || 'right' == $sAlign)
  800. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  801. return '<div class="bx-page-image-container" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  802. }
  803. /**
  804. * Get content for 'rss' block type.
  805. * @return string
  806. */
  807. protected function _getBlockRss ($aBlock)
  808. {
  809. if (empty($aBlock['content']))
  810. return false;
  811. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  812. $iNum = (int)$iNum;
  813. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  814. }
  815. /**
  816. * Get content for 'menu' block type.
  817. * @return string
  818. */
  819. protected function _getBlockMenu ($aBlock)
  820. {
  821. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  822. return $oMenu ? $oMenu->getCode () : '';
  823. }
  824. /**
  825. * Get content for 'service' block type.
  826. * @return string
  827. */
  828. protected function _getBlockService ($aBlock)
  829. {
  830. $aMarkers = array_merge($this->_aMarkers, [
  831. 'block_id' => $aBlock['id']
  832. ]);
  833. return BxDolService::callSerialized($aBlock['content'], $aMarkers);
  834. }
  835. /**
  836. * Get page title.
  837. * @return string
  838. */
  839. protected function _getPageTitle()
  840. {
  841. return $this->_replaceMarkers(_t($this->_aObject['title']));
  842. }
  843. /**
  844. * Get page meta description.
  845. * @return string
  846. */
  847. protected function _getPageMetaDesc()
  848. {
  849. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  850. }
  851. /**
  852. * Get page meta image.
  853. * @return string
  854. */
  855. protected function _getPageMetaImage()
  856. {
  857. return '';
  858. }
  859. /**
  860. * Get page meta keywords.
  861. * @return string
  862. */
  863. protected function _getPageMetaKeywords()
  864. {
  865. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  866. }
  867. /**
  868. * Get page meta robots.
  869. * @return string
  870. */
  871. protected function _getPageMetaRobots()
  872. {
  873. return _t($this->_aObject['meta_robots']);
  874. }
  875. /**
  876. * Get access denied message HTML.
  877. * @return string
  878. */
  879. protected function _getPageAccessDeniedMsg ()
  880. {
  881. return MsgBox(_t('_Access denied'));
  882. }
  883. /**
  884. * Get page cache object.
  885. * @return cache object instance
  886. */
  887. protected function _getPageCacheObject ()
  888. {
  889. return $this->_getCacheObject ();
  890. }
  891. /**
  892. * Get block cache object.
  893. * @return cache object instance
  894. */
  895. protected function _getBlockCacheObject ()
  896. {
  897. return $this->_getCacheObject ('Block', 'pb');
  898. }
  899. protected function _getCacheObject ($sSuffixObj = 'Page', $sSuffixParam = 'page')
  900. {
  901. $sObj = '_o' . $sSuffixObj . 'CacheObject';
  902. $sParam = 'sys_' . $sSuffixParam . '_cache_engine';
  903. if ($this->{$sObj} != null) {
  904. return $this->{$sObj};
  905. } else {
  906. $sEngine = getParam($sParam);
  907. $this->{$sObj} = bx_instance ('BxDolCache' . $sEngine);
  908. if (!$this->{$sObj}->isAvailable())
  909. $this->{$sObj} = bx_instance ('BxDolCacheFile');
  910. return $this->{$sObj};
  911. }
  912. }
  913. /**
  914. * Get page cache key.
  915. * @param $isPrefixOnly return key prefix only.
  916. * @return string
  917. */
  918. protected function _getPageCacheKey ($isPrefixOnly = false)
  919. {
  920. $s = 'page_' . $this->_aObject['object'] . '_';
  921. if ($isPrefixOnly)
  922. return $s;
  923. $s .= $this->_getPageCacheParams ();
  924. $s .= bx_site_hash() . '.php';
  925. return $s;
  926. }
  927. /**
  928. * Get block cache key.
  929. * @param $isPrefixOnly return key prefix only.
  930. * @return string
  931. */
  932. protected function _getBlockCacheKey ($isPrefixOnly = false, $aBlock = array())
  933. {
  934. $s = $this->_getPageCacheKey(true) . 'block_';
  935. if ($isPrefixOnly)
  936. return $s;
  937. $s .= ($aBlock ? $aBlock['id'] : 0) . '_' . $this->_getPageCacheParams ();
  938. $s .= bx_site_hash() . '.php';
  939. return $s;
  940. }
  941. /**
  942. * Additional cache key. In the case of dynamic page.
  943. * For example - profile page, where each profile has own page.
  944. * @return string
  945. */
  946. protected function _getPageCacheParams ()
  947. {
  948. return md5(serialize($_GET) . bx_lang_name());
  949. }
  950. /**
  951. * Clean page cache.
  952. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  953. * @return string
  954. */
  955. protected function cleanCache ($isDelAllWithPagePrefix = false)
  956. {
  957. $a = ['Page', 'Block'];
  958. $bRet = false;
  959. foreach ($a as $s) {
  960. $oCache = $this->{'_get' . $s . 'CacheObject'} ();
  961. $sKey = $this->{'_get'. $s . 'CacheKey'} ($isDelAllWithPagePrefix);
  962. if ($isDelAllWithPagePrefix)
  963. $bRet = $bRet && $oCache->removeAllByPrefix($sKey);
  964. else
  965. $bRet = $bRet && $oCache->delData($sKey);
  966. }
  967. return $bRet;
  968. }
  969. }
  970. /** @} */