BxBasePage.php 39 KB

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