You are here

advanced_menu_menu_admin.module in Advanced Menu 7

Same filename and directory in other branches
  1. 6.x menu_admin/advanced_menu_menu_admin.module

Provides advanced menu permissioning.

File

menu_admin/advanced_menu_menu_admin.module
View 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) {
  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($form['menu_name']['#default_value']),
          '#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':
      $menu = menu_load($form['original_item']['#value']['menu_name']);
      $item = menu_link_load($form['mlid']['#value']);
      if (!$item) {
        $item = array(
          'link_title' => '',
          'mlid' => 0,
          'plid' => 0,
          'menu_name' => $menu['menu_name'],
          'weight' => 0,
          'link_path' => '',
          'options' => array(),
          'module' => 'menu',
          'expanded' => 0,
          'hidden' => 0,
          'has_children' => 0,
        );
      }

      // 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'] . ':' . (isset($item['plid']) ? $item['plid'] : 0);
      if (!isset($options[$default])) {
        $default = 'navigation:0';
      }
      $form['parent'] = array(
        '#type' => 'select',
        '#title' => t('Parent link'),
        '#default_value' => $default,
        '#options' => $options,
        '#description' => t('The maximum depth for a link and all its children is fixed at !maxdepth. Some menu links may not be available as parents if selecting them would exceed this limit.', array(
          '!maxdepth' => MENU_MAX_DEPTH,
        )),
        '#attributes' => array(
          'class' => array(
            'menu-title-select',
          ),
        ),
      );
      $form['submit']['#weight'] = 2;
      $form['#submit'][] = 'advanced_menu_menu_admin_form_submit';
      break;
  }
}

/**
 * Helper function to determine current roles allowed to edit the menu.
 */
function _advanced_menu_menu_admin_roles($menu) {
  $roles = db_select('advanced_menu_menu_admin_roles', 'r')
    ->condition('r.menu_name', $menu)
    ->fields('r', array(
    'rid',
  ))
    ->execute();
  $advanced_menu_menu_admin_roles = array();
  foreach ($roles as $role) {
    $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;
  $roleMenus = array();
  $role_select = db_select('advanced_menu_menu_admin_roles', 'r')
    ->fields('r', array(
    'menu_name',
  ))
    ->fields('c', array(
    'title',
  ));
  $role_ids = array();
  foreach ($user->roles as $key => $value) {
    $role_ids[] = $key;
  }
  $role_select
    ->condition('rid', $role_ids, 'IN');
  $role_select
    ->join('menu_custom', 'c', 'r.menu_name = c.menu_name');
  $role_result = $role_select
    ->execute();
  foreach ($role_result as $role_menu) {
    $roleMenus[$role_menu->menu_name] = $role_menu->title;
  }
  if ($user->uid == 1) {
    $roleMenus = menu_get_menus();
  }
  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_delete('advanced_menu_menu_admin_roles')
        ->condition('menu_name', $menu_name)
        ->execute();
      foreach ($menu['roles'] as $role) {
        if ($role != 0) {
          db_insert('advanced_menu_menu_admin_roles')
            ->fields(array(
            'menu_name' => $menu_name,
            'rid' => $role,
          ))
            ->execute();
        }
      }
      break;
  }
  menu_rebuild();
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 *
 * Adds menu item fields to the node form.
 *
 * @see menu_node_submit()
 */
function advanced_menu_menu_admin_form_node_form_alter(&$form, $form_state) {

  // Generate a list of possible parents.
  // @todo This must be handled in a #process handler.
  $type = $form['#node']->type;

  // 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, $type);

  // If no possible parent menu items were found, there is nothing to display.
  if (empty($options)) {
    return;
  }
  $link = $form['#node']->menu;
  $form['menu'] = array(
    '#type' => 'fieldset',
    '#title' => t('Menu settings'),
    '#access' => advanced_menu_core_access('administer menu', 'edit node'),
    '#collapsible' => TRUE,
    '#collapsed' => !$link['link_title'],
    '#group' => 'additional_settings',
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'menu') . '/menu.js',
      ),
    ),
    '#tree' => TRUE,
    '#weight' => -2,
    '#attributes' => array(
      'class' => array(
        'menu-link-form',
      ),
    ),
  );
  $form['menu']['enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Provide a menu link'),
    '#default_value' => (int) (bool) $link['mlid'],
  );
  $form['menu']['link'] = array(
    '#type' => 'container',
    '#parents' => array(
      'menu',
    ),
    '#states' => array(
      'invisible' => array(
        'input[name="menu[enabled]"]' => array(
          'checked' => FALSE,
        ),
      ),
    ),
  );

  // Populate the element with the link data.
  foreach (array(
    'mlid',
    'module',
    'hidden',
    'has_children',
    'customized',
    'options',
    'expanded',
    'hidden',
    'parent_depth_limit',
  ) as $key) {
    $form['menu']['link'][$key] = array(
      '#type' => 'value',
      '#value' => $link[$key],
    );
  }
  $form['menu']['link']['link_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Menu link title'),
    '#default_value' => $link['link_title'],
  );
  $form['menu']['link']['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => isset($link['options']['attributes']['title']) ? $link['options']['attributes']['title'] : '',
    '#rows' => 1,
    '#description' => t('Shown when hovering over the menu link.'),
  );
  $default = $link['mlid'] ? $link['menu_name'] . ':' . $link['plid'] : variable_get('menu_parent_' . $type, 'main-menu:0');

  // @todo This will fail with the new selective menus per content type.
  if (!isset($options[$default])) {
    $default = 'navigation:0';
  }
  $form['menu']['link']['parent'] = array(
    '#type' => 'select',
    '#title' => t('Parent item'),
    '#default_value' => $default,
    '#options' => $options,
    '#attributes' => array(
      'class' => array(
        'menu-parent-select',
      ),
    ),
  );
  $form['menu']['link']['weight'] = array(
    '#type' => 'weight',
    '#title' => t('Weight'),
    '#delta' => 50,
    '#default_value' => $link['weight'],
    '#description' => t('Menu links with smaller weights are displayed before links with larger weights.'),
  );
}

Functions

Namesort descending Description
advanced_menu_menu_admin_form_alter Implementation of hook_form_alter().
advanced_menu_menu_admin_form_node_form_alter Implements hook_form_BASE_FORM_ID_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.