BxDolMenu.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. * Menus.
  11. *
  12. * Menu uses some set of items and the template to display it,
  13. * so it is possible to have several menus which uses the same set of items but different templates.
  14. *
  15. * Menu is any set of some links or actions, for example menu can be links in site's footer or actions in profile view.
  16. *
  17. *
  18. * @section menu_create Creating the Menu object:
  19. *
  20. * 1. Add record to 'sys_objects_menu' table:
  21. *
  22. * - 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.
  23. * - title: name of the menu, displayed in the studio menu builder.
  24. * - set_name: name of items' set.
  25. * - module: the module this menu belongs to.
  26. * - template_id: the template to use for menu displaying, this is id from 'sys_menu_templates' table.
  27. * - deletable: it determines if menu can be deleted from the studio menu builder.
  28. * - active: it is possible to disable particular menu, then it will not be displayed.
  29. * - override_class_name: user defined class name which is derived from BxTemplMenu.
  30. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  31. *
  32. * Menu templates are stored in 'sys_menu_templates' table:
  33. * - id: template id.
  34. * - template: template file.
  35. * - title: template title to display in the studio menu builder.
  36. * 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__.
  37. *
  38. *
  39. * 2. Add menu an empty menu set to 'sys_menu_sets' table (if you want to use new set of items for created menu):
  40. * - set_name: the set name.
  41. * - module: the module this set belongs to.
  42. * - title: name of the set, displayed in studio menu builder.
  43. * - deletable: it determines if the set can be deleted from menu builder.
  44. *
  45. *
  46. * 3. Add menu items to the set by adding records to 'sys_menu_items' table:
  47. * - set_name: the set name this item belogs to.
  48. * - module: the module this item belongs to.
  49. * - name: name of the item (not displayed to the end user), unique in the particular set.
  50. * - title: menu item title to display to the end user, please note that some templates can still display menu as icons without text titles.
  51. * - link: menu item URL.
  52. * - onclick: menu item onclick event.
  53. * - target: menu item target.
  54. * - icon: menu item icon, please note that some templates can still display menu as text without icons.
  55. * - 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.
  56. * - markers: service method to provide additional replacement markers, this is serialized array of service call parameters: module - module name, method - service method name, params - array of parameters.
  57. * - 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:
  58. * - user level id = 1 -> 2^(1-1) = 1
  59. * - user level id = 2 -> 2^(2-1) = 2
  60. * - user level id = 3 -> 2^(3-1) = 4
  61. * - user level id = 4 -> 2^(4-1) = 8
  62. * - active: it is possible to disable particular menu item, then it will not be displayed.
  63. * - order: menu item order in the particular set.
  64. *
  65. *
  66. * 4. Display Menu.
  67. * Use the following sample code to display menu:
  68. * @code
  69. * $oMenu = BxTemplMenu::getObjectInstance('sample_menu'); // 'sample_menu' is 'object' field from 'sys_objects_menu' table.
  70. * if ($oMenu)
  71. * echo $oMenu->getCode; // display menu
  72. * @endcode
  73. *
  74. * But in most cases you don't need to use above code to display menu,
  75. * menu objects are integrated into pages - there is special 'menu' page block type for it.
  76. *
  77. */
  78. class BxDolMenu extends BxDolFactory implements iBxDolFactoryObject, iBxDolReplaceable
  79. {
  80. protected static $SEL_MODULE = '';
  81. protected static $SEL_NAME = '';
  82. protected $_bIsApi;
  83. protected $_bHx;
  84. protected $_bHxHead;
  85. protected $_mHxPreload;
  86. protected $_aHx;
  87. protected $_bDynamicMode;
  88. protected $_bAddNoFollow;
  89. protected $_bSelModuleCheck;
  90. protected $_sSelModule;
  91. protected $_sSelName;
  92. protected $_sObject;
  93. protected $_aObject;
  94. protected $_oQuery;
  95. protected $_oPermalinks;
  96. protected $_aMarkers = array();
  97. protected $_isMultilevel = null;
  98. protected $_sSessionKeyCollapsed;
  99. protected $_aContentParams;
  100. /**
  101. * Constructor
  102. * @param $aObject array of menu options
  103. */
  104. protected function __construct($aObject)
  105. {
  106. parent::__construct();
  107. $this->_bIsApi = bx_is_api();
  108. $this->_bHx = false;
  109. $this->_aHx = [];
  110. $this->_bDynamicMode = false;
  111. $this->_bAddNoFollow = getParam('sys_add_nofollow') == 'on';
  112. $this->_bSelModuleCheck = false;
  113. $this->_sObject = isset($aObject['object']) ? $aObject['object'] : 'bx-menu-obj-' . time() . rand(0, PHP_INT_MAX);
  114. $this->_aObject = $aObject;
  115. $this->_oQuery = new BxDolMenuQuery($this->_aObject);
  116. $this->_oPermalinks = BxDolPermalinks::getInstance();
  117. $this->_sSessionKeyCollapsed = 'bx_menu_collapsed_';
  118. $this->_aContentParams = [];
  119. $this->addMarkers([
  120. 'object' => $this->_sObject
  121. ]);
  122. if(isLogged() && ($oProfile = BxDolProfile::getInstance()) !== false) {
  123. $sUrl = $oProfile->getUrl();
  124. if($this->_bIsApi)
  125. $sUrl = bx_api_get_relative_url($sUrl);
  126. $this->addMarkers([
  127. 'member_id' => $oProfile->id(),
  128. 'member_display_name' => $oProfile->getDisplayName(),
  129. 'member_url' => $sUrl,
  130. 'content_id' => $oProfile->getContentId()
  131. ]);
  132. }
  133. }
  134. /**
  135. * Get menu object instance by object name
  136. * @param $sObject object name
  137. * @return object instance or false on error
  138. */
  139. static public function getObjectInstance($sObject, $oTemplate = false)
  140. {
  141. $oMenu = false;
  142. if (!isset($GLOBALS['bxDolClasses']['BxDolMenu!'.$sObject])) {
  143. $aObject = BxDolMenuQuery::getMenuObject($sObject);
  144. if (!$aObject || !is_array($aObject) || (int)$aObject['active'] == 0)
  145. return false;
  146. $sClass = 'BxTemplMenu';
  147. if (!empty($aObject['override_class_name'])) {
  148. $sClass = $aObject['override_class_name'];
  149. if (!empty($aObject['override_class_file']))
  150. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  151. }
  152. $oMenu = new $sClass($aObject, $oTemplate);
  153. $GLOBALS['bxDolClasses']['BxDolMenu!'.$sObject] = $oMenu;
  154. }
  155. else
  156. $oMenu = $GLOBALS['bxDolClasses']['BxDolMenu!'.$sObject];
  157. /**
  158. * @hooks
  159. * @hookdef hook-system-get_object 'system', 'get_object' - hook to override menu object
  160. * - $unit_name - equals `system`
  161. * - $action - equals `get_object`
  162. * - $object_id - not used
  163. * - $sender_id - not used
  164. * - $extra_params - array of additional params with the following array keys:
  165. * - `type` - [string] object type, equals to 'menu'
  166. * - `name` - [string] menu object name
  167. * - `object` - [object] by ref, an instance of menu, @see BxDolMenu, can be overridden in hook processing
  168. * @hook @ref hook-system-get_object
  169. */
  170. bx_alert('system', 'get_object', 0, false, [
  171. 'type' => 'menu',
  172. 'name' => $sObject,
  173. 'object' => &$oMenu,
  174. ]);
  175. return $oMenu;
  176. }
  177. /**
  178. * Set selected menu item globally.
  179. * @param $sModule menu item module to set as selected
  180. * @param $sName menu item name to set as selected
  181. */
  182. static public function setSelectedGlobal ($sModule, $sName)
  183. {
  184. self::$SEL_MODULE = $sModule;
  185. self::$SEL_NAME = $sName;
  186. }
  187. /**
  188. * Process menu triggers.
  189. * 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.
  190. * For example module "Notes" adds menu items to all profiles modules (Persons, Organizations, etc)
  191. * with no difference if persons module was installed before or after "Notes" module was installed.
  192. * @param $sMenuTriggerName trigger name to process, usually specified in module installer class - @see BxBaseModGeneralInstaller
  193. * @return always true, always success
  194. */
  195. static public function processMenuTrigger ($sMenuTriggerName)
  196. {
  197. // get list of active modules
  198. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array(
  199. 'type' => 'modules',
  200. 'active' => 1,
  201. ));
  202. // get list of menu triggers
  203. $aMenuItems = BxDolMenuQuery::getMenuTriggers($sMenuTriggerName);
  204. // check each menu item trigger for all modules
  205. foreach ($aMenuItems as $aMenuItem) {
  206. foreach ($aModules as $aModule) {
  207. if (!BxDolRequest::serviceExists($aModule['name'], 'get_menu_set_name_for_menu_trigger'))
  208. continue;
  209. $mixedMenuSet = BxDolService::call($aModule['name'], 'get_menu_set_name_for_menu_trigger', array($sMenuTriggerName));
  210. if(empty($mixedMenuSet))
  211. continue;
  212. if(is_string($mixedMenuSet))
  213. $mixedMenuSet = array($mixedMenuSet);
  214. foreach($mixedMenuSet as $sMenuSet) {
  215. if(empty($sMenuSet))
  216. continue;
  217. $aMenuItem['set_name'] = $sMenuSet;
  218. BxDolMenuQuery::addMenuItemToSet($aMenuItem);
  219. }
  220. }
  221. }
  222. return true;
  223. }
  224. public function isHtmx()
  225. {
  226. return $this->_bHx;
  227. }
  228. /**
  229. * Check if the menu is visible. The menu is visible if at least one menu item is visible.
  230. * @return boolean
  231. */
  232. public function isVisible()
  233. {
  234. if((int)$this->_aObject['active'] == 0)
  235. return false;
  236. if(!isset($this->_aObject['menu_items']))
  237. $this->_aObject['menu_items'] = $this->_oQuery->getMenuItems();
  238. $bVisible = false;
  239. foreach ($this->_aObject['menu_items'] as $a) {
  240. if(!$this->_isActive($a) || !$this->_isVisible($a))
  241. continue;
  242. $bVisible = true;
  243. break;
  244. }
  245. return $bVisible;
  246. }
  247. public function getQueryObject()
  248. {
  249. return $this->_oQuery;
  250. }
  251. public function getTemplateId()
  252. {
  253. return $this->_aObject['template_id'];
  254. }
  255. /**
  256. * Get template name with checking for custom template related to exactly this menu object.
  257. * @return string with template name.
  258. */
  259. public function getTemplateName($sName = '')
  260. {
  261. if(empty($sName))
  262. $sName = $this->_aObject['template'];
  263. $sNameCustom = str_replace('.html', '_' . $this->_sObject . '.html', $sName);
  264. return $this->_oTemplate->isHtml($sNameCustom) ? $sNameCustom : $sName;
  265. }
  266. public function setTemplateById ($iTemplateId)
  267. {
  268. $aTemplate = $this->_oQuery->getMenuTemplateById($iTemplateId);
  269. if(empty($aTemplate) || !is_array($aTemplate))
  270. return;
  271. $this->_aObject['template'] = $aTemplate['template'];
  272. }
  273. /**
  274. * Set selected menu item for current menu object only.
  275. * @param $sModule menu item module to set as selected
  276. * @param $sName menu item name to set as selected
  277. */
  278. public function setSelected ($sModule, $sName)
  279. {
  280. $this->_sSelModule = $sModule;
  281. $this->_sSelName = $sName;
  282. }
  283. public function setDynamicMode ($bDynamicMode)
  284. {
  285. $this->_bDynamicMode = $bDynamicMode;
  286. }
  287. public function setHtmx($bHx)
  288. {
  289. $this->_bHx = $bHx;
  290. }
  291. /**
  292. * Get an arrey of replacable markers.
  293. * @return an array with markers
  294. */
  295. public function getMarkers()
  296. {
  297. return $this->_aMarkers;
  298. }
  299. /**
  300. * Add replace markers.
  301. * @param $a array of markers as key => value
  302. * @return true on success or false on error
  303. */
  304. public function addMarkers ($a)
  305. {
  306. if (empty($a) || !is_array($a))
  307. return false;
  308. $this->_aMarkers = array_merge ($this->_aMarkers, $a);
  309. return true;
  310. }
  311. /**
  312. * Remove marker
  313. * @param $s marker key
  314. */
  315. public function removeMarker ($s)
  316. {
  317. unset($this->_aMarkers[$s]);
  318. }
  319. public function initContentParams()
  320. {
  321. $this->_aContentParams = [];
  322. }
  323. public function setContentParams($aParams)
  324. {
  325. $this->_aContentParams = $aParams;
  326. return true;
  327. }
  328. public function getContentParams()
  329. {
  330. if(!$this->_aContentParams)
  331. $this->initContentParams();
  332. return $this->_aContentParams;
  333. }
  334. public function performActionSetCollapsed($mixedValue)
  335. {
  336. $this->_setCollapsed($this->_sObject, (int)$mixedValue);
  337. }
  338. public function performActionSetCollapsedSubmenu($sMenuItem, $mixedValue)
  339. {
  340. $this->_setCollapsed($this->_sObject . '_' . $sMenuItem, (int)$mixedValue);
  341. }
  342. public function getUserChoiceCollapsed($sObject = '')
  343. {
  344. $iProfile = bx_get_logged_profile_id();
  345. if(!$iProfile)
  346. return false;
  347. if(!$sObject)
  348. $sObject = $this->_sObject;
  349. $sSessionKey = $this->_sSessionKeyCollapsed . $iProfile;
  350. $aCollapsed = BxDolSession::getInstance()->getValue($sSessionKey);
  351. if(!isset($aCollapsed[$sObject]))
  352. return false;
  353. return (int)$aCollapsed[$sObject];
  354. }
  355. public function getUserChoiceCollapsedSubmenu($mixedItem, $sObject = '')
  356. {
  357. if(!$sObject)
  358. $sObject = $this->_sObject;
  359. if(is_array($mixedItem) && isset($mixedItem['name']))
  360. $sObject .= '_' . $mixedItem['name'];
  361. else if(is_string($mixedItem))
  362. $sObject .= '_' . $mixedItem;
  363. return $this->getUserChoiceCollapsed($sObject);
  364. }
  365. protected function _setCollapsed($sName, $mixedValue)
  366. {
  367. $iProfile = bx_get_logged_profile_id();
  368. if(!$iProfile)
  369. return;
  370. $oSession = BxDolSession::getInstance();
  371. $sSessionKey = $this->_sSessionKeyCollapsed . $iProfile;
  372. $aCollapsed = $oSession->getValue($sSessionKey);
  373. if(!is_array($aCollapsed))
  374. $aCollapsed = [];
  375. $aCollapsed[$sName] = $mixedValue;
  376. $oSession->setValue($sSessionKey, $aCollapsed);
  377. }
  378. /**
  379. * Check if menu items is selected.
  380. * @param $a menu item array
  381. * @return boolean
  382. */
  383. protected function _isSelected ($a)
  384. {
  385. if($this->_sSelModule || $this->_sSelName)
  386. return (!$this->_bSelModuleCheck || !isset($a['module']) || $a['module'] == $this->_sSelModule) && (isset($a['name']) && $a['name'] == $this->_sSelName) ? true : false;
  387. return (!$this->_bSelModuleCheck || !isset($a['module']) || $a['module'] == self::$SEL_MODULE) && (isset($a['name']) && $a['name'] == self::$SEL_NAME) ? true : false;
  388. }
  389. /**
  390. * Check if menu items is active.
  391. * @param $a menu item array
  392. * @return boolean
  393. */
  394. protected function _isActive ($a)
  395. {
  396. if($this->_bIsApi)
  397. return !isset($a['active_api']) || (int)$a['active_api'] !=0;
  398. else
  399. return !isset($a['active']) || (int)$a['active'] != 0;
  400. }
  401. /**
  402. * Check if menu items is visible.
  403. * @param $a menu item array
  404. * @return boolean
  405. */
  406. protected function _isVisible ($a)
  407. {
  408. if(isset($a['visible_for_levels']) && !BxDolAcl::getInstance()->isMemberLevelInSet($a['visible_for_levels']))
  409. return false;
  410. if(!empty($a['visibility_custom']) && !BxDolService::callSerialized($a['visibility_custom'], $this->_aMarkers))
  411. return false;
  412. if($this->_iPageType && !empty($a['hidden_on_pt']) && ((1 << ($this->_iPageType - 2)) & (int)$a['hidden_on_pt']))
  413. return false;
  414. return true;
  415. }
  416. protected function isMultilevel ()
  417. {
  418. if($this->_isMultilevel === null)
  419. $this->_isMultilevel = !empty($this->_aObject['set_name']) && $this->_oQuery->isSetMultilevel($this->_aObject['set_name']);
  420. return $this->_isMultilevel;
  421. }
  422. protected function _getVisibilityClass($a)
  423. {
  424. $aHiddenOn = array(
  425. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  426. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  427. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  428. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  429. );
  430. $aHiddenOnCol = array(
  431. pow(2, 1) => 'bx-def-thin-col-hide',
  432. pow(2, 2) => 'bx-def-half-col-hide',
  433. pow(2, 3) => 'bx-def-wide-col-hide',
  434. pow(2, 4) => 'bx-def-full-col-hide'
  435. );
  436. $sHiddenOnCssClasses = '';
  437. if(!empty($a['hidden_on']))
  438. foreach($aHiddenOn as $iHiddenOn => $sClass)
  439. if((int)$a['hidden_on'] & $iHiddenOn)
  440. $sHiddenOnCssClasses .= ' ' . $sClass;
  441. if(!empty($a['hidden_on_col'])){
  442. foreach($aHiddenOnCol as $iHiddenOn => $sClass)
  443. if((int)$a['hidden_on_col'] & $iHiddenOn)
  444. $sHiddenOnCssClasses .= ' ' . $sClass;
  445. }
  446. return $sHiddenOnCssClasses;
  447. }
  448. protected function _updateVisibilityParamsAPI(&$a)
  449. {
  450. $aValues = [
  451. 'hidden_on' => [
  452. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'phone',
  453. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'tablet',
  454. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'desktop',
  455. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'mobile-app'
  456. ],
  457. 'hidden_on_col' => [
  458. pow(2, 0) => 'thin-col',
  459. pow(2, 1) => 'half-col',
  460. pow(2, 2) => 'wide-col',
  461. pow(2, 3) => 'full-col'
  462. ]
  463. ];
  464. foreach(['hidden_on', 'hidden_on_col'] as $sK)
  465. if(!empty($a[$sK])) {
  466. $aHoResults = [];
  467. foreach($aValues[$sK] as $iHiddenOn => $sHiddenOn)
  468. if((int)$a[$sK] & $iHiddenOn)
  469. $aHoResults[] = $sHiddenOn;
  470. $a[$sK] = $aHoResults;
  471. }
  472. else
  473. $a[$sK] = false;
  474. }
  475. /**
  476. * Replace provided markers in menu item array, curently markers are replaced in title, link and onclick fields.
  477. * @param $a menu item array
  478. * @return array where markes are replaced with real values
  479. */
  480. protected function _replaceMarkers ($a)
  481. {
  482. if(empty($this->_aMarkers))
  483. return $a;
  484. $aReplacebleFields = ['title', 'title_attr', 'link', 'onclick'];
  485. foreach($aReplacebleFields as $sField)
  486. if(isset($a[$sField]))
  487. $a[$sField] = bx_replace_markers($a[$sField], $this->_aMarkers);
  488. return $a;
  489. }
  490. }
  491. /** @} */