gmap_taxonomy.module in GMap Module 5
Same filename and directory in other branches
GMap Taxonomy Markers
Taxonomy based markers.
File
gmap_taxonomy.moduleView source
<?php
/**
* @file
* GMap Taxonomy Markers
*
* Taxonomy based markers.
*/
/**
* Implementation of hook_form_alter().
*/
function gmap_taxonomy_form_alter($form_id, &$form) {
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],
);
$form['submit']['#weight']++;
if ($form['delete']) {
$form['delete']['#weight'] += 2;
}
}
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_result(db_query('SELECT marker FROM {gmap_taxonomy_term} WHERE tid = %d', $form['tid']['#value']))) {
$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,
);
$form['submit']['#weight']++;
if ($form['delete']) {
$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':
$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':
$vocabs = variable_get('gmap_taxonomy_vocabs', array());
if (isset($vocabs[$array['vid']]) && $vocabs[$array['vid']]) {
db_query('DELETE FROM {gmap_taxonomy_term} WHERE tid = %d', $array['tid']);
// Do we have an assigned marker?
if (!empty($array['gmap_taxonomy_marker'])) {
db_query("INSERT INTO {gmap_taxonomy_term} (tid, marker) VALUES (%d, '%s')", $array['tid'], $array['gmap_taxonomy_marker']);
// Update name changes in the gmap_taxonomy_node table.
db_query("UPDATE {gmap_taxonomy_node} SET marker = '%s' WHERE tid = %d", $array['gmap_taxonomy_marker'], $array['tid']);
}
gmap_taxonomy_reassign_marker($array['tid']);
}
break;
case 'delete':
// Delete and reassign even if the term isn't currently gmap_taxonomy enabled.
db_query('DELETE FROM {gmap_taxonomy_term} WHERE tid = %d', $array['tid']);
// 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_query('DELETE FROM {gmap_taxonomy_node} WHERE nid = %d', $node->nid);
$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 = %d', $term);
if ($m = db_fetch_object($result)) {
$marker = $m->marker;
$markertid = $m->tid;
}
}
}
}
if (!empty($marker)) {
// There's an applicable taxonomy marker. Stash the association in the db.
db_query("INSERT INTO {gmap_taxonomy_node} (nid, tid, marker) VALUES (%d, %d, '%s')", $node->nid, $markertid, $marker);
}
}
break;
case 'delete':
db_query('DELETE FROM {gmap_taxonomy_node} WHERE nid = %d', $node->nid);
break;
}
}
/**
* Reassign markers associated with a term that's going away.
*/
function gmap_taxonomy_reassign_marker($tid, $table = 'term_node') {
$result = db_query('SELECT nid FROM {' . db_escape_table($table) . '} WHERE tid = %d', $tid);
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.nid = %d ORDER BY v.weight DESC, t.weight DESC, t.name DESC', $node->nid);
if ($marker = db_fetch_object($markers)) {
// Fallback found.
db_query("UPDATE {gmap_taxonomy_node} SET tid = %d, marker = '%s' WHERE nid = %d", $marker->tid, $marker->marker, $node->nid);
}
else {
// No replacement marker, delete the row.
db_query("DELETE FROM {gmap_taxonomy_node} WHERE nid = %d", $marker->nid);
}
}
}
/**
* Implementation of hook_views_tables().
*/
function gmap_taxonomy_views_tables() {
$tables['gmap_taxonomy_node'] = array(
'fields' => array(
'marker' => array(
'name' => t('GMap Taxonomy: Marker'),
'help' => t('Displays the marker name GMap Taxonomy associates with this node.'),
),
),
'join' => array(
'left' => array(
'table' => 'node',
'field' => 'nid',
),
'right' => array(
'field' => 'nid',
),
),
);
return $tables;
}
/**
* In case you were implementing support for gmap_taxonomy in your own modules,
* here's a canary function to go with the change so you can continue to support
* older snapshots if you wish. (Only applicable to D5, D6 has term_node revisions.)
*/
function gmap_taxonomy_vid_is_gone() {
return TRUE;
}
Functions
Name | Description |
---|---|
gmap_taxonomy_form_alter | Implementation of hook_form_alter(). |
gmap_taxonomy_nodeapi | Implementation of hook_nodeapi(). |
gmap_taxonomy_reassign_marker | Reassign markers associated with a term that's going away. |
gmap_taxonomy_taxonomy | Implementation of hook_taxonomy(). |
gmap_taxonomy_vid_is_gone | In case you were implementing support for gmap_taxonomy in your own modules, here's a canary function to go with the change so you can continue to support older snapshots if you wish. (Only applicable to D5, D6 has term_node revisions.) |
gmap_taxonomy_views_tables | Implementation of hook_views_tables(). |