<?php
// $Id: location_taxonomy.module,v 1.1.2.2 2009/06/29 14:34:54 bdragon Exp $

/**
 * @file
 * Associate locations with taxonomy terms.
 */

/**
 * NOTE: At the moment, you'll have to do your own theming.
 * To load locations for a tid:
 * $locations = location_load_locations('taxonomy:'. $tid, 'genid');
 */

/**
 * Implementation of hook_taxonomy().
 */
function location_taxonomy_taxonomy($op, $type, $array = NULL) {
  if ($type == 'vocabulary') {
    switch ($op) {
      case 'insert':
      case 'update':
        if (isset($array['location_settings'])) {
          variable_set('location_taxonomy_'. $array['vid'], $array['location_settings']);
        }
        break;

      case 'delete';
        variable_del('location_taxonomy_'. $array['vid']);
        break;
    }
  }
  else {
    switch ($op) {
      case 'insert':
      case 'update':
        $settings = variable_get('location_taxonomy_'. $array['vid'], FALSE);
        if ($settings && $settings['multiple']['max']) {
          location_save_locations($array['locations'], array('genid' => 'taxonomy:'. $array['tid']));
        }
        break;
      case 'delete':
        $locations = array();
        location_save_locations($locations, array('genid' => 'taxonomy:'. $array['tid']));
        break;
    }
  }
}

/**
 * Implementation of hook_form_alter().
 */
function location_taxonomy_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'taxonomy_form_vocabulary') {
    $settings = array();
    if (isset($form['vid'])) {
      $settings = variable_get('location_taxonomy_'. $form['vid']['#value'], array());
    }
    $form['location_settings'] = location_settings($settings);
  }
  if ($form_id == 'taxonomy_form_term') {
    $vid = $form['vid']['#value'];
    $settings = variable_get('location_taxonomy_'. $vid, FALSE);
    if ($settings && $settings['multiple']['max']) {
      $locations = array();
      if (isset($form['tid']) && $form['tid']['#value']) {
        $locations = location_load_locations('taxonomy:'. $form['tid']['#value'], 'genid');
      }
      $form['locations'] = location_form($settings, $locations);
    }
  }

  // Move the Save and Delete buttons down below our additions.
  if ($form_id == 'taxonomy_form_vocabulary' || $form_id == 'taxonomy_form_term') {
    if (isset($form['submit']['#weight'])) {
      $form['submit']['#weight']++;
    }
    else {
      $form['submit']['#weight'] = 1;
    }
    if (isset($form['delete'])) {
      if (isset($form['delete']['#weight'])) {
        $form['delete']['#weight']+=2;
      }
      else {
        $form['delete']['#weight'] = 2;
      }
    }
  }
}

/**
 * Implementation of hook_locationapi().
 */
function location_taxonomy_locationapi(&$obj, $op, $a3 = NULL, $a4 = NULL, $a5 = NULL) {
  switch ($op) {
    case 'instance_links':
      foreach ($obj as $k => $v) {
        if (substr($v['genid'], 0, 9) == 'taxonomy:') {
          $data = explode(':', $v['genid']);
          $obj[$k]['href'] = 'taxonomy/term/'. $data[1];
          $obj[$k]['title'] = t('Term location');
          $obj[$k]['type'] = t('Taxonomy location');
        }
      }
  }
}
