<?php
/**
 * $Id: dhtml_menu.module,v 1.6.2.14 2007/11/28 21:12:57 brmassa Exp $
 * @file dhtml_menu.module
 * DHTML menus allow click expansion of the menu tree via javascript, with
 * graceful degradation.
 *
 * @todo Provide all menus that are both parent menus and have
 * important content. This adds a pseudo menu item as the first item in
 * the container, and lets you rename it too.
 * @todo Finish the item bullet feature
 */

/**
 * Implementation of hook_block().
 * Basically a copy of menu_block(). We're providing our own version
 * of each menu.
 */
function dhtml_menu_block($op = 'list', $delta = 0) {
  global $user;
  if ($op == 'list') {
    $blocks = array();
    $root_menus = menu_get_root_menus();
    foreach ($root_menus as $mid => $title) {
      $blocks[$mid]['info'] = t('DHTML: ') . $title;
    }
    return $blocks;
  }
  else if ($op == 'view') {
    $item = menu_get_item($delta);
    if ($user->uid > 0 && check_plain($item['title']) == t('Navigation') ) {
      $data['subject'] = $user->name;
    }
    else {
      $data['subject'] = check_plain($item['title']);
    }
    $data['content'] = theme('dhtml_menu_tree', $delta);
    return $data;
  }
}

/**
 * Implementation of hook_menu().
 */
function dhtml_menu_menu($may_cache) {
  if (!empty($may_cache)) {
    $items[] = array(
    'path'               => 'admin/settings/dhtml_menu',
    'title'              => t('DHTML Menu'),
    'callback'           => 'drupal_get_form',
    'callback arguments' => array('dhtml_menu_settings'),
    'access'             => user_access('administer menu'),
    'type'               => MENU_NORMAL_ITEM
    );
    return $items;
  }
}

/**
 * DHTML Menu Settings page.
 * @ingroup form
 */
function dhtml_menu_settings() {
  $form['dhtml_menu_use_effects'] = array(
    '#type'          => 'checkbox',
    '#title'         => t('jQuery slide effect'),
    '#description'   => t('Menu slides when it opens and closes.'),
    '#default_value' => variable_get('dhtml_menu_use_effects', 0)
  );
//   $form['dhtml_menu_bullet'] = array(
//     '#type'          => 'checkbox',
//     '#title'         => t('Item bullets'),
//     '#description'   => t('The menu only collapses/exapand when clicking over the item bullet. WARNING: some themes do not show bullets!'),
//     '#default_value' => variable_get('dhtml_menu_bullet', 0)
//   );

  $form['dhtml_menu_duplicated'] = array(
    '#type' => 'textarea',
    '#title' => t('Duplicated menu items'),
    '#description' => t('You can specify a list of menu items that should be displayed twice: Once as a parent item that expands and collapses the sub-menu and again as its own child item, which points directly to the page (the parent items still links on double-click). Enter one internal path (like "admin") on each line. To specify a custom link text, put it after the path, separated by a space.'),
    '#rows' => 4,
    '#default_value' => variable_get('dhtml_menu_duplicated', 'admin'),
  );
 
  return system_settings_form($form);
}

/**
 * Fetch duplicated menu items - this is a configurable setting.
 */
function _dhtml_get_add_links() {
  static $dhtml_menu_duplicated;
  if ($dhtml_menu_duplicated) return $dhtml_menu_duplicated;

  $dhtml_menu_duplicated = array();
  $text = variable_get('dhtml_menu_duplicated', 'admin');
  $text = explode("\n", $text);

  if ($text) {
    foreach ($text as $line) {
      $line = trim($line);
      preg_match('/^([^ ]+)( (.*))?$/', $line, $match);
      $dhtml_menu_duplicated[$match[1]] = empty($match[3]) ? TRUE : $match[3] ;
    }
  }
  return $dhtml_menu_duplicated;  
}

/**
 * Convert menus into DHTML menus
 * @param $pid
 *    Number, parent menu item ID
 */
function dhtml_menu_tree($pid = 1) {
  $menu = menu_get_menu();
  $output = '';
  if (isset($menu['visible'][$pid]) && !empty($menu['visible'][$pid]['children'])) {
    foreach ($menu['visible'][$pid]['children'] as $mid) {
      $counter = !empty($menu['visible'][$mid]['children']) ? 0 : 1;
      $output .= theme('dhtml_menu_item', $mid, theme('dhtml_menu_tree', $mid, false), $counter);
    }
  }
  return $output;
}

/**
 * Format each menu item
 * @ingroup themable
 */
function theme_dhtml_menu_item($mid, $children = '', $leaf = true) {
  static $expanded = null;
  $active = $display = '';

  if (!is_array($expanded)) {
    $expanded = explode(',', $_COOKIE['dhtml_menu']);
  }
  if ($mid == menu_get_active_nontask_item()) {
    $active = ' active';
  }
  if ($leaf) {
    return "  <li class=\"leaf$active\">". menu_item_link($mid) ."</li>\n";
  }
  else {
    $menu = menu_get_menu();

    $type = isset($menu['visible'][$mid]['type']) ? $menu['visible'][$mid]['type'] : null;
    if (menu_in_active_trail($mid) || ($type & MENU_EXPANDED) || in_array("sub$mid", $expanded)) {
      $state = 'expanded';
    }
    else {
      $state = 'collapsed';
      $display = 'style="display: none;"';
    }
    $output = "<li class=\"menutitle $state\" id=\"menu-sub$mid\" >";
    $output .= menu_item_link($mid);
    $output .= "<div class=\"submenu\" id=\"sub$mid\" $display>\n";
    $output .= "$children\n</div>\n";
    $output .= "</li>\n";
    return $output;
  }
}

/**
 * Format each menu tree
 * @ingroup themable
 */
function theme_dhtml_menu_tree($pid = 1, $toplevel = true) {
  if ($tree = dhtml_menu_tree($pid)) {
    $add_links = _dhtml_get_add_links();
    $menu = menu_get_menu();
    if (!empty($add_links[$menu['items'][$pid]['path']])) {
      $duplication = menu_get_item($pid);
      if ($add_links[$menu['items'][$pid]['path']] !== TRUE) {
        $duplication['title'] = $add_links[$menu['items'][$pid]['path']];
      }
      $tree = "<li class='leaf menu-duplicated'>".
        theme('menu_item_link', $duplication, $duplication) ."</li>\n" . $tree;
    }

    $class = 'menu dhtml_menu';
    if (!empty($toplevel)) {
      $class .= ' menu-root';
      $path = drupal_get_path('module', 'dhtml_menu');
      drupal_add_js(array('dhtmlMenu' => array(
        'useEffects' => variable_get('dhtml_menu_use_effects', 0))), 'setting');
      drupal_add_js($path .'/dhtml_menu.js');
    }
    return "\n<ul class=\"$class\">\n$tree\n</ul>\n";
  }
}

// panels 2 integration
function dhtml_menu_panels_block_info($module, $delta, &$info) {
  $info['icon'] = 'icon_core_block_menu.png';
  $info['category'] = array(t('Menus'), -2); 
}

// function dhtml_menu_item_link($item, $link_item) {
//   $add_links = _dhtml_get_add_links();
//   $title = ($link_item['path'] && $add_links[$link_item['path']] ? $add_links[$link_item['path']] : $item['title']);
//   return l($title, $link_item['path'], isset($item['description']) ? array('title' => $item['description']) : array());
// }
