6 function menu_admin_page(){
10 if(user_access('menu admin', 'menu')){
11 $admin_links['Menu']['Settings'][] = array('href' => 'menu/admin', 'text' => 'Administer Menus', 'help' => 'Administer the menu module');
17 * Add tabs to the admin pages
19 function menu_admin_tabs($add = array()){
21 $skin_tabs = array('menu/admin' => 'List Menus', 'menu/new' => 'Add Menu');
23 foreach($add as $link => $content){
24 $skin_tabs[$link] = $content;
27 skin_tabs($skin_tabs);
31 * Helper function to sort menus
33 function _menu_admin_sort($parent){
35 //$all_regions = explode(',', $_POST['block_regions']);
37 //First lets see which menus we should be expecting
38 $menus = db_query("SELECT menu.mid FROM {PRE_}menu WHERE parent = ".$parent."", TRUE);
40 foreach($menus as $menu){
41 db_query("UPDATE {PRE_}menu SET pos = ".$_POST['menu_'.$menu['mid']]." WHERE mid = ".$menu['mid']." AND parent = '".$parent."'");
46 function menu_admin(){
51 $skin['page_title'] = "Menu Administration page";
53 $menus = db_query("SELECT * FROM {PRE_}menu WHERE parent = 0", TRUE);
55 $content = 'Select a menu from the list to edit it. <br>';
57 foreach($menus as $menu){
58 $content .= path_link('menu/admin/'.$menu['mid'], $menu['name']) . "<br>";
66 * Menu administration page
69 * The menu id to administer
71 * Use $db['num_rows'] when it works in order to see if a menu was found
73 function menu_admin_menu($mid){
80 if(isset($_POST['menu_sort'])){
81 _menu_admin_sort($mid);
84 $menus = db_query("SELECT * FROM {PRE_}menu WHERE parent = ".$mid." OR mid = ".$mid." ORDER BY POS ASC", TRUE);
86 //Go away if the menu does not exist
88 path_redirect("menu/admin");
91 core_load('form', 'api');
93 $content = 'Click on any menu to modify it. <br>';
94 $content .= 'Or click here to edit the ' . path_link('menu/admin/item/'.$mid, 'Parent Menu');
96 $content .= form_set_head('menu_admin_menu').
97 '<table id="menu_admin_menu">
106 foreach($menus as $m){
109 'name' => 'menu_'.$m['mid'],
111 'options' => array(-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10),
112 'value' => $m['pos'],
118 if($m['parent'] == $mid){
119 $parent = $m['name'];
123 <td>'.path_link('menu/admin/item/'.$m['mid'] ,$m['name']).'</td>
124 <td>'.$m['details'].'</td>
125 <td>'.$m['owner'].'</td>
126 <td>'.form_select_render_skin($form).'</td>
131 $form = array('name'=>'menu_sort', 'type' => 'submit', 'value' => 'Sort Them');
133 <td colspan="4" align="center">'.form_input_render_skin($form).'</td>
137 $content .= '</table></form>';
139 $skin['page_title'] = "Menu Administration page for: " . $parent;
145 function menu_admin_item($mid = NULL){
147 global $skin, $form_status;
149 $skin['page_title'] = 'Edit menu Item';
151 $menu = db_query("SELECT * FROM {PRE_}menu WHERE mid = ".$mid."", TRUE, TRUE);
153 menu_admin_tabs(array('menu/admin/'.$menu['parent'] => 'Back one Up'));
155 $form = form_get_form('menu_form_admin_item', $menu, 'edit');
157 $content = form_form($form);
159 if($form_status == 'delete_confirmed'){
160 $q = _menu_delete_item($mid);
163 if($_POST['parent'] == 0){
164 $path = 'menu/admin';
167 $path = 'menu/admin/'.$_POST['parent'];
169 path_redirect($path);
172 path_redirect('menu/admin/'.$mid);
176 if($form_status == 'verified'){ //isset($_POST['submit'])){
177 _menu_update_item($_POST);
185 function menu_form_admin_item($data = NULL, $state = 'new'){
187 $form['menu_item']['name'] = array(
191 $form['menu_item']['parent'] = array(
194 'options' => menu_get_menus(),
198 $form['menu_item']['url'] = array(
202 'help' => 'Use <front> for the front page'
204 $form['menu_item']['active'] = array(
205 'type' => 'checkbox',
209 $form['menu_item']['expand'] = array(
210 'type' => 'checkbox',
213 'help' => 'If you want this menu to be always expanded'
215 $form['menu_item']['target'] = array(
218 'options' => array('_parent', '_blank'),
221 $form['menu_item']['mid'] = array(
224 $form['menu_item']['details'] = array(
227 'help' => 'This will be used as the title'
229 $form['menu_item']['submit'] = array(
231 'value' => ($state != 'new' ? 'Edit Menu' : 'Add Menu')
233 if($data['owner'] == 'by_user'){
234 $form['menu_item']['form_delete'] = array(
236 'value' => 'Remove menu'
244 function _menu_update_item($info, $type = 'update'){
248 //Lets try to update the item
249 $uptade = db_query("UPDATE {PRE_}menu SET
250 name = '".$info['name']."',
251 parent = ".$info['parent'].",
252 url = '".$info['url']."',
253 active = ".(isset($info['active']) ? 1 : 0) .",
254 expand = ".(isset($info['expand']) ? 1 : 0) .",
255 target = '".$info['target']."',
256 details = '".$info['details']."'
257 WHERE mid = ".$info['mid']."
262 $insert = db_query("INSERT INTO {PRE_}menu (name, parent, url, active, expand, target, details, owner)
263 values('".$info['name']."',
266 ".(isset($info['active']) ? 1 : 0) .",
267 ".(isset($info['expand']) ? 1 : 0) .",
268 '".$info['target']."',
269 '".$info['details']."',
270 '".(isset($info['owner']) ? $info['owner'] : 'by_user')."')");
275 * Deletes a menu item
278 * The menu id to be deleted
281 function _menu_delete_item($mid){
285 //Lets see if this will cause bad parenting
286 $childs = db_query("SELECT * FROM {PRE_}menu WHERE parent = ".$mid."");
288 if($db['num_rows'] > 0){ print_r($childs); exit;
289 system_warnings("Sorry, deleting this item would be bad parenting. You must first delete or reasign its child menus.", "error");
293 $delete = db_query("DELETE FROM {PRE_}menu WHERE mid=".$mid."");
303 $form = form_get_form('menu_form_admin_item');
305 $content = form_form($form);
307 if($form_status == 'verified'){ //isset($_POST['submit'])){
308 _menu_update_item($_POST, 'new');
310 if($_POST['parent'] == 0){
311 $path = 'menu/admin';
314 $path = 'menu/admin/'.$_POST['parent'];
316 path_redirect($path);
323 function menu_get_menus(){
325 $menus = db_query("SELECT menu.mid as id, menu.parent, menu.name FROM {PRE_}menu ORDER BY parent", TRUE);
327 return _menu_get_sons($menus);
332 function _menu_get_sons($data, $parent = 0, $sep = '', $recursive = TRUE){
337 if(!isset($contents[0]))
338 $contents[0] = 'Root';
340 foreach($data as $item){
341 if($item['parent'] == $parent){
342 $contents[$item['id']] = $sep . $item['name'];
345 $sons = _menu_get_sons($data, $item['id'], $sep . '--');
346 if(count($sons) > 0){
347 foreach($sons as $son => $info){
348 $contents[$son] = $info;