BxDolMenu.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolMenuQuery');
  10. bx_import('BxDolPermalinks');
  11. /**
  12. * @page objects
  13. * @section menu Menu
  14. * @ref BxDolMenu
  15. */
  16. /**
  17. * Menus.
  18. *
  19. * Menu uses some set of items and the template to display it,
  20. * so it is possible to have several menus which uses the same set of items but different templates.
  21. *
  22. * Menu is any set of some links or actions, for example menu can be links in site's footer or actions in profile view.
  23. *
  24. *
  25. * @section menu_create Creating the Menu object:
  26. *
  27. * 1. Add record to 'sys_objects_menu' table:
  28. *
  29. * - object: name of the menu object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing; for example: bx_groups_actions - actions menu in group view.
  30. * - title: name of the menu, displayed in the studio menu builder.
  31. * - set_name: name of items' set.
  32. * - module: the module this menu belongs to.
  33. * - template_id: the template to use for menu displaying, this is id from 'sys_menu_templates' table.
  34. * - deletable: it determines if menu can be deleted from the studio menu builder.
  35. * - active: it is possible to disable particular menu, then it will not be displayed.
  36. * - override_class_name: user defined class name which is derived from BxTemplMenu.
  37. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  38. *
  39. * Menu templates are stored in 'sys_menu_templates' table:
  40. * - id: template id.
  41. * - template: template file.
  42. * - title: template title to display in the studio menu builder.
  43. * All menu templates iterate through 'bx_repeat:menu_items' and use the following template variables for each menu item: __link__, __target__, __onclick__, __title__, __class_add__.
  44. *
  45. *
  46. * 2. Add menu an empty menu set to 'sys_menu_sets' table (if you want to use new set of items for created menu):
  47. * - set_name: the set name.
  48. * - module: the module this set belongs to.
  49. * - title: name of the set, displayed in studio menu builder.
  50. * - deletable: it determines if the set can be deleted from menu builder.
  51. *
  52. *
  53. * 3. Add menu items to the set by adding records to 'sys_menu_items' table:
  54. * - set_name: the set name this item belogs to.
  55. * - module: the module this item belongs to.
  56. * - name: name of the item (not displayed to the end user), unique in the particular set.
  57. * - title: menu item title to display to the end user, please note that some templates can still display menu as icons without text titles.
  58. * - link: menu item URL.
  59. * - onclick: menu item onclick event.
  60. * - target: menu item target.
  61. * - icon: menu item icon, please note that some templates can still display menu as text without icons.
  62. * - 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:
  63. * - user level id = 1 -> 2^(1-1) = 1
  64. * - user level id = 2 -> 2^(2-1) = 2
  65. * - user level id = 3 -> 2^(3-1) = 4
  66. * - user level id = 4 -> 2^(4-1) = 8
  67. * - active: it is possible to disable particular menu item, then it will not be displayed.
  68. * - order: menu item order in the particular set.
  69. *
  70. *
  71. * 4. Display Menu.
  72. * Use the following sample code to display menu:
  73. * @code
  74. * bx_import('BxTemplMenu');
  75. * $oMenu = BxTemplMenu::getObjectInstance('sample_menu'); // 'sample_menu' is 'object' field from 'sys_objects_menu' table.
  76. * if ($oMenu)
  77. * echo $oMenu->getCode; // display menu
  78. * @endcode
  79. *
  80. * But in most cases you don't need to use above code to display menu,
  81. * menu objects are integrated into pages - there is special 'menu' page block type for it.
  82. *
  83. */
  84. class BxDolMenu extends BxDol {
  85. protected static $SEL_MODULE = '';
  86. protected static $SEL_NAME = '';
  87. protected $_sObject;
  88. protected $_aObject;
  89. protected $_oQuery;
  90. protected $_oPermalinks;
  91. protected $_aMarkers = array ();
  92. /**
  93. * Constructor
  94. * @param $aObject array of menu options
  95. */
  96. public function __construct($aObject) {
  97. parent::__construct();
  98. $this->_sObject = isset($aObject['object']) ? $aObject['object'] : 'bx-menu-obj-' . time() . rand(0, PHP_INT_MAX);
  99. $this->_aObject = $aObject;
  100. $this->_oQuery = new BxDolMenuQuery($this->_aObject);
  101. $this->_oPermalinks = BxDolPermalinks::getInstance();
  102. }
  103. /**
  104. * Get menu object instance by object name
  105. * @param $sObject object name
  106. * @return object instance or false on error
  107. */
  108. static public function getObjectInstance($sObject) {
  109. if (isset($GLOBALS['bxDolClasses']['BxDolMenu!'.$sObject]))
  110. return $GLOBALS['bxDolClasses']['BxDolMenu!'.$sObject];
  111. $aObject = BxDolMenuQuery::getMenuObject($sObject);
  112. if (!$aObject || !is_array($aObject))
  113. return false;
  114. bx_import('BxTemplMenu');
  115. $sClass = 'BxTemplMenu';
  116. if (!empty($aObject['override_class_name'])) {
  117. $sClass = $aObject['override_class_name'];
  118. if (!empty($aObject['override_class_file']))
  119. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  120. else
  121. bx_import($sClass);
  122. }
  123. $o = new $sClass($aObject);
  124. return ($GLOBALS['bxDolClasses']['BxDolMenu!'.$sObject] = $o);
  125. }
  126. /**
  127. * Set selected menu item.
  128. * @param $sModule menu item module to set as selected
  129. * @param $sName menu item name to set as selected
  130. */
  131. static public function setSelected ($sModule, $sName) {
  132. self::$SEL_MODULE = $sModule;
  133. self::$SEL_NAME = $sName;
  134. }
  135. /**
  136. * Add replace markers.
  137. * @param $a array of markers as key => value
  138. * @return true on success or false on error
  139. */
  140. public function addMarkers ($a) {
  141. if (empty($a) || !is_array($a))
  142. return false;
  143. $this->_aMarkers = array_merge ($this->_aMarkers, $a);
  144. return true;
  145. }
  146. /**
  147. * Check if menu items is selected.
  148. * @param $a menu item array
  149. * @return boolean
  150. */
  151. protected function _isSelected ($a) {
  152. return (!isset($a['module']) || $a['module'] == self::$SEL_MODULE) && (isset($a['name']) && $a['name'] == self::$SEL_NAME) ? true : false;
  153. }
  154. /**
  155. * Check if menu items is visible.
  156. * @param $a menu item array
  157. * @return boolean
  158. */
  159. protected function _isVisible ($a) {
  160. bx_import('BxDolAcl');
  161. return BxDolAcl::getInstance()->isMemberLevelInSet($a['visible_for_levels']);
  162. }
  163. /**
  164. * Replace provided markers in menu item array, curently markers are replaced in title, link and onclick fields.
  165. * @param $a menu item array
  166. * @return array where markes are replaced with real values
  167. */
  168. protected function _replaceMarkers ($a) {
  169. if (empty($this->_aMarkers))
  170. return $a;
  171. $aReplacebleFields = array ('title', 'link', 'onclick');
  172. foreach ($aReplacebleFields as $sField)
  173. if(isset($a[$sField]))
  174. foreach ($this->_aMarkers as $sKey => $sValue)
  175. $a[$sField] = str_replace('{' . $sKey . '}', $sValue, $a[$sField]);
  176. return $a;
  177. }
  178. }
  179. /** @} */