You are here

gmap_taxonomy.module in GMap Module 7

GMap Taxonomy Markers

Taxonomy based markers.

File

gmap_taxonomy.module
View source
<?php

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

/**
 * Implementation of hook_form_alter().
 */
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;
    $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' => gmap_taxonomy_vocabulary_is_enabled($vid),
    );
  }

  // @@@ Why does this get called up on delete?
  if ($form_id == 'taxonomy_form_term' && empty($form['confirm']['#value'])) {
    $term = (object) $form['#term'];
    if (gmap_taxonomy_vocabulary_is_enabled($form['#vocabulary'])) {
      $temp = '';
      if (!empty($term->tid)) {
        if ($t = db_query('SELECT marker FROM {gmap_taxonomy_term} WHERE tid = :tid', array(
          ':tid' => $term->tid,
        ))
          ->fetchField()) {
          $temp = $t;
        }
      }
      $form['gmap_taxonomy_marker'] = array(
        '#title' => t('GMap Marker'),
        '#type' => 'select',
        '#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;
      }
    }
  }
  */
}

/**
 * Implement hook_taxonomy_vocabulary_insert().
 */
function gmap_taxonomy_taxonomy_vocabulary_insert($vocabulary) {
  return gmap_taxonomy_taxonomy_vocabulary_update($vocabulary);
}

/**
 * Implement hook_taxonomy_vocabulary_update().
 */
function gmap_taxonomy_taxonomy_vocabulary_update($vocabulary) {
  if (isset($vocabulary->gmap_taxonomy_enable)) {
    variable_set('gmap_taxonomy_vocab_' . $vocabulary->machine_name, TRUE);
  }
  else {
    variable_del('gmap_taxonomy_vocab_' . $vocabulary->machine_name);
  }
}

/**
 * Implement hook_taxonomy_vocabulary_delete().
 */
function gmap_taxonomy_taxonomy_vocabulary_delete($vocabulary) {
  variable_del('gmap_taxonomy_vocab_' . $vocabulary->machine_name);
}

/**
 * Implement hook_taxonomy_term_insert().
 */
function gmap_taxonomy_taxonomy_term_insert($term) {
  return gmap_taxonomy_taxonomy_term_update($term);
}

/**
 * Implement hook_taxonomy_term_update().
 */
function gmap_taxonomy_taxonomy_term_update($term) {
  if (gmap_taxonomy_vocabulary_is_enabled($term->vid)) {
    db_delete('gmap_taxonomy_term')
      ->condition('tid', $term->tid)
      ->execute();

    // Do we have an assigned marker?
    if (!empty($term->gmap_taxonomy_marker)) {
      db_insert('gmap_taxonomy_term')
        ->fields(array(
        'tid' => $term->tid,
        'marker' => $term->gmap_taxonomy_marker,
      ))
        ->execute();

      // Update name changes in the gmap_taxonomy_node table.
      db_update('gmap_taxonomy_node')
        ->fields(array(
        'marker' => $term->gmap_taxonomy_marker,
      ))
        ->condition('tid', $term->tid)
        ->execute();
    }
    gmap_taxonomy_reassign_marker($term->tid);
  }
}

/**
 * Implement hook_taxonomy_term_delete().
 */
function gmap_taxonomy_taxonomy_term_delete($term) {
  db_delete('gmap_taxonomy_term')
    ->condition('tid', $term->tid)
    ->execute();

  // Use gmap_taxonomy_node for search because term_node rows are already gone.
  gmap_taxonomy_reassign_marker($term->tid, TRUE);
}

/**
 * Implement hook_node_insert().
 */
function gmap_taxonomy_node_insert($node) {
  gmap_taxonomy_node_update($node);
}

/**
 * Implement hook_node_update().
 */
function gmap_taxonomy_node_update($node) {

  // Remove the marker association if present. We'll readd it later if it's
  // still applicable.
  db_delete('gmap_taxonomy_node')
    ->condition('nid', $node->nid)
    ->execute();

  // Get list of taxonomy_term_reference field names
  $gmap_taxonomy_fields = gmap_taxonomy_get_instances();

  // Get the markers
  $gmap_taxonomy_markers = gmap_taxonomy_get_markers();

  // If this node doesn't use taxonomy_term_reference fields at all, skip the rest.
  if (!isset($gmap_taxonomy_fields[$node->type])) {
    return;
  }

  // Loop through the page's terms and insert markers if applicable
  foreach ($gmap_taxonomy_fields[$node->type] as $fieldname) {

    // Get this node's field values for this field
    $terms = field_get_items('node', $node, $fieldname, $node->language);

    // Skip if term has no value
    if (!$terms) {
      continue;
    }

    // Otherwise loop through and set marker for term, if term has marker
    foreach ($terms as $term) {
      if (!isset($gmap_taxonomy_markers[$term['tid']])) {
        continue;
      }

      // Insert to gmap_taxonomy table
      db_insert('gmap_taxonomy_node')
        ->fields(array(
        'nid' => $node->nid,
        'tid' => $term['tid'],
        'marker' => $gmap_taxonomy_markers[$term['tid']],
      ))
        ->execute();
    }
  }
}

/**
 * Implement hook_node_delete().
 */
function gmap_taxonomy_node_delete($node) {
  db_delete('gmap_taxonomy_node')
    ->condition('nid', $node->nid)
    ->execute();
}

/**
 * Implement hook_node_revision_delete().
 */

/*
function gmap_taxonomy_node_revision_delete($node) {
  db_delete('gmap_taxonomy_node')
    ->condition('vid', $node->vid)
    ->execute();
}
*/

/**
 * Check if gmap taxonomy is enabled for the given vocabulary
 *
 * @param $vocabulary
 *   The vocabulary to check. Entire object and vid are allowed.
 * @return bool
 *   TRUE if the vocabulary is enabled
 */
function gmap_taxonomy_vocabulary_is_enabled($vocabulary) {
  if (!is_object($vocabulary)) {
    $vocabulary = taxonomy_vocabulary_load($vocabulary);
  }
  return $vocabulary && variable_get('gmap_taxonomy_vocab_' . $vocabulary->machine_name, FALSE);
}

/**
 * Get all gmap taxonomy markers
 * Helper function to return cached version of gmap taxonomy markers from the database
 * @return
 *   Associative array of tid => marker.
 */
function gmap_taxonomy_get_markers() {
  $gmap_taxonomy_markers =& drupal_static(__FUNCTION__);
  if (!isset($gmap_taxonomy_markers)) {
    if ($cache = cache_get('gmap_taxonomy_markers_data')) {
      $gmap_taxonomy_markers = $cache->data;
    }
    else {
      $gmap_taxonomy_markers = db_query('SELECT tid, marker FROM {gmap_taxonomy_term}')
        ->fetchAllKeyed();
      cache_set('gmap_taxonomy_markers_data', $gmap_taxonomy_markers, 'cache', CACHE_TEMPORARY);
    }
  }
  return $gmap_taxonomy_markers;
}

/**
 * Get fields that are using taxonomy_term_reference + gmap taxonomy
 * Helper function to return cached version of fields using taxonomy_term_reference
 * @return
 *   Associative array keyed with node type and with values as array of field names.
 */
function gmap_taxonomy_get_instances() {
  $gmap_taxonomy_fields =& drupal_static(__FUNCTION__);

  // TODO: Could do something with the vid, but probably too much overhead to use it here
  // as field_info_fields() only gives us machine names and gmap_taxonomy_vocabs only gives us vid
  // $gmap_taxonomy_vocabs = variable_get('gmap_taxonomy_vocabs', array());
  if (!isset($gmap_taxonomy_fields)) {
    if ($cache = cache_get('gmap_taxonomy_fields_data')) {
      $gmap_taxonomy_fields = $cache->data;
    }
    else {
      $gmap_taxonomy_fields = array();

      // get all field info
      $fields = field_info_fields();

      // loop through to get the ones of field type taxonomy_term_reference
      foreach ($fields as $field) {
        if ($field['type'] == 'taxonomy_term_reference') {
          foreach ($field['bundles']['node'] as $node_type) {
            $gmap_taxonomy_fields[$node_type][] = $field['field_name'];
          }
        }
      }
      cache_set('gmap_taxonomy_fields_data', $gmap_taxonomy_fields, 'cache', CACHE_TEMPORARY);
    }
  }
  return $gmap_taxonomy_fields;
}

/**
 * Reassign markers associated with a term that's going away.
 */
function gmap_taxonomy_reassign_marker($tid, $deletion = FALSE) {
  $nids = array();
  if ($deletion) {
    $result = db_query('SELECT nid FROM {gmap_taxonomy_node} WHERE tid = :tid', array(
      ':tid' => $tid,
    ));
    foreach ($result as $node) {
      $nids[] = $node->nid;
    }
  }
  else {
    $result = db_query('SELECT nid FROM {taxonomy_index} WHERE tid = :tid', array(
      ':tid' => $tid,
    ));
    foreach ($result as $node) {
      $nids[] = $node->nid;
    }
  }
  foreach ($nids as $nid) {
    $markers = db_query('SELECT t.tid, gt.marker FROM {taxonomy_index} r INNER JOIN {gmap_taxonomy_term} gt ON r.tid = gt.tid INNER JOIN {taxonomy_term_data} t ON r.tid = t.tid INNER JOIN {taxonomy_vocabulary} v ON t.vid = v.vid WHERE r.nid = :nid ORDER BY v.weight DESC, t.weight DESC, t.name DESC', array(
      ':nid' => $nid,
    ));
    if ($marker = $markers
      ->fetchObject()) {

      // Fallback found.
      db_update('gmap_taxonomy_node')
        ->fields(array(
        'tid' => $marker->tid,
        'marker' => $marker->marker,
      ))
        ->condition('nid', $nid)
        ->execute();
    }
    else {

      // No replacement marker, delete the row.
      db_delete('gmap_taxonomy_node')
        ->condition('nid', $nid)
        ->execute();
    }
  }
}

/**
 * Implementation of hook_views_api().
 */
function gmap_taxonomy_views_api() {
  return array(
    'api' => 2,
  );
}

Functions

Namesort descending Description
gmap_taxonomy_form_alter Implementation of hook_form_alter().
gmap_taxonomy_get_instances Get fields that are using taxonomy_term_reference + gmap taxonomy Helper function to return cached version of fields using taxonomy_term_reference
gmap_taxonomy_get_markers Get all gmap taxonomy markers Helper function to return cached version of gmap taxonomy markers from the database
gmap_taxonomy_node_delete Implement hook_node_delete().
gmap_taxonomy_node_insert Implement hook_node_insert().
gmap_taxonomy_node_update Implement hook_node_update().
gmap_taxonomy_reassign_marker Reassign markers associated with a term that's going away.
gmap_taxonomy_taxonomy_term_delete Implement hook_taxonomy_term_delete().
gmap_taxonomy_taxonomy_term_insert Implement hook_taxonomy_term_insert().
gmap_taxonomy_taxonomy_term_update Implement hook_taxonomy_term_update().
gmap_taxonomy_taxonomy_vocabulary_delete Implement hook_taxonomy_vocabulary_delete().
gmap_taxonomy_taxonomy_vocabulary_insert Implement hook_taxonomy_vocabulary_insert().
gmap_taxonomy_taxonomy_vocabulary_update Implement hook_taxonomy_vocabulary_update().
gmap_taxonomy_views_api Implementation of hook_views_api().
gmap_taxonomy_vocabulary_is_enabled Check if gmap taxonomy is enabled for the given vocabulary