ソースを参照

used new property API for analysis module

Stephen Ficklin 11 年 前
コミット
f3944944b0

+ 7 - 469
tripal_analysis/includes/tripal_analysis.form.inc

@@ -183,26 +183,10 @@ function chado_analysis_form($node, &$form_state) {
        someone to recreate the analysis, including materials and methods
        someone to recreate the analysis, including materials and methods
        for collection of the source data and performing the analysis'),
        for collection of the source data and performing the analysis'),
   );
   );
-  $form['properties'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Analysis Details'),
-    '#description' => t('You may add additional properties by
-      selecting a property type from the dropdown and adding text.  You may add 
-      as many properties as desired by clicking the add button on the right.  To 
-      remove a property, click the minus button.  If a property is not available
-      you may add it by ' . l('adding the term', 'admin/tripal/tripal_cv/cvterm/add') . '
-      to the <b>analysis_property</b> vocabulary within the <b>tripal</b> database'),
-  );
-  $form['properties']['table'] = array(
-    '#type' => 'markup',
-    '#value' =>  '',
-    '#prefix' => '<div id="tripal-analysis-edit-properties-table">',
-    '#suffix' => '</div>',
-  );
   
   
   // get the analysis properties
   // get the analysis properties
-  $properties_select = array();
-  $properties_select[] = 'Select a Property';
+  $properties = array();
+  $properties[] = 'Select a Property';
   $sql = "
   $sql = "
     SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
     SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
     FROM  {cvterm} CVT
     FROM  {cvterm} CVT
@@ -214,352 +198,16 @@ function chado_analysis_form($node, &$form_state) {
   ";
   ";
   $prop_types = chado_query($sql);
   $prop_types = chado_query($sql);
   while ($prop = $prop_types->fetchObject()) {
   while ($prop = $prop_types->fetchObject()) {
-    $properties_select[$prop->cvterm_id] = $prop->name;
-  }
-  
-  // this array keeps track of all properties we have and allows the functions
-  // below to select the next rank if a property is dupliated
-  $ranks = array();
- 
-  // add in the properties from the Chado analysisprop table (only pertains to existing analyses)
-  if ($analysis_id) {
-    chado_analysis_node_form_add_analysisprop_table_props($form, $form_state, $analysis_id, $ranks, $d_removed);
+    $properties[$prop->cvterm_id] = $prop->name;
   }
   }
   
   
-  // add in any new properties that have been added by the user through an AHAH callback
-  chado_analysis_node_form_add_new_props($form, $form_state, $ranks, $d_removed);
+  $exclude = array();
+  $instructions = t('To add additional properties to the drop down. ' . l("Add terms to the analysis_property vocabulary", "admin/tripal/chado/tripal_cv/cvterm/add") . ".");
+  tripal_core_properties_form($form, $form_state, 'analysisprop', 'analysis_id', 'analysis_property',
+    $properties, $analysis_id, $exclude, $instructions);
 
 
-  // add an empty row of field to allow for addition of a new property
-  chado_analysis_node_form_add_new_empty_props($form, $form_state, $properties_select);
-  
-  $form['#theme'] = 'chado_analysis_form';
   return $form;
   return $form;
 }
 }
-/**
- * This function is responsible for adding a blank row to the properties table for
- * adding a new property.
- */
-function chado_analysis_node_form_add_new_empty_props(&$form, &$form_state, $properties_select) {
-
-  // get the field defaults either from $form_state['values'] or $form_state['input']
-  $description = '';
-  $text = '';
-  $id = 0;
-  if (array_key_exists('values', $form_state)) {
-    $id = $form_state['values']['new_id'];
-    $text = $form_state['values']['new_value'];
-  }
-  
-  // if we have a property ID then get it's definition to display to the user
-  if($id) { 
-    $values = array('cvterm_id' => $id);
-    $cvterm = tripal_core_chado_select('cvterm', array('definition'), $values);
-  
-    if ($cvterm[0]->definition) {
-      $description = $cvterm[0]->definition;
-    }
-  }
-  $rows = 1;
-  
-  // add one more blank set of property fields
-  $form['properties']['table']['new']["new_id"] = array(
-    '#type'          => 'select',
-    '#options'       => $properties_select,
-    '#default_value' => $id,
-    '#ajax' => array(
-      'callback' => "tripal_analysis_property_get_description",
-      'wrapper'  => 'tripal-analysis-new_value',
-      'effect'   => 'fade',
-      'method'   => 'replace',
-    ),
-  );
-  $form['properties']['table']['new']["new_value"] = array(
-    '#type'           => 'textarea',
-    '#default_value'  => $text,
-    '#cols'           => 50,
-    '#rows'           => $rows,
-    '#prefix'         => '<div id="tripal-analysis-new_value">',
-    '#description'    => $description,
-    '#suffix'         => '</div>',
-  );
-  
-  $form['properties']['table']['new']["add"] = array(
-    '#type'    => 'button',
-    '#value'   => t('Add'),
-    '#name'    => 'add',
-    '#ajax'      => array(
-      'callback' => "tripal_analysis_property_ajax_update",
-      'wrapper'  => 'tripal-analysis-edit-properties-table',
-      'effect'   => 'fade',
-      'method'   => 'replace',
-      'prevent'  => 'click'
-    ),
-    // When this button is clicked, the form will be validated and submitted. 
-    // Therefore, we set custom submit and validate functions to override the
-    // default form submit.  In the validate function we set the form_state
-    // to rebuild the form so the submit function never actually gets called,
-    // but we need it or Drupal will run the default validate anyway.
-    // we also set #limit_validation_errors to empty so fields that
-    // are required that don't have values won't generate warnings. 
-    '#submit'   => array('chado_analysis_node_form_props_button_submit'),
-    '#validate' => array('chado_analysis_node_form_props_button_validate'),
-    '#limit_validation_errors' => array(array('new_id')),
-  );
-}
-/**
- * This function is used to rebuild the form if an ajax call is made vai a button.
- * The button causes the form to be submitted. We don't want this so we override
- * the validate and submit routines on the form button. Therefore, this function
- * only needs to tell Drupal to rebuild the form
- */
-function chado_analysis_node_form_props_button_validate($form, &$form_state){
-  if (array_key_exists('triggering_element', $form_state) and 
-      $form_state['triggering_element']['#name'] == 'add' and
-      $form_state['input']['new_id'] == 0 ){
-    form_set_error('new_id', "Please specify a property type");
-    return;
-  }
-  $form_state['rebuild'] = TRUE; 
-}
-/**
- * This function is just a dummy to override the default form submit on ajax calls for buttons
- */
-function chado_analysis_node_form_props_button_submit($form, &$form_state){
-  // do nothing
-}
-/**
- * This adds 
- */
-function chado_analysis_node_form_add_new_props(&$form, &$form_state, &$ranks, &$d_removed) {
-   
-  // set some default values
-  $j = 0;
-  $num_properties = 0;
-  
-  $values = array();
-  if (array_key_exists('values', $form_state)) {
-    $values = $form_state['values'];
-  }
-  if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
-    $values = $form_state['input'];
-  }
-  
-  // first, add in all of the new properties that were added previously via this form
-  foreach ($values as $element_name => $value) {
-    if (preg_match('/new_value-(\d+)-(\d+)/', $element_name, $matches)) {
-      $new_id = $matches[1];
-      $rank = $matches[2];
-  
-      // skip any properties that the user requested to delete through a previous
-      // ajax callback or through the current ajax callback
-      if (array_key_exists("$new_id-$rank", $d_removed)) {
-        continue;
-      }
-      if (array_key_exists('triggering_element', $form_state) and 
-         $form_state['triggering_element']['#name'] == 'remove-' . $new_id . '-' . $rank) {
-        $d_removed["$new_id-$rank"] = 1;
-        continue;
-      }
-  
-      // get this new_id information
-      $args = array('cvterm_id' => $new_id);
-      $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), $args);
-
-      // add it to the $ranks array
-      $ranks[$new_id][$rank]['name']  = $cvterm[0]->name;
-      $ranks[$new_id][$rank]['id']    = $new_id;
-      $ranks[$new_id][$rank]['value'] = $value;
-      $ranks[$new_id][$rank]['definition']  = $cvterm[0]->definition;
-      $num_properties++;
-  
-      // determine how many rows we need in the textarea
-      $rows = 1;
-      // add the new fields
-      $form['properties']['table']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
-        '#markup' => $cvterm[0]->name
-      );
-      $form['properties']['table']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
-        '#type'          => 'textarea',
-        '#default_value' => $value,
-        '#cols'          => 50,
-        '#rows'          => $rows,
-        '#description'   => $cvterm[0]->definition,
-      );
-  
-      $form['properties']['table']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
-        '#type'         => 'button',
-        '#value'        => t('Remove'),
-        '#name'         => "remove-$new_id-$rank",
-        '#ajax' => array(
-          'callback' => "tripal_analysis_property_ajax_update",
-          'wrapper'  => 'tripal-analysis-edit-properties-table',
-          'effect'   => 'fade',
-          'event'    => 'mousedown',
-          'method'   => 'replace',
-          'prevent'  => 'click'
-        ),
-        // When this button is clicked, the form will be validated and submitted.
-        // Therefore, we set custom submit and validate functions to override the
-        // default form submit.  In the validate function we set the form_state
-        // to rebuild the form so the submit function never actually gets called,
-        // but we need it or Drupal will run the default validate anyway.
-        // we also set #limit_validation_errors to empty so fields that
-        // are required that don't have values won't generate warnings.
-        '#submit'   => array('chado_analysis_node_form_props_button_submit'),
-        '#validate' => array('chado_analysis_node_form_props_button_validate'),
-        '#limit_validation_errors' => array(),
-      );
-    }
-  }
-
-  // second add in any new properties added during this callback 
-  if (array_key_exists('triggering_element', $form_state) and 
-      $form_state['triggering_element']['#name'] == 'add' and
-      $form_state['input']['new_id'] != 0) {
-    $new_id    = $form_state['input']['new_id'];
-    $new_value = $form_state['input']['new_value'];    
-
-    // get the rank by counting the number of entries
-    $rank = count($ranks[$new_id]);
-
-    // get this new_id information
-    $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), array('cvterm_id' => $new_id));
-
-    // add it to the $ranks array
-    $ranks[$new_id][$rank]['name']  = $cvterm[0]->name;
-    $ranks[$new_id][$rank]['id']    = $new_id;
-    $ranks[$new_id][$rank]['value'] = $value;
-    $ranks[$new_id][$rank]['definition']  = $cvterm[0]->definition;
-    $num_properties++;
-
-    // determine how many rows we need in the textarea
-    $rows = 1;
-
-    // add the new fields
-    $form['properties']['table']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
-      '#markup' => $cvterm[0]->name
-    );
-    $form['properties']['table']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
-      '#type'          => 'textarea',
-      '#default_value' => $new_value,
-      '#cols'          => 50,
-      '#rows'          => $rows,
-      '#description'   => $cvterm[0]->definition,
-    );
-
-    $form['properties']['table']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
-      '#type'         => 'button',
-      '#value'        => t('Remove'),
-      '#name'         => "remove-$new_id-$rank",
-      '#ajax' => array(
-        'callback' => "tripal_analysis_property_ajax_update",
-        'wrapper'  => 'tripal-analysis-edit-properties-table',
-        'effect'   => 'fade',
-        'event'    => 'mousedown',
-        'method'   => 'replace',
-        'prevent'  => 'click'
-      ),
-      // When this button is clicked, the form will be validated and submitted.
-      // Therefore, we set custom submit and validate functions to override the
-      // default form submit.  In the validate function we set the form_state
-      // to rebuild the form so the submit function never actually gets called,
-      // but we need it or Drupal will run the default validate anyway.
-      // we also set #limit_validation_errors to empty so fields that
-      // are required that don't have values won't generate warnings.
-      '#submit'   => array('chado_analysis_node_form_props_button_submit'),
-      '#validate' => array('chado_analysis_node_form_props_button_validate'),
-      '#limit_validation_errors' => array(),
-    );
-
-  }
-
-  return $num_properties;
-}
-/*
- *
-*/
-function chado_analysis_node_form_add_analysisprop_table_props(&$form, $form_state, $analysis_id, &$ranks, &$d_removed) {
-
-  // get the properties for this analysis
-  $num_properties = 0;
-
-  if (!$analysis_id) {
-    return $num_properties;
-  }
-
-  $sql = "
-    SELECT CVT.cvterm_id, CVT.name, CVT.definition, PP.value, PP.rank
-    FROM {analysisprop} PP
-      INNER JOIN {cvterm} CVT on CVT.cvterm_id = PP.type_id
-      INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
-    WHERE PP.analysis_id = :analysis_id and CV.name = 'analysis_property'
-    ORDER BY CVT.name, PP.rank
-  ";
-  $analysis_props = chado_query($sql, array(':analysis_id' => $analysis_id));
-  while ($prop = $analysis_props->fetchObject()) {
-
-    $type_id = $prop->cvterm_id;
-    $rank = 0;
-    if(array_key_exists($type_id, $ranks)) {
-      $rank = count($ranks[$type_id]);
-    }
-
-    // skip any properties that the user requested to delete through a previous
-    // AHAH callback or through the current AHAH callback
-    if (array_key_exists("$type_id-$rank", $d_removed)) {
-      continue;
-    }
-    if (array_key_exists('triggering_element', $form_state) and 
-        $form_state['triggering_element']['#name'] == 'remove-' . $type_id . '-' . $rank) {
-      $d_removed["$type_id-$rank"] = 1;
-      continue;
-    }
-
-    $ranks[$type_id][$rank]['name']  = $prop->name;
-    $ranks[$type_id][$rank]['id']    = $type_id;
-    $ranks[$type_id][$rank]['value'] = $prop->value;
-    $ranks[$type_id][$rank]['definition']  = $prop->definition;
-    $num_properties++;
-    $rows = 1;
-
-    $form['properties']['table'][$type_id][$rank]["prop_id-$type_id-$rank"] = array(
-      '#markup'        => $prop->name,
-    );
-    $form['properties']['table'][$type_id][$rank]["prop_value-$type_id-$rank"] = array(
-      '#type'          => 'textarea',
-      '#default_value' => $prop->value,
-      '#cols'          => 50,
-      '#rows'          => $rows,
-      '#description'   => $prop->definition,
-    );
-
-    $form['properties']['table'][$type_id][$rank]["remove-$type_id-$rank"] = array(
-      '#type'         => 'button',
-      '#value'        => t('Remove'),
-      '#name'         => "remove-$type_id-$rank",
-      '#ajax' => array(
-        'callback' => "tripal_analysis_property_ajax_update",
-        'wrapper'  => 'tripal-analysis-edit-properties-table',
-        'effect'   => 'fade',
-        'event'    => 'mousedown',
-        'method'   => 'replace',
-        'prevent'  => 'click'
-      ),
-      // When this button is clicked, the form will be validated and submitted.
-      // Therefore, we set custom submit and validate functions to override the
-      // default form submit.  In the validate function we set the form_state
-      // to rebuild the form so the submit function never actually gets called,
-      // but we need it or Drupal will run the default validate anyway.
-      // we also set #limit_validation_errors to empty so fields that
-      // are required that don't have values won't generate warnings.
-      '#submit'   => array('chado_analysis_node_form_props_button_submit'),
-      '#validate' => array('chado_analysis_node_form_props_button_validate'),
-      '#limit_validation_errors' => array(),
-    );
-  }
-  return $num_properties;
-}
-
 /**
 /**
  * Validates the user input before creating an analysis node
  * Validates the user input before creating an analysis node
  *
  *
@@ -667,114 +315,4 @@ function tripal_analysis_validate($node, &$form_state) {
       }
       }
     }
     }
   }
   }
-}
-
-
-/*
- *
- */
-function tripal_analysis_theme_node_form_properties($form) {
-  $rows = array();
-
-  if (array_key_exists('properties', $form)) {
-
-    // first add in the properties derived from the analysisprop table
-    // the array tree for these properties looks like this:
-    // $form['properties']['table'][$type_id][$rank]["prop_id-$type_id-$rank"]
-    foreach ($form['properties']['table'] as $type_id => $elements) {
-      // there are other fields in the properties array so we only
-      // want the numeric ones those are our type_id
-      if (is_numeric($type_id)) {
-        foreach ($elements as $rank => $element) {
-          if (is_numeric($rank)) {
-            $rows[] = array(
-              drupal_render($element["prop_id-$type_id-$rank"]),
-              drupal_render($element["prop_value-$type_id-$rank"]),
-              drupal_render($element["remove-$type_id-$rank"]),
-            );
-          }
-        }
-      }
-    }
-
-    // second, add in any new properties added by the user through AHAH callbacks
-    // the array tree for these properties looks like this:
-    // $form['properties']['table']['new'][$type_id][$rank]["new_id-$new_id-$rank"]
-    foreach ($form['properties']['table']['new'] as $type_id => $elements) {
-      if (is_numeric($type_id)) {
-        foreach ($elements as $rank => $element) {
-          if (is_numeric($rank)) {
-            $rows[] = array(
-              drupal_render($element["new_id-$type_id-$rank"]),
-              drupal_render($element["new_value-$type_id-$rank"]),
-              drupal_render($element["remove-$type_id-$rank"]),
-            );
-          }
-        }
-      }
-    }
-
-    // finally add in a set of blank field for adding a new property
-    $rows[] = array(
-      drupal_render($form['properties']['table']['new']['new_id']),
-      array(
-        'data' => drupal_render($form['properties']['table']['new']['new_value']),
-        'width' => '60%',
-      ),
-      drupal_render($form['properties']['table']['new']['add']),
-    );
-  }
-  $headers = array('Property Type', 'Value', 'Actions');
-  $table = array(
-    'header' => $headers,
-    'rows' => $rows,
-    'attributes' => array(),
-    'sticky' => TRUE,
-    'caption' => '',
-    'colgroups' => array(),
-    'empty' => '',
-  );
-  
-  return theme_table($table); 
-}
-
-/**
- * Form AJAX callback for adding a blank property row
- * 
- * We only want to return the properties as that's all we'll replace with this callback
- */
-function tripal_analysis_property_ajax_update($form, $form_state) {
-  $properties_html = tripal_analysis_theme_node_form_properties($form); 
-  $form['properties']['table'] = array(
-    '#markup' => $properties_html,
-    '#prefix' => '<div id="tripal-analysis-edit-properties-table">',
-    '#suffix' => '</div>',
-  );
-  return $form['properties']['table'];
-}
-/**
- * Form AJAX callback for updating a property description. This 
- * function only gets called when the property drop down is changed
- * on the bottom (empty) row of properties
- */
-function tripal_analysis_property_get_description($form, $form_state) {
-  return $form['properties']['table']['new']["new_value"];
-}
-
-/**
- * We need to theme the analysis form so that the properties fields look good
- */
-function theme_chado_analysis_form($variables) {
-  $form = $variables['form'];
-
-  $properties_table = tripal_analysis_theme_node_form_properties($form);
-  $markup = $properties_table;
-
-  $form['properties']['table'] = array(
-    '#markup' => $markup,
-    '#prefix' => '<div id="tripal-analysis-edit-properties-table">',
-    '#suffix' => '</div>',
-  );
-  $form['buttons']['#weight'] = 50; 
-  return drupal_render_children($form);
 }
 }

+ 1 - 8
tripal_analysis/theme/tripal_analysis/tripal_analysis_properties.tpl.php

@@ -3,14 +3,7 @@
 // get the analysis object and expand it to include the records from the analysisprop table
 // get the analysis object and expand it to include the records from the analysisprop table
 $analysis = $variables['node']->analysis;
 $analysis = $variables['node']->analysis;
 $analysis = tripal_core_expand_chado_vars($analysis,'table', 'analysisprop', array('return_array' => 1));
 $analysis = tripal_core_expand_chado_vars($analysis,'table', 'analysisprop', array('return_array' => 1));
-$analysisprops = $analysis->analysisprop;
-
-// put the properties in an array for easier access
-$properties = array();
-foreach ($analysisprops as $property) {
-  $property = tripal_core_expand_chado_vars($property,'field','analysisprop.value');
-  $properties[] = $property;
-}
+$properties = $analysis->analysisprop;
 
 
 if (count($properties) > 0) { ?>
 if (count($properties) > 0) { ?>
   <div id="tripal_analysis-properties-box" class="tripal_analysis-info-box tripal-info-box">
   <div id="tripal_analysis-properties-box" class="tripal_analysis-info-box tripal-info-box">

+ 17 - 112
tripal_analysis/tripal_analysis.module

@@ -186,11 +186,6 @@ function tripal_analysis_theme($existing, $type, $theme, $path) {
       'variables' =>  array('node' => NULL),
       'variables' =>  array('node' => NULL),
       'path' => "$path/theme/tripal_analysis",
       'path' => "$path/theme/tripal_analysis",
     ),
     ),
-    
-    // form theme
-    'chado_analysis_form' => array(
-      'render element' => 'form',
-    )
 
 
   );
   );
 
 
@@ -361,6 +356,20 @@ function chado_analysis_insert($node) {
       return;
       return;
     }
     }
     $analysis_id = $analysis['analysis_id'];
     $analysis_id = $analysis['analysis_id'];
+    
+    // now add in the properties
+    $properties = tripal_core_properties_form_retreive($node, 'analysis_property');
+    foreach ($properties as $property => $elements) {
+      foreach ($elements as $rank => $value) {
+    
+        $status = tripal_analysis_insert_property($analysis_id, $property, $value, FALSE, 'analysis_property');
+        if (!$status) {
+          drupal_set_message("Error cannot add property: $property", "error");
+          watchdog('t_analysis', "Error cannot add property: %prop",
+          array('%property' => $property), WATCHDOG_ERROR);
+        }
+      }
+    }
   }
   }
   else {
   else {
     $analysis_id = $node->analysis_id;
     $analysis_id = $node->analysis_id;
@@ -381,60 +390,6 @@ function chado_analysis_insert($node) {
   // use by other analysis modules that may be using this function
   // use by other analysis modules that may be using this function
   $node->analysis = $analysis;
   $node->analysis = $analysis;
   $node->analysis_id = $analysis_id; // we need to set this for children
   $node->analysis_id = $analysis_id; // we need to set this for children
-
-  // now add the properties
-  $properties = array(); // stores all of the properties we need to add
-
-  // get the list of properties for easy lookup (without doing lots of database queries
-  $properties_list = array();
-  $sql = "
-      SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
-      FROM  {cvterm} CVT
-        INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
-      WHERE
-        CV.name = 'analysis_property' AND
-        NOT CVT.is_obsolete = 1
-      ORDER BY CVT.name ASC
-  ";
-  $prop_types = chado_query($sql);
-  while ($prop = $prop_types->fetchObject()) {
-    $properties_list[$prop->cvterm_id] = $prop->name;
-  }
-
-  // get the properties that should be added. Properties are in one of two forms:
-  //  1) prop_value-[type id]-[index]
-  //  2) new_value-[type id]-[index]
-  //  3) new_id, new_value
-
-  foreach ($node as $name => $value) {
-    if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
-      $type_id = $matches[1];
-      $index = $matches[2];
-      $name = $properties_list[$type_id];
-      $properties[$name][$index] = trim($value);
-    }
-  }
-  if ($node->new_id and $node->new_value) {
-    $type_id = $node->new_id;
-    $name = $properties_list[$type_id];
-    $index = 0;
-    if (array_key_exists($name, $properties)) {
-      $index = count($properties[$name]);
-    }
-    $properties[$name][$index] = trim($node->new_value);
-  }
-  
-  // now add in the properties
-  foreach ($properties as $property => $elements) {
-    foreach ($elements as $rank => $value) {
-      $status = tripal_analysis_insert_property($analysis_id, $property, $value, FALSE, 'analysis_property');
-      if (!$status) {
-          drupal_set_message("Error cannot add property: $property", "error");
-          watchdog('t_analysis', "Error cannot add property: %prop",
-            array('%property' => $property), WATCHDOG_ERROR);
-      }
-    }
-  }
 }
 }
 
 
 /**
 /**
@@ -517,67 +472,17 @@ function chado_analysis_update($node) {
   // now update the properties
   // now update the properties
   $properties = array(); // stores all of the properties we need to add
   $properties = array(); // stores all of the properties we need to add
   
   
-  // get the list of properties for easy lookup (without doing lots of database queries
-  $properties_list = array();
-  $sql = "
-    SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
-    FROM  {cvterm} CVT
-      INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
-    WHERE CV.name = 'analysis_property' AND NOT CVT.is_obsolete = 1
-    ORDER BY CVT.name ASC
-  ";
-  $prop_types = chado_query($sql);
-  while ($prop = $prop_types->fetchObject()) {
-    $properties_list[$prop->cvterm_id] = $prop->name;
-  }
-
-  // get the properties that should be added. Properties are in one of three forms:
-  //  1) prop_value-[type id]-[index]
-  //  2) new_value-[type id]-[index]
-  //  3) new_id, new_value
-  foreach ($node as $key => $value) {
-    if (preg_match('/^prop_value-(\d+)-(\d+)/', $key, $matches)) {
-      $type_id = $matches[1];
-      $index = $matches[2];
-      $name = $properties_list[$type_id];
-      $properties[$name][$index] = trim($value);
-    }
-    if (preg_match('/^new_value-(\d+)-(\d+)/', $key, $matches)) {
-      $type_id = $matches[1];
-      $index = $matches[2];
-      $name = $properties_list[$type_id];
-      $properties[$name][$index] = trim($value);
-    }
-  }
-  if ($node->new_id and $node->new_value) {
-    $type_id = $node->new_id;
-    $name = $properties_list[$type_id];
-    $index = count($properties[$name]);
-    $properties[$name][$index] = trim($node->new_value);
-  }
-
   // now add in the properties by first removing any the analysis
   // now add in the properties by first removing any the analysis
   // already has and adding the ones we have
   // already has and adding the ones we have
-  $sql = "
-    DELETE FROM {analysisprop} WHERE analysis_id = :analysis_id AND type_id IN (
-      SELECT CVT.cvterm_id
-      FROM  {cvterm} CVT
-        INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
-      WHERE CV.name = 'analysis_property')
-  ";
-  $success = chado_query($sql, array(':analysis_id' => $analysis_id));
-  if (!$success) {
-    drupal_set_message("Cannot update analysis properties", "error");
-    watchdog('t_analysis', "Cannot update analysis properties.", array(), WATCHDOG_ERROR);
-    return;
-  }
+  tripal_core_chado_delete('analysisprop', array('analysis_id' => $analysis_id));
+  $properties = tripal_core_properties_form_retreive($node, 'analysis_property');
   foreach ($properties as $property => $elements) {
   foreach ($properties as $property => $elements) {
     foreach ($elements as $rank => $value) {
     foreach ($elements as $rank => $value) {
       $status = tripal_analysis_insert_property($analysis_id, $property, $value, FALSE, 'analysis_property');
       $status = tripal_analysis_insert_property($analysis_id, $property, $value, FALSE, 'analysis_property');
       if (!$status) {
       if (!$status) {
         drupal_set_message("Error cannot add property: '$property'", "error");
         drupal_set_message("Error cannot add property: '$property'", "error");
         watchdog('t_analysis', "Error cannot add property: '%prop'",
         watchdog('t_analysis', "Error cannot add property: '%prop'",
-          array('%prop' => $property), WATCHDOG_ERROR);
+        array('%prop' => $property), WATCHDOG_ERROR);
       }
       }
     }
     }
   }
   }

+ 9 - 7
tripal_contact/theme/tripal_contact/tripal_contact_properties.tpl.php

@@ -5,14 +5,16 @@ $contact = $variables['node']->contact;
 $contact = tripal_core_expand_chado_vars($contact,'table', 'contactprop', array('return_array' => 1));
 $contact = tripal_core_expand_chado_vars($contact,'table', 'contactprop', array('return_array' => 1));
 $contactprops = $contact->contactprop;
 $contactprops = $contact->contactprop;
 
 
-// put the properties in an array for easier access
+// put the properties in an array so we can remove the contact_description property
 $properties = array();
 $properties = array();
-foreach ($contactprops as $property) {
-  // we want to keep all properties but the contact_description as that
-  // property is shown on the base template page.
-  if($property->type_id->name != 'contact_description') {
-    $property = tripal_core_expand_chado_vars($property,'field','contactprop.value');
-    $properties[] = $property;
+if ($contactprops) {
+  foreach ($contactprops as $property) {
+    // we want to keep all properties but the contact_description as that
+    // property is shown on the base template page.
+    if($property->type_id->name != 'contact_description') {
+      $property = tripal_core_expand_chado_vars($property,'field','contactprop.value');
+      $properties[] = $property;
+    }
   }
   }
 }
 }
 
 

+ 7 - 4
tripal_core/api/tripal_core_chado.api.inc

@@ -1482,7 +1482,7 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
 
 
   // Expandable fields without value needed for criteria--------------------------------------------
   // Expandable fields without value needed for criteria--------------------------------------------
   $all->expandable_fields = array();
   $all->expandable_fields = array();
-  if (array_key_exists('referring_tables', $table_desc)) {
+  if (array_key_exists('referring_tables', $table_desc) and $table_desc['referring_tables']) {
     $all->expandable_tables = $table_desc['referring_tables'];
     $all->expandable_tables = $table_desc['referring_tables'];
   }
   }
   else {
   else {
@@ -1620,21 +1620,24 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
             // add the foreign record to the current object in a nested manner
             // add the foreign record to the current object in a nested manner
             $object->{$foreign_key} = $foreign_object;
             $object->{$foreign_key} = $foreign_object;
             // Flatten expandable_x arrays so only in the bottom object
             // Flatten expandable_x arrays so only in the bottom object
-            if (property_exists($object->{$foreign_key}, 'expandable_fields') and is_array($object->{$foreign_key}->expandable_fields)) {
+            if (property_exists($object->{$foreign_key}, 'expandable_fields') and 
+                is_array($object->{$foreign_key}->expandable_fields)) {
               $object->expandable_fields = array_merge(
               $object->expandable_fields = array_merge(
                 $object->expandable_fields,
                 $object->expandable_fields,
                 $object->{$foreign_key}->expandable_fields
                 $object->{$foreign_key}->expandable_fields
               );
               );
               unset($object->{$foreign_key}->expandable_fields);
               unset($object->{$foreign_key}->expandable_fields);
             }
             }
-            if (property_exists($object->{$foreign_key}, 'expandable_tables') and is_array($object->{$foreign_key}->expandable_tables)) {
+            if (property_exists($object->{$foreign_key}, 'expandable_tables') and 
+                is_array($object->{$foreign_key}->expandable_tables)) {
               $object->expandable_tables = array_merge(
               $object->expandable_tables = array_merge(
                 $object->expandable_tables,
                 $object->expandable_tables,
                 $object->{$foreign_key}->expandable_tables
                 $object->{$foreign_key}->expandable_tables
               );
               );
               unset($object->{$foreign_key}->expandable_tables);
               unset($object->{$foreign_key}->expandable_tables);
             }
             }
-            if (property_exists($object->{$foreign_key}, 'expandable_nodes') and is_array($object->{$foreign_key}->expandable_nodes)) {
+            if (property_exists($object->{$foreign_key}, 'expandable_nodes') and 
+                is_array($object->{$foreign_key}->expandable_nodes)) {
               $object->expandable_nodes = array_merge(
               $object->expandable_nodes = array_merge(
                 $object->expandable_nodes,
                 $object->expandable_nodes,
                 $object->{$foreign_key}->expandable_nodes
                 $object->{$foreign_key}->expandable_nodes

+ 2 - 2
tripal_core/api/tripal_core_properties.api.inc

@@ -116,8 +116,8 @@ $cv_name, $value, $update_if_present = 0) {
   $options = array();
   $options = array();
   $term = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);
   $term = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);
   if (!$term or count($term) == 0) {
   if (!$term or count($term) == 0) {
-    watchdog('tripal_core', "Cannot find property '%prop_name'.",
-    array('%prop_name' => $property), WATCHDOG_ERROR);
+    watchdog('tripal_core', "Cannot find property '%prop_name' in vocabulary '%cvname'.",
+    array('%prop_name' => $property, '%cvname' => $cv_name), WATCHDOG_ERROR);
     return FALSE;
     return FALSE;
   }
   }