Subs-Menu.php 12 KB

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