<?php
/**
 * @file alternative to core menu management
 *
 * Needed when menus get to big to load on one page.
 *
 * CONFLICTS WITH tiny_menu. Appropriately enough.
 *
 * Some of the code here - especially the form cache rebuild trigger in
 * bigmenu_slice_form_js() and the parameters sent on the AJAX URL string feel
 * quite ungainly.
 * However the main target - to be able to layer on this flexibility without
 * modifying core - made a few work-arounds neccessary.
 *
 * I did NOT use full Drupal core AHAH routines, as they seemed to rely on the
 * entire form being submitted and rebuilt in the background each page load.
 *
 * As the focus of this module is on *scaling* - I couldn't add that overhead,
 * so the subforms are generated independently, not as part of the main overview
 * form. This is why the bigmenu_slice_form_js() cheats form_cache a little bit.
 *
 * @author Dan (dman) Morrison dan@coders.co.nz
 * @version 2011
 */

/**
 * Declare admin links and AJAX callbacks
 *
 * hook_menu()
 */
function bigmenu_menu() {
  $items['admin/structure/bigmenu-customize/%menu'] = array(
    'title' => 'Customize menu',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('bigmmenu_overview_form', 3),
    'title callback' => 'menu_overview_title',
    'title arguments' => array(3),
    'access arguments' => array('use bigmenu'),
    'type' => MENU_CALLBACK,
  );
  // Edit just a part of a menu. Arg 5 is the parent menu link id
  $items['admin/structure/menu/manage/%menu/bigmenu-customize/subform/%menu_link'] = array(
    'title' => 'Edit a slice of a menu',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('bigmenu_slice_form', 4, 7),
    'title callback' => 'bigmenu_parent_title',
    'title arguments' => array(7),
    'access arguments' => array('use bigmenu'),
    'type' => MENU_CALLBACK,
    'file' => 'bigmenu.admin.inc',
  );
  // Same as above, but triggers an equivalent json response
  // This also expects arg 7,8 to be a form ID and form cache id
  $items['admin/structure/menu/manage/%menu/bigmenu-customize/subform/%menu_link/js'] = array(
    'page callback' => 'bigmenu_slice_form_js',
    'page arguments' => array(4, 7),
    'access arguments' => array('use bigmenu'),
    'type' => MENU_CALLBACK,
    'file' => 'bigmenu.admin.inc',
  );
  $items['admin/config/user-interface/bigmenu'] = array(
    'title' => t('Big Menu Settings'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('bigmenu_settings'),
    'description' => t('Configure settings for bigmenu module.'),
    'access arguments' => array('administer bigmenu'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'bigmenu.admin.inc',
  );

  return $items;
}

/**
 * Take over core menu admin page
 *
 * hook_menu_alter()
 *
 * TODO - should be an admin toggle for this setting
 */
function bigmenu_menu_alter(&$items) {
  $items['admin/structure/menu/manage/%menu']['page arguments'] = array('bigmenu_overview_form', 4);
  $items['admin/structure/menu/manage/%menu']['file'] = 'bigmenu.admin.inc';
  $items['admin/structure/menu/manage/%menu']['file path'] = drupal_get_path('module', 'bigmenu');
}

/**
 * Implemenation of hook_theme().
 */
function bigmenu_theme() {
  return array(
    'bigmenu_overview_form' => array(
      'file' => 'bigmenu.admin.inc',
      'arguments' => array('form' => NULL),
      'render element' => 'form',
    ),
  );
}

/**
 * Implements hook_permission().
 */
function bigmenu_permission() {
  return array(
    'administer bigmenu' =>  array(
      'title' => t('Administer Big Menu'),
      'description' => t('Allows configuration of Big Menu'),
    ),
    'use bigmenu' =>  array(
      'title' => t('Use Big Menu'),
      'description' => t('Allows the use of Big Menu'),
    ),
  );
}
