BxBasePage.php 40 KB

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