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