BxBasePage.php 40 KB

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