Skip to content
Snippets Groups Projects
gmap_taxonomy.module 6.96 KiB
Newer Older
<?php
// $Id$

/**
 * @file
 * GMap Taxonomy Markers
 *
 * Taxonomy based markers.
 */

/**
 * Implementation of hook_form_alter().
 */
Brandon Bergren's avatar
Brandon Bergren committed
function gmap_taxonomy_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'taxonomy_form_vocabulary') {
    $form['gmap_taxonomy'] = array(
      '#type' => 'fieldset',
      '#title' => t('GMap markers'),
    );
    $vid = isset($form['vid']) ? $form['vid']['#value'] : -1;
    $temp = variable_get('gmap_taxonomy_vocabs', array());
    if (!isset($temp[$vid])) {
      $temp[$vid] = 0;
    }
    $form['gmap_taxonomy']['gmap_taxonomy_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable'),
      '#description' => t('Enable choosing a marker for terms in this vocabulary.'),
      '#default_value' => $temp[$vid],
    );
  }
  if ($form_id == 'taxonomy_form_term') {
    $vid = $form['vid']['#value'];
    $vocs = variable_get('gmap_taxonomy_vocabs', array());
    if (isset($vocs[$vid]) && $vocs[$vid]) {
      $temp = '';
      if (isset($form['tid'])) {
        if ($t = db_query('SELECT marker FROM {gmap_taxonomy_term} WHERE tid = :tid', array(':tid' => $form['tid']['#value']))->fetchField()) {
          $temp = $t;
        }
      }
      $form['gmap_taxonomy_marker'] = array(
        '#title' => t('GMap Marker'),
        '#type' => 'select',
Brandon Bergren's avatar
Brandon Bergren committed
        '#options' => array('' => t('No Marker')) + gmap_get_marker_titles(),
        '#description' => t('If you would like nodes tagged as this term to have a special marker, choose one here.'),
        '#default_value' => $temp,
      );
    }
  }

  // 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_taxonomy().
 */
function gmap_taxonomy_taxonomy($op, $type, $array = NULL) {
  if ($type == 'vocabulary') {
    switch ($op) {
      case 'insert':
      case 'update':
        // This can get called in other places than vocabulary form submission.
        // @@@ TODO move this to the form itself..
        if (isset($array['gmap_taxonomy_enable'])) {
          $status = variable_get('gmap_taxonomy_vocabs', array());
          $status[$array['vid']] = $array['gmap_taxonomy_enable'];
          variable_set('gmap_taxonomy_vocabs', $status);
        }
        break;
      case 'delete':
        $status = variable_get('gmap_taxonomy_vocabs', array());
        unset($status[$array['vid']]);
        variable_set('gmap_taxonomy_vocabs', $status);
    }
  }
  else {
    switch ($op) {
      case 'insert':
      case 'update':
Brandon Bergren's avatar
Brandon Bergren committed
        $vocabs = variable_get('gmap_taxonomy_vocabs', array());
        if (isset($vocabs[$array['vid']]) && $vocabs[$array['vid']]) {
        db_delete('gmap_taxonomy_term')
          ->condition('tid', $array['tid'])
          ->execute();
Brandon Bergren's avatar
Brandon Bergren committed
          // Do we have an assigned marker?
          if (!empty($array['gmap_taxonomy_marker'])) {
            db_insert('gmap_taxonomy_term')
              ->fields(array(
                'tid' => $array['tid'],
                'marker' => $array['gmap_taxonomy_marker'],
              ))
              ->execute();
Brandon Bergren's avatar
Brandon Bergren committed
            // Update name changes in the gmap_taxonomy_node table.
            db_update('gmap_taxonomy_node')
              ->fields(array(
                'marker' => $array['gmap_taxonomy_marker'],
              ))
              ->condition('tid', $array['tid'])
              ->execute();
Brandon Bergren's avatar
Brandon Bergren committed
          }
Brandon Bergren's avatar
Brandon Bergren committed
          gmap_taxonomy_reassign_marker($array['tid']);
        }
        break;
      case 'delete':
Brandon Bergren's avatar
Brandon Bergren committed
        // Delete and reassign even if the term isn't currently gmap_taxonomy enabled.
        db_delete('gmap_taxonomy_term')
          ->condition('tid', $array['tid'])
          ->execute();
Brandon Bergren's avatar
Brandon Bergren committed
        // Use gmap_taxonomy_node for search because term_node rows are already gone.
        gmap_taxonomy_reassign_marker($array['tid'], 'gmap_taxonomy_node');
    }
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function gmap_taxonomy_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'insert':
    case 'update':
      // Remove the marker association if present. We'll readd it later if it's
      // still applicable.
      db_delete('gmap_taxonomy_node')
        ->condition('vid', $node->vid)
        ->execute();
      $status = variable_get('gmap_taxonomy_vocabs', array());
      $marker = '';
      if (isset($node->taxonomy) && is_array($node->taxonomy)) {
        foreach ($node->taxonomy as $voc => $terms) {
          if (isset($status[$voc]) && $status[$voc]) {
            $t = $terms;
            if (!is_array($t)) {
              $t = array($t);
            }
            foreach ($t as $term) {
              $result = db_query('SELECT marker, tid FROM {gmap_taxonomy_term} WHERE tid = :tid', array(':tid' => $term));
              if ($m = db_fetch_object($result)) {
                $marker = $m->marker;
                $markertid = $m->tid;
              }
            }
          }
        }
        if (!empty($marker)) {
          db_insert('gmap_taxonomy_node')
            ->fields(array(
              'nid' => $node->nid,
              'vid' => $node->vid,
              'tid' => $markertid,
              'marker' => $marker,
            ))
            ->execute();
      db_delete('gmap_taxonomy_node')
        ->condition('nid', $node->nid)
        ->execute();
      break;
    case 'delete revision':
      db_delete('gmap_taxonomy_node')
        ->condition('vid', $node->vid)
        ->execute();
Brandon Bergren's avatar
Brandon Bergren committed
/**
 * Reassign markers associated with a term that's going away.
 */
function gmap_taxonomy_reassign_marker($tid, $table = 'term_node') {
  $result = db_query('SELECT vid FROM {:table} WHERE tid = :tid', array(':table' => db_escape_table($table), ':tid' => $tid));
Brandon Bergren's avatar
Brandon Bergren committed
  while ($node = db_fetch_object($result)) {
    $markers = db_query('SELECT t.tid, gt.marker FROM {term_node} r INNER JOIN {gmap_taxonomy_term} gt ON r.tid = gt.tid INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.vid = :vid ORDER BY v.weight DESC, t.weight DESC, t.name DESC', array(':vid' => $node->vid));
Brandon Bergren's avatar
Brandon Bergren committed
    if ($marker = db_fetch_object($markers)) {
      // Fallback found.
      db_update('gmap_taxonomy_node')
        ->fields(array(
          'tid' => $marker->tid,
          'marker' => $marker->marker,
        ))
        ->condition('vid', $node->vid)
        ->execute();
Brandon Bergren's avatar
Brandon Bergren committed
    }
    else {
      // No replacement marker, delete the row.
      db_delete('gmap_taxonomy_node')
        ->condition('vid', $marker->vid)
        ->execute();
 * Implementation of hook_views_api().
function gmap_taxonomy_views_api() {
  return array(
    'api' => 2,