BxDolPage.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * Pages.
  11. *
  12. * It allows to display pages which are built from studio.
  13. *
  14. * The new system has the following page features:
  15. * - Layouts: page can have any structure, not just columns.
  16. * - SEO: page can have own SEO options, like meta tags and meta keywords, as well as instructions for search bots.
  17. * - Cache: page can be cached on the server.
  18. * - Access control: page access can be controlled using member levels.
  19. *
  20. *
  21. *
  22. * @section page_create Creating the Page object:
  23. *
  24. * 1. add record to 'sys_objects_page' table:
  25. *
  26. * - object: name of the page object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing; for example: bx_profiles_view - profile view page.
  27. * - title: name to display as page title.
  28. * - module: the module this page belongs to.
  29. * - layout_id: page layout to use, this is id of the record from 'sys_pages_layouts' table.
  30. * - visible_for_levels: bit field with set of member level ids. To use member level id in bit field - the level id minus 1 as power of 2 is used, for example:
  31. * - user level id = 1 -> 2^(1-1) = 1
  32. * - user level id = 2 -> 2^(2-1) = 2
  33. * - user level id = 3 -> 2^(3-1) = 4
  34. * - user level id = 4 -> 2^(4-1) = 8
  35. * - visible_for_levels_editable: it determines if 'visible_for_levels' field is editable from page builder, visibility options can be overriden by custom class and shouldn't be editable in this case.
  36. * - url: the page url, if it is static page.
  37. * - meta_description: meta description of the page.
  38. * - meta_keywords: meta keywords of the page.
  39. * - meta_robots: instructions for search bots.
  40. * - cache_lifetime: number of seconds to store cache for.
  41. * - cache_editable: it determines if cache can be edited from page builder.
  42. * - deletable: it determines if page can be deleted from page builder.
  43. * - override_class_name: user defined class name which is derived from BxTemplPage.
  44. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  45. *
  46. * Page can select appropriate menu automatically if 'module' and 'object' fields in 'sys_objects_page' table are matched with 'module' and 'name' fields in 'sys_menu_items' table.
  47. *
  48. *
  49. * Page layout are stored in 'sys_pages_layouts' table:
  50. * - name: inner unique layout name.
  51. * - icon: layout icon to display in page builder, it should represent basic view of the layout to help studio operator determine the layout structure.
  52. * - title: layout name to display in page builder.
  53. * - template: template name to use to display page with certain layout.
  54. * - cells_number: number of areas in the layout, page blocks can be places into this areas(cells).
  55. * To define areas in the layout they should be named as '__cell_N__', where N is cell number, starting from 1.
  56. *
  57. *
  58. * 2. Add page blocks to 'sys_pages_blocks' table:
  59. * - object: page object name this block belongs to.
  60. * - cell_id: cell number in page layout to place block to.
  61. * - module: module name this block belongs to.
  62. * - title: block title.
  63. * - designbox_id: design box to use to diplay page block, it is id of the record from 'sys_pages_design_boxes' table.
  64. * - visible_for_levels: bit field with set of member level ids. To use member level id in bit field - the level id minus 1 as power of 2 is used, for example:
  65. * - user level id = 1 -> 2^(1-1) = 1
  66. * - user level id = 2 -> 2^(2-1) = 2
  67. * - user level id = 3 -> 2^(3-1) = 4
  68. * - user level id = 4 -> 2^(4-1) = 8
  69. * - type: block type
  70. * - raw: HTML block, displayed in page builder as HTML textarea.
  71. * - html: HTML block, displayed in page builder as visual editor, like TinyMCE.
  72. * - lang: translatable language string, displayed in page builder as editable language key.
  73. * - image: just an image, displayed in page builder as HTML upload form.
  74. * - rss: RSS block, displayed in page builder as editable URL to RSS resource, along with number of displayed items.
  75. * - menu: menu block, displayed as menu selector.
  76. * - service: to display block content, the provided service method is used.
  77. * - content: depending on 'type' field:
  78. * - raw: HTML string.
  79. * - html: HTML string.
  80. * - lang: language key.
  81. * - image: image id in the storage and alignment (left, center, right) for example: 36\#center
  82. * - rss: URL to RSS with number of displayed items, for example: http://www.example.com/rss#4
  83. * - menu: menu object name.
  84. * - service: serialized array of service call parameters: module - module name, method - service method name, params - array of parameters.
  85. * - deletable: is block deletable from page builder
  86. * - copyable: is block can be copied to any other page from page builder.
  87. * - order: block order in particular cell.
  88. *
  89. * Block design boxes are stored in 'sys_pages_design_boxes' table:
  90. * - id: consistent id, there are the following defines can be used in the code for each system block style:
  91. * - 0 - BX_DB_CONTENT_ONLY: design box with content only - no borders, no background, no caption.
  92. * - 1 - BX_DB_DEF: default design box with content, borders and caption.
  93. * - 2 - BX_DB_EMPTY: just empty design box, without anything.
  94. * - 3 - BX_DB_NO_CAPTION: design box with content, like BX_DB_DEF but without caption.
  95. * - 10 - BX_DB_PADDING_CONTENT_ONLY: design box with content only wrapped with default padding - no borders, no background, no caption; it can be used to just wrap content with default padding.
  96. * - 11 - BX_DB_PADDING_DEF: default design box with content wrapped with default padding, borders and caption.
  97. * - 13 - BX_DB_PADDING_NO_CAPTION: design box with content wrapped with default padding, like BX_DB_DEF but without caption.
  98. * - title: block name which is displayed in studio, describes block styles.
  99. * - template: template name to use to display page block.
  100. *
  101. *
  102. * 3. Display Page.
  103. * Use the following sample code to display page:
  104. * @code
  105. * $oPage = BxDolPage::getObjectInstance('sample'); // 'sample' is 'object' field from 'sys_objects_page' table, it automatically creates instance of default or custom class by object name
  106. * if ($oPage)
  107. * echo $oPage->getCode(); // print page
  108. * @endcode
  109. *
  110. */
  111. class BxDolPage extends BxDolFactory implements iBxDolFactoryObject, iBxDolReplaceable
  112. {
  113. protected $_sObject;
  114. protected $_aObject;
  115. protected $_oQuery;
  116. protected $_aMarkers = array ();
  117. /**
  118. * Constructor
  119. * @param $aObject array of page options
  120. */
  121. protected function __construct($aObject)
  122. {
  123. parent::__construct();
  124. $this->_sObject = $aObject['object'];
  125. $this->_aObject = $aObject;
  126. $this->_oQuery = new BxDolPageQuery($this->_aObject);
  127. }
  128. /**
  129. * Get page object instance by Module name and page URI
  130. * @param $sModule module name
  131. * @param $sURI unique page URI
  132. * @return object instance or false on error
  133. */
  134. static public function getObjectInstanceByModuleAndURI($sModule, $sURI = '', $oTemplate = false)
  135. {
  136. if(empty($sURI) && bx_get('i') !== false)
  137. $sURI = bx_process_input(bx_get('i'));
  138. if(empty($sURI))
  139. return false;
  140. $sObject = BxDolPageQuery::getPageObjectNameByURI($sURI, $sModule);
  141. return $sObject ? self::getObjectInstance($sObject, $oTemplate) : false;
  142. }
  143. /**
  144. * Get page object instance by page URI
  145. * @param $sURI unique page URI
  146. * @return object instance or false on error
  147. */
  148. static public function getObjectInstanceByURI($sURI = '', $oTemplate = false)
  149. {
  150. if(empty($sURI) && bx_get('i') !== false)
  151. $sURI = bx_process_input(bx_get('i'));
  152. if(empty($sURI))
  153. return false;
  154. $sObject = BxDolPageQuery::getPageObjectNameByURI($sURI);
  155. return $sObject ? self::getObjectInstance($sObject, $oTemplate) : false;
  156. }
  157. /**
  158. * Get page object instance by object name
  159. * @param $sObject object name
  160. * @return object instance or false on error
  161. */
  162. static public function getObjectInstance($sObject, $oTemplate = false)
  163. {
  164. if (isset($GLOBALS['bxDolClasses']['BxDolPage!'.$sObject]))
  165. return $GLOBALS['bxDolClasses']['BxDolPage!'.$sObject];
  166. $aObject = BxDolPageQuery::getPageObject($sObject);
  167. if (!$aObject || !is_array($aObject))
  168. return false;
  169. $sClass = 'BxTemplPage';
  170. if (!empty($aObject['override_class_name'])) {
  171. $sClass = $aObject['override_class_name'];
  172. if (!empty($aObject['override_class_file']))
  173. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  174. }
  175. $o = new $sClass($aObject, $oTemplate);
  176. return ($GLOBALS['bxDolClasses']['BxDolPage!'.$sObject] = $o);
  177. }
  178. /**
  179. * Process page triggers.
  180. * Page triggers allow to automatically add page blocks to modules with no different if dependant module was install before or after the module page block belongs to.
  181. * For example module "Notes" adds page blocks to all profiles modules (Persons, Organizations, etc)
  182. * with no difference if persons module was installed before or after "Notes" module was installed.
  183. * @param $sPageTriggerName trigger name to process, usually specified in module installer class - @see BxBaseModGeneralInstaller
  184. * @return always true, always success
  185. */
  186. static public function processPageTrigger ($sPageTriggerName)
  187. {
  188. // get list of active modules
  189. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array(
  190. 'type' => 'modules',
  191. 'active' => 1,
  192. ));
  193. // get list of page block triggers
  194. $aPageBlocks = BxDolPageQuery::getPageTriggers($sPageTriggerName);
  195. // check each page block trigger for all modules
  196. foreach ($aPageBlocks as $aPageBlock) {
  197. foreach ($aModules as $aModule) {
  198. if (!BxDolRequest::serviceExists($aModule['name'], 'get_page_object_for_page_trigger'))
  199. continue;
  200. $mixedPageObject = BxDolService::call($aModule['name'], 'get_page_object_for_page_trigger', array($sPageTriggerName));
  201. if (!$mixedPageObject)
  202. continue;
  203. $aPageBlockRow = $aPageBlock;
  204. if (is_array($mixedPageObject))
  205. $aPageBlockRow = array_merge($aPageBlockRow, $mixedPageObject);
  206. else
  207. $aPageBlockRow['object'] = $mixedPageObject;
  208. BxDolPageQuery::addPageBlockToPage($aPageBlockRow);
  209. }
  210. }
  211. return true;
  212. }
  213. /**
  214. * Display complete page
  215. */
  216. public function displayPage ($oTemplate = null)
  217. {
  218. if (!$oTemplate)
  219. $oTemplate = BxDolTemplate::getInstance();
  220. $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  221. $oTemplate->setPageType ($this->getType());
  222. $oTemplate->setPageContent ('page_main_code', $this->getCode());
  223. $oTemplate->getPageCode();
  224. }
  225. public function getType ()
  226. {
  227. return (int)$this->_aObject['type_id'];
  228. }
  229. /**
  230. * Add replace markers. Markers are replaced in raw, html, lang blocks and page title, description, keywords and block titles.
  231. * @param $a array of markers as key => value
  232. * @return true on success or false on error
  233. */
  234. public function addMarkers ($a)
  235. {
  236. if (empty($a) || !is_array($a))
  237. return false;
  238. $this->_aMarkers = array_merge ($this->_aMarkers, $a);
  239. return true;
  240. }
  241. /**
  242. * Replace provided markers in a string
  243. * @param $mixed string or array to replace markers in
  244. * @return string where all occured markers are replaced
  245. */
  246. protected function _replaceMarkers ($mixed)
  247. {
  248. return bx_replace_markers($mixed, $this->_aMarkers);
  249. }
  250. /**
  251. * Check if page block is visible.
  252. */
  253. protected function _isVisibleBlock ($a)
  254. {
  255. return BxDolAcl::getInstance()->isMemberLevelInSet($a['visible_for_levels']);
  256. }
  257. /**
  258. * Check if page is visible.
  259. */
  260. protected function _isVisiblePage ($a)
  261. {
  262. return isAdmin() || BxDolAcl::getInstance()->isMemberLevelInSet($a['visible_for_levels']);
  263. }
  264. }
  265. /** @} */