advanced_menu_menu_admin.module in Advanced Menu 6.x
Same filename and directory in other branches
Provides advanced menu permissioning.
File
menu_admin/advanced_menu_menu_admin.moduleView source
<?php
// $Id: advanced_menu_menu_admin.module $
/**
* @file
* Provides advanced menu permissioning.
*/
/**
* Implementation of hook_form_alter().
*/
function advanced_menu_menu_admin_form_alter(&$form, $form_state, $form_id) {
$menu = $form;
switch ($form_id) {
case 'menu_edit_menu':
if (user_access('administer advanced menu permissions')) {
$form['submit']['#weight'] = 2;
$form['roles'] = array(
'#type' => 'checkboxes',
'#title' => t('Editors'),
'#default_value' => _advanced_menu_menu_admin_roles($menu),
'#options' => user_roles(TRUE),
'#description' => t('Roles that are checked will be able to administer this menu.'),
'#weight' => 1,
);
$form['#submit'][] = 'advanced_menu_menu_admin_form_submit';
}
break;
case 'menu_edit_item':
// Generate a list of possible parents (not including this item or descendants).
$menus = menu_get_menus();
if (!user_access('administer menu')) {
$menus = _advanced_menu_menu_admin_menus($menus);
}
$item = $form['menu']['#item'] ? $form['menu']['#item'] : menu_load($form['menu']['original_item']['#value']['menu_name']);
$options = menu_parent_options($menus, $item);
$default = $item['menu_name'] . ':' . (isset($item['plid']) ? $item['plid'] : 0);
if (!isset($options[$default])) {
$default = 'navigation:0';
}
$form['menu']['parent'] = array(
'#type' => 'select',
'#title' => t('Parent item'),
'#default_value' => $default,
'#options' => $options,
'#description' => t('The maximum depth for an item and all its children is fixed at !maxdepth. Some menu items may not be available as parents if selecting them would exceed this limit.', array(
'!maxdepth' => MENU_MAX_DEPTH,
)),
'#attributes' => array(
'class' => 'menu-title-select',
),
);
$form['submit']['#weight'] = 2;
$form['#submit'][] = 'advanced_menu_menu_admin_form_submit';
break;
default:
if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id) {
$form['menu'] = array(
'#type' => 'fieldset',
'#title' => t('Menu settings'),
'#access' => advanced_menu_core_access('administer menu', 'edit node'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#tree' => TRUE,
'#weight' => -2,
'#attributes' => array(
'class' => 'menu-item-form',
),
);
$item = $form['#node']->menu;
if ($item['mlid']) {
// There is an existing link.
$form['menu']['delete'] = array(
'#type' => 'checkbox',
'#title' => t('Delete this menu item.'),
);
}
if (!$item['link_title']) {
$form['menu']['#collapsed'] = TRUE;
}
foreach (array(
'mlid',
'module',
'hidden',
'has_children',
'customized',
'options',
'expanded',
'hidden',
'parent_depth_limit',
) as $key) {
$form['menu'][$key] = array(
'#type' => 'value',
'#value' => $item[$key],
);
}
$form['menu']['#item'] = $item;
$form['menu']['link_title'] = array(
'#type' => 'textfield',
'#title' => t('Menu link title'),
'#default_value' => $item['link_title'],
'#description' => t('The link text corresponding to this item that should appear in the menu. Leave blank if you do not wish to add this post to the menu.'),
'#required' => FALSE,
);
// Generate a list of possible parents (not including this item or descendants).
$menus = menu_get_menus();
if (!user_access('administer menu')) {
$menus = _advanced_menu_menu_admin_menus($menus);
}
$options = menu_parent_options($menus, $item);
$default = $item['menu_name'] . ':' . $item['plid'];
if (!isset($options[$default])) {
$default = 'primary-links:0';
}
$form['menu']['parent'] = array(
'#type' => 'select',
'#title' => t('Parent item'),
'#default_value' => $default,
'#options' => $options,
'#description' => t('The maximum depth for an item and all its children is fixed at !maxdepth. Some menu items may not be available as parents if selecting them would exceed this limit.', array(
'!maxdepth' => MENU_MAX_DEPTH,
)),
'#attributes' => array(
'class' => 'menu-title-select',
),
);
$form['#submit'][] = 'menu_node_form_submit';
$form['menu']['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight'),
'#delta' => 50,
'#default_value' => $item['weight'],
'#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
);
}
break;
}
}
/**
* Helper function to determine current roles allowed to edit the menu.
*/
function _advanced_menu_menu_admin_roles($menu) {
$roles = db_query("SELECT rid FROM {advanced_menu_menu_admin_roles} WHERE menu_name = '%s'", $menu['menu_name']['#value']);
$advanced_menu_menu_admin_roles = array();
while ($role = db_fetch_object($roles)) {
$advanced_menu_menu_admin_roles[] = $role->rid;
}
return $advanced_menu_menu_admin_roles;
}
/**
* Helper function to determine menus the current user can edit.
*/
function _advanced_menu_menu_admin_menus($menus = array()) {
global $user;
$select_condition = '';
$roleMenus = array();
foreach ($user->roles as $key => $value) {
if ($select_condition != '') {
$select_condition .= ' OR ';
}
$select_condition .= 'rid = ' . $key;
}
if ($select_condition != '') {
$res = db_query("SELECT r.menu_name, c.title FROM {advanced_menu_menu_admin_roles} r LEFT JOIN {menu_custom} c ON r.menu_name = c.menu_name WHERE (" . $select_condition . ")");
while ($resObj = db_fetch_object($res)) {
$roleMenus[$resObj->menu_name] = $resObj->title;
}
}
return $roleMenus;
}
/**
* Save role information for this menu.
*/
function advanced_menu_menu_admin_form_submit(&$form, $form_state) {
$menu = $form_state['values'];
$menu_name = '';
switch ($menu['form_id']) {
case 'menu_edit_menu':
if ($form['#insert']) {
$menu['menu_name'] = 'menu-' . $menu['menu_name'];
}
$menu_name = $menu['menu_name'];
db_query("DELETE FROM {advanced_menu_menu_admin_roles} WHERE menu_name = '%s'", $menu_name);
foreach ($menu['roles'] as $role) {
if ($role != 0) {
db_query("INSERT INTO {advanced_menu_menu_admin_roles} (menu_name, rid) VALUES ('%s', %d)", $menu_name, $role);
}
}
break;
}
menu_rebuild();
}
Functions
Name | Description |
---|---|
advanced_menu_menu_admin_form_alter | Implementation of hook_form_alter(). |
advanced_menu_menu_admin_form_submit | Save role information for this menu. |
_advanced_menu_menu_admin_menus | Helper function to determine menus the current user can edit. |
_advanced_menu_menu_admin_roles | Helper function to determine current roles allowed to edit the menu. |