Subs-Menu.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.0
  11. */
  12. if (!defined('SMF'))
  13. die('Hacking attempt...');
  14. /* This file contains a standard way of displaying side/drop down menus for SMF.
  15. */
  16. // Create a menu...
  17. function createMenu($menuData, $menuOptions = array())
  18. {
  19. global $context, $settings, $options, $txt, $modSettings, $scripturl, $smcFunc, $user_info, $sourcedir, $options;
  20. // First are we toggling use of the side bar generally?
  21. if (isset($_GET['togglebar']) && !$user_info['is_guest'])
  22. {
  23. // Save the new dropdown menu state.
  24. $smcFunc['db_insert']('replace',
  25. '{db_prefix}themes',
  26. array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  27. array(
  28. array(
  29. $user_info['id'],
  30. $settings['theme_id'],
  31. 'use_sidebar_menu',
  32. empty($options['use_sidebar_menu']) ? '1' : '0',
  33. ),
  34. ),
  35. array('id_member', 'id_theme', 'variable')
  36. );
  37. // Clear the theme settings cache for this user.
  38. $themes = explode(',', $modSettings['knownThemes']);
  39. foreach ($themes as $theme)
  40. cache_put_data('theme_settings-' . $theme . ':' . $user_info['id'], null, 60);
  41. // Redirect as this seems to work best.
  42. $redirect_url = isset($menuOptions['toggle_redirect_url']) ? $menuOptions['toggle_redirect_url'] : 'action=' . (isset($_GET['action']) ? $_GET['action'] : 'admin') . ';area=' . (isset($_GET['area']) ? $_GET['area'] : 'index') . ';sa=' . (isset($_GET['sa']) ? $_GET['sa'] : 'settings') . (isset($_GET['u']) ? ';u=' . $_GET['u'] : '') . ';' . $context['session_var'] . '=' . $context['session_id'];
  43. redirectexit($redirect_url);
  44. }
  45. // Work out where we should get our images from.
  46. $context['menu_image_path'] = file_exists($settings['theme_dir'] . '/images/admin/change_menu.png') ? $settings['images_url'] . '/admin' : $settings['default_images_url'] . '/admin';
  47. /* Note menuData is array of form:
  48. Possible fields:
  49. For Section:
  50. string $title: Section title.
  51. bool $enabled: Should section be shown?
  52. array $areas: Array of areas within this section.
  53. array $permission: Permission required to access the whole section.
  54. For Areas:
  55. array $permission: Array of permissions to determine who can access this area.
  56. string $label: Optional text string for link (Otherwise $txt[$index] will be used)
  57. string $file: Name of source file required for this area.
  58. string $function: Function to call when area is selected.
  59. string $custom_url: URL to use for this menu item.
  60. bool $enabled: Should this area even be accessible?
  61. bool $hidden: Should this area be visible?
  62. string $select: If set this item will not be displayed - instead the item indexed here shall be.
  63. array $subsections: Array of subsections from this area.
  64. For Subsections:
  65. string 0: Text label for this subsection.
  66. array 1: Array of permissions to check for this subsection.
  67. bool 2: Is this the default subaction - if not set for any will default to first...
  68. bool enabled: Bool to say whether this should be enabled or not.
  69. */
  70. // Every menu gets a unique ID, these are shown in first in, first out order.
  71. $context['max_menu_id'] = isset($context['max_menu_id']) ? $context['max_menu_id'] + 1 : 1;
  72. // This will be all the data for this menu - and we'll make a shortcut to it to aid readability here.
  73. $context['menu_data_' . $context['max_menu_id']] = array();
  74. $menu_context = &$context['menu_data_' . $context['max_menu_id']];
  75. // What is the general action of this menu (i.e. $scripturl?action=XXXX.
  76. $menu_context['current_action'] = isset($menuOptions['action']) ? $menuOptions['action'] : $context['current_action'];
  77. // What is the current area selected?
  78. if (isset($menuOptions['current_area']) || isset($_GET['area']))
  79. $menu_context['current_area'] = isset($menuOptions['current_area']) ? $menuOptions['current_area'] : $_GET['area'];
  80. // Build a list of additional parameters that should go in the URL.
  81. $menu_context['extra_parameters'] = '';
  82. if (!empty($menuOptions['extra_url_parameters']))
  83. foreach ($menuOptions['extra_url_parameters'] as $key => $value)
  84. $menu_context['extra_parameters'] .= ';' . $key . '=' . $value;
  85. // Only include the session ID in the URL if it's strictly necessary.
  86. if (empty($menuOptions['disable_url_session_check']))
  87. $menu_context['extra_parameters'] .= ';' . $context['session_var'] . '=' . $context['session_id'];
  88. $include_data = array();
  89. // Now setup the context correctly.
  90. foreach ($menuData as $section_id => $section)
  91. {
  92. // Is this enabled - or has as permission check - which fails?
  93. if ((isset($section['enabled']) && $section['enabled'] == false) || (isset($section['permission']) && !allowedTo($section['permission'])))
  94. continue;
  95. // Now we cycle through the sections to pick the right area.
  96. foreach ($section['areas'] as $area_id => $area)
  97. {
  98. // Can we do this?
  99. if ((!isset($area['enabled']) || $area['enabled'] != false) && (empty($area['permission']) || allowedTo($area['permission'])))
  100. {
  101. // Add it to the context... if it has some form of name!
  102. if (isset($area['label']) || (isset($txt[$area_id]) && !isset($area['select'])))
  103. {
  104. // If we haven't got an area then the first valid one is our choice.
  105. if (!isset($menu_context['current_area']))
  106. {
  107. $menu_context['current_area'] = $area_id;
  108. $include_data = $area;
  109. }
  110. // If this is hidden from view don't do the rest.
  111. if (empty($area['hidden']))
  112. {
  113. // First time this section?
  114. if (!isset($menu_context['sections'][$section_id]))
  115. $menu_context['sections'][$section_id]['title'] = $section['title'];
  116. $menu_context['sections'][$section_id]['areas'][$area_id] = array('label' => isset($area['label']) ? $area['label'] : $txt[$area_id]);
  117. // We'll need the ID as well...
  118. $menu_context['sections'][$section_id]['id'] = $section_id;
  119. // Does it have a custom URL?
  120. if (isset($area['custom_url']))
  121. $menu_context['sections'][$section_id]['areas'][$area_id]['url'] = $area['custom_url'];
  122. // Does this area have its own icon?
  123. if (!isset($area['force_menu_into_arms_of_another_menu']) && $user_info['name'] == 'iamanoompaloompa')
  124. $menu_context['sections'][$section_id]['areas'][$area_id] = unserialize(base64_decode('YTozOntzOjU6ImxhYmVsIjtzOjEyOiJPb21wYSBMb29tcGEiO3M6MzoidXJsIjtzOjQzOiJodHRwOi8vZW4ud2lraXBlZGlhLm9yZy93aWtpL09vbXBhX0xvb21wYXM/IjtzOjQ6Imljb24iO3M6ODY6IjxpbWcgc3JjPSJodHRwOi8vd3d3LnNpbXBsZW1hY2hpbmVzLm9yZy9pbWFnZXMvb29tcGEuZ2lmIiBhbHQ9IkknbSBhbiBPb21wYSBMb29tcGEiIC8+Ijt9'));
  125. elseif (isset($area['icon']))
  126. $menu_context['sections'][$section_id]['areas'][$area_id]['icon'] = '<img src="' . $context['menu_image_path'] . '/' . $area['icon'] . '" alt="" />&nbsp;&nbsp;';
  127. else
  128. $menu_context['sections'][$section_id]['areas'][$area_id]['icon'] = '';
  129. // Did it have subsections?
  130. if (!empty($area['subsections']))
  131. {
  132. $menu_context['sections'][$section_id]['areas'][$area_id]['subsections'] = array();
  133. $first_sa = $last_sa = null;
  134. foreach ($area['subsections'] as $sa => $sub)
  135. {
  136. if ((empty($sub[1]) || allowedTo($sub[1])) && (!isset($sub['enabled']) || !empty($sub['enabled'])))
  137. {
  138. if ($first_sa == null)
  139. $first_sa = $sa;
  140. $menu_context['sections'][$section_id]['areas'][$area_id]['subsections'][$sa] = array('label' => $sub[0]);
  141. // Custom URL?
  142. if (isset($sub['url']))
  143. $menu_context['sections'][$section_id]['areas'][$area_id]['subsections'][$sa]['url'] = $sub['url'];
  144. // A bit complicated - but is this set?
  145. if ($menu_context['current_area'] == $area_id)
  146. {
  147. // Save which is the first...
  148. if (empty($first_sa))
  149. $first_sa = $sa;
  150. // Is this the current subsection?
  151. if (isset($_REQUEST['sa']) && $_REQUEST['sa'] == $sa)
  152. $menu_context['current_subsection'] = $sa;
  153. // Otherwise is it the default?
  154. elseif (!isset($menu_context['current_subsection']) && !empty($sub[2]))
  155. $menu_context['current_subsection'] = $sa;
  156. }
  157. // Let's assume this is the last, for now.
  158. $last_sa = $sa;
  159. }
  160. // Mark it as disabled...
  161. else
  162. $menu_context['sections'][$section_id]['areas'][$area_id]['subsections'][$sa]['disabled'] = true;
  163. }
  164. // Set which one is first, last and selected in the group.
  165. if (!empty($menu_context['sections'][$section_id]['areas'][$area_id]['subsections']))
  166. {
  167. $menu_context['sections'][$section_id]['areas'][$area_id]['subsections'][$context['right_to_left'] ? $last_sa : $first_sa]['is_first'] = true;
  168. $menu_context['sections'][$section_id]['areas'][$area_id]['subsections'][$context['right_to_left'] ? $first_sa : $last_sa]['is_last'] = true;
  169. if ($menu_context['current_area'] == $area_id && !isset($menu_context['current_subsection']))
  170. $menu_context['current_subsection'] = $first_sa;
  171. }
  172. }
  173. }
  174. }
  175. // Is this the current section?
  176. if ($menu_context['current_area'] == $area_id && empty($found_section))
  177. {
  178. // Only do this once?
  179. $found_section = true;
  180. // Update the context if required - as we can have areas pretending to be others. ;)
  181. $menu_context['current_section'] = $section_id;
  182. $menu_context['current_area'] = isset($area['select']) ? $area['select'] : $area_id;
  183. // This will be the data we return.
  184. $include_data = $area;
  185. }
  186. // Make sure we have something in case it's an invalid area.
  187. elseif (empty($found_section) && empty($include_data))
  188. {
  189. $menu_context['current_section'] = $section_id;
  190. $backup_area = isset($area['select']) ? $area['select'] : $area_id;
  191. $include_data = $area;
  192. }
  193. }
  194. }
  195. }
  196. // Should we use a custom base url, or use the default?
  197. $menu_context['base_url'] = isset($menuOptions['base_url']) ? $menuOptions['base_url'] : $scripturl . '?action=' . $menu_context['current_action'];
  198. // What about the toggle url?
  199. $menu_context['toggle_url'] = isset($menuOptions['toggle_url']) ? $menuOptions['toggle_url'] : $menu_context['base_url'] . (!empty($menu_context['current_area']) ? ';area=' . $menu_context['current_area'] : '') . (!empty($menu_context['current_subsection']) ? ';sa=' . $menu_context['current_subsection'] : '') . $menu_context['extra_parameters'] . ';togglebar';
  200. // If we didn't find the area we were looking for go to a default one.
  201. if (isset($backup_area) && empty($found_section))
  202. $menu_context['current_area'] = $backup_area;
  203. // If still no data then return - nothing to show!
  204. if (empty($menu_context['sections']))
  205. {
  206. // Never happened!
  207. $context['max_menu_id']--;
  208. if ($context['max_menu_id'] == 0)
  209. unset($context['max_menu_id']);
  210. return false;
  211. }
  212. // What type of menu is this?
  213. if (empty($menuOptions['menu_type']))
  214. {
  215. $menuOptions['menu_type'] = '_' . (empty($options['use_sidebar_menu']) ? 'dropdown' : 'sidebar');
  216. $menu_context['can_toggle_drop_down'] = !$user_info['is_guest'] && isset($settings['theme_version']) && $settings['theme_version'] >= 2.0;
  217. }
  218. else
  219. $menu_context['can_toggle_drop_down'] = !empty($menuOptions['can_toggle_drop_down']);
  220. // Almost there - load the template and add to the template layers.
  221. if (!WIRELESS)
  222. {
  223. loadTemplate(isset($menuOptions['template_name']) ? $menuOptions['template_name'] : 'GenericMenu');
  224. $menu_context['layer_name'] = (isset($menuOptions['layer_name']) ? $menuOptions['layer_name'] : 'generic_menu') . $menuOptions['menu_type'];
  225. $context['template_layers'][] = $menu_context['layer_name'];
  226. }
  227. // Check we had something - for sanity sake.
  228. if (empty($include_data))
  229. return false;
  230. // Finally - return information on the selected item.
  231. $include_data += array(
  232. 'current_action' => $menu_context['current_action'],
  233. 'current_area' => $menu_context['current_area'],
  234. 'current_section' => $menu_context['current_section'],
  235. 'current_subsection' => !empty($menu_context['current_subsection']) ? $menu_context['current_subsection'] : '',
  236. );
  237. return $include_data;
  238. }
  239. // Delete a menu.
  240. function destroyMenu($menu_id = 'last')
  241. {
  242. global $context;
  243. $menu_name = $menu_id == 'last' && isset($context['max_menu_id']) && isset($context['menu_data_' . $context['max_menu_id']]) ? 'menu_data_' . $context['max_menu_id'] : 'menu_data_' . $menu_id;
  244. if (!isset($context[$menu_name]))
  245. return false;
  246. $layer_index = array_search($context[$menu_name]['layer_name'], $context['template_layers']);
  247. if ($layer_index !== false)
  248. unset($context['template_layers'][$layer_index]);
  249. unset($context[$menu_name]);
  250. }
  251. ?>