BxDolStudioMenuTop.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 UnaStudio UNA Studio
  7. * @{
  8. */
  9. require_once(BX_DOL_DIR_STUDIO_INC . 'utils.inc.php');
  10. define('BX_DOL_STUDIO_MT_LEFT', 'left');
  11. define('BX_DOL_STUDIO_MT_CENTER', 'center');
  12. define('BX_DOL_STUDIO_MT_RIGHT', 'right');
  13. define('BX_DOL_STUDIO_MTB_CIRCLE', 'circ');
  14. define('BX_DOL_STUDIO_MTB_RECTANGLE', 'rect');
  15. class BxDolStudioMenuTop extends BxDol implements iBxDolSingleton
  16. {
  17. public static $sHistorySessionKey = 'sys_studio_history';
  18. public static $iHistoryLength = 5;
  19. public static $iToolbarLength = 10;
  20. protected $sPageName;
  21. protected $aItems;
  22. protected $aVisible;
  23. protected $aSelected;
  24. public function __construct()
  25. {
  26. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  27. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  28. parent::__construct();
  29. $this->sPageName = '';
  30. $this->aVisible = array(
  31. BX_DOL_STUDIO_MT_LEFT => getParam('sys_std_show_header_left') == 'on',
  32. BX_DOL_STUDIO_MT_CENTER => getParam('sys_std_show_header_center') == 'on',
  33. BX_DOL_STUDIO_MT_RIGHT => getParam('sys_std_show_header_right') == 'on',
  34. );
  35. $this->aSelected = array(
  36. BX_DOL_STUDIO_MT_LEFT => '',
  37. BX_DOL_STUDIO_MT_CENTER => '',
  38. BX_DOL_STUDIO_MT_RIGHT => ''
  39. );
  40. $this->aItems = array(
  41. BX_DOL_STUDIO_MT_LEFT => '',
  42. BX_DOL_STUDIO_MT_CENTER => '',
  43. BX_DOL_STUDIO_MT_RIGHT => ''
  44. );
  45. $this->aItems[BX_DOL_STUDIO_MT_LEFT] = BxTemplStudioFunctions::getInstance()->getLogo();
  46. $this->aItems[BX_DOL_STUDIO_MT_CENTER] = [
  47. 'template' => 'menu_top_center.html',
  48. 'menu_items' => []
  49. ];
  50. $this->aItems[BX_DOL_STUDIO_MT_CENTER]['menu_items'] = [];
  51. if(!$this->aVisible[BX_DOL_STUDIO_MT_LEFT])
  52. $this->aItems[BX_DOL_STUDIO_MT_CENTER]['menu_items']['launcher'] = [
  53. 'name' => 'launcher',
  54. 'icon' => 'wi-home.svg',
  55. 'link' => '{url_studio}',
  56. 'title' => '_adm_tmi_cpt_launcher'
  57. ];
  58. //--- Get Featured
  59. $aMenuItems = array();
  60. $oRolesUtils = BxDolStudioRolesUtils::getInstance();
  61. $oWidgetsDb = BxDolStudioWidgetsQuery::getInstance();
  62. // Every app in the dock carries its context menu trigger (studio/js/context_menu.js loads the menu on first use).
  63. $fContextAttrs = function($sPageName, $iWidgetId = 0) {
  64. return 'data-bx-context-menu="#bx-std-cmenu-' . $sPageName . '" data-bx-context-menu-load="' . $sPageName . ':' . (int)$iWidgetId . '"';
  65. };
  66. $aFeatured = $oWidgetsDb->getWidgets(array('type' => 'all_featured', 'featured' => 1));
  67. $aFeatured = array_column($aFeatured, null, 'page_name');
  68. foreach($aFeatured as $aItem)
  69. if(empty($aItem['type']) || $oRolesUtils->isActionAllowed('use ' . $aItem['type']))
  70. $aMenuItems[$aItem['page_name']] = array(
  71. 'class' => 'bx-menu-item-static',
  72. 'name' => $aItem['page_name'],
  73. 'icon' => $aItem['icon'],
  74. 'link' => $aItem['url'],
  75. 'onclick' => $aItem['click'],
  76. 'title' => $aItem['caption'],
  77. 'attrs_add' => $fContextAttrs($aItem['page_name'], $aItem['id'])
  78. );
  79. //--- Get Bookmarks
  80. $aBookmarks = $oWidgetsDb->getWidgets(array('type' => 'all_bookmarks', 'bookmark' => 1, 'profile_id' => bx_get_logged_profile_id()));
  81. foreach($aBookmarks as $aBookmark) {
  82. if(array_key_exists($aBookmark['page_name'], $aMenuItems))
  83. continue;
  84. $aFeatured[$aBookmark['page_name']] = $aBookmark; // the module lookup below reads it like a featured row
  85. if(!empty($aBookmark['type']) && !$oRolesUtils->isActionAllowed('use ' . $aBookmark['type']))
  86. continue;
  87. $aMenuItems[$aBookmark['page_name']] = array(
  88. 'class' => 'bx-menu-item-static',
  89. 'name' => $aBookmark['page_name'],
  90. 'icon' => $aBookmark['icon'],
  91. 'link' => $aBookmark['url'],
  92. 'onclick' => $aBookmark['click'],
  93. 'title' => $aBookmark['caption'],
  94. 'attrs_add' => $fContextAttrs($aBookmark['page_name'], $aBookmark['id'])
  95. );
  96. }
  97. $bStatic = !empty($aMenuItems);
  98. $aMenuItems['divider'] = [
  99. 'class' => 'bx-menu-item-divider',
  100. 'name' => 'divider',
  101. 'icon' => '',
  102. 'link' => '',
  103. 'onclick' => '',
  104. 'title' => ''
  105. ];
  106. //--- Get History
  107. $bHistory = false;
  108. $aHistory = self::historyGetList();
  109. if(!empty($aHistory) && is_array($aHistory))
  110. foreach($aHistory as $sPageName => $aMenuItem) {
  111. if(array_key_exists($sPageName, $aMenuItems))
  112. continue;
  113. $aMenuItem['class'] = 'bx-menu-item-dynamic';
  114. $aMenuItem['attrs_add'] = $fContextAttrs($sPageName);
  115. $aMenuItems[$sPageName] = $aMenuItem;
  116. $bHistory = true;
  117. }
  118. // A disabled app is dimmed in the dock as it is in the launcher (the widget rows carry the module; history rows are looked up by page).
  119. $oModuleQuery = BxDolModuleQuery::getInstance();
  120. $oPageQuery = BxDolStudioPageQuery::getInstance();
  121. foreach($aMenuItems as $sPageName => &$aMenuItem) {
  122. if(in_array($sPageName, ['divider', 'launcher']))
  123. continue;
  124. $sModule = '';
  125. if(isset($aFeatured[$sPageName]['module']))
  126. $sModule = $aFeatured[$sPageName]['module'];
  127. else if(($aPage = $oPageQuery->getPages(['type' => 'by_page_name_full', 'value' => $sPageName])) && !empty($aPage['wid_module']))
  128. $sModule = $aPage['wid_module'];
  129. if(!$sModule)
  130. continue;
  131. $aModule = $oModuleQuery->getModuleByName($sModule);
  132. if(!empty($aModule) && is_array($aModule) && (int)$aModule['enabled'] == 0)
  133. $aMenuItem['class_add'] = 'bx-menu-item-disabled';
  134. }
  135. unset($aMenuItem);
  136. // The divider only makes sense between two groups: drop it while either side is empty (e.g. right after install).
  137. if(!$bStatic || !$bHistory)
  138. unset($aMenuItems['divider']);
  139. if(!empty($aMenuItems) && is_array($aMenuItems)) {
  140. if(count($aMenuItems) > BxTemplStudioMenuTop::$iToolbarLength)
  141. $aMenuItems = array_slice($aMenuItems, 0, BxTemplStudioMenuTop::$iToolbarLength);
  142. if(array_key_last($aMenuItems) == 'divider')
  143. unset($aMenuItems['divider']);
  144. if(($sKey = 'launcher') && isset($this->aItems[BX_DOL_STUDIO_MT_CENTER]['menu_items'][$sKey]))
  145. $this->aItems[BX_DOL_STUDIO_MT_CENTER]['menu_items'] = bx_array_insert_after($aMenuItems, $this->aItems[BX_DOL_STUDIO_MT_CENTER]['menu_items'], $sKey);
  146. else
  147. $this->aItems[BX_DOL_STUDIO_MT_CENTER]['menu_items'] = $aMenuItems;
  148. }
  149. }
  150. public static function getInstance()
  151. {
  152. if (!isset($GLOBALS['bxDolClasses']['BxBaseStudioMenuTop']))
  153. $GLOBALS['bxDolClasses']['BxBaseStudioMenuTop'] = new BxTemplStudioMenuTop();
  154. return $GLOBALS['bxDolClasses']['BxBaseStudioMenuTop'];
  155. }
  156. public static function historyGetList()
  157. {
  158. $aHistory = BxDolSession::getInstance()->getValue(self::$sHistorySessionKey);
  159. if(empty($aHistory) || !is_array($aHistory))
  160. return array();
  161. if(count($aHistory) > BxTemplStudioMenuTop::$iHistoryLength)
  162. $aHistory = array_slice($aHistory, -BxTemplStudioMenuTop::$iHistoryLength);
  163. return array_reverse($aHistory);
  164. }
  165. public static function historyAdd($aPage)
  166. {
  167. $oSession = BxDolSession::getInstance();
  168. $aHistory = $oSession->getValue(self::$sHistorySessionKey);
  169. if(!empty($aHistory) && isset($aHistory[$aPage['name']]))
  170. return;
  171. if (!is_array($aHistory))
  172. $aHistory = [];
  173. $aHistory[$aPage['name']] = array(
  174. 'name' => $aPage['name'],
  175. 'icon' => $aPage['wid_icon'],
  176. 'link' => $aPage['wid_url'],
  177. 'onclick' => $aPage['wid_click'],
  178. 'title' => $aPage['wid_caption']
  179. );
  180. if(count($aHistory) > BxTemplStudioMenuTop::$iHistoryLength)
  181. $aHistory = array_slice($aHistory, -BxTemplStudioMenuTop::$iHistoryLength);
  182. $oSession->setValue(self::$sHistorySessionKey, $aHistory);
  183. }
  184. public static function historyDelete($mixedPage)
  185. {
  186. if(is_array($mixedPage)) {
  187. if(!isset($mixedPage['name']))
  188. return;
  189. $mixedPage = $mixedPage['name'];
  190. }
  191. $oSession = BxDolSession::getInstance();
  192. $aHistory = $oSession->getValue(self::$sHistorySessionKey);
  193. if(empty($aHistory) || !isset($aHistory[$mixedPage]))
  194. return;
  195. unset($aHistory[$mixedPage]);
  196. $oSession->setValue(self::$sHistorySessionKey, $aHistory);
  197. }
  198. public function setPageName($sName)
  199. {
  200. $this->sPageName = $sName;
  201. }
  202. public function setContent($sPosition, $mixedContent)
  203. {
  204. $this->aItems[$sPosition] = $mixedContent;
  205. }
  206. public function setSelected($sPosition, $sValue)
  207. {
  208. if(!isset($this->aItems[$sPosition]['menu_items'][$sValue]))
  209. return;
  210. $this->aItems[$sPosition]['menu_items'][$sValue]['selected'] = true;
  211. }
  212. public function setVisible($sPosition, $bValue = true)
  213. {
  214. $this->aVisible[$sPosition] = $bValue;
  215. }
  216. public function setVisibleAll($bValue = true)
  217. {
  218. $this->aVisible = array(
  219. BX_DOL_STUDIO_MT_LEFT => $bValue,
  220. BX_DOL_STUDIO_MT_CENTER => $bValue,
  221. BX_DOL_STUDIO_MT_RIGHT => $bValue
  222. );
  223. }
  224. }
  225. /** @} */