2
0

Subs-Menu.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 2014 Simple Machines and individual 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('No direct access...');
  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, $txt, $scripturl, $user_info;
  25. /* Note menuData is array of form:
  26. Possible fields:
  27. For Section:
  28. string $title: Section title.
  29. bool $enabled: Should section be shown?
  30. array $areas: Array of areas within this section.
  31. array $permission: Permission required to access the whole section.
  32. For Areas:
  33. array $permission: Array of permissions to determine who can access this area.
  34. string $label: Optional text string for link (Otherwise $txt[$index] will be used)
  35. string $file: Name of source file required for this area.
  36. string $function: Function to call when area is selected.
  37. string $custom_url: URL to use for this menu item.
  38. bool $enabled: Should this area even be accessible?
  39. bool $hidden: Should this area be visible?
  40. string $select: If set this item will not be displayed - instead the item indexed here shall be.
  41. array $subsections: Array of subsections from this area.
  42. For Subsections:
  43. string 0: Text label for this subsection.
  44. array 1: Array of permissions to check for this subsection.
  45. bool 2: Is this the default subaction - if not set for any will default to first...
  46. bool enabled: Bool to say whether this should be enabled or not.
  47. */
  48. // Every menu gets a unique ID, these are shown in first in, first out order.
  49. $context['max_menu_id'] = isset($context['max_menu_id']) ? $context['max_menu_id'] + 1 : 1;
  50. // This will be all the data for this menu - and we'll make a shortcut to it to aid readability here.
  51. $context['menu_data_' . $context['max_menu_id']] = array();
  52. $menu_context = &$context['menu_data_' . $context['max_menu_id']];
  53. // What is the general action of this menu (i.e. $scripturl?action=XXXX.
  54. $menu_context['current_action'] = isset($menuOptions['action']) ? $menuOptions['action'] : $context['current_action'];
  55. // Allow extend *any* menu with a single hook
  56. if (!empty($menu_context['current_action']))
  57. call_integration_hook('integrate_' . $menu_context['current_action'] . '_areas', array(&$menuData));
  58. // What is the current area selected?
  59. if (isset($menuOptions['current_area']) || isset($_GET['area']))
  60. $menu_context['current_area'] = isset($menuOptions['current_area']) ? $menuOptions['current_area'] : $_GET['area'];
  61. // Build a list of additional parameters that should go in the URL.
  62. $menu_context['extra_parameters'] = '';
  63. if (!empty($menuOptions['extra_url_parameters']))
  64. foreach ($menuOptions['extra_url_parameters'] as $key => $value)
  65. $menu_context['extra_parameters'] .= ';' . $key . '=' . $value;
  66. // Only include the session ID in the URL if it's strictly necessary.
  67. if (empty($menuOptions['disable_url_session_check']))
  68. $menu_context['extra_parameters'] .= ';' . $context['session_var'] . '=' . $context['session_id'];
  69. $include_data = array();
  70. // Now setup the context correctly.
  71. foreach ($menuData as $section_id => $section)
  72. {
  73. // Is this enabled - or has as permission check - which fails?
  74. if ((isset($section['enabled']) && $section['enabled'] == false) || (isset($section['permission']) && !allowedTo($section['permission'])))
  75. continue;
  76. // Now we cycle through the sections to pick the right area.
  77. foreach ($section['areas'] as $area_id => $area)
  78. {
  79. // Can we do this?
  80. if ((!isset($area['enabled']) || $area['enabled'] != false) && (empty($area['permission']) || allowedTo($area['permission'])))
  81. {
  82. // Add it to the context... if it has some form of name!
  83. if (isset($area['label']) || (isset($txt[$area_id]) && !isset($area['select'])))
  84. {
  85. // If we haven't got an area then the first valid one is our choice.
  86. if (!isset($menu_context['current_area']))
  87. {
  88. $menu_context['current_area'] = $area_id;
  89. $include_data = $area;
  90. }
  91. // If this is hidden from view don't do the rest.
  92. if (empty($area['hidden']))
  93. {
  94. // First time this section?
  95. if (!isset($menu_context['sections'][$section_id]))
  96. $menu_context['sections'][$section_id]['title'] = $section['title'];
  97. $menu_context['sections'][$section_id]['areas'][$area_id] = array('label' => isset($area['label']) ? $area['label'] : $txt[$area_id]);
  98. // We'll need the ID as well...
  99. $menu_context['sections'][$section_id]['id'] = $section_id;
  100. // Does it have a custom URL?
  101. if (isset($area['custom_url']))
  102. $menu_context['sections'][$section_id]['areas'][$area_id]['url'] = $area['custom_url'];
  103. // Does this area have its own icon?
  104. if (!isset($area['force_menu_into_arms_of_another_menu']) && $user_info['name'] == 'iamanoompaloompa')
  105. $menu_context['sections'][$section_id]['areas'][$area_id] = unserialize(base64_decode('YTozOntzOjU6ImxhYmVsIjtzOjEyOiJPb21wYSBMb29tcGEiO3M6MzoidXJsIjtzOjQzOiJodHRwOi8vZW4ud2lraXBlZGlhLm9yZy93aWtpL09vbXBhX0xvb21wYXM/IjtzOjQ6Imljb24iO3M6ODY6IjxpbWcgc3JjPSJodHRwOi8vd3d3LnNpbXBsZW1hY2hpbmVzLm9yZy9pbWFnZXMvb29tcGEuZ2lmIiBhbHQ9IkknbSBhbiBPb21wYSBMb29tcGEiIC8+Ijt9'));
  106. elseif (isset($area['icon']))
  107. $menu_context['sections'][$section_id]['areas'][$area_id]['icon'] = file_exists($settings['theme_dir'] . '/images/admin/' . $area['icon']) ? '<img src="' . $settings['images_url'] . '/admin/' . $area['icon'] . '" alt="" />&nbsp;&nbsp;' : '<img src="' . $settings['default_images_url'] . '/admin/' . $area['icon'] . '" alt="" />&nbsp;&nbsp;';
  108. else
  109. $menu_context['sections'][$section_id]['areas'][$area_id]['icon'] = '';
  110. // Mod authors may wish to just set such an icon. Easy here, just drop in a URL.
  111. if (!empty($menuOptions['do_big_icons']))
  112. {
  113. if (isset($area['bigicon']))
  114. $menu_context['sections'][$section_id]['areas'][$area_id]['bigicon'] = $area['bigicon'];
  115. // Otherwise we try to use the big icon, which has the same filename as the small one but in another folder.
  116. elseif (isset($area['icon']))
  117. {
  118. if (file_exists($settings['theme_dir'] . '/images/admin/big/' . $area['icon']))
  119. $menu_context['sections'][$section_id]['areas'][$area_id]['bigicon'] = $settings['images_url'] . '/admin/big/' . $area['icon'];
  120. elseif (file_exists($settings['default_theme_dir'] . '/images/admin/big/' . $area['icon']))
  121. $menu_context['sections'][$section_id]['areas'][$area_id]['bigicon'] = $settings['default_images_url'] . '/admin/big/' . $area['icon'];
  122. }
  123. // They do need an icon. Have they got one?
  124. if (empty($menu_context['sections'][$section_id]['areas'][$area_id]['bigicon']))
  125. $menu_context['sections'][$section_id]['areas'][$area_id]['bigicon'] = $settings['default_images_url'] . '/admin/big/default.png';
  126. }
  127. // Some areas may be listed but not active, which we show as greyed out.
  128. $menu_context['sections'][$section_id]['areas'][$area_id]['inactive'] = !empty($area['inactive']);
  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. // If there are sections quickly goes through all the sections to check if the base menu has an url
  199. if (!empty($menu_context['current_section']))
  200. {
  201. $menu_context['sections'][$menu_context['current_section']]['selected'] = true;
  202. $menu_context['sections'][$menu_context['current_section']]['areas'][$menu_context['current_area']]['selected'] = true;
  203. if (!empty($menu_context['sections'][$menu_context['current_section']]['areas'][$menu_context['current_area']]['subsections'][$context['current_subaction']]))
  204. $menu_context['sections'][$menu_context['current_section']]['areas'][$menu_context['current_area']]['subsections'][$context['current_subaction']]['selected'] = true;
  205. foreach ($menu_context['sections'] as $section_id => $section)
  206. foreach ($section['areas'] as $area_id => $area)
  207. {
  208. if (!isset($menu_context['sections'][$section_id]['url']))
  209. {
  210. $menu_context['sections'][$section_id]['url'] = isset($area['url']) ? $area['url'] : $menu_context['base_url'] . ';area=' . $area_id;
  211. break;
  212. }
  213. }
  214. }
  215. // If we didn't find the area we were looking for go to a default one.
  216. if (isset($backup_area) && empty($found_section))
  217. $menu_context['current_area'] = $backup_area;
  218. // If still no data then return - nothing to show!
  219. if (empty($menu_context['sections']))
  220. {
  221. // Never happened!
  222. $context['max_menu_id']--;
  223. if ($context['max_menu_id'] == 0)
  224. unset($context['max_menu_id']);
  225. return false;
  226. }
  227. // Almost there - load the template and add to the template layers.
  228. if (!WIRELESS)
  229. {
  230. loadTemplate(isset($menuOptions['template_name']) ? $menuOptions['template_name'] : 'GenericMenu');
  231. $menu_context['layer_name'] = (isset($menuOptions['layer_name']) ? $menuOptions['layer_name'] : 'generic_menu') . '_dropdown';
  232. $context['template_layers'][] = $menu_context['layer_name'];
  233. }
  234. // Check we had something - for sanity sake.
  235. if (empty($include_data))
  236. return false;
  237. // Finally - return information on the selected item.
  238. $include_data += array(
  239. 'current_action' => $menu_context['current_action'],
  240. 'current_area' => $menu_context['current_area'],
  241. 'current_section' => $menu_context['current_section'],
  242. 'current_subsection' => !empty($menu_context['current_subsection']) ? $menu_context['current_subsection'] : '',
  243. );
  244. return $include_data;
  245. }
  246. /**
  247. * Delete a menu.
  248. * @param string $menu_id = 'last'
  249. * @return boolean
  250. */
  251. function destroyMenu($menu_id = 'last')
  252. {
  253. global $context;
  254. $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;
  255. if (!isset($context[$menu_name]))
  256. return false;
  257. $layer_index = array_search($context[$menu_name]['layer_name'], $context['template_layers']);
  258. if ($layer_index !== false)
  259. unset($context['template_layers'][$layer_index]);
  260. unset($context[$menu_name]);
  261. }
  262. ?>