<?php
// $Id: menu_admin_per_menu.module,v 1.6 2010/10/29 10:31:08 anrikun Exp $

/**
 * @file menu_admin_per_menu.module
 * Main file for the menu_admin_per_menu module.
 *
 * By default, Drupal allows only users with administrer menu permission to add, modify or delete menu items.
 * In case you want for instance to let certain users manage primary links or secondary links but not navigation menu,
 * this module provides this functionality.
 * Developped by Henri MEDOT <henri.medot[AT]absyx[DOT]fr>
 * http://www.absyx.fr
 */

/**
 * Implementation of hook_permission().
 */
function menu_admin_per_menu_permission() {
  $perms = array();
  $menus = menu_get_menus();
  foreach ($menus as $name => $title) {
    $perms['administer '. $name .' menu items'] = array('title' => t('Administer %menu menu items', array('%menu' => $title)));
  }
  return $perms;
}

/**
 * Implementation of hook_menu_alter()
 */
function menu_admin_per_menu_menu_alter(&$items) {
  $items['admin/structure/menu']['page callback'] = '_menu_admin_per_menu_menu_overview_page';
  $items['admin/structure/menu']['access callback'] = '_menu_admin_per_menu_access';
  unset($items['admin/structure/menu']['access arguments']);
  $items['admin/structure/menu/manage/%menu']['access callback'] = '_menu_admin_per_menu_menu_access';
  $items['admin/structure/menu/manage/%menu']['access arguments'] = array(4);
  $items['admin/structure/menu/manage/%menu/add']['access callback'] = '_menu_admin_per_menu_menu_access';
  $items['admin/structure/menu/manage/%menu/add']['access arguments'] = array(4);
  $items['admin/structure/menu/item/%menu_link/edit']['access callback'] = '_menu_admin_per_menu_menu_link_access';
  $items['admin/structure/menu/item/%menu_link/edit']['access arguments'] = array(4);
  $items['admin/structure/menu/item/%menu_link/reset']['access callback'] = '_menu_admin_per_menu_menu_link_access';
  $items['admin/structure/menu/item/%menu_link/reset']['access arguments'] = array(4);
  $items['admin/structure/menu/item/%menu_link/delete']['access callback'] = '_menu_admin_per_menu_menu_link_access';
  $items['admin/structure/menu/item/%menu_link/delete']['access arguments'] = array(4);
}

function _menu_admin_per_menu_menu_overview_page() {
  if (user_access('administer menu')) {
    return menu_overview_page();
  }

  $result = db_query("SELECT * FROM {menu_custom} ORDER BY title", array(), array('fetch' => PDO::FETCH_ASSOC));
  $header = array(t('Title'), array('data' => t('Operations'), 'colspan' => '2'));
  $rows = array();
  foreach ($result as $menu) {
    if (user_access('administer '. $menu['menu_name'] .' menu items')) {
      $row = array(theme('menu_admin_overview', array('title' => $menu['title'], 'name' => $menu['menu_name'], 'description' => $menu['description'])));
      $row[] = array('data' => l(t('list links'), 'admin/structure/menu/manage/' . $menu['menu_name']));
      $row[] = array('data' => l(t('add link'), 'admin/structure/menu/manage/' . $menu['menu_name'] . '/add'));
      $rows[] = $row;
    }
  }

  return theme('table', array('header' => $header, 'rows' => $rows));
}

function _menu_admin_per_menu_access() {
  if (user_access('administer menu')) {
    return TRUE;
  }
  $perms = array_keys(menu_admin_per_menu_permission());
  foreach ($perms as $perm) {
    if (user_access($perm)) {
      return TRUE;
    }
  }
  return FALSE;
}

function _menu_admin_per_menu_menu_access($menu = NULL) {
  if (user_access('administer menu')
   || user_access('administer '. $menu['menu_name'] .' menu items')) {
    return TRUE;
  }
  return FALSE;
}

function _menu_admin_per_menu_menu_link_access($menu_link = NULL) {
  if (user_access('administer menu')
   || user_access('administer '. $menu_link['menu_name'] .' menu items')) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implementation of hook_form_alter()
 */
function menu_admin_per_menu_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['menu']['link']['parent']) && !user_access('administer menu')) {
    $form['menu']['#access'] = FALSE;
    if (_menu_admin_per_menu_filter_parent_options($form['menu']['link'])) {
      $option_keys = array_keys($form['menu']['link']['parent']['#options']);
      $form['menu']['#access'] = TRUE;
      if (empty($form['menu']['link']['mlid']['#value']) && !in_array($form['menu']['link']['parent']['#default_value'], $option_keys)) {
        $form['menu']['link']['parent']['#default_value'] = $option_keys[0];
      }
    }
  }
}

function _menu_admin_per_menu_filter_parent_options(&$element) {
  $perm_menus = _menu_admin_per_menu_get_perm_menus();
  if ((count($perm_menus) > 0) && is_array($element['parent']['#options'])) {
    $has_mlid = !empty($element['mlid']['#value']);
    $default_value = $element['parent']['#default_value'];
    $option_keys = array_keys($element['parent']['#options']);
    foreach ($option_keys as $option_key) {
      list($menu, $item) = explode(':', $option_key);
      if (!in_array($menu, $perm_menus)) {
        unset($element['parent']['#options'][$option_key]);
        if ($has_mlid && ($option_key == $default_value)) {
          return FALSE;
        }
      }
    }
    return count($element['parent']['#options']) > 0;
  }
  return FALSE;
}

function _menu_admin_per_menu_get_perm_menus() {
  $perm_menus = array();
  $menus = menu_get_menus();
  foreach ($menus as $name => $title) {
    if (user_access('administer '. $name .' menu items')) {
      $perm_menus[] = $name;
    }
  }
  return $perm_menus;
}

/**
 * Implementation of hook_form_FORM_ID_alter()
 */
function menu_admin_per_menu_form_menu_edit_item_alter(&$form, &$form_state, $form_id) {
  if (isset($form['parent']) && !user_access('administer menu') && _menu_admin_per_menu_filter_parent_options($form)) {
    $option_keys = array_keys($form['parent']['#options']);
    if (empty($form['mlid']['#value']) && !in_array($form['parent']['#default_value'], $option_keys)) {
      $form['parent']['#default_value'] = $option_keys[0];
    }
  }
}
