BxDolMenu.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 menu Menu
  12. * @ref BxDolMenu
  13. */
  14. /**
  15. * Menus.
  16. *
  17. * Menu uses some set of items and the template to display it,
  18. * so it is possible to have several menus which uses the same set of items but different templates.
  19. *
  20. * Menu is any set of some links or actions, for example menu can be links in site's footer or actions in profile view.
  21. *
  22. *
  23. * @section menu_create Creating the Menu object:
  24. *
  25. * 1. Add record to 'sys_objects_menu' table:
  26. *
  27. * - 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.
  28. * - title: name of the menu, displayed in the studio menu builder.
  29. * - set_name: name of items' set.
  30. * - module: the module this menu belongs to.
  31. * - template_id: the template to use for menu displaying, this is id from 'sys_menu_templates' table.
  32. * - deletable: it determines if menu can be deleted from the studio menu builder.
  33. * - active: it is possible to disable particular menu, then it will not be displayed.
  34. * - override_class_name: user defined class name which is derived from BxTemplMenu.
  35. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  36. *
  37. * Menu templates are stored in 'sys_menu_templates' table:
  38. * - id: template id.
  39. * - template: template file.
  40. * - title: template title to display in the studio menu builder.
  41. * 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__.
  42. *
  43. *
  44. * 2. Add menu an empty menu set to 'sys_menu_sets' table (if you want to use new set of items for created menu):
  45. * - set_name: the set name.
  46. * - module: the module this set belongs to.
  47. * - title: name of the set, displayed in studio menu builder.
  48. * - deletable: it determines if the set can be deleted from menu builder.
  49. *
  50. *
  51. * 3. Add menu items to the set by adding records to 'sys_menu_items' table:
  52. * - set_name: the set name this item belogs to.
  53. * - module: the module this item belongs to.
  54. * - name: name of the item (not displayed to the end user), unique in the particular set.
  55. * - title: menu item title to display to the end user, please note that some templates can still display menu as icons without text titles.
  56. * - link: menu item URL.
  57. * - onclick: menu item onclick event.
  58. * - target: menu item target.
  59. * - icon: menu item icon, please note that some templates can still display menu as text without icons.
  60. * - addon: display additional data near menu item, only for supported menu templates, this is serialized array of service call parameters: module - module name, method - service method name, params - array of parameters.
  61. * - 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:
  62. * - user level id = 1 -> 2^(1-1) = 1
  63. * - user level id = 2 -> 2^(2-1) = 2
  64. * - user level id = 3 -> 2^(3-1) = 4
  65. * - user level id = 4 -> 2^(4-1) = 8
  66. * - active: it is possible to disable particular menu item, then it will not be displayed.
  67. * - order: menu item order in the particular set.
  68. *
  69. *
  70. * 4. Display Menu.
  71. * Use the following sample code to display menu:
  72. * @code
  73. * $oMenu = BxTemplMenu::getObjectInstance('sample_menu'); // 'sample_menu' is 'object' field from 'sys_objects_menu' table.
  74. * if ($oMenu)
  75. * echo $oMenu->getCode; // display menu
  76. * @endcode
  77. *
  78. * But in most cases you don't need to use above code to display menu,
  79. * menu objects are integrated into pages - there is special 'menu' page block type for it.
  80. *
  81. */
  82. class BxDolMenu extends BxDol implements iBxDolFactoryObject, iBxDolReplaceable
  83. {
  84. protected static $SEL_MODULE = '';
  85. protected static $SEL_NAME = '';
  86. protected $_sSelModule;
  87. protected $_sSelName;
  88. protected $_sObject;
  89. protected $_aObject;
  90. protected $_oQuery;
  91. protected $_oPermalinks;
  92. protected $_aMarkers = array ();
  93. /**
  94. * Constructor
  95. * @param $aObject array of menu options
  96. */
  97. public function __construct($aObject)
  98. {
  99. parent::__construct();
  100. $this->_sObject = isset($aObject['object']) ? $aObject['object'] : 'bx-menu-obj-' . time() . rand(0, PHP_INT_MAX);
  101. $this->_aObject = $aObject;
  102. $this->_oQuery = new BxDolMenuQuery($this->_aObject);
  103. $this->_oPermalinks = BxDolPermalinks::getInstance();
  104. if (isLogged()) {
  105. $oProfile = BxDolProfile::getInstance();
  106. if ($oProfile) {
  107. $this->addMarkers(array(
  108. 'member_id' => $oProfile->id(),
  109. 'member_display_name' => $oProfile->getDisplayName(),
  110. 'member_url' => $oProfile->getUrl(),
  111. ));
  112. }
  113. }
  114. }
  115. /**
  116. * Get menu object instance by object name
  117. * @param $sObject object name
  118. * @return object instance or false on error
  119. */
  120. static public function getObjectInstance($sObject, $oTemplate = false)
  121. {
  122. if (isset($GLOBALS['bxDolClasses']['BxDolMenu!'.$sObject]))
  123. return $GLOBALS['bxDolClasses']['BxDolMenu!'.$sObject];
  124. $aObject = BxDolMenuQuery::getMenuObject($sObject);
  125. if (!$aObject || !is_array($aObject))
  126. return false;
  127. $sClass = 'BxTemplMenu';
  128. if (!empty($aObject['override_class_name'])) {
  129. $sClass = $aObject['override_class_name'];
  130. if (!empty($aObject['override_class_file']))
  131. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  132. }
  133. $o = new $sClass($aObject, $oTemplate);
  134. return ($GLOBALS['bxDolClasses']['BxDolMenu!'.$sObject] = $o);
  135. }
  136. /**
  137. * Set selected menu item globally.
  138. * @param $sModule menu item module to set as selected
  139. * @param $sName menu item name to set as selected
  140. */
  141. static public function setSelectedGlobal ($sModule, $sName)
  142. {
  143. self::$SEL_MODULE = $sModule;
  144. self::$SEL_NAME = $sName;
  145. }
  146. /**
  147. * Process menu triggers.
  148. * Menu triggers allow to automatically add menu items to modules with no different if dependant module was install before or after the module menu item belongs to.
  149. * For example module "Notes" adds menu items to all profiles modules (Persons, Organizations, etc)
  150. * with no difference if persons module was installed before or after "Notes" module was installed.
  151. * @param $sMenuTriggerName trigger name to process, usually specified in module installer class - @see BxBaseModGeneralInstaller
  152. * @return always true, always success
  153. */
  154. static public function processMenuTrigger ($sMenuTriggerName)
  155. {
  156. // get list of active modules
  157. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array(
  158. 'type' => 'modules',
  159. 'active' => 1,
  160. ));
  161. // get list of menu triggers
  162. $aMenuItems = BxDolMenuQuery::getMenuTriggers($sMenuTriggerName);
  163. // check each menu item trigger for all modules
  164. foreach ($aMenuItems as $aMenuItem) {
  165. foreach ($aModules as $aModule) {
  166. if (!BxDolRequest::serviceExists($aModule['name'], 'get_menu_set_name_for_menu_trigger'))
  167. continue;
  168. if (!($sMenuSet = BxDolService::call($aModule['name'], 'get_menu_set_name_for_menu_trigger', array($sMenuTriggerName))))
  169. continue;
  170. $aMenuItem['set_name'] = $sMenuSet;
  171. BxDolMenuQuery::addMenuItemToSet($aMenuItem);
  172. }
  173. }
  174. return true;
  175. }
  176. /**
  177. * Set selected menu item for current menu object only.
  178. * @param $sModule menu item module to set as selected
  179. * @param $sName menu item name to set as selected
  180. */
  181. public function setSelected ($sModule, $sName)
  182. {
  183. $this->_sSelModule = $sModule;
  184. $this->_sSelName = $sName;
  185. }
  186. /**
  187. * Add replace markers.
  188. * @param $a array of markers as key => value
  189. * @return true on success or false on error
  190. */
  191. public function addMarkers ($a)
  192. {
  193. if (empty($a) || !is_array($a))
  194. return false;
  195. $this->_aMarkers = array_merge ($this->_aMarkers, $a);
  196. return true;
  197. }
  198. /**
  199. * Remove marker
  200. * @param $s marker key
  201. */
  202. public function removeMarker ($s)
  203. {
  204. unset($this->_aMarkers[$s]);
  205. }
  206. /**
  207. * Check if menu items is selected.
  208. * @param $a menu item array
  209. * @return boolean
  210. */
  211. protected function _isSelected ($a)
  212. {
  213. if ($this->_sSelModule || $this->_sSelName)
  214. return (!isset($a['module']) || $a['module'] == $this->_sSelModule) && (isset($a['name']) && $a['name'] == $this->_sSelName) ? true : false;
  215. return (!isset($a['module']) || $a['module'] == self::$SEL_MODULE) && (isset($a['name']) && $a['name'] == self::$SEL_NAME) ? true : false;
  216. }
  217. /**
  218. * Check if menu items is visible.
  219. * @param $a menu item array
  220. * @return boolean
  221. */
  222. protected function _isVisible ($a)
  223. {
  224. return BxDolAcl::getInstance()->isMemberLevelInSet($a['visible_for_levels']);
  225. }
  226. /**
  227. * Replace provided markers in menu item array, curently markers are replaced in title, link and onclick fields.
  228. * @param $a menu item array
  229. * @return array where markes are replaced with real values
  230. */
  231. protected function _replaceMarkers ($a)
  232. {
  233. if (empty($this->_aMarkers))
  234. return $a;
  235. $aReplacebleFields = array ('title', 'link', 'onclick');
  236. foreach ($aReplacebleFields as $sField)
  237. if (isset($a[$sField]))
  238. $a[$sField] = bx_replace_markers($a[$sField], $this->_aMarkers);
  239. return $a;
  240. }
  241. }
  242. /** @} */