gmap_taxonomy.module

Tracking 6.x-2.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_views_apiImplementation of hook_views_api().

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