<?php
/**
 * @file
 * Taxonomy wrangler main module file
 */

/**
 * Implements hook_menu_alter().
 *
 * Override the main taxonomy overview page.
 */
function taxonomy_wrangler_menu_alter(&$items) {
  $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name']['page arguments'] = array('taxonomy_wrangler_overview_terms', 3);
  $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name']['file'] = 'taxonomy_wrangler.admin.inc';
  $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name']['file path'] = drupal_get_path('module', 'taxonomy_wrangler');
}

/**
 * Implements hook_theme().
 */
function taxonomy_wrangler_theme() {
  return array(
    'taxonomy_wrangler_overview_terms' => array(
      'render element' => 'form',
    ),
  );
}

/**
 * Copy of drupal_add_tabledrag().
 *
 * This is a copy of drupal_add_tabledrag used just to instantiate
 * our own tabledrag class vs Drupal's.
 * @see drupal_add_tabledrag
 */
function taxonomy_wrangler_add_tabledrag($table_id, $action, $relationship, $group, $subgroup = NULL, $source = NULL, $hidden = TRUE, $limit = 0) {
  $js_added = &drupal_static(__FUNCTION__, FALSE);
  if (!$js_added) {
    // Add the table drag JavaScript to the page before the module JavaScript
    // to ensure that table drag behaviors are registered before any module
    // uses it.
    drupal_add_library('system', 'jquery.cookie');
    drupal_add_js('misc/tabledrag.js', array('weight' => -1));
    $js_added = TRUE;
  }

  // If a subgroup or source isn't set, assume it is the same as the group.
  $target = isset($subgroup) ? $subgroup : $group;
  $source = isset($source) ? $source : $target;
  $settings['taxonomyWrangler']['tables'][$table_id][$group][] = array(
    'target' => $target,
    'source' => $source,
    'relationship' => $relationship,
    'action' => $action,
    'hidden' => $hidden,
    'limit' => $limit,
  );
  drupal_add_js($settings, 'setting');
}
