BxDolStudioMenuTop.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. $aFeatured = $oWidgetsDb->getWidgets(array('type' => 'all_featured', 'featured' => 1));
  63. foreach($aFeatured as $aItem)
  64. if(empty($aItem['type']) || $oRolesUtils->isActionAllowed('use ' . $aItem['type']))
  65. $aMenuItems[$aItem['page_name']] = array(
  66. 'class' => 'bx-menu-item-static',
  67. 'name' => $aItem['page_name'],
  68. 'icon' => $aItem['icon'],
  69. 'link' => $aItem['url'],
  70. 'onclick' => $aItem['click'],
  71. 'title' => $aItem['caption']
  72. );
  73. //--- Get Bookmarks
  74. $aBookmarks = $oWidgetsDb->getWidgets(array('type' => 'all_bookmarks', 'bookmark' => 1, 'profile_id' => bx_get_logged_profile_id()));
  75. foreach($aBookmarks as $aBookmark) {
  76. if(array_key_exists($aBookmark['page_name'], $aMenuItems))
  77. continue;
  78. if(!empty($aBookmark['type']) && !$oRolesUtils->isActionAllowed('use ' . $aBookmark['type']))
  79. continue;
  80. $aMenuItems[$aBookmark['page_name']] = array(
  81. 'class' => 'bx-menu-item-static',
  82. 'name' => $aBookmark['page_name'],
  83. 'icon' => $aBookmark['icon'],
  84. 'link' => $aBookmark['url'],
  85. 'onclick' => $aBookmark['click'],
  86. 'title' => $aBookmark['caption']
  87. );
  88. }
  89. $aMenuItems['divider'] = [
  90. 'class' => 'bx-menu-item-divider',
  91. 'name' => 'divider',
  92. 'icon' => '',
  93. 'link' => '',
  94. 'onclick' => '',
  95. 'title' => ''
  96. ];
  97. //--- Get History
  98. $aHistory = self::historyGetList();
  99. if(!empty($aHistory) && is_array($aHistory))
  100. foreach($aHistory as $sPageName => $aMenuItem) {
  101. if(array_key_exists($sPageName, $aMenuItems))
  102. continue;
  103. $aMenuItem['class'] = 'bx-menu-item-dynamic';
  104. $aMenuItems[$sPageName] = $aMenuItem;
  105. }
  106. if(!empty($aMenuItems) && is_array($aMenuItems)) {
  107. if(count($aMenuItems) > BxTemplStudioMenuTop::$iToolbarLength)
  108. $aMenuItems = array_slice($aMenuItems, 0, BxTemplStudioMenuTop::$iToolbarLength);
  109. if(($sKey = 'launcher') && isset($this->aItems[BX_DOL_STUDIO_MT_CENTER]['menu_items'][$sKey]))
  110. $this->aItems[BX_DOL_STUDIO_MT_CENTER]['menu_items'] = bx_array_insert_after($aMenuItems, $this->aItems[BX_DOL_STUDIO_MT_CENTER]['menu_items'], $sKey);
  111. else
  112. $this->aItems[BX_DOL_STUDIO_MT_CENTER]['menu_items'] = $aMenuItems;
  113. }
  114. }
  115. public static function getInstance()
  116. {
  117. if (!isset($GLOBALS['bxDolClasses']['BxBaseStudioMenuTop']))
  118. $GLOBALS['bxDolClasses']['BxBaseStudioMenuTop'] = new BxTemplStudioMenuTop();
  119. return $GLOBALS['bxDolClasses']['BxBaseStudioMenuTop'];
  120. }
  121. public static function historyGetList()
  122. {
  123. $aHistory = BxDolSession::getInstance()->getValue(self::$sHistorySessionKey);
  124. if(empty($aHistory) || !is_array($aHistory))
  125. return array();
  126. if(count($aHistory) > BxTemplStudioMenuTop::$iHistoryLength)
  127. $aHistory = array_slice($aHistory, -BxTemplStudioMenuTop::$iHistoryLength);
  128. return array_reverse($aHistory);
  129. }
  130. public static function historyAdd($aPage)
  131. {
  132. $oSession = BxDolSession::getInstance();
  133. $aHistory = $oSession->getValue(self::$sHistorySessionKey);
  134. if(!empty($aHistory) && isset($aHistory[$aPage['name']]))
  135. return;
  136. if (!is_array($aHistory))
  137. $aHistory = [];
  138. $aHistory[$aPage['name']] = array(
  139. 'name' => $aPage['name'],
  140. 'icon' => $aPage['wid_icon'],
  141. 'link' => $aPage['wid_url'],
  142. 'onclick' => $aPage['wid_click'],
  143. 'title' => $aPage['wid_caption']
  144. );
  145. if(count($aHistory) > BxTemplStudioMenuTop::$iHistoryLength)
  146. $aHistory = array_slice($aHistory, -BxTemplStudioMenuTop::$iHistoryLength);
  147. $oSession->setValue(self::$sHistorySessionKey, $aHistory);
  148. }
  149. public static function historyDelete($mixedPage)
  150. {
  151. if(is_array($mixedPage)) {
  152. if(!isset($mixedPage['name']))
  153. return;
  154. $mixedPage = $mixedPage['name'];
  155. }
  156. $oSession = BxDolSession::getInstance();
  157. $aHistory = $oSession->getValue(self::$sHistorySessionKey);
  158. if(empty($aHistory) || !isset($aHistory[$mixedPage]))
  159. return;
  160. unset($aHistory[$mixedPage]);
  161. $oSession->setValue(self::$sHistorySessionKey, $aHistory);
  162. }
  163. public function setPageName($sName)
  164. {
  165. $this->sPageName = $sName;
  166. }
  167. public function setContent($sPosition, $mixedContent)
  168. {
  169. $this->aItems[$sPosition] = $mixedContent;
  170. }
  171. public function setSelected($sPosition, $sValue)
  172. {
  173. if(!isset($this->aItems[$sPosition]['menu_items'][$sValue]))
  174. return;
  175. $this->aItems[$sPosition]['menu_items'][$sValue]['selected'] = true;
  176. }
  177. public function setVisible($sPosition, $bValue = true)
  178. {
  179. $this->aVisible[$sPosition] = $bValue;
  180. }
  181. public function setVisibleAll($bValue = true)
  182. {
  183. $this->aVisible = array(
  184. BX_DOL_STUDIO_MT_LEFT => $bValue,
  185. BX_DOL_STUDIO_MT_CENTER => $bValue,
  186. BX_DOL_STUDIO_MT_RIGHT => $bValue
  187. );
  188. }
  189. }
  190. /** @} */