Subs-Menu.php 12 KB

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