BxBasePage.php 40 KB

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