Subs-Menu.php 14 KB

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