gmap_taxonomy.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/gmap/gmap_taxonomy.module
    2. 6 contributions/gmap/gmap_taxonomy.module
    3. 7 contributions/gmap/gmap_taxonomy.module

GMap Taxonomy Markers

Taxonomy based markers.

Functions & methods

NameDescription
gmap_taxonomy_form_alterImplementation of hook_form_alter().
gmap_taxonomy_nodeapiImplementation of hook_nodeapi().
gmap_taxonomy_reassign_markerReassign markers associated with a term that's going away.
gmap_taxonomy_taxonomyImplementation of hook_taxonomy().
gmap_taxonomy_vid_is_goneIn 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_tablesImplementation of hook_views_tables().

File

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