Index: synonyms.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/synonyms/synonyms.info,v
retrieving revision 1.3
diff -u -r1.3 synonyms.info
--- synonyms.info	7 Jan 2009 13:38:40 -0000	1.3
+++ synonyms.info	26 Mar 2010 13:44:21 -0000
@@ -1,5 +1,8 @@
 ; $Id $
 name = Synonyms
-description = Allow term synonyms to be used by the search module.
-core = 6.x
+description = Define synonyms for taxonomy terms that can be used by the search module.
+core = "7.x"
+package = Taxonomy
 dependencies[] = taxonomy
+files[] = synonyms.module
+files[] = synonyms.install
\ No newline at end of file
Index: synonyms.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/synonyms/synonyms.module,v
retrieving revision 1.4
diff -u -r1.4 synonyms.module
--- synonyms.module	7 Jan 2009 13:38:40 -0000	1.4
+++ synonyms.module	26 Mar 2010 13:44:22 -0000
@@ -1,36 +1,94 @@
 <?php
+// $Id$
+
 /**
- * Implementattion of hook_nodeapi
- * This hook loads when Drupal starts updating it's search index for a node.
- * We  then get all terms from the categories associated to it, and find their synonyms.
- * The synonyms are then added to the results
-*/
-
-function synonyms_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
-  if ($op == 'update index') {
-    // Check if it's a node and if there are any taxonomy terms.
-    if ($node->nid > 0 && !empty($node->taxonomy)) {
-        $text = array();
-        $getTerms = $node->taxonomy;
-
-        foreach($getTerms as $nodeTerm) {
-            $childSynonyms = taxonomy_get_synonyms($nodeTerm->tid);
-            foreach($childSynonyms as $childSynonym){
-                $text[] = $childSynonym;
-            }
-            $parents = taxonomy_get_parents($nodeTerm->tid);
-            foreach($parents as $parent) {
-                $text[] = $parent->name;
-                $synonyms = taxonomy_get_synonyms($parent->tid);
-                foreach($synonyms as $synonym){
-                    $text[] = $synonym;
-                }
-            }
-        }
+ * @file
+ * Synonyms module.
+ */
 
-        return '<strong>('. implode(', ', $text) .')</strong>';
-    } else {
-        return false;
-    }
+/**
+ * Implements hook_taxonomy_vocabulary_insert().
+ * 
+ * Attach an instance of the synonym field to every
+ * new vocabulary on creation.
+ */
+function synonyms_taxonomy_vocabulary_insert($vocabulary) {
+  $instance = array(
+    'field_name' => 'synonyms_synonym',
+    'object_type' => 'taxonomy_term',
+    'bundle' => $vocabulary->machine_name,
+    'label' => t('Synonyms for this term'),
+  );
+  field_create_instance($instance);
+}
+
+/**
+ * Returns all synonyms for a given term id.
+ * 
+ *  @param $tid
+ *   A term ID.
+ *
+ * @return
+ *   An array containing all synonyms of the given term id.
+ */
+function synonyms_get_synonyms($tid) {
+  if ($tid) {
+    return db_query('SELECT synonyms_synonym_value FROM {field_data_synonyms_synonym} WHERE entity_id = :tid', array(':tid' => $tid))->fetchCol();
+  }
+  else {
+    return array();
   }
 }
+
+/**
+ * Returns the term object that has the given string as a synonym.
+ * 
+ * @param $synonym
+ *  A string containing the synonym to search for.
+ *  
+ * @return
+ *  An array of term objects containing that synonym. 
+ */
+function synonyms_get_synonym_root($synonym) {
+    $tids = db_query('SELECT tid FROM {field_data_synonyms_synonym} s, {taxonomy_term_data} t WHERE t.tid = s.entity_id AND s.synonyms_synonym_value = :name', array(':name' => $synonym))->fetchCol();
+    $terms = taxonomy_term_load_multiple($tids);
+    return $terms;
+}
+
+/**
+ * Implements hook_node_update_index().
+ *
+ * Ensures that synonyms are included when indexing a node
+ */
+function synonyms_node_update_index($node) {
+ // Get a list of all term reference fields attached to this node type
+  $term_reference_fields = db_query(
+    "SELECT fc.field_name
+     FROM {field_config_instance} fci
+     JOIN {field_config} fc ON fci.field_id = fc.id
+     WHERE fci.bundle = :type
+     AND fc.type = 'taxonomy_term_reference'", array(':type' => $node->type))->fetchCol();
+
+  $synonyms = array();
+  // Loop through each vocabulary attached to this node type
+  foreach ($term_reference_fields as $term_reference_field) {
+    // Check if the node contains any taxonomy terms from this vocabulary
+    if ($node->nid > 0 && !empty($node->$term_reference_field)) {
+      // Get the vocabulary object
+      $taxonomy_tags = $node->$term_reference_field;
+      // Loop through each term attached to the node
+      foreach ($taxonomy_tags['und'] as $term) {
+        // Loop through each synonym of this term and add them to the array
+        foreach (synonyms_get_synonyms($term['tid']) as $synonym) {
+          $synonyms[] = $synonym;
+        }
+      }      
+    }
+  }
+  if (!empty($synonyms)) {
+    return '<strong>(' . implode(', ', $synonyms) . ')</strong>';
+  }
+  else {
+    return FALSE;
+  }
+}
\ No newline at end of file
Index: .project
===================================================================
RCS file: .project
diff -N .project
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ .project	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>synonyms-HEAD</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+	</buildSpec>
+	<natures>
+	</natures>
+</projectDescription>
Index: synonyms.install
===================================================================
RCS file: synonyms.install
diff -N synonyms.install
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ synonyms.install	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,83 @@
+<?php
+// $Id: synonyms.install$
+/**
+ * @file
+ * install file for Synonyms module.
+ */
+
+/**
+ * Implements hook_install().
+ */
+function synonyms_install() {
+  // Create the synonym field
+  $field = array(
+    'field_name' => 'synonyms_synonym', // The unique machine name used to refer to this field
+    'type' => 'text', // Each synonym will be a text string
+    'cardinality' => FIELD_CARDINALITY_UNLIMITED, // Allow an unlimited number of synonyms per term
+  );
+  field_create_field($field);
+  
+  // Attach an instance of the field to every vocabulary
+  foreach (taxonomy_get_vocabularies() as $vocabulary) {
+    $instance = array(
+      'field_name' => 'synonyms_synonym',
+      'object_type' => 'taxonomy_term',
+      'bundle' => $vocabulary->machine_name,
+      'label' => st('Synonyms'),
+    );
+    field_create_instance($instance);
+    
+    drupal_set_message('Synonyms field has been added to vocabulary <em>' . $vocabulary->name . '</em>');
+  }
+
+  // Populate the synonym fields with any existing synonyms
+  synonyms_convert_taxonomy_term_synonym_table_to_fields();
+}
+
+/**
+ * Implements hook_uninstall().
+ */
+function synonyms_uninstall() {
+  // Delete the field (and all instances of it)
+  field_delete_field('synonyms_synonym');
+}
+
+/**
+ * Implements hook_update_N().
+ */
+function synonyms_update_7100() {
+  // The conversion of taxonomy_term_synonym table needs to occur
+  // if upgrading from a previous version of synonyms, or if run
+  // on an upgraded D6 database that had synonyms defined (but not
+  // synonyms moodule). Therefore run this function in both hook_install
+  // and hook_update_N.
+  // See http://lists.drupal.org/pipermail/development/2009-November/034177.html
+  synonyms_convert_taxonomy_term_synonym_table_to_fields();
+}
+
+/**
+ * Converts taxonomy term synonyms to use Fields API.
+ */
+function synonyms_convert_taxonomy_term_synonym_table_to_fields() {
+  if (db_table_exists('taxonomy_term_synonym')) {
+    // Create an array of all existing synonyms
+    $result = db_query('SELECT tid, name FROM {taxonomy_term_synonym} ORDER BY tsid ASC');
+    foreach ($result as $record) {
+      $synonyms[$record->tid][] = $record->name;
+    }
+
+    // Load each term, populate synonym field information, and save.
+    $terms = taxonomy_term_load_multiple(array_keys($synonyms));
+    foreach ($terms as $tid => $term) {
+      foreach ($synonyms[$tid] as $delta => $synonym) {
+        $term->synonyms_synonym['und'][$delta]['value'] = $synonym;
+      }
+      taxonomy_term_save($term);
+    }
+
+    // Drop the old synonyms table
+    db_drop_table('taxonomy_term_synonym');
+    
+    drupal_set_message('Existing synonyms for ' . count($terms) . ' terms have been imported.');
+  }
+}
\ No newline at end of file
