=== modified file 'field_types/text/taxonomy_enhancer_text.info'
--- field_types/text/taxonomy_enhancer_text.info	2008-08-03 17:09:24 +0000
+++ field_types/text/taxonomy_enhancer_text.info	2008-09-27 14:26:44 +0000
@@ -1,11 +1,11 @@
 ; $Id: taxonomy_enhancer_text.info,v 1.1.2.1 2008/07/31 15:53:46 njt1982 Exp $
 name = "Text Field"
 description = "Provides 'text field' and 'text area' options"
-dependencies = taxonomy_enhancer
+dependencies[] = taxonomy_enhancer
 package = "Taxonomy Enhancer"
+core = 6.x
 
-; Information added by drupal.org packaging script on 2008-07-31
-version = "5.x-2.1"
+; Information added by drupal.org packaging script on 2008-08-01
+version = "6.x-2.x-dev"
 project = "taxonomy_enhancer"
-datestamp = "1217521835"
-
+datestamp = "1217549504"

=== modified file 'field_types/text/taxonomy_enhancer_text.install'
--- field_types/text/taxonomy_enhancer_text.install	2008-08-03 17:09:24 +0000
+++ field_types/text/taxonomy_enhancer_text.install	2009-02-21 17:07:16 +0000
@@ -6,19 +6,38 @@
  * This instal file will install the Text Field type with its own table for storing text-specific values
  */
 
+/**
+ * Implementation of hook_install().
+ */
 function taxonomy_enhancer_text_install() {
-  switch ($GLOBALS['db_type']) {
-    case 'mysqli':
-    case 'mysql':
-      $sql = 'CREATE TABLE {taxonomy_enhancer_value_text} (
-                tid int(11) unsigned NOT NULL,
-                fid varchar(255) NOT NULL default "",
-                value text NOT NULL,
-                format int(11) default NULL,
-                PRIMARY KEY  (tid,fid)
-              ) /*!40100 DEFAULT CHARACTER SET utf8 */;';
-
-      db_query($sql);
-      break;
-  }
+  drupal_install_schema('taxonomy_enhancer_text');
+}
+
+/**
+ * Implementation of hook_uninstall().
+ */
+function taxonomy_enhancer_text_uninstall() {
+  drupal_uninstall_schema('taxonomy_enhancer_text');
+}
+
+
+function taxonomy_enhancer_text_schema() {
+  $schema['taxonomy_enhancer_value_text'] = array(
+    'fields' => array(
+       'tid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'disp-width' => '11'),
+       'fid' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
+       'value' => array('type' => 'text', 'not null' => TRUE),
+       'delta' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'default' => 0),
+       'format' => array('type' => 'int', 'not null' => FALSE, 'disp-width' => '11')),
+    'primary key' => array('tid', 'fid', 'delta'),
+  );
+  return $schema;
+}
+
+function taxonomy_enhancer_text_update_6001() {
+  $ret = array();
+  db_add_field($ret, 'taxonomy_enhancer_value_text', 'delta', array('initial' => 0, 'type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'default' => 0));
+  db_drop_primary_key($ret, 'taxonomy_enhancer_value_text');
+  db_add_primary_key($ret, 'taxonomy_enhancer_value_text', array('tid', 'fid', 'delta'));
+  return $ret;
 }

=== modified file 'field_types/text/taxonomy_enhancer_text.module'
--- field_types/text/taxonomy_enhancer_text.module	2008-08-03 17:09:24 +0000
+++ field_types/text/taxonomy_enhancer_text.module	2008-09-27 14:16:48 +0000
@@ -23,7 +23,8 @@
      */
     case 'insert' :
       if ($type == 'field') {
-        //INSERT CODE GOES HERE - IF NEEDED.
+        //Return settings
+        taxonomy_enhancer_text_te_api('update', 'field', $field, $term);
       }
       elseif ($type == 'value') {
         //Update is the same as insert really!
@@ -40,33 +41,52 @@
       if ($type == 'field') {
         //Return settings
         return array(
+          'multiple' => $field['multiple'],
           'rows' => $field['rows'],
           'format' => $field['format'],
+          'allowed_values' => $field['allowed_values'],
+          'options_type' => $field['options_type'],
         );
       }
       elseif ($type == 'value') {
         db_query('DELETE FROM {taxonomy_enhancer_value_text} WHERE tid = %d AND fid = "%s"', $term['tid'], $field->fid);
-        switch ($field->format) {
-          case 'formatted' :
-            if (!empty($term['fields'][$field->fid]['value'])) {
-              db_query('INSERT INTO {taxonomy_enhancer_value_text} (tid,fid,value,format) VALUES(%d, "%s", "%s", %d)',
-                $term['tid'],
-                $field->fid,
-                $term['fields'][$field->fid]['value'],
-                $term['fields'][$field->fid]['format']
-              );
-            }
-            break;
-
-          default:
-            if (!empty($term['fields'][$field->fid])) {
-              db_query('INSERT INTO {taxonomy_enhancer_value_text} (tid,fid,value) VALUES(%d, "%s", "%s")',
-                $term['tid'],
-                $field->fid,
+        if ($field->type == 'text') {
+          if (is_array($term['fields'][$field->fid])) {
+            foreach ($term['fields'][$field->fid] as $delta => $value) {
+              if (!empty($term['fields'][$field->fid][$delta]['value']) && is_numeric($delta)) {
+                db_query('INSERT INTO {taxonomy_enhancer_value_text} (tid, fid, delta, value, format) VALUES(%d, "%s", %d, "%s", %d)',
+                  $term['tid'],
+                  $field->fid,
+                  $delta,
+                  $term['fields'][$field->fid][$delta]['value'],
+                  $term['fields'][$field->fid]['format']
+                );
+              }
+            }
+          }
+        }
+        else if ($field->type == 'options') {
+          if ($field->multiple) {
+            $delta = 0;
+            if (is_array($term['fields'][$field->fid])) {
+              foreach ($term['fields'][$field->fid] as $key => $value) {
+                db_query('INSERT INTO {taxonomy_enhancer_value_text} (tid, fid, delta, value) VALUES(%d, "%s", %d, "%s")',
+                  $term['tid'],
+                  $field->fid,
+                  $delta++,
+                  $value
+                );
+              }
+            }
+          }
+          else {
+            db_query('INSERT INTO {taxonomy_enhancer_value_text} (tid, fid, delta, value) VALUES(%d, "%s", %d, "%s")',
+                $term['tid'],
+                $field->fid,
+                0,
                 $term['fields'][$field->fid]
-              );
-            }
-            break;
+            );
+          }
         }
       }
       break;
@@ -92,13 +112,13 @@
         return $field;
       }
       elseif ($type == 'value') {
-        switch ($field->format) {
-          case 'formatted' :
-            return db_fetch_array(db_query('SELECT value, format FROM {taxonomy_enhancer_value_text} WHERE tid = %d AND fid = "%s"', $term->tid, $field->fid));
-
-          default:
-            return db_result(db_query('SELECT value FROM {taxonomy_enhancer_value_text} WHERE tid = %d AND fid = "%s"', $term->tid, $field->fid));
+        $values = array();
+        $sql = db_query('SELECT * FROM {taxonomy_enhancer_value_text} WHERE tid = %d AND fid = "%s"', $term->tid, $field->fid);
+        while ($data = db_fetch_array($sql)) {
+          $values[$data["delta"]]['value'] = $data["value"];
+          $values[$data["delta"]]['format'] = $data["format"];
         }
+        return $values;
       }
       break;
 
@@ -109,13 +129,12 @@
      */
     case 'view' :
       if ($type == 'value') {
-        switch ($field->format) {
-          case 'formatted' :
-            $value = db_fetch_object(db_query('SELECT value, format FROM {taxonomy_enhancer_value_text} WHERE tid = %d AND fid = "%s"', $term->tid, $field->fid));
-            return check_markup($value->value, $value->format, FALSE);
-
-          default:
-            return db_result(db_query('SELECT value FROM {taxonomy_enhancer_value_text} WHERE tid = %d AND fid = "%s"', $term->tid, $field->fid));
+        foreach ($term->{$field->fid} as $delta => $value) {
+       	  $value = $value['value'];//helps select list render
+          if ($field->format) {
+            $value = check_markup($value, $value['format'], FALSE);
+          }
+          $term->{$field->fid}[$delta]['safe'] = $value;
         }
       }
       break;
@@ -129,6 +148,7 @@
       if ($type == 'field') {
         return array(
           'text' => t('Text'),
+          'options' => t('Options'),
         );
       }
       break;
@@ -164,31 +184,57 @@
  */
 function _taxonomy_enhancer_text_term_form($field, $term = NULL) {
   $form = array();
-
-  if ($field->rows == 1) {
-    $type = 'textfield';
-  }
-  else {
-    $type = 'textarea';
-  }
-
-  switch ($field->format) {
-    case 'formatted' :
-      $form[$field->fid]['value'] = array(
-        '#type' => $type,
-        '#title' => check_plain($field->title),
-        '#default_value' => isset($term->{$field->fid}['value']) ? $term->{$field->fid}['value'] : '',
-      );
-      $form[$field->fid]['format'] = filter_form($term->{$field->fid}['format'], NULL, array('fields', $field->fid, 'format'));
-      break;
-
-    default :
-      $form[$field->fid] = array(
-        '#type' => $type,
-        '#title' => check_plain($field->title),
-        '#default_value' => isset($term->{$field->fid}) ? $term->{$field->fid} : '',
-      );
-      break;
+  $form[$field->fid]['#tree'] = TRUE;
+  
+  if ($field->type == 'options') {
+    $default_values = array();
+    if (is_array($term->{$field->fid})) {
+      foreach ($term->{$field->fid} as $delta => $value) {
+        $default_values[] = $value['value'];
+      }
+    }
+    $form[$field->fid] = array(
+      '#type' => ($field->options_type == 'select') ? 'select' : (($field->multiple) ? 'checkboxes' : 'radios'),
+      '#title' => check_plain($field->title),
+      '#default_value' => ($field->multiple) ? $default_values : reset($default_values),
+      '#options' => taxonomy_enhancer_text_allowed_values($field),
+      '#multiple' => $field->multiple,
+    );
+  }
+  else if ($field->type == 'text') {
+    if ($field->rows == 1) {
+      $type = 'textfield';
+    }
+    else {
+      $type = 'textarea';
+    }
+    
+    if ($field->multiple) {
+      $form[$field->fid]['#title'] = check_plain($field->title);
+      $form[$field->fid]['#type'] = 'fieldset';
+      
+      $count = count($term->{$field->fid}) + 3;
+      for($i=0; $i < $count; $i++) {
+        $form[$field->fid][$i]['value'] = array(
+          '#type' => $type,
+          '#size' => '35', 
+          '#cols' => '35',
+          '#default_value' => isset($term->{$field->fid}[$i]['value']) ? $term->{$field->fid}[$i]['value'] : '',
+        );
+      }
+    }
+    else {
+      $form[$field->fid][0]['value'] = array(
+        '#title' => check_plain($field->title),
+        '#type' => $type,
+        '#size' => '35', 
+        '#cols' => '35',
+        '#default_value' => isset($term->{$field->fid}[0]['value']) ? $term->{$field->fid}[0]['value'] : '',
+      );
+    }
+    if ($field->format == "formatted") {
+      $form[$field->fid]['format'] = filter_form($term->{$field->fid}[0]['format'], NULL, array('fields', $field->fid, 'format'));
+    }
   }
 
   return $form;
@@ -203,26 +249,78 @@
  * @return array
  *   Form API Array
  */
-function _taxonomy_enhancer_text_field_form($field) {
+function _taxonomy_enhancer_text_field_form($field) {  
   $form = array();
-
-  $form['rows'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Number of rows'),
-    '#required' => TRUE,
-    '#size' => 3,
-    '#default_value' => isset($field->rows) ? $field->rows : 1,
-    '#description' => t('The number of rows controls whether this is a single-line textfield or a multi-line textarea.')
-  );
-
-  $form['format'] = array(
-    '#type' => 'select',
-    '#title' => t('Output Format'),
-    '#required' => TRUE,
-    '#options' => array('raw' => t('Raw'), 'plain' => t('Plain Text'), 'formatted' => t('Formatted')),
-    '#default_value' => isset($field->format) ? $field->format : 'plain',
-    '#description' => t('The <em>output format</em> controls whether the field is <em>Raw</em> (data is simply outputted with no filtering), <em>Plain Text</em> (the data is filtered for plain text only - no HTML markup allowed) or <em>Formatted</em> (the data is parsed through Drupal\'d Input Filters to allow restricted HTML, BBCode or any other configured filters).')
-  );
+  $form['multiple'] = array(
+    '#type' => 'checkbox', 
+    '#title' => t('Multiple'),
+    '#default_value' => isset($field->multiple) ? $field->multiple : 0,
+  );
+  
+  if ($field->type == 'text') {
+    $form['rows'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Number of rows'),
+      '#required' => TRUE,
+      '#size' => 3,
+      '#default_value' => isset($field->rows) ? $field->rows : 1,
+      '#description' => t('The number of rows controls whether this is a single-line textfield or a multi-line textarea.')
+    );
+
+    $form['format'] = array(
+      '#type' => 'select',
+      '#title' => t('Output Format'),
+      '#required' => TRUE,
+      '#options' => array('raw' => t('Raw'), 'plain' => t('Plain Text'), 'formatted' => t('Formatted')),
+      '#default_value' => isset($field->format) ? $field->format : 'plain',
+      '#description' => t('The <em>output format</em> controls whether the field is <em>Raw</em> (data is simply outputted with no filtering), <em>Plain Text</em> (the data is filtered for plain text only - no HTML markup allowed) or <em>Formatted</em> (the data is parsed through Drupal\'d Input Filters to allow restricted HTML, BBCode or any other configured filters).')
+    );
+  }
+  
+  if ($field->type == 'options') {
+    $form['options_type'] = array(
+      '#type' => 'radios', 
+      '#title' => t('Option Type'),
+      '#options' => array('select' => t('Select List'), 'buttons' => 'Checkboxes/Radios'),
+      '#required' => TRUE,
+      '#default_value' => isset($field->options_type) ? $field->options_type : '',
+    );
+    $form['allowed_values'] = array(
+      '#type' => 'textarea',
+      '#title' => t('Allowed values list'),
+      '#default_value' => !empty($field->allowed_values) ? $field->allowed_values : '',
+      '#required' => FALSE,
+      '#rows' => 5,
+      '#required' => TRUE,
+      '#description' => t('Enter one value per line, in the format key|label.'),
+    );
+  }
 
   return $form;
 }
+
+/**
+ * Returns an options array out of the specified allowed values
+ *
+ * @param $field The field object
+ * @return An array with options
+ */
+function taxonomy_enhancer_text_allowed_values($field) {
+  $allowed_values = array();
+  if (!$field->multiple) {
+    $allowed_values[] = '';
+  }
+  $list = explode("\n", $field->allowed_values);
+  $list = array_map('trim', $list);
+  $list = array_filter($list, 'strlen');
+  foreach ($list as $opt) {
+    if (strpos($opt, '|') !== FALSE) {
+      list($key, $value) = explode('|', $opt);
+      $allowed_values[$key] = $value ? $value : $key;
+    }
+    else {
+      $allowed_values[$opt] = $opt;
+    }
+  }
+  return $allowed_values;
+}

=== modified file 'taxonomy_enhancer.info'
--- taxonomy_enhancer.info	2008-08-03 17:09:24 +0000
+++ taxonomy_enhancer.info	2008-09-27 14:28:52 +0000
@@ -2,9 +2,10 @@
 name = "Taxonomy Enhancer"
 description = "Extend taxonomy terms with customizable fields"
 package = "Taxonomy Enhancer"
+core = 6.x
 
-; Information added by drupal.org packaging script on 2008-07-31
-version = "5.x-2.1"
+; Information added by drupal.org packaging script on 2008-08-01
+version = "6.x-2.x-dev"
 project = "taxonomy_enhancer"
-datestamp = "1217521835"
+datestamp = "1217549504"
 

=== modified file 'taxonomy_enhancer.install'
--- taxonomy_enhancer.install	2008-08-03 17:09:24 +0000
+++ taxonomy_enhancer.install	2008-09-27 14:16:48 +0000
@@ -7,25 +7,56 @@
  */
 
 function taxonomy_enhancer_install() {
-  switch ($GLOBALS['db_type']) {
-    case 'mysqli':
-    case 'mysql':
-      $sql = 'CREATE TABLE {taxonomy_enhancer_fields} (
-                vid int(11) unsigned NOT NULL,
-                fid varchar(255) NOT NULL default "",
-                title varchar(255) NOT NULL,
-                module varchar(255) NOT NULL,
-                type varchar(255) NOT NULL,
-                weight int(11) NOT NULL default "0",
-                settings text,
-                PRIMARY KEY  (vid,fid)
-              ) /*!40100 DEFAULT CHARACTER SET utf8 */;';
-
-      db_query($sql);
-      break;
-  }
-}
-
+  drupal_install_schema('taxonomy_enhancer');
+}
+
+function taxonomy_enhancer_uninstall() {
+  drupal_uninstall_schema('taxonomy_enhancer');
+}
+
+function taxonomy_enhancer_schema() {
+  $schema['taxonomy_enhancer_fields'] = array(
+    'fields' => array(
+      'vid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+      ),
+      'fid' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE,
+      ),
+      'title' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE
+      ),
+      'module' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE,
+        'description' => 'taxonomy enhancer field provider module'
+      ),
+      'type' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE
+      ),
+      'weight' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'settings' => array(
+        'serialize' => TRUE,
+        'type' => 'text',
+      ),
+    ),
+    'primary key' => array('vid', 'fid'),
+  );
+  return $schema;
+}
 
 function taxonomy_enhancer_update_10() {
   $ret = array();

=== modified file 'taxonomy_enhancer.module'
--- taxonomy_enhancer.module	2008-08-03 17:09:24 +0000
+++ taxonomy_enhancer.module	2009-02-21 17:08:47 +0000
@@ -9,33 +9,26 @@
  */
 
 /**
- * Implementation of hook_init().
- *
- * This is used to check if any updates failed
- */
-function taxonomy_enhancer_init() {
-  $level = variable_get('taxonomy_enhancer_update_fail', FALSE);
-  if ($level !== FALSE) {
-    db_query('UPDATE {system} SET schema_version = %d WHERE name = "taxonomy_enhancer"', $level);
-    variable_del('taxonomy_enhancer_update_fail');
-  }
-}
-
-
-/**
  * Implementation of hook_form_alter().
  *
  * @param string $form_id
  * @param array $form
  */
-function taxonomy_enhancer_form_alter($form_id, &$form) {
+function taxonomy_enhancer_form_alter(&$form, &$form_state, $form_id) {
   switch ($form_id) {
     case 'taxonomy_form_vocabulary' :
-      _taxonomy_enhancer_form_alter_vocabulary($form);
+      _taxonomy_enhancer_form_alter_vocabulary($form, $form_state, $form_id);
       break;
 
     case 'taxonomy_form_term' :
-      _taxonomy_enhancer_form_alter_term($form);
+      _taxonomy_enhancer_form_alter_term($form, $form_state, $form_id);
+      break;
+
+    case 'taxonomy_manager_form':
+      //only add enhancer fields when the term data form is loaded
+      if (is_array($form['term_data']['tid'])) {
+        _taxonomy_enhancer_form_alter_term($form['term_data'], $form_state, $form_id);
+      }
       break;
   }
 }
@@ -46,7 +39,8 @@
  *
  * @param array $form
  */
-function _taxonomy_enhancer_form_alter_vocabulary(&$form) {
+function _taxonomy_enhancer_form_alter_vocabulary(&$form, &$form_state, $form_id) {
+
   if (isset($form['vid']['#value'])) {
     $vid = $form['vid']['#value'];
   }
@@ -99,7 +93,6 @@
         '#type' => 'textfield',
         '#title' => t('Field Title'),
         '#default_value' => $field->title,
-        '#required' => TRUE,
         '#description' => t('The title is the human-friendly identified of the field.'),
       );
 
@@ -107,12 +100,13 @@
         '#type' => 'select',
         '#title' => t('Field Type'),
         '#options' => taxonomy_enhancer_get_field_types(),
-        '#default_value' => $field->type,
+        '#default_value' => array($field->module .'|'. $field->type),
         '#description' => t('The type is used to define the data that this field represents, eg text, date, etc.'),
       );
       $form['fields']['created'][$id]['weight'] = array(
         '#type' => 'weight',
         '#title' => t('Field Weight'),
+        '#default_value' => $field->weight,
         '#description' => t('The weight controls where this field appears in the <em>Extended Terms</em> form.'),
       );
 
@@ -128,7 +122,7 @@
   $form['fields']['new'] = array(
     '#type' => 'fieldset',
     '#title' => t('Add New Field'),
-    '#validate' => array('_taxonomy_enhancer_validate_new' => array()),
+    '#element_validate' => array('_taxonomy_enhancer_validate_new'),
     '#collapsed' => TRUE,
     '#collapsible' => TRUE,
   );
@@ -179,22 +173,28 @@
  *
  * @param array $form
  */
-function _taxonomy_enhancer_validate_new($form) {
-  if (empty($form['fid']['#value']) && empty($form['title']['#value'])) {
+function _taxonomy_enhancer_validate_new($form, &$form_state) {
+  $form_values = $form_state['values'];
+  $field_id = $form_values['fields']['new']['fid'];
+  $field_title = $form_values['fields']['new']['title'];
+
+  if (empty($field_id) && empty($field_title)) {
     return;
   }
-
-  if (preg_match('|[^a-z0-9_\-]|', $form_values['fid']['#value'])) {
+  if (preg_match('|[^a-z0-9_\-]|', $field_id)) {
     form_set_error('fields][new][fid', t('Name/Field ID must be lowercase alphanumeric & underscore only'));
   }
-
-  if (!empty($form['fid']['#value']) && empty($form['title']['#value'])) {
+  if (!empty($field_id) && empty($field_title)) {
     form_set_error('fields][new][title', t('Both field Name and Title must be set when creating a new field'));
   }
-
-  if (empty($form['fid']['#value']) && !empty($form['title']['#value'])) {
+  if (empty($field_id) && !empty($field_title)) {
     form_set_error('fields][new][fid', t('Both field Name and Title must be set when creating a new field'));
   }
+  // Add the 'field_' prefix.
+  if (!empty($field_id) && substr($field_id, 0, 6) != 'field_') {
+    $field_id = 'field_'. $field_id;
+    form_set_value($form['fid'], $field_id, $form_state);
+  }
 }
 
 
@@ -203,7 +203,7 @@
  *
  * @param array $form
  */
-function _taxonomy_enhancer_form_alter_term(&$form) {
+function _taxonomy_enhancer_form_alter_term(&$form, &$form_state, $form_id) {
   if (isset($form['tid']['#value'])) {
     $tid = $form['tid']['#value'];
     $term = taxonomy_get_term($tid);
@@ -230,28 +230,27 @@
     $form['fields'] = array_merge($form['fields'], $extra);
   }
 
-
   $form['submit']['#weight'] = $form['delete']['#weight'] = 10;
 }
 
 
 /**
  * Public function to "load" the extended data for a term.
- * Note: Load is different to View! Load will attach the data in a more 'raw' form. The data is not ready for viewing.
+ * Note: Load is different to View! Load will attach the data in a more 'raw' form. The data is not ready for
+ * viewing.
  *
  * @param object $term
  *   Term object to be extended
  */
 function taxonomy_enhancer_load_term(&$term) {
   $fields = taxonomy_enhancer_get_fields_by_vocabulary($term->vid);
-  $term->fields = array();
-
   foreach ($fields as $field) {
     $term->{$field->fid} = module_invoke($field->module, 'te_api', 'load', 'value', $field, $term);
   }
 }
 
 
+
 /**
  * Public function to extend a given term. This is effectively the "View" function.
  * This will invoke the view hooks and generate the extended fields in a form ready for display.
@@ -261,12 +260,14 @@
  */
 function taxonomy_enhancer_extend_term(&$term) {
   $fields = taxonomy_enhancer_get_fields_by_vocabulary($term->vid);
-  $term->fields = array();
-
+  $output = "";
   foreach ($fields as $field) {
-    $data = module_invoke($field->module, 'te_api', 'view', 'value', $field, $term);
-    $term->{$field->fid} = taxonomy_enhancer_theme('taxonomy_enhanced_field', $field, $data);
+    $term->{$field->fid} = module_invoke($field->module, 'te_api', 'load', 'value', $field, $term);
+    module_invoke($field->module, 'te_api', 'view', 'value', $field, $term);
+    $output .=  theme('taxonomy_enhancer_field',$field, $term)."<br />";
   }
+
+  return $output;
 }
 
 
@@ -279,104 +280,99 @@
  */
 function taxonomy_enhancer_taxonomy($op, $type, $array = NULL) {
   switch ($type) {
-    case 'vocabulary' :
-      switch ($op) {
-        case 'update' :
-          if (!empty($array['fields']['new']['fid'])) {
-            //Get the module and field type from the concatendated form type field.
-            list($module, $field_type) = explode('|', $array['fields']['new']['type']);
-
-            //Build a form array for inserting and module_invoking...ing.
-            $field = array(
-              'vid' => $array['vid'],
-              'fid' => $array['fields']['new']['fid'],
-              'title' => $array['fields']['new']['title'],
-              'module' => $module,
-              'type' => $field_type,
-              'weight' => $array['fields']['new']['weight'],
-              'settings' => serialize(array()),
-            );
-
-            //Create the entry in the DB
-            db_query('INSERT INTO {taxonomy_enhancer_fields} (vid, fid, title, module, type, weight, settings) VALUES(%d, "%s", "%s", "%s", "%s", %d, "%s")', $field);
-
-            //Invoke the module's hook
-            module_invoke($module, 'te_api', 'insert', 'field', $field);
-          }
-
-         //Handle updating of fields...
-          if (isset($array['fields']['created']) && is_array($array['fields']['created'])) {
-            foreach ($array['fields']['created'] as $fid => $field) {
-              //Get the module and type from the concatenated field (eg, 'taxonomy_enhancer_text|text'
-              list($module, $type) = explode('|', $field['type']);
-
-              //Compare the old field's module and type to the submitted field. If its changed then blank out the settings.
-              $old_field = taxonomy_enhancer_get_field($array['vid'], $field['fid']);
-              if ($old_field->module == $module && $old_field->type = $type) {
-                $settings = module_invoke($module, 'te_api', 'update', 'field', $field);
-              }
-              else {
-                $settings = array();
-              }
-
-              //Write the field.
-              db_query(
-                'UPDATE {taxonomy_enhancer_fields} SET title = "%s", module = "%s", type = "%s", weight = %d, settings = "%s" WHERE vid = %d AND fid = "%s"',
-                $field['title'], $module, $type, $field['weight'], serialize($settings), $array['vid'], $field['fid']
-              );
-            }
-          }
-
-          //Delete any checked fields
-          $delete = array_filter($array['fields']['delete']['fields']);
-          if (!empty($delete)) {
-            foreach ($delete as $fid) {
-              $field = taxonomy_enhancer_get_field($array['vid'], $fid);
-              module_invoke($field->module, 'te_api', 'delete', 'field', $field);
-              db_query('DELETE FROM {taxonomy_enhancer_fields} WHERE vid = %d AND fid = "%s"', $array['vid'], $field->fid);
-            }
-          }
-          break;
-
-
-        case 'delete' :
-          $fields = taxonomy_enhancer_get_fields_by_vocabulary($array['vid']);
-          foreach ($fields as $field) {
+    case 'vocabulary':
+      _taxonomy_enhancer_vocabulary($op, $array);
+      break;
+
+    case 'term':
+      _taxonomy_enhancer_term($op, $array);
+      break;
+  }
+}
+
+function _taxonomy_enhancer_term($op, $term) {
+  $fields = taxonomy_enhancer_get_fields_by_vocabulary($term['vid']);
+
+  foreach ($fields as $field) {
+    module_invoke($field->module, 'te_api', $op, 'value', $field, $term);
+  }
+}
+
+function _taxonomy_enhancer_vocabulary($op, $vocabulary) {
+  switch ($op) {
+    case 'update' :
+      if (!empty($vocabulary['fields']['new']['fid'])) {
+        //Get the module and field type from the concatendated form type field.
+        list($module, $field_type) = explode('|', $vocabulary['fields']['new']['type']);
+
+        //Build a form array for inserting and module_invoking...ing.
+        $field = array(
+          'vid' => $vocabulary['vid'],
+          'fid' => $vocabulary['fields']['new']['fid'],
+          'title' => $vocabulary['fields']['new']['title'],
+          'module' => $module,
+          'type' => $field_type,
+          'weight' => $vocabulary['fields']['new']['weight'],
+          'settings' => array(),
+        );
+
+        //Create the entry in the DB
+        drupal_write_record('taxonomy_enhancer_fields', $field);
+
+        //Invoke the module's hook
+        module_invoke($module, 'te_api', 'insert', 'field', $field);
+      }
+
+      //Handle updating of fields...
+      if (isset($vocabulary['fields']['created']) && is_array($vocabulary['fields']['created'])) {
+        foreach ($vocabulary['fields']['created'] as $fid => $field) {
+          //Get the module and type from the concatenated field (eg, 'taxonomy_enhancer_text|text'
+          list($module, $type) = explode('|', $field['type']);
+
+          //Compare the old field's module and type to the submitted field. If its changed then blank out the settings.
+          $old_field = taxonomy_enhancer_get_field($vocabulary['vid'], $field['fid']);
+          if ($old_field->module == $module && $old_field->type == $type) {
+            $settings = module_invoke($module, 'te_api', 'update', 'field', $field);
+          }
+          else {
+            $settings = array();
+          }
+
+          //Write the field.
+          $record->title = $field['title'];
+          $record->module = $module;
+          $record->type = $type;
+          $record->weight = $field['weight'];
+          $record->settings = $settings;
+          $record->vid = $vocabulary['vid'];
+          $record->fid = $field['fid'];
+          drupal_write_record('taxonomy_enhancer_fields', $record, array('vid', 'fid'));
+        }
+      }
+
+      //Delete any checked fields
+      if ($vocabulary['fields']['delete']['fields']) {
+        $delete = array_filter($vocabulary['fields']['delete']['fields']);
+        if (!empty($delete)) {
+          foreach ($delete as $fid) {
+            $field = taxonomy_enhancer_get_field($vocabulary['vid'], $fid);
             module_invoke($field->module, 'te_api', 'delete', 'field', $field);
-            db_query('DELETE FROM {taxonomy_enhancer_fields} WHERE vid = %d AND fid = "%s"', $array['vid'], $field->fid);
+            db_query('DELETE FROM {taxonomy_enhancer_fields} WHERE vid = %d AND fid = "%s"', $vocabulary['vid'], $field->fid);
           }
-          break;
+        }
       }
       break;
 
-
-    case 'term' :
-      $fields = taxonomy_enhancer_get_fields_by_vocabulary($array['vid']);
-
-      switch ($op) {
-        case 'update' :
-          foreach ($fields as $field) {
-            module_invoke($field->module, 'te_api', 'update', 'value', $field, $array);
-          }
-          break;
-
-        case 'insert' :
-          foreach ($fields as $field) {
-            module_invoke($field->module, 'te_api', 'insert', 'value', $field, $array);
-          }
-          break;
-
-        case 'delete' :
-          foreach ($fields as $field) {
-            module_invoke($field->module, 'te_api', 'delete', 'value', $field, $array);
-          }
-          break;
+    case 'delete' :
+      $fields = taxonomy_enhancer_get_fields_by_vocabulary($vocabulary['vid']);
+      foreach ($fields as $field) {
+        module_invoke($field->module, 'te_api', 'delete', 'field', $field);
+        db_query('DELETE FROM {taxonomy_enhancer_fields} WHERE vid = %d AND fid = "%s"', $vocabulary['vid'], $field->fid);
       }
       break;
   }
 }
 
-
 /**
  * Get an individual field identified by Vocabulary ID and Field ID
  *
@@ -388,15 +384,15 @@
  *   Array of field objects
  */
 function taxonomy_enhancer_get_field($vid, $fid) {
-  static $field_cache;
+  static $fields;
 
-  if (!isset($field_cache[$vid][$fid])) {
-    $field_cache[$vid][$fid] = db_fetch_object(db_query('SELECT * FROM {taxonomy_enhancer_fields} f WHERE vid = %d AND fid = "%s"', $vid, $fid));
-    $field_cache[$vid][$fid] = drupal_unpack($field_cache[$vid][$fid], 'settings');
-    unset($field_cache[$vid][$fid]->setttings);
+  if (!isset($fields[$vid][$fid])) {
+    $fields[$vid][$fid] = db_fetch_object(db_query('SELECT * FROM {taxonomy_enhancer_fields} f WHERE vid = %d AND fid = "%s"', $vid, $fid));
+    $fields[$vid][$fid] = drupal_unpack($fields[$vid][$fid], 'settings');
+    unset($fields[$vid][$fid]->setttings);
   }
 
-  return $field_cache[$vid][$fid];
+  return $fields[$vid][$fid];
 }
 
 
@@ -425,7 +421,6 @@
     $fields[$vid] = $temp;
   }
 
-
   return $fields[$vid];
 }
 
@@ -435,6 +430,7 @@
  *   - Returns -1 (negative one) if 'A' is less than 'B'
  *   - Returns  1 (positive one) if 'A' is more than 'B'
  *   - Returns  0 (zero) if 'A' and 'B' are identical
+ * ripped from _user_sort.
  *
  * @param object $a
  *   Field Object A
@@ -444,18 +440,9 @@
  *   Comparison result
  */
 function _taxonomy_enhancer_field_sort($a, $b) {
-  if ($a->weight < $b->weight) {
-    return -1;
-  }
-  elseif ($aweight > $bweight) {
-    return 1;
-  }
-  elseif (isset($a->title) && isset($b->title)) {
-    return strnatcasecmp($a->title, $b->title);
-  }
-  else {
-    return 1;
-  }
+  $a = (array)$a + array('weight' => 0, 'title' => '');
+  $b = (array)$b + array('weight' => 0, 'title' => '');
+  return $a['weight'] < $b['weight'] ? -1 : ($a['weight'] > $b['weight'] ? 1 : ($a['title'] < $b['title'] ? -1 : 1));
 }
 
 
@@ -468,7 +455,6 @@
 function taxonomy_enhancer_get_field_types() {
   $items = array();
 
-
   foreach (module_implements('te_api') as $module) {
     $result = module_invoke($module, 'te_api', 'list', 'field');
 
@@ -483,50 +469,113 @@
 }
 
 
-/**
- * Theme function inspired by/leeched from Views (views_theme)
- *
- * Easily theme any item to an enhanced term field.
- * @param $function
- *   The name of the function to call.
+
+
+//TODO: may change themeing
+//TODO: may need to sort on type
+/**
+ * taxonomy_enhancer_theme is takes a term object and renders formatted content
+ * this is called in the theme_taxonomy_term_page function, which should be placed in the theme.
+ */
+//
+function taxonomy_enhancer_theme(&$term) {
+  $path = drupal_get_path('module', 'taxonomy_enhanced') .'/theme';
+  return array(
+    'taxonomy_enhancer_field' => array(
+  //  'template' => 'taxonomy-enhanced-field', //TODO: escaped for now need to fix the path
+  //TODO: if object changes we may just need null to execute this
+  //'arguments' => array('form' => array()),
+      'arguments' => array('form' => NULL),//TODO may need argument here
+      'path' => $path,
+  ),
+  );
+}
+
+
+function theme_taxonomy_enhancer_field($field, $term) {
+  //$field comes from $term object, but renders like cck fields, so we are changing the terminology here
+  //$field contains attributes of the field; $term contains the content with the field as an array item
+  $fid = $field->fid;
+  //can specify 'value' or 'safe' for the type
+  $field_output = taxonomy_enhancer_content_format($fid, $term->{$fid}, 'safe'); //is an array
+  $output = "";
+  if(!empty($field_output)){
+    $output .= "<p><strong>".$field->title."</strong></p>";
+    $output .= "<p>".$field_output."</p>";
+  }
+  return $output;
+}
+
+
+
+/**
+ * Format a field item for display.
+ * Based on CCK content_format, but reduced to match up with taxonomy_enhancer text module capabilities
+ *
+ * Used to display a field's values outside the context of the $node, as
+ * when fields are displayed in Views, or to display a field in a template
+ * using a different formatter than the one set up on the Display Fields tab
+ * for the node's context.
+ *
  * @param $field
- *   The field being themed.
- * @param $data
- *   The data being outputted
- */
-function taxonomy_enhancer_theme() {
-  $args = func_get_args();
-  $function = array_shift($args);
-  $field = $args[0];
-
-  if (!($func = theme_get_function($function .'_'. $field->fid))) {
-    $func = theme_get_function($function);
-  }
-
-  if ($func) {
-    return call_user_func_array($func, $args);
-  }
-}
-
-
-/**
- * Default Theme function for a generic field.
- *
- * @param object $field
- *   Field object
- * @param string $data
- *   String to be outputted
- * @return string
- *   Rendered Output
- */
-function theme_taxonomy_enhanced_field($field, $data) {
-  switch ($field->format) {
-    case 'raw' :
-      return $data;
-    default :
-      return '<div id="field_'. $field->fid .'" class="taxonomy_enhanced_field '. $field->type .'">'. $data .'</div>';
-  }
-}
+ *   Either a field array or the name of the field.
+ * @param $item
+ *   The field item(s) to be formatted (such as $node->field_foo[0],
+ *   or $node->field_foo if the formatter handles multiple values itself)
+ *
+ * @param $type
+ *  can be 'safe' or 'value' depending whether or not the text should be run through the filters.
+ *
+ * @return
+ *   A string containing the contents of the field item(s) sanitized for display.
+ *   It will have been passed through the necessary check_plain() or check_markup()
+ *   functions as necessary.
+ */
+function taxonomy_enhancer_content_format($field,  $item, $type = 'safe') {
+  /*
+   * Test if array is just data, 1 field, or multiple
+   */
+  //TODO: deal with empty content here
+  if(!is_array($item)){
+    $output = $item; //TODO: this might be incorrect depending what kind of field you are using...
+  }
+  else if(count($item) == 1){
+    if($type == 'value'){
+      $output = $item[0]['value'];
+    }
+    else{
+      $output = $item[0]['safe'];
+    }
+  }
+  else{
+    $output = "";
+    $items = $item;
+    foreach($items as $key=>$item){
+      $output .= $items[$key]['safe'];
+
+      if($type == 'value'){
+        $output = $items[$key]['value'];
+      }
+      else{
+        $output = $items[$key]['safe'];
+      }
+
+    }
+  }
+
+  return $output;
+
+}
+
+
+
+
+
+
+
+
+
+
 
 /**
  * Implementation of hook_token_values().
@@ -561,7 +610,6 @@
   }
 }
 
-
 function _taxonomy_enhancer_get_distinct_implemented_fields() {
   static $fields = NULL;
 
@@ -575,3 +623,4 @@
 
   return $fields;
 }
+

