BxDolPage.php 11 KB

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