BxBasePage.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. public function __construct ($aObject, $oTemplate)
  20. {
  21. parent::__construct ($aObject);
  22. if ($oTemplate)
  23. $this->_oTemplate = $oTemplate;
  24. else
  25. $this->_oTemplate = BxDolTemplate::getInstance();
  26. $this->_sStorage = 'sys_images';
  27. }
  28. /**
  29. * Get page code with automatic caching, adding necessary css/js files and system template vars.
  30. * @return string.
  31. */
  32. public function getCode ()
  33. {
  34. if (bx_get('dynamic') && ($iBlockId = (int)bx_get('pageBlock'))) {
  35. if (!$this->_isVisiblePage($this->_aObject)) {
  36. header('HTTP/1.0 403 Forbidden');
  37. exit;
  38. }
  39. bx_alert('system', 'page_output_block', 0, false, array(
  40. 'page_name' => $this->_sObject,
  41. 'page_object' => $this,
  42. 'page_query' => $this->_oQuery,
  43. 'block_id' => (int)$iBlockId,
  44. ));
  45. header( 'Content-type:text/html;charset=utf-8' );
  46. echo $this->_getBlockOnlyCode($iBlockId);
  47. exit;
  48. }
  49. if (!$this->_isVisiblePage($this->_aObject))
  50. return $this->_getPageAccessDeniedMsg ();
  51. $this->_addJsCss();
  52. $this->_addSysTemplateVars();
  53. $this->_selectMenu();
  54. if (!getParam('sys_page_cache_enable') || !$this->_aObject['cache_lifetime'])
  55. return $this->_getPageCode();
  56. $oCache = $this->_getPageCacheObject();
  57. $sKey = $this->_getPageCacheKey();
  58. $mixedRet = $oCache->getData($sKey, $this->_aObject['cache_lifetime']);
  59. if ($mixedRet !== null) {
  60. return $mixedRet;
  61. } else {
  62. $sPageCode = $this->_getPageCode();
  63. $oCache->setData($sKey, $sPageCode, $this->_aObject['cache_lifetime']);
  64. }
  65. bx_alert('system', 'page_output', 0, false, array(
  66. 'page_name' => $this->_sObject,
  67. 'page_object' => $this,
  68. 'page_query' => $this->_oQuery,
  69. 'page_code' => &$sPageCode,
  70. ));
  71. return $sPageCode;
  72. }
  73. /**
  74. * Is page cover enabled.
  75. * @return string
  76. */
  77. public function isPageCover()
  78. {
  79. return (int)$this->_aObject['cover'] == 1;
  80. }
  81. public function setPageCover($bCover = true)
  82. {
  83. $this->_aObject['cover'] = (bool)$bCover;
  84. }
  85. public function getPageCoverImage($bTranscoder = true)
  86. {
  87. $iId = (int)$this->_aObject['cover_image'];
  88. if(empty($iId)) {
  89. $iId = (int)getParam('sys_site_cover_common');
  90. if(empty($iId))
  91. return array();
  92. }
  93. $aResult = array(
  94. 'id' => $iId
  95. );
  96. if($bTranscoder)
  97. $aResult['transcoder'] = BX_DOL_TRANSCODER_OBJ_COVER;
  98. else
  99. $aResult['object'] = $this->_sStorage;
  100. return $aResult;
  101. }
  102. public function getPageCoverParams()
  103. {
  104. $aParams = BxDolMenu::getObjectInstance('sys_site_submenu')->getPageCoverParams();
  105. if(!empty($aParams) && is_array($aParams))
  106. return $aParams;
  107. return array (
  108. 'title' => $this->_getPageTitle(),
  109. 'actions' => '',
  110. 'bx_if:image' => array (
  111. 'condition' => false,
  112. 'content' => array(),
  113. ),
  114. 'bx_if:icon' => array (
  115. 'condition' => false,
  116. 'content' => array(),
  117. ),
  118. );
  119. }
  120. /**
  121. * Get block title.
  122. * @return string
  123. */
  124. public function getBlockTitle ($aBlock)
  125. {
  126. return $this->_replaceMarkers(_t($aBlock['title']));
  127. }
  128. /**
  129. * Get page code only.
  130. * @return string
  131. */
  132. protected function _getPageCode ()
  133. {
  134. $aHiddenOn = array(
  135. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  136. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  137. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide'
  138. );
  139. $aVars = array (
  140. 'page_id' => 'bx-page-' . $this->_aObject['uri'],
  141. );
  142. $aBlocks = $this->_oQuery->getPageBlocks();
  143. foreach ($aBlocks as $sKey => $aCell) {
  144. $sCell = '';
  145. foreach ($aCell as $aBlock) {
  146. $sContentWithBox = $this->_getBlockCode($aBlock);
  147. $sHiddenOn = '';
  148. if(!empty($aBlock['hidden_on']))
  149. foreach($aHiddenOn as $iHiddenOn => $sClass)
  150. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  151. $sHiddenOn .= ' ' . $sClass;
  152. if ($sContentWithBox)
  153. $sCell .= '<div class="bx-page-block-container bx-def-padding-sec-topbottom' . $sHiddenOn . '" id="bx-page-block-' . $aBlock['id'] . '">' . $sContentWithBox . '</div>';
  154. }
  155. $aVars[$sKey] = $sCell;
  156. }
  157. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  158. }
  159. /**
  160. * Get one block code only.
  161. * @return string
  162. */
  163. protected function _getBlockOnlyCode ($iBlockId)
  164. {
  165. $aBlock = $this->_oQuery->getPageBlock((int)$iBlockId);
  166. return $this->_getBlockCode($aBlock);
  167. }
  168. /**
  169. * Get block code.
  170. * @return string
  171. */
  172. protected function _getBlockCode($aBlock)
  173. {
  174. $sContentWithBox = '';
  175. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  176. if ($this->_isVisibleBlock($aBlock)) {
  177. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  178. $mixedContent = $this->$sFunc($aBlock);
  179. $sTitle = $this->getBlockTitle($aBlock);
  180. if (is_array($mixedContent) && !empty($mixedContent['content'])) {
  181. $sContentWithBox = DesignBoxContent(
  182. isset($mixedContent['title']) ? $mixedContent['title'] : $sTitle,
  183. $mixedContent['content'],
  184. isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'],
  185. isset($mixedContent['menu']) ? $mixedContent['menu'] : false
  186. );
  187. }
  188. elseif (is_string($mixedContent) && !empty($mixedContent)) {
  189. $sContentWithBox = DesignBoxContent($sTitle, $mixedContent, $aBlock['designbox_id']);
  190. }
  191. }
  192. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  193. return $sContentWithBox;
  194. }
  195. /**
  196. * Add necessary js and css files.
  197. */
  198. protected function _addJsCss()
  199. {
  200. $this->_oTemplate->addCss('page_layouts.css');
  201. }
  202. /**
  203. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  204. */
  205. protected function _addSysTemplateVars ()
  206. {
  207. $sPageTitle = $this->_getPageTitle();
  208. if ($sPageTitle)
  209. BxDolTemplate::getInstance()->setPageHeader ($sPageTitle);
  210. $sMetaDesc = $this->_getPageMetaDesc();
  211. if ($sMetaDesc)
  212. BxDolTemplate::getInstance()->setPageDescription ($sMetaDesc);
  213. $sMetaKeywords = $this->_getPageMetaKeywords();
  214. if ($sMetaKeywords)
  215. BxDolTemplate::getInstance()->addPageKeywords ($sMetaKeywords);
  216. $sMetaRobots = $this->_getPageMetaRobots();
  217. if ($sMetaRobots)
  218. BxDolTemplate::getInstance()->setPageMetaRobots ($sMetaRobots);
  219. }
  220. /**
  221. * Select menu from page properties.
  222. */
  223. protected function _selectMenu ()
  224. {
  225. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  226. }
  227. /**
  228. * Get content for 'raw' block type.
  229. * @return string
  230. */
  231. protected function _getBlockRaw ($aBlock)
  232. {
  233. return '<div class="bx-page-raw-container">' . $this->_replaceMarkers(BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())) . '</div>';
  234. }
  235. /**
  236. * Get content for 'html' block type.
  237. * @return string
  238. */
  239. protected function _getBlockHtml ($aBlock)
  240. {
  241. return '<div class="bx-page-html-container">' . $this->_replaceMarkers($aBlock['content']) . '</div>';
  242. }
  243. /**
  244. * Get content for 'lang' block type.
  245. * @return string
  246. */
  247. protected function _getBlockLang ($aBlock)
  248. {
  249. return '<div class="bx-page-lang-container">' . $this->_replaceMarkers(_t(trim($aBlock['content']))) . '</div>';
  250. }
  251. /**
  252. * Get content for 'image' block type.
  253. * @return string
  254. */
  255. protected function _getBlockImage ($aBlock)
  256. {
  257. if (empty($aBlock['content']))
  258. return false;
  259. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  260. $iFileId = (int)$iFileId;
  261. if (!$iFileId)
  262. return false;
  263. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  264. if (!$oStorage)
  265. return false;
  266. $sUrl = $oStorage->getFileUrlById($iFileId);
  267. if (!$sUrl)
  268. return false;
  269. $sStyleAdd = '';
  270. if ('center' == $sAlign || 'left' == $sAlign || 'right' == $sAlign)
  271. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  272. return '<div class="bx-page-image-container" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  273. }
  274. /**
  275. * Get content for 'rss' block type.
  276. * @return string
  277. */
  278. protected function _getBlockRss ($aBlock)
  279. {
  280. if (empty($aBlock['content']))
  281. return false;
  282. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  283. $iNum = (int)$iNum;
  284. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  285. }
  286. /**
  287. * Get content for 'menu' block type.
  288. * @return string
  289. */
  290. protected function _getBlockMenu ($aBlock)
  291. {
  292. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  293. return $oMenu ? $oMenu->getCode () : '';
  294. }
  295. /**
  296. * Get content for 'service' block type.
  297. * @return string
  298. */
  299. protected function _getBlockService ($aBlock)
  300. {
  301. return BxDolService::callSerialized($aBlock['content'], $this->_aMarkers);
  302. }
  303. /**
  304. * Get page title.
  305. * @return string
  306. */
  307. protected function _getPageTitle()
  308. {
  309. return $this->_replaceMarkers(_t($this->_aObject['title']));
  310. }
  311. /**
  312. * Get page meta description.
  313. * @return string
  314. */
  315. protected function _getPageMetaDesc()
  316. {
  317. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  318. }
  319. /**
  320. * Get page meta keywords.
  321. * @return string
  322. */
  323. protected function _getPageMetaKeywords()
  324. {
  325. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  326. }
  327. /**
  328. * Get page meta robots.
  329. * @return string
  330. */
  331. protected function _getPageMetaRobots()
  332. {
  333. return _t($this->_aObject['meta_robots']);
  334. }
  335. /**
  336. * Get access denied message HTML.
  337. * @return string
  338. */
  339. protected function _getPageAccessDeniedMsg ()
  340. {
  341. return MsgBox(_t('_Access denied'));
  342. }
  343. /**
  344. * Get page cache object.
  345. * @return cache object instance
  346. */
  347. protected function _getPageCacheObject ()
  348. {
  349. if ($this->_oPageCacheObject != null) {
  350. return $this->_oPageCacheObject;
  351. } else {
  352. $sEngine = getParam('sys_page_cache_engine');
  353. $this->_oPageCacheObject = bx_instance ('BxDolCache' . $sEngine);
  354. if (!$this->_oPageCacheObject->isAvailable())
  355. $this->_oPageCacheObject = bx_instance ('BxDolCacheFile');
  356. return $this->_oPageCacheObject;
  357. }
  358. }
  359. /**
  360. * Get page cache key.
  361. * @param $isPrefixOnly return key prefix only.
  362. * @return string
  363. */
  364. protected function _getPageCacheKey ($isPrefixOnly = false)
  365. {
  366. $s = 'page_' . $this->_aObject['object'] . '_';
  367. if ($isPrefixOnly)
  368. return $s;
  369. $s .= $this->_getPageCacheParams ();
  370. $s .= bx_site_hash() . '.php';
  371. return $s;
  372. }
  373. /**
  374. * Additional cache key. In the case of dynamic page.
  375. * For example - profile page, where each profile has own page.
  376. * @return string
  377. */
  378. protected function _getPageCacheParams ()
  379. {
  380. return '';
  381. }
  382. /**
  383. * Clean page cache.
  384. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  385. * @return string
  386. */
  387. protected function cleanCache ($isDelAllWithPagePrefix = false)
  388. {
  389. $oCache = $this->_getPageCacheObject ();
  390. $sKey = $this->_getPageCacheKey($isDelAllWithPagePrefix);
  391. if ($isDelAllWithPagePrefix)
  392. return $oCache->removeAllByPrefix($sKey);
  393. else
  394. return $oCache->delData($sKey);
  395. }
  396. }
  397. /** @} */