BxBasePage.php 40 KB

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