Subs-Menu.php 13 KB

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