| 1 |
<?php
|
| 2 |
// $Id: gmap_taxonomy.module,v 1.6 2008/11/24 22:31:59 bdragon Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* GMap Taxonomy Markers
|
| 7 |
*
|
| 8 |
* Taxonomy based markers.
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_form_alter().
|
| 13 |
*/
|
| 14 |
function gmap_taxonomy_form_alter(&$form, &$form_state, $form_id) {
|
| 15 |
if ($form_id == 'taxonomy_form_vocabulary') {
|
| 16 |
$form['gmap_taxonomy'] = array(
|
| 17 |
'#type' => 'fieldset',
|
| 18 |
'#title' => t('GMap markers'),
|
| 19 |
);
|
| 20 |
$vid = isset($form['vid']) ? $form['vid']['#value'] : -1;
|
| 21 |
$temp = variable_get('gmap_taxonomy_vocabs', array());
|
| 22 |
if (!isset($temp[$vid])) {
|
| 23 |
$temp[$vid] = 0;
|
| 24 |
}
|
| 25 |
$form['gmap_taxonomy']['gmap_taxonomy_enable'] = array(
|
| 26 |
'#type' => 'checkbox',
|
| 27 |
'#title' => t('Enable'),
|
| 28 |
'#description' => t('Enable choosing a marker for terms in this vocabulary.'),
|
| 29 |
'#default_value' => $temp[$vid],
|
| 30 |
);
|
| 31 |
}
|
| 32 |
if ($form_id == 'taxonomy_form_term') {
|
| 33 |
$vid = $form['vid']['#value'];
|
| 34 |
$vocs = variable_get('gmap_taxonomy_vocabs', array());
|
| 35 |
if (isset($vocs[$vid]) && $vocs[$vid]) {
|
| 36 |
$temp = '';
|
| 37 |
if (isset($form['tid'])) {
|
| 38 |
if ($t = db_result(db_query('SELECT marker FROM {gmap_taxonomy_term} WHERE tid = %d', $form['tid']['#value']))) {
|
| 39 |
$temp = $t;
|
| 40 |
}
|
| 41 |
}
|
| 42 |
$form['gmap_taxonomy_marker'] = array(
|
| 43 |
'#title' => t('GMap Marker'),
|
| 44 |
'#type' => 'select',
|
| 45 |
'#options' => array('' => t('No Marker')) + gmap_get_marker_titles(),
|
| 46 |
'#description' => t('If you would like nodes tagged as this term to have a special marker, choose one here.'),
|
| 47 |
'#default_value' => $temp,
|
| 48 |
);
|
| 49 |
}
|
| 50 |
}
|
| 51 |
|
| 52 |
// Move the Save and Delete buttons down below our additions.
|
| 53 |
if ($form_id == 'taxonomy_form_vocabulary' || $form_id == 'taxonomy_form_term') {
|
| 54 |
if (isset($form['submit']['#weight'])) {
|
| 55 |
$form['submit']['#weight']++;
|
| 56 |
}
|
| 57 |
else {
|
| 58 |
$form['submit']['#weight'] = 1;
|
| 59 |
}
|
| 60 |
if (isset($form['delete'])) {
|
| 61 |
if (isset($form['delete']['#weight'])) {
|
| 62 |
$form['delete']['#weight']+=2;
|
| 63 |
}
|
| 64 |
else {
|
| 65 |
$form['delete']['#weight'] = 2;
|
| 66 |
}
|
| 67 |
}
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
* Implementation of hook_taxonomy().
|
| 73 |
*/
|
| 74 |
function gmap_taxonomy_taxonomy($op, $type, $array = NULL) {
|
| 75 |
if ($type == 'vocabulary') {
|
| 76 |
switch ($op) {
|
| 77 |
case 'insert':
|
| 78 |
case 'update':
|
| 79 |
// This can get called in other places than vocabulary form submission.
|
| 80 |
// @@@ TODO move this to the form itself..
|
| 81 |
if (isset($array['gmap_taxonomy_enable'])) {
|
| 82 |
$status = variable_get('gmap_taxonomy_vocabs', array());
|
| 83 |
$status[$array['vid']] = $array['gmap_taxonomy_enable'];
|
| 84 |
variable_set('gmap_taxonomy_vocabs', $status);
|
| 85 |
}
|
| 86 |
break;
|
| 87 |
case 'delete':
|
| 88 |
$status = variable_get('gmap_taxonomy_vocabs', array());
|
| 89 |
unset($status[$array['vid']]);
|
| 90 |
variable_set('gmap_taxonomy_vocabs', $status);
|
| 91 |
}
|
| 92 |
}
|
| 93 |
else {
|
| 94 |
switch ($op) {
|
| 95 |
case 'insert':
|
| 96 |
case 'update':
|
| 97 |
$vocabs = variable_get('gmap_taxonomy_vocabs', array());
|
| 98 |
if (isset($vocabs[$array['vid']]) && $vocabs[$array['vid']]) {
|
| 99 |
db_query('DELETE FROM {gmap_taxonomy_term} WHERE tid = %d', $array['tid']);
|
| 100 |
// Do we have an assigned marker?
|
| 101 |
if (!empty($array['gmap_taxonomy_marker'])) {
|
| 102 |
db_query("INSERT INTO {gmap_taxonomy_term} (tid, marker) VALUES (%d, '%s')", $array['tid'], $array['gmap_taxonomy_marker']);
|
| 103 |
// Update name changes in the gmap_taxonomy_node table.
|
| 104 |
db_query("UPDATE {gmap_taxonomy_node} SET marker = '%s' WHERE tid = %d", $array['gmap_taxonomy_marker'], $array['tid']);
|
| 105 |
}
|
| 106 |
|
| 107 |
gmap_taxonomy_reassign_marker($array['tid']);
|
| 108 |
}
|
| 109 |
break;
|
| 110 |
case 'delete':
|
| 111 |
// Delete and reassign even if the term isn't currently gmap_taxonomy enabled.
|
| 112 |
db_query('DELETE FROM {gmap_taxonomy_term} WHERE tid = %d', $array['tid']);
|
| 113 |
// Use gmap_taxonomy_node for search because term_node rows are already gone.
|
| 114 |
gmap_taxonomy_reassign_marker($array['tid'], 'gmap_taxonomy_node');
|
| 115 |
}
|
| 116 |
}
|
| 117 |
}
|
| 118 |
|
| 119 |
/**
|
| 120 |
* Implementation of hook_nodeapi().
|
| 121 |
*/
|
| 122 |
function gmap_taxonomy_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
|
| 123 |
switch ($op) {
|
| 124 |
case 'insert':
|
| 125 |
case 'update':
|
| 126 |
// Remove the marker association if present. We'll readd it later if it's
|
| 127 |
// still applicable.
|
| 128 |
db_query('DELETE FROM {gmap_taxonomy_node} WHERE vid = %d', $node->vid);
|
| 129 |
|
| 130 |
$status = variable_get('gmap_taxonomy_vocabs', array());
|
| 131 |
$marker = '';
|
| 132 |
if (isset($node->taxonomy) && is_array($node->taxonomy)) {
|
| 133 |
foreach ($node->taxonomy as $voc => $terms) {
|
| 134 |
if (isset($status[$voc]) && $status[$voc]) {
|
| 135 |
$t = $terms;
|
| 136 |
if (!is_array($t)) {
|
| 137 |
$t = array($t);
|
| 138 |
}
|
| 139 |
foreach ($t as $term) {
|
| 140 |
$result = db_query('SELECT marker, tid FROM {gmap_taxonomy_term} WHERE tid = %d', $term);
|
| 141 |
if ($m = db_fetch_object($result)) {
|
| 142 |
$marker = $m->marker;
|
| 143 |
$markertid = $m->tid;
|
| 144 |
}
|
| 145 |
}
|
| 146 |
}
|
| 147 |
}
|
| 148 |
if (!empty($marker)) {
|
| 149 |
db_query("INSERT INTO {gmap_taxonomy_node} (nid, vid, tid, marker) VALUES (%d, %d, %d, '%s')", $node->nid, $node->vid, $markertid, $marker);
|
| 150 |
}
|
| 151 |
}
|
| 152 |
break;
|
| 153 |
|
| 154 |
case 'delete':
|
| 155 |
db_query('DELETE FROM {gmap_taxonomy_node} WHERE nid = %d', $node->nid);
|
| 156 |
break;
|
| 157 |
case 'delete revision':
|
| 158 |
db_query('DELETE FROM {gmap_taxonomy_node} WHERE vid = %d', $node->vid);
|
| 159 |
break;
|
| 160 |
}
|
| 161 |
}
|
| 162 |
|
| 163 |
/**
|
| 164 |
* Reassign markers associated with a term that's going away.
|
| 165 |
*/
|
| 166 |
function gmap_taxonomy_reassign_marker($tid, $table = 'term_node') {
|
| 167 |
$result = db_query('SELECT vid FROM {'. db_escape_table($table) .'} WHERE tid = %d', $tid);
|
| 168 |
while ($node = db_fetch_object($result)) {
|
| 169 |
$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 = %d ORDER BY v.weight DESC, t.weight DESC, t.name DESC', $node->vid);
|
| 170 |
if ($marker = db_fetch_object($markers)) {
|
| 171 |
// Fallback found.
|
| 172 |
db_query("UPDATE {gmap_taxonomy_node} SET tid = %d, marker = '%s' WHERE vid = %d", $marker->tid, $marker->marker, $node->vid);
|
| 173 |
}
|
| 174 |
else {
|
| 175 |
// No replacement marker, delete the row.
|
| 176 |
db_query("DELETE FROM {gmap_taxonomy_node} WHERE vid = %d", $marker->vid);
|
| 177 |
}
|
| 178 |
}
|
| 179 |
}
|
| 180 |
|
| 181 |
/**
|
| 182 |
* Implementation of hook_views_api().
|
| 183 |
*/
|
| 184 |
function gmap_taxonomy_views_api() {
|
| 185 |
return array(
|
| 186 |
'api' => 2,
|
| 187 |
);
|
| 188 |
}
|