Browse Source

admin functions of Tripal cv module are working with drupal 7.

spficklin 11 years ago
parent
commit
b3b5061ff7

+ 5 - 282
tripal_core/api/tripal_core_chado.api.inc

@@ -1329,6 +1329,7 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
   }
   $all->expandable_nodes = array();
 
+  /*
   // Get fields to be removed by name.................................
   $fields_to_remove = module_invoke_all('exclude_field_from_' . $table . '_by_default');
   foreach ($fields_to_remove as $field_name => $criteria) {
@@ -1342,11 +1343,6 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
     //if criteria then remove from query
     // @coder-ignore: only module designers can populate $criteria -not security risk
     $success = php_eval('<?php return ' . $criteria . '; ?>');
-    //    watchdog('tripal_core',
-    //      'Evaluating criteria (%criteria) for field %field in tripal_core_generate_chado_var for %table evaluated to %success',
-    //      array('%table' => $table, '%criteria'=>$criteria, '%field' => $field_name, '%success'=>$success),
-    //      WATCHDOG_NOTICE
-    //    );
     if ($success) {
       unset($table_columns[array_search($field_name, $table_columns)]);
       unset($fields_to_remove[$field_name]);
@@ -1378,11 +1374,6 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
         //if criteria then remove from query
         // @coder-ignore: only module designers can populate $criteria -not security risk
         $success = php_eval('<?php return ' . $criteria . '; ?>');
-        //        watchdog('tripal_core',
-        //          'Evaluating criteria (%criteria) for field %field of $type in tripal_core_generate_chado_var for %table evaluated to %success',
-        //          array('%table'=>$table, '%criteria'=>$criteria, '%field'=>$field_name, '%type'=>$field_type, '%success'=>$success),
-        //          WATCHDOG_NOTICE
-        //        );
         if ($success) {
           unset($table_columns[array_search($field_name, $table_columns)]);
           $all->expandable_fields[] = $table . '.' . $field_name;
@@ -1390,7 +1381,7 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
       } //end of foreach field of that type
     }
   } //end of foreach type to be removed
-
+*/
   // get the values for the record in the current table---------------------------------------------
   $results = tripal_core_chado_select($table, $table_columns, $values, $base_options);
   if ($results) {
@@ -1419,26 +1410,19 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
         }
       }
 
-      // remove any fields where criteria need to be evalulated---------------------------------------
-      foreach ($fields_to_remove as $field_name => $criteria) {
+      // remove any fields where criteria needs to be evalulated---------------------------------------
+/*      foreach ($fields_to_remove as $field_name => $criteria) {
         if (!isset($object->{$field_name})) {
           break;
         }
         $criteria = preg_replace('/&gt;field_value&lt; /', addslashes($object->{$field_name}), $criteria);
-        //if criteria then remove from query
-        // @coder-ignore: only module designers can populate $criteria -not security risk
         $success = php_eval('<?php return ' . $criteria . '; ?>');
-        //      watchdog('tripal_core',
-        //        'Evaluating criteria (%criteria) for field %field in tripal_core_generate_chado_var for   %table evaluated to %success',
-        //        array('%table' => $table, '%criteria'=>$criteria, '%field' => $field_name, '%success'=>$success),
-        //        WATCHDOG_NOTICE
-        //      );
         if ($success) {
           unset($object->{$field_name});
           $object->expandable_fields[] = $table . '.' . $field_name;
         }
       }
-
+*/
       // recursively follow foreign key relationships nesting objects as we go------------------------
       if ($table_desc['foreign keys']) {
         foreach ($table_desc['foreign keys'] as $foreign_key_array) {
@@ -2339,267 +2323,6 @@ function tripal_core_delete_property_by_id($basetable, $record_id) {
   return tripal_core_chado_delete($basetable . 'prop', $match);
 }
 
-/**
- * Indicates if the SQL statement is prepapred
- *
- * @param $statement_name
- *   The name of the statement to check if it is prepared.
- *
- * @return
- *   TRUE if the statement is preapred, FALSE otherwise
- */
-function tripal_core_is_sql_prepared($statement_name) {
-/*	
-  global $_tripal_core_prepared_statements;
-
-  if (!is_array($_tripal_core_prepared_statements)) {
-    watchdog('tripal_core', "tripal_core_is_sql_prepared: argument must be an array", array(), WATCHDOG_ERROR);
-      return FALSE;
-  }
-
-  // check to see if the statement is prepared already
-  if (in_array($statement_name, $_tripal_core_prepared_statements)) {
-    return TRUE;
-  }
-
-  // @coder-ignore: acting on postgres tables rather then drupal schema therefore, table prefixing does not apply
-  $sql = "SELECT name FROM pg_prepared_statements WHERE name = :name";
-  $results = db_query($sql, array(':name' => $statement_name));
-  $result = $results->fetchObject();
-
-  if ($result) {
-    return TRUE;
-  }
-  return FALSE;
-*/  
-}
-
-/**
- * Prepare a chado query
- *
- * @param $statement_name
- *   The name of the prepared statement
- * @param $psql
- *   The SQL statement to be executed via chado_query.
- *   Should be of the form PREPARE [statement name] AS [SQL Statement to be prepared]
- * @param $args
- *   An array of arguements required to execute the prepared statement. The keys of
- *   the array should correspond to the variables in the prepared statement and the value should
- *   be the type of value needed (ie: text, int, etc.)
- */
-function tripal_core_chado_prepare($statement_name, $psql, $args) {
-/*	
-  global $_tripal_core_persistent_chado;
-  global $_tripal_core_prepared_statements;
-
-  if (!$_tripal_core_persistent_chado) {
-    watchdog('tripal_core', "chado_prepare: not able to prepare '%name' statement as no persistent connection is available", array('%name' => $statement_name, '%sql' => $psql), WATCHDOG_ERROR);
-    return FALSE;
-  }
-
-  // Check to see if this statement was already prepared
-  if (tripal_core_is_sql_prepared($statement_name)) {
-    // check that the arguments are the same
-    $prepared_args = $_tripal_core_prepared_statements[$statement_name]['prepared_args'];
-    $prepared_sql = $_tripal_core_prepared_statements[$statement_name]['prepared_sql'];
-    if ($prepared_args == $args) {
-      // This statement is already prepared
-      return TRUE;
-    }
-    else {
-      // Although a statement with this name is already prepared it is not the same!
-      watchdog('tripal_core', "chado_prepare: '%name' statement already prepared with different arguments! " .
-        "You want to prepare \n%sql\n with \n%values\n and the existing statement is \n%esql\n with \n%existing",
-        array('%name' => $statement_name, '%sql' => $psql, '%values' => print_r($args, TRUE), '%esql' => $prepared_sql,
-          '%existing' => print_r($prepared_args, TRUE)), WATCHDOG_ERROR);
-      return FALSE;
-    }
-  }
-
-  $status = chado_query($psql);
-  if (!$status) {
-    watchdog('tripal_core', "chado_prepare: not able to prepare '%name' statement for: %sql", array('%name' => $statement_name, '%sql' => $psql), WATCHDOG_ERROR);
-    return FALSE;
-  }
-  else {
-    $_tripal_core_prepared_statements[$statement_name] = array();
-    $_tripal_core_prepared_statements[$statement_name]['prepared_args'] = $args;
-    $_tripal_core_prepared_statements[$statement_name]['prepared_sql'] = $psql;
-    return TRUE;
-  }
-*/  
-}
-
-/**
- * Execute a prepared statement with the supplied values
- *
- * @param $statement_name
- *   The name of the prepared statement
- * @param $sql
- *   The SQL to execute using chado query.
- *   Should be of the form EXECUTE [statement_name] ([arg1],[arg2]...[argn])
- * @param $values
- *   An array of values in the execute sql statement
- */
-function tripal_core_chado_execute_prepared($statement_name, $sql, $values) {
-/*	
-  global $_tripal_core_prepared_statements;
-
-  if (!tripal_core_is_sql_prepared($statement_name)) {
-    watchdog('tripal_core', "tripal_core_chado_execute_prepared: Cannot execute an unprepared statement: '%name'", array('%name' => $statement_name), WATCHDOG_ERROR);
-    return FALSE;
-  }
-
-  // Before Executing, Ensure that all the values are supplied
-  $required_values = $_tripal_core_prepared_statements[$statement_name]['prepared_args'];
-  if (!$required_values) {
-    watchdog('tripal_core', "tripal_core_chado_execute_prepared: missing prepare arguments for this statement: '%name'", array('%name' => $statement_name), WATCHDOG_ERROR);
-    return FALSE;
-  }
-
-  if (sizeof($required_values) == sizeof($values)) {
-
-    $error = FALSE;
-    foreach ($values as $k => $v) {
-      if (isset($required_values[$k])) {
-        switch ($required_values[$k]) {
-          case 'text':
-            // anything can be converted to a string, so if the type is 'text' let's just convert it
-            $values[$k] = (string) $v;
-
-            break;
-          case 'int':
-            $check = is_numeric($v);
-            if (!$check) {
-              watchdog('tripal_core', "chado_execute_prepared: wrong argument type supplied for '%name' statement, field %k. Expected %required but recieved '%value'",
-                array('%name' => $statement_name, '%k' => $k+1, '%required' => $required_values[$k], '%value' => print_r($v, TRUE)), WATCHDOG_ERROR);
-              return FALSE;
-            }
-            break;
-          case 'bool':
-            if ($v != 'TRUE' and $v != 'FALSE') {
-              watchdog('tripal_core', "chado_execute_prepared: wrong argument type supplied for '%name' statement, field %k. Expected %required but recieved '%value'",
-                array('%name' => $statement_name, '%k' => $k+1, '%required' => $required_values[$k], '%value' => print_r($v, TRUE)), WATCHDOG_ERROR);
-              return FALSE;
-            }
-            break;
-          case 'numeric':
-            $check = is_numeric($v);
-            if (!$check) {
-              watchdog('tripal_core', "chado_execute_prepared: wrong argument type supplied for '%name' statement, field %k. Expected %required but recieved '%value'",
-                array('%name' => $statement_name, '%k' => $k+1, '%required' => $required_values[$k], '%value' => print_r($v, TRUE)), WATCHDOG_ERROR);
-              return FALSE;
-            }
-            break;
-          default:
-            watchdog('tripal_core', "chado_execute_prepared: unsupported argument type (supplied for '%name' statement %type)",
-
-            array('%name' => $statement_name, '%type' => $required_values[$k]), WATCHDOG_WARNING);
-            break;
-        }
-      }
-      else {
-        watchdog('tripal_core', "chado_execute_prepared: wrong number of arguments supplied for '%name' statement. Expected %required but recieved %values",
-          array('%name' => $statement_name, '%required' => print_r($required_values, TRUE), '%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
-        return FALSE;
-      }
-    }
-
-    // Since all values are supplied, execute
-    $resource = chado_query($sql, $values);
-    return $resource;
-  }
-  else {
-    watchdog('tripal_core', "chado_execute_prepared: wrong number of arguments supplied for '%name' statement. ' .
-      'Expected %required but recieved %values. Statement: %statement.",
-      array('%name' => $statement_name, '%required' => print_r($required_values, TRUE),
-        '%values' => print_r($values, TRUE), '%statement' => $_tripal_core_prepared_statements[$statement_name]['prepared_sql']), WATCHDOG_ERROR);
-    return FALSE;
-  }
-  */
-}
-
-
-/**
- * Clears prepared statements to avoid conflicts
- *
- * If no statement_name_regex is supplied then it clears ALL prepared statements;
- * Otherwise, it clears prepared statement names that match the regex provided
- */
-function tripal_core_chado_clear_prepared($statement_name_regex = NULL) {
-/*	
-  global $_tripal_core_prepared_statements;
-
-  if ($statement_name_regex) {
-  	$sql = "SELECT * FROM pg_catalog.pg_prepared_statements WHERE name~:name_regex";
-    $resource = chado_query($sql, array(':name_regex' => $statement_name_regex));
-    foreach ($resource as $r) {
-      $k = array_search($r->name, $_tripal_core_prepared_statements);
-      unset($_tripal_core_prepared_statements[$k]);
-      chado_query('DEALLOCATE PREPARE :statement_name', array(':statement_name' => $r->name));
-    }
-  }
-  else {
-    $_tripal_core_prepared_statements = array();
-    chado_query('DEALLOCATE PREPARE ALL');
-  }
-  */
-}
-
-/**
- * Instantiate or Return a persistent chado connection.  This should not be confused with
- * PHP persistent connections. Here we use the drupal db_connect function to
- *
- * NOTE: cannot use $active_db since a new connection is created each time
- * db_set_active() is called
- *
- * @return
- *   A postgresql connection object which can be used by pg_prepare, pg_execute, etc.
- */
-function tripal_db_persistent_chado() {
-/*	
-  global $db_url;
-  global $_tripal_core_persistent_chado;
-
-  // get connection if it already exists otherwise we need to set it
-  if ($_tripal_core_persistent_chado) {
-    return $_tripal_core_persistent_chado;
-  }
-  else {
-    if (is_array($db_url) && isset($db_url['chado'])) {
-      $connection = db_connect($db_url['chado']);
-      if (!$connection) {
-        watchdog('tripal_core', "Could not create persistant connection", array(), WATCHDOG_ERROR);
-        return FALSE;
-      }
-      $_tripal_core_persistent_chado = $connection;
-    }
-    else {
-      if (is_array($db_url)) {
-        $connection = db_connect($db_url['default']);
-      }
-      else {
-        $connection = db_connect($db_url);
-      }
-      if (!$connection) {
-        $_tripal_core_persistent_chado = NULL;
-        watchdog('tripal_core', "Could not create persistant connection", array(), WATCHDOG_ERROR);
-        return FALSE;
-      }
-      $_tripal_core_persistent_chado = $connection;
-    }
-    return $connection;
-  }
-  return FALSE;
-  */
-}
-
-/**
- * Release a persistent chado connection
- */
-function tripal_db_release_persistent_chado() {
-//  $_tripal_core_persistent_chado = NULL;
-}
 
 /**
  * Start a transaction block. Ensures the use of a persistent chado connection

+ 3 - 5
tripal_cv/api/tripal_cv.api.inc

@@ -152,8 +152,7 @@ function tripal_cv_get_cv_by_id($cv_id) {
 function tripal_cv_get_cv_id($cv_name) {
 
   $sql = "SELECT cv_id FROM {cv} WHERE name = :name";
-  $result = chado_query($sql, array(':name' => $cv_name));
-  $cv = $result->fetchObject();
+  $cv = chado_query($sql, array(':name' => $cv_name))->fetchObject();
 
   return $cv->cv_id;
 }
@@ -368,14 +367,13 @@ function tripal_cv_update_cvtermpath($cvid, $job_id = NULL) {
 
   // first get the controlled vocabulary name:
   $sql = "SELECT * FROM {cv} WHERE cv_id = :cv_id";
-  $result = chado_query($sql, array(':cv_id' => $cvid));
-  $cv = $result->fetchObject();
+  $cv = chado_query($sql, array(':cv_id' => $cvid))->fetchObject();
   
   print "\nUpdating cvtermpath for $cv->name...\n";
   
   $previous = tripal_db_set_active('chado');
   try {
-    $sql = "SELECT * FROM fill_cvtermpath(':name')";
+    $sql = "SELECT * FROM fill_cvtermpath(:name)";
     db_query($sql, array(':name' => $cv->name));
     tripal_db_set_active($previous); 
   }

+ 2 - 2
tripal_cv/includes/charts.inc

@@ -74,8 +74,8 @@
  *    $analysis_id = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$chart_id);
  *    $type        = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$3",$chart_id);
  *
- *    $sql = "SELECT * FROM {analysis} WHERE analysis_id = %d";
- *    $analysis = db_fetch_object(chado_query($sql,$analysis_id));
+ *    $sql = "SELECT * FROM {analysis} WHERE analysis_id = :analysis_id";
+ *    $analysis = chado_query($sql, array(':analysis_id' => $analysis_id))->fetchObject();
  *
  *    if (strcmp($type,'mf')==0) {
  *       $class = 'molecular_function';

+ 259 - 0
tripal_cv/includes/cv_form.inc

@@ -0,0 +1,259 @@
+<?php 
+/**
+ * Purpose: Provides the actual "Select CV" form on the Update/Delete Controlled
+ *   Vocabulary page. This form also triggers the edit javascript
+ * @todo Modify this form to use Drupal AJAX
+ *
+ * @ingroup tripal_cv
+ */
+function tripal_cv_cv_edit_form($form, &$form_state) {
+
+  // get the cv_d if form was submitted via AJAX
+  $cv_id = 0;
+  if (array_key_exists('values', $form_state)) {
+    $cv_id = $form_state['values']['cv_id'];
+  }    
+  
+  // get a list of db from chado for user to choose
+  $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
+  $results = chado_query($sql);
+
+  $cvs = array();
+  $cvs[] = 'Select a vocabulary';
+  foreach ($results as $cv) {
+    $cvs[$cv->cv_id] = $cv->name;
+  }
+
+  $form['cv_id'] = array(
+    '#title' => t('Controlled Vocabulary Name'),
+    '#type' => 'select',
+    '#options' => $cvs,
+    '#ajax' => array(
+      'callback' => 'tripal_cv_edit_form_ajax',
+      'wrapper'  => 'cv-edit-div',
+      'effect'   => 'fade',
+      'event'    => 'change',
+      'method'   => 'replace',
+    ),
+    '#default_value' => $cv_id, 
+  );
+
+        
+  // if we don't have a db_id then we can  return the form, otherwise
+  // add in the other fields
+  if ($cv_id) {
+    tripal_cv_add_cv_form_fields($form, $form_state, $cv_id);
+        
+    $form['update'] = array(
+      '#type'         => 'submit',
+      '#value'        => t('Update'),
+    );
+    $form['delete'] = array(
+      '#type'         => 'submit',
+      '#value'        => t('Delete'),
+      '#attributes'   => array('onclick' => 'if(!confirm("Really Delete?")){return false;}'), 
+    );
+  }
+  else {
+    // if we don't have a dbid then this is the first time the form has
+    // benn loaded and we need to create the div where ajax replacement elements get stored
+    $form['div_replace'] = array(
+      '#type' => 'item',
+      '#prefix' => '<div id="cv-edit-div">',
+      '#suffix' => '</div>',
+    );
+  } 
+  return $form;
+}
+/**
+ * 
+ * @param $form
+ * @param $form_state
+ * 
+ * @ingroup tripal_db
+ */
+function tripal_cv_cv_add_form($form, $form_state) {
+  
+  // add in the form fields to this form
+  tripal_cv_add_cv_form_fields($form, $form_state);
+  
+  $form['add'] = array(
+    '#type'         => 'submit',
+    '#value'        => t('Add'),
+    '#weight'       => 5,
+  );
+  return $form;
+}
+/**
+ * 
+ * @param $form
+ * @param $form_state
+ * @param $cv_id
+ * 
+ * @ingroup tripal_db
+ */
+function tripal_cv_add_cv_form_fields(&$form, $form_state, $cv_id = NULL) {
+
+  $default_name = '';
+  $default_desc = '';
+
+  if ($cv_id) {
+    $values = array('cv_id' => $cv_id);
+    $result = tripal_core_chado_select('cv', array('*'), $values);
+    $cv = $result[0]; 
+    $default_name = $cv->name;
+    $default_desc = $cv->definition; 
+  }   
+  
+  // add a fieldset for the Drupal Schema API
+  $form['fields'] = array(
+    '#type' => 'fieldset',
+    '#title' => 'Controlled Vocabulary Details',
+    '#collapsible' => 0,
+  );      
+
+  $form['fields']['name']= array(
+    '#type'          => 'textfield',
+    '#title'         => t("Controlled Vocabulary name"),
+    '#description'   => t('Please enter the name for this vocabulary.'),
+    '#required'      => TRUE,
+    '#default_value' => $default_name,
+    '#maxlength'     => 255,  
+  );
+
+  $form['fields']['definition']= array(
+    '#type'          => 'textarea',
+    '#title'         => t('Description'),
+    '#description'   => t('Please enter a definition for this vocabulary'),
+    '#default_value' => $default_desc,
+  );
+
+  return $form;
+}
+
+/**
+ * Validation fucntion for tripal_cv_cv_add_form
+ * @param $form
+ * @param $form_state
+ * 
+ * @ingroup tripal_cv
+ */
+function tripal_cv_cv_add_form_validate($form, &$form_state) {
+  tripal_cv_form_fields_validate($form, $form_state); 
+}
+/**
+ * Validation fucntion for tripal_cv_cv_edit_form
+ * @param unknown_type $form
+ * @param unknown_type $form_state
+ * 
+ * @ingroup tripal_cv
+ */
+function tripal_cv_cv_edit_form_validate($form, &$form_state) {
+  tripal_cv_form_fields_validate($form, $form_state); 
+}
+/**
+ * Genetic validation form for shared fields of both the edit and add forms
+ * @param $form
+ * @param $form_state
+ * 
+ * @ingroup tripal_cv
+ */
+function tripal_cv_form_fields_validate($form, &$form_state) {
+  $name =  array_key_exists('name', $form_state['values'])       ? trim($form_state['values']['name']) : '';
+  $desc =  array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
+  $cv_id = array_key_exists('cv_id', $form_state['values'])      ? trim($form_state['values']['cv_id']) : '';
+  
+   
+  // make sure the cv name is unique
+  $values = array('name' => $name);
+  $results = tripal_core_chado_select('cv', array('cv_id'), $values);   
+  if (count($results) > 0 and $results[0]->cv_id != $cv_id) {
+    form_set_error('name', 'The vocabulary name must be unique');
+  }
+}
+/**
+ * 
+ * @param $form
+ * @param $form_state
+ */
+function tripal_cv_cv_add_form_submit($form, &$form_state) {
+
+  $name =  array_key_exists('name', $form_state['values'])       ? trim($form_state['values']['name']) : '';
+  $desc =  array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
+
+  $values = array(
+    'name' => $name,
+    'definition' => $desc,
+  );
+  $success = tripal_core_chado_insert('cv', $values);
+  if ($success) {
+    drupal_set_message(t("Controlled vocabulary added"));
+  }
+  else {
+    drupal_set_message(t("Failed to add controlled vocabulary."));
+  }     
+}
+/**
+ * 
+ * @param unknown_type $form
+ * @param unknown_type $form_state
+ */
+function tripal_cv_cv_edit_form_submit($form, &$form_state) {
+  $name  = array_key_exists('name', $form_state['values'])       ? trim($form_state['values']['name']) : '';
+  $desc  = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
+  $cv_id = array_key_exists('cv_id', $form_state['values'])      ? trim($form_state['values']['cv_id']) : '';
+  $op    = array_key_exists('op', $form_state['values'])         ? trim($form_state['values']['op']) : '';
+  
+  $values = array(
+    'name' => $name,
+    'definition' => $desc,
+  );
+  if (strcmp($op, 'Update')==0) {      
+    $match = array('cv_id' => $cv_id);
+    $success = tripal_core_chado_update('cv', $match, $values);
+    if ($success) {
+      drupal_set_message(t("Controlled vocabulary updated"));
+    }
+    else {
+      drupal_set_message(t("Failed to update controlled vocabulary."));
+    }
+  }
+  if (strcmp($op, 'Delete')==0) {
+    $match = array('cv_id' => $cv_id);
+    $success = tripal_core_chado_delete('cv', $match);
+    if ($success) {
+      drupal_set_message(t("Controlled vocabulary deleted"));
+    }
+    else {
+      drupal_set_message(t("Failed to delete controlled vocabulary."));
+    }
+  }
+}
+
+/**
+ * Ajax callback for the tripal_cv_form
+ * @ingroup tripal_cv
+ */
+function tripal_cv_edit_form_ajax($form, $form_state) {
+  
+  $elements = array();
+  
+  // add in the form fields and the buttons 
+  if (array_key_exists('cv_id', $form_state['values'])) {
+    $elements['fields'] = $form['fields'];
+    $elements['update'] = $form['update'];
+    $elements['delete'] = $form['delete'];
+  }
+  
+   // add back in the cv-edit-div that is used for the next round of AJAX
+  $elements['fields']['#prefix'] =  '<div id="cv-edit-div">';
+  $elements['fields']['#suffix'] =  '</div">';
+  
+  // reset the values for the fields to the defaults
+  $elements['fields']['name']['#value']        = $elements['fields']['name']['#default_value'];
+  $elements['fields']['definition']['#value']  = $elements['fields']['definition']['#default_value'];
+  
+  //drupal_set_message('<pre>' . print_r($elements, TRUE) . '</pre>', "status");
+  
+  return $elements;
+}

+ 475 - 0
tripal_cv/includes/cvterm_form.inc

@@ -0,0 +1,475 @@
+<?php 
+
+/**
+ * 
+ * @param $form
+ * @param $form_state
+ */
+function tripal_cv_cvterm_edit_form($form, &$form_state) {
+
+	$step = 0;
+  if (empty($form_state['storage']['step'])) {
+    $form_state['storage']['step'] = 0;
+  }
+  else {
+  	$step = $form_state['storage']['step'];
+  }
+
+  $cv_id = 0;
+  if ($step == 1) {
+  	$cv_id = $form_state['storage']['cv_id'];
+  	$cvterm_name = $form_state['storage']['name'];
+  	$cvterm_id = $form_state['storage']['cvterm_id'];
+  }	
+  // get the cv if form was submitted via AJAX
+  $cvterm = '';
+  if (array_key_exists('values', $form_state)) {
+    $cv_id = $form_state['values']['cv_id'];   
+    if (array_key_exists('cvterm', $form_state['values'])) {
+    	$cvterm = $form_state['values']['cvterm'];
+    }    
+  }   
+    
+  // get a list of CVs
+  $cvs = array();
+  $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
+  $results = chado_query($sql);
+  $cvs[] = 'Select a vocabulary';
+  foreach ($results as $cv) {
+    $cvs[$cv->cv_id] = $cv->name;
+  }    
+  $form['cv_id'] = array(
+    '#title' => t('Controlled Vocabulary (Ontology) Name'),
+    '#type' => 'select',
+    '#options' => $cvs,
+    '#required' => TRUE,
+    '#default_value' => $cv_id,
+    '#ajax' => array(
+       'callback'    => 'tripal_cv_cvterm_edit_form_ajax',
+       'wrapper' => 'cvterm-edit-div',
+       'event'   => 'change',
+       'method'  => 'replace',
+       'event'    => 'change',
+    ),    
+  );
+    
+  if ($cv_id and $step == 0) {
+  	
+  	$form['name']= array(
+      '#type'          => 'textfield',
+      '#title'         => t("Term Name"),
+      '#default_value' => $cvterm,
+      '#required'      => TRUE,
+      '#autocomplete_path' => "admin/tripal/tripal_cv/cvterm/auto_name/$cv_id",
+  	  '#description'   => t('Enter the term to edit.')
+    );
+    $form['continue']= array(
+      '#type'          => 'submit',
+      '#value'         => 'continue',
+    );
+  }
+  elseif ($step == 1) {  	
+  	   
+    tripal_cv_add_cvterm_form_fields($form, $form_state, $cv_id, $cvterm_name);
+    
+    // when editing there are certain fields the user should not change for a term
+    // let's mark those as disabled
+    $form['cv_id']['#disabled'] = TRUE;
+    $form['fields']['db_id']['#disabled'] = TRUE;
+    $form['fields']['accession']['#disabled'] = TRUE;
+    
+    // add in the div for replacing the fields if needed
+    $form['fields']['#prefix'] = '<div id="cvterm-edit-div">';
+    $form['fields']['#suffix'] = '</div>';
+
+    // add in the cvterm id
+    $form['fields']['cvterm_id'] = array(
+      '#type' => 'hidden',
+      '#value' =>  $cvterm_id,
+    );
+    
+    $form['update'] = array(
+      '#type'         => 'submit',
+      '#value'        => t('Update'),
+    );
+    $form['delete'] = array(
+      '#type'         => 'submit',
+      '#value'        => t('Delete'),
+      '#attributes'   => array('onclick' => 'if(!confirm("Really Delete?")){return false;}'), 
+    );    
+  } 
+  
+  if ($step == 0) {
+  	// if we don't have a cv_id then this is the first time the form has
+    // benn loaded and we need to create the div where ajax replacement elements get stored
+    $form['div_replace'] = array(
+      '#type' => 'item',
+      '#prefix' => '<div id="cvterm-edit-div">',
+      '#suffix' => '</div>',
+    );
+  }
+  return $form;
+}
+/**
+ * 
+ * @param $form
+ * @param $form_state
+ */
+function tripal_cv_cvterm_add_form($form, &$form_state) {
+	$cv_id = 0;
+	if (array_key_exists('values', $form_state)) {
+	  $cv_id = $form_state['values']['cv_id'];	 
+	}
+	
+	// get a list of CVs
+  $cvs = array();
+  $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
+  $results = chado_query($sql);
+  $cvs[] = 'Select a vocabulary';
+  foreach ($results as $cv) {
+    $cvs[$cv->cv_id] = $cv->name;
+  }    
+  $form['cv_id'] = array(
+    '#title' => t('Controlled Vocabulary (Ontology) Name'),
+    '#type' => 'select',
+    '#options' => $cvs,
+    '#required' => TRUE,
+    '#default_value' => $cv_id,
+  );
+  tripal_cv_add_cvterm_form_fields($form, $form_state);
+  
+  $form['add'] = array(
+    '#type'         => 'submit',
+    '#value'        => t('Add Term'),
+  );
+  
+  return $form;
+}
+
+/**
+ * 
+ * @param $form
+ * @param $form_state
+ * @param $cv_id
+ * 
+ * @ingroup tripal_db
+ */
+function tripal_cv_add_cvterm_form_fields(&$form, $form_state, $cv_id = 0, $cvterm_name = '') {
+
+  $name = '';
+  $definition = '';
+  $is_relationship = '';
+  $is_obsolete = '';
+  $db_id = '';
+  $accession = '';
+
+  // get default values
+  if ($cvterm_name) {
+    $values = array('cv_id' => $cv_id, 'name' => $cvterm_name);
+    $cvterm = tripal_core_generate_chado_var('cvterm', $values);
+    $name = $cvterm->name;
+    $definition = $cvterm->definition; 
+    $is_relationship = $cvterm->is_relationshiptype;
+    $is_obsolete = $cvterm->is_obsolete;
+    $db_id = $cvterm->dbxref_id->db_id->db_id;
+    $accession = $cvterm->dbxref_id->accession;
+  }    
+  
+  // add a fieldset for the Drupal Schema API
+  $form['fields'] = array(
+    '#type' => 'fieldset',
+    '#title' => 'Term Details',
+    '#collapsible' => 0,
+  );      
+  
+  
+  $form['fields']['name']= array(
+    '#type'          => 'textfield',
+    '#title'         => t("Term Name"),
+    '#default_value' => $name,
+    '#required'      => TRUE,
+  );
+  
+  $form['fields']['definition']= array(
+    '#type'          => 'textarea',
+    '#title'         => t('Description'),
+    '#description'   => t('Please enter a description for this term'),
+    '#default_value' => $definition,
+  );
+  
+  $form['fields']['is_relationship'] = array(
+    '#type'          => 'checkbox',
+    '#title'         => t('This term describes a relationship?'),
+    '#default_value' => $is_relationship,
+  );
+  
+  $form['fields']['is_obsolete'] = array(
+    '#type'          => 'checkbox',
+    '#title'         => t('This term is obsolete?'),
+    '#default_value' => $is_obsolete,
+  );
+  
+  $values = array();
+  $columns = array('db_id', 'name');
+  $options = array('order_by' => array('name' => 'ASC'));
+  $results = tripal_core_chado_select('db', $columns, $values, $options);
+  $dbs = array();
+  $dbs[] = '';
+  foreach ($results as $db) {
+    $dbs[$db->db_id] = $db->name;
+  }
+  $form['fields']['db_id'] = array(
+    '#type'         => 'select',
+    '#title'         => t('Database'),
+    '#description'   => t('All terms must be assocated with an external database.
+                          Please select the external database to associate with
+                          this term'),
+    '#options'      => $dbs,
+    '#default_value' => $db_id,
+    '#required' => TRUE,
+  );
+  
+  $form['fields']['accession']= array(
+    '#type'          => 'textfield',
+    '#title'         => t("Accession"),
+    '#description'   => t('If this term has an existing accession (unique identifier) in the database 
+       please enter that here.  If the accession is numeric with a database prefix (e.g. GO:003023), please
+       enter just the numeric value.  The database prefix will be appended whenever the term is displayed. 
+       If the accession is not numeric then enter it as is.  If no value is provied, the term name 
+       provided above will be used as the accession.'),
+    '#required'      => TRUE,
+    '#default_value' => $accession,
+  );     
+}
+
+/**
+ * 
+ * @param $form
+ * @param $form_state
+ */
+function tripal_cv_cvterm_edit_form_validate($form, &$form_state) {
+	$cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
+  $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
+  $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
+  $cvterm_id = array_key_exists('cvterm_id', $form_state['values']) ? $form_state['values']['cvterm_id'] : '';
+  $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
+  
+  $step = $form_state['storage']['step'];
+	 
+  // make sure the cv term name is unique for this vocabulary
+  if ($step == 1) {
+	  $values = array('name' => $name, 'cv_id' => $cv_id);
+	  $results = tripal_core_chado_select('cvterm', array('cvterm_id'), $values);
+	  foreach ($results as $r) {   
+	    if ($r->cvterm_id != $cvterm_id) {
+	      form_set_error('name', 'The term name must be unique for this vocabulary. Another term with this name already exists.');
+	    }
+	  }
+  }
+}
+/**
+ * 
+ * @param $form
+ * @param $form_state
+ */
+function tripal_cv_cvterm_add_form_validate($form, &$form_state) {
+	$cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
+  $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
+  $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
+  $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
+		
+  $values = array('cv_id' => $cv_id);
+  $results = tripal_core_chado_select('cv', array('name'), $values);
+  if (!$results or count($results) == 0){
+    form_set_error('cv_id', 'The controlled vocabulary does not exist');
+  }
+
+  // make sure the DB exists
+  $values = array('db_id' => $db_id);
+  $results = tripal_core_chado_select('db', array('name'), $values);
+  if (!$results or count($results) == 0){
+    form_set_error('db_id', 'The database name does not exist');
+  }
+	
+  // make sure the cv term name is unique for this vocabulary
+  $values = array('name' => $name, 'cv_id' => $cv_id);
+  $results = tripal_core_chado_select('cvterm', array('cvterm_id'), $values);   
+  if (count($results) > 0) {
+    form_set_error('name', 'The term name must be unique for this vocabulary. Another term with this name already exists.');
+  }
+  
+  // make sure this accession is unique for the database
+  $values = array('accession' => $accession, 'db_id' => $db_id);
+  $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $values);   
+  if (count($results) > 0 ) {
+    form_set_error('accession', 'The accession is not uniuqe for this vocabulary\'s database.');
+  }  
+
+}
+
+
+/**
+ * Purpose: Adds terms to an existing controlled vocabulary
+ *
+ * @ingroup tripal_cv
+ */
+function tripal_cv_cvterm_edit_form_submit($form, &$form_state) {
+	
+	$cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
+  $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
+  $definition = array_key_exists('definition', $form_state['values']) ? $form_state['values']['definition'] : '';
+  $is_relationship = array_key_exists('is_relationship', $form_state['values']) ? $form_state['values']['is_relationship'] : '';
+  $is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? $form_state['values']['is_obsolete'] : '';
+  $cvterm_id = array_key_exists('cvterm_id', $form_state['values']) ? $form_state['values']['cvterm_id'] : '';
+  
+  $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
+  $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
+  
+  $op    = array_key_exists('op', $form_state['values'])         ? trim($form_state['values']['op']) : '';
+  
+  
+	$step = $form_state['storage']['step'];
+
+	switch ($step) {
+		case 0:  // a cvterm name has been selected
+			$cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
+			$name  = array_key_exists('name', $form_state['values'])  ? trim($form_state['values']['name'])  : '';
+			
+			// get the original cvterm_id
+	    $values = array('name' => $name, 'cv_id' => $cv_id);
+      $results = tripal_core_chado_select('cvterm', array('cvterm_id'), $values);  
+      $cvterm = $results[0]; 
+    
+      $form_state['storage']['cv_id'] = $cv_id;
+      $form_state['storage']['name'] = $name;
+      $form_state['storage']['step'] = 1;
+      $form_state['storage']['cvterm_id'] = $cvterm->cvterm_id;
+      $form_state['rebuild'] = TRUE;
+			break;
+			
+		case 1:  // update/delete button has been clicked					
+		  
+			if ($op == 'Update') {
+			  // get the cv
+			  $values = array('cv_id' => $cv_id);
+			  $results = tripal_core_chado_select('cv', array('name'), $values);
+			  $cv = $results[0];
+			  
+			  // get the db
+			  $values = array('db_id' => $db_id);
+			  $results = tripal_core_chado_select('db', array('name'), $values);
+			  $db = $results[0];
+			  
+			  // now add the term
+			  $term = array(    
+			    'name' => $name,
+			    'namespace' => $cv->name,
+			    'id' => $accession,
+			    'def' => $definition,
+			    'is_obsolete' => $is_obsolete,
+			  );  
+			  
+			  $cvterm = tripal_cv_add_cvterm($term, $cv->name, $is_relationship, TRUE, $db->name);
+			  if ($cvterm) {
+			    drupal_set_message('Term updated successfully.');
+			  } 
+			  else {
+			    drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');  
+			  }
+			}
+			if ($op == 'Delete') {
+			  $values = array('cvterm_id' => $cvterm_id);
+			  $success = tripal_core_chado_delete('cvterm', $values);
+			  if ($success) {
+			    drupal_set_message('Term deleted successfully.');	
+			  } 
+			  else {
+			    drupal_set_message('Could not delete term term. Check Drupal recent logs for error messages.', 'error');	
+			  }			     	
+			}
+			break;
+	} 		 
+}
+
+/**
+ * 
+ * @param unknown_type $form
+ * @param unknown_type $form_state
+ */
+function tripal_cv_cvterm_add_form_submit($form, &$form_state) {
+	$cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
+	$name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
+	$definition = array_key_exists('definition', $form_state['values']) ? $form_state['values']['definition'] : '';
+	$is_relationship = array_key_exists('is_relationship', $form_state['values']) ? $form_state['values']['is_relationship'] : '';
+	$is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? $form_state['values']['is_obsolete'] : '';
+	
+	$db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
+	$accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
+	
+  // get the database
+  $values = array('db_id' => $db_id);
+  $results = tripal_core_chado_select('db', array('name'), $values);
+  $db = $results[0];
+      
+  // get the cv
+  $values = array('cv_id' => $cv_id);
+  $results = tripal_core_chado_select('cv', array('name'), $values);
+  $cv = $results[0];
+
+  // now add the term
+  $term = array(    
+    'name' => $name,
+    'namespace' => $cv->name,
+    'id' => $accession,
+    'def' => $definition,
+    'is_obsolete' => $is_obsolete,
+  );  
+      
+  $cvterm = tripal_cv_add_cvterm($term, $cv->name, $is_relationship, TRUE, $db->name);
+  if ($cvterm) {
+    drupal_set_message('Term added successfully.');
+  } 
+  else {
+    drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');  
+  }    
+}
+/**
+ * Ajax callback for the tripal_cv_form
+ * @ingroup tripal_cv
+ */
+function tripal_cv_cvterm_edit_form_ajax($form, $form_state) {
+
+  $elements = array();
+  
+  $elements['name'] = $form['name'];
+  $elements['continue'] = $form['continue'];   
+  
+  // add back in the cv-edit-div that is used for the next round of AJAX
+  $elements['name']['#prefix'] =  '<div id="cvterm-edit-div">';
+  $elements['name']['#suffix'] =  '</div">';  
+  
+    
+  return $elements;
+}
+/**
+ * 
+ * @param $cv_id
+ * @param $string
+ * 
+ * @ingroup tripal_cv
+ */
+function tripal_cv_cvterm_name_autocomplete($cv_id, $string = '') {
+  $sql = "
+    SELECT cvterm_id, name 
+    FROM {cvterm} 
+    WHERE cv_id = :cv_id and name like :name 
+    ORDER by name 
+    LIMIT 25 OFFSET 0
+  ";
+  $results = chado_query($sql, array(':cv_id' => $cv_id, ':name' => $string . '%'));
+  $items = array();
+  foreach ($results as $term) {
+     $items[$term->name] = $term->name;
+  }  
+  drupal_json_output($items);   
+}

+ 62 - 0
tripal_cv/includes/cvtermpath_form.inc

@@ -0,0 +1,62 @@
+<?php
+ /**
+ * Form for re-doing the cvterm path
+ *
+ * @ingroup tripal_cv
+ */
+function tripal_cv_cvtermpath_form() {
+
+  // get a list of db from chado for user to choose
+  $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
+  $results = chado_query($sql);
+
+  $cvs = array();
+  $cvs[] = '';
+  foreach ($results as $cv) {
+    $cvs[$cv->cv_id] = $cv->name;
+  }
+
+  $form['cvid'] = array(
+    '#title' => t('Controlled Vocabulary/Ontology Name'),
+    '#type' => 'select',
+    '#options' => $cvs,
+    '#description' => t('The Chado cvtermpath is a database table that provides lineage for ontology terms 
+      and is useful for quickly finding any ancestor parent of a term.  This table must be populated for each
+      ontology.  Select a controlled vocabulary for which you would like to upate the cvtermpath.'),
+  );
+
+  $form['description'] = array(
+    '#type' => 'item',
+    '#value' => t("Submit a job to update chado cvtermpath table."),
+    '#weight' => 1,
+  );
+
+  $form['button'] = array(
+    '#type' => 'submit',
+    '#value' => t('Update cvtermpath'),
+    '#weight' => 2,
+  );
+
+  return $form;
+} 
+/**
+ * Cvterm path form submit
+ *
+ * @ingroup tripal_cv
+ */
+function tripal_cv_cvtermpath_form_submit($form, &$form_state) {
+  global $user;
+
+  $cvid =  $form_state['values']['cvid'];
+
+  // first get the controlled vocabulary name:
+  $sql = "SELECT * FROM {cv} WHERE cv_id = :cv_id";
+  $cv = chado_query($sql, array(':cv_id' => $cvid))->fetchObject();
+
+  // Submit a job to update cvtermpath
+  $job_args = array($cvid);
+  if ($form_state['values']['op'] == t('Update cvtermpath')) {
+    tripal_add_job("Update cvtermpath: $cv->name", 'tripal_cv',
+       'tripal_cv_update_cvtermpath', $job_args, $user->uid);
+  }
+}

+ 0 - 42
tripal_cv/includes/obo_loader.inc

@@ -103,48 +103,6 @@ function tripal_cv_obo_form_submit($form, &$form_state) {
   tripal_cv_submit_obo_job($obo_id, $obo_name, $obo_url, $obo_file);
 }
 
-/**
- * Form for re-doing the cvterm path
- *
- * @ingroup tripal_cv
- */
-function tripal_cv_cvtermpath_form() {
-
-  // get a list of db from chado for user to choose
-  $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
-  $results = chado_query($sql);
-
-  $cvs = array();
-  $cvs[] = '';
-  foreach ($results as $cv) {
-    $cvs[$cv->cv_id] = $cv->name;
-  }
-
-  $form['cvid'] = array(
-    '#title' => t('Controlled Vocabulary/Ontology Name'),
-    '#type' => 'select',
-    '#options' => $cvs,
-    '#description' => t('The Chado cvtermpath is a database table that provides lineage for ontology terms 
-      and is useful for quickly finding any ancestor parent of a term.  This table must be populated for each
-      ontology.  Select a controlled vocabulary for which you would like to upate the cvtermpath.'),
-  );
-
-  $form['description'] = array(
-    '#type' => 'item',
-    '#value' => t("Submit a job to update chado cvtermpath table."),
-    '#weight' => 1,
-  );
-
-  $form['button'] = array(
-    '#type' => 'submit',
-    '#value' => t('Update cvtermpath'),
-    '#weight' => 2,
-  );
-
-  return $form;
-}
-
-
 /**
  *
  * @ingroup tripal_obo_loader

+ 20 - 20
tripal_cv/includes/trees.inc

@@ -191,9 +191,9 @@ function tripal_cv_init_tree($cv_id, $cnt_table = NULL, $fk_column = NULL,
   $sql = "
     SELECT *
     FROM {cv_root_mview} CRM
-    WHERE cv_id = %d
+    WHERE cv_id = :cv_id
   ";
-  $results = chado_query($sql, $cv_id);
+  $results = chado_query($sql, array(':cv_id' => $cv_id));
 
   // prepare the SQL statement that will allow us to pull out count
   // information for each term in the tree.
@@ -205,17 +205,17 @@ function tripal_cv_init_tree($cv_id, $cnt_table = NULL, $fk_column = NULL,
        SELECT CVT.name, CVT.cvterm_id, CNT.$cnt_column as num_items
        FROM {$cnt_table} CNT
         INNER JOIN {cvterm} CVT on CNT.$fk_column = CVT.cvterm_id
-       WHERE $filter AND CVT.cvterm_id = %d
+       WHERE $filter AND CVT.cvterm_id = :cvterm_id
        ORDER BY $cnt_column desc
     ";
   }
 
-  while ($term = db_fetch_object($results)) {
+  foreach ($results as $term) {
     $name = $term->name;
     $count = 0;
     if ($cnt_table) {
-      $cnt_results = chado_query($cnt_sql, $term->cvterm_id);
-      while ($cnt = db_fetch_object($cnt_results)) {
+      $cnt_results = chado_query($cnt_sql, array(':cvterm_id' => $term->cvterm_id));
+      foreach ($cnt_results as $cnt) {
         $count += $cnt->cnt;
       }
       if ($count > 0) {
@@ -257,10 +257,10 @@ function tripal_cv_get_term_children($cvterm_id, $cnt_table = NULL,
        INNER JOIN {CVTerm} CVT2 on CVTR.object_id = CVT2.cvterm_id
        INNER JOIN {CVTerm} CVT3 on CVTR.type_id = CVT3.cvterm_id
        INNER JOIN {CV} on CV.cv_id = CVT1.cv_id
-    WHERE CVTR.object_id = %d
+    WHERE CVTR.object_id = :cvterm_id
     ORDER BY CVT1.name
   ";
-  $results = chado_query($sql, $cvterm_id);
+  $results = chado_query($sql, array(':cvterm_id' => $cvterm_id));
 
   // prepare the SQL statement that will allow us to pull out count
   // information for each term in the tree.
@@ -272,26 +272,26 @@ function tripal_cv_get_term_children($cvterm_id, $cnt_table = NULL,
        SELECT CVT.name, CVT.cvterm_id, CNT.$cnt_column as num_items
        FROM {$cnt_table} CNT
         INNER JOIN {cvterm} CVT on CNT.$fk_column = CVT.cvterm_id
-       WHERE $filter AND CVT.cvterm_id = %d
+       WHERE $filter AND CVT.cvterm_id = :cvterm_id
        ORDER BY $cnt_column desc
     ";
   }
 
   // populate the JSON content array
-  while ($term = db_fetch_object($results)) {
+  foreach ($results as $term) {
     // count the number of items per term if requested
     $name = $term->subject_name;
     $count = 0;
     if ($cnt_table) {
-      $cnt_results = chado_query($cnt_sql, $term->subject_id);
-      while ($cnt = db_fetch_object($cnt_results)) {
+      $cnt_results = chado_query($cnt_sql, array(':cvterm_id' => $term->subject_id));
+      foreach ($cnt_results as $cnt) {
         $count += $cnt->num_items;
       }
       if ($count > 0) {
         $name .= " (" . number_format($count) . " $label)";
 
         // check if we have any children if so then set the value
-        $children = db_fetch_object(chado_query($sql, $term->subject_id));
+        $children = chado_query($sql, array(':cvterm_id' => $term->subject_id))->fetchObject();
         $state = 'leaf';
         if ($children) {
           $state = 'closed';
@@ -308,7 +308,7 @@ function tripal_cv_get_term_children($cvterm_id, $cnt_table = NULL,
     }
     else {
       // check if we have any children if so then set the value
-      $children = db_fetch_object(chado_query($sql, $term->subject_id));
+      $children = chado_query($sql, array(':cvterm_id' => $term->subject_id))->fetchObject();
       $state = 'leaf';
       if ($children) {
         $state = 'closed';
@@ -380,19 +380,19 @@ function tripal_cv_cvterm_info($cvterm_id) {
       INNER JOIN {CV} on CVT.cv_id = CV.cv_id
       INNER JOIN {dbxref} DBX on CVT.dbxref_id = DBX.dbxref_id
       INNER JOIN {DB} on DBX.db_id = DB.db_id
-    WHERE CVT.cvterm_id = %d
+    WHERE CVT.cvterm_id = :cvterm_id
   ";
-  $cvterm = db_fetch_object(chado_query($sql, $cvterm_id));
+  $cvterm = chado_query($sql, array(':cvterm_id' => $cvterm_id))->fetchObject();
 
   $sql = "
     SELECT CVTS.synonym, CVT.name as cvname
     FROM {cvtermsynonym} CVTS
       INNER JOIN {cvterm} CVT on CVTS.type_id = CVT.cvterm_id
-    WHERE CVTS.cvterm_id = %d
+    WHERE CVTS.cvterm_id = :cvterm_id
 
   ";
-  $results = chado_query($sql, $cvterm_id);
-  while ($synonym = db_fetch_object($results)) {
+  $results = chado_query($sql, array(':cvterm_id' => $cvterm_id));
+  foreach ($results as $synonym) {
     $synonym_rows .= "<b>$synonym->cvname:</b>  $synonym->synonym<br />";
   }
 
@@ -445,7 +445,7 @@ function tripal_cv_list_form($form_state) {
 
   $blastdbs = array();
   $cvs[''] = '';
-  while ($cv = db_fetch_object($results)) {
+  foreach ($results as $cv) {
     $cvs[$cv->cv_id] = $cv->name;
   }
 

+ 0 - 577
tripal_cv/includes/tripal_cv_admin.inc

@@ -1,577 +0,0 @@
-<?php
-/**
- * Purpose: Provides the form for Updating and Deleteing existing
- *   chado controlled vocabularies (See chado cv table)
- *
- * @ingroup tripal_cv
- */
-function tripal_cv_edit_page() {
-  $output .= drupal_get_form('tripal_cv_select_form');
-  $output .= '<div id="cv-edit-div">Please select a vocabulary above to view or edit</div>';
-
-  return $output;
-}
-
-/**
- * Purpose: Provides the actual "Select CV" form on the Update/Delete Controlled
- *   Vocabulary page. This form also triggers the edit javascript
- * @todo Modify this form to use Drupal AJAX
- *
- * @ingroup tripal_cv
- */
-function tripal_cv_select_form() {
-
-  // get a list of db from chado for user to choose
-  $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
-  $results = chado_query($sql);
-
-  $cvs = array();
-  $cvs[] = '';
-  while ($cv = db_fetch_object($results)) {
-    $cvs[$cv->cv_id] = $cv->name;
-  }
-
-  $form['cvid'] = array(
-    '#title' => t('Controlled Vocabulary/Ontology Name'),
-    '#type' => 'select',
-    '#options' => $cvs,
-    '#ahah' => array(
-      'path' => 'admin/tripal/tripal_cv/cv/edit/js',
-      'wrapper' => 'cv-edit-div',
-      'effect' => 'fade',
-      'event' => 'change',
-      'method' => 'replace',
-    ),
-  );
-
-  return $form;
-}
-
-/**
- * Purpose: The edit controlled vocabulary javascript
- *
- * @ingroup tripal_cv
- */
-function tripal_ajax_cv_edit() {
-
-  // get the database id, build the form and then return the JSON object
-  $cvid = filter_xss($_POST['cvid']);
-  $form = drupal_get_form('tripal_cv_edit_form', $cvid);
-  drupal_json(array('status' => TRUE, 'data' => $form));
-
-}
-
-/**
- * Purpose: Provides a form to allow updating/deleteing of controlled vocabularies
- *
- * @ingroup tripal_cv
- */
-function tripal_cv_edit_form(&$form_state = NULL, $cvid = NULL) {
-
-  $sql = "SELECT * FROM {cv} WHERE cv_id = %d ";
-  $cv = db_fetch_object(chado_query($sql, $cvid));
-
-  // set the default values.  If there is a value set in the
-  // form_state then let's use that, otherwise, we'll pull
-  // the values from the database
-  $default_db = $form_state['values']['name'];
-  $default_desc = $form_state['values']['description'];
-  $default_url = $form_state['values']['url'];
-  $default_urlprefix = $form_state['values']['urlprefix'];
-  if (!$default_db) {
-    $default_cv = $cv->name;
-  }
-  if (!$default_desc) {
-    $default_desc = $cv->definition;
-  }
-
-  $form['cvid'] = array(
-    '#type' => 'hidden',
-    '#value' => $cvid
-  );
-
-  $form['name']= array(
-    '#type'          => 'textfield',
-    '#title'         => t("Controlled Vocabulary name"),
-    '#description'   => t('Please enter the name for this vocabulary.'),
-    '#required'      => FALSE,
-    '#default_value' => $default_cv,
-    '#weight'        => 1
-  );
-
-  $form['definition']= array(
-    '#type'          => 'textarea',
-    '#title'         => t('Description'),
-    '#description'   => t('Please enter a description for this vocabulary'),
-    '#default_value' => $default_desc,
-    '#weight'        => 2
-  );
-
-  $form['update'] = array(
-    '#type'         => 'submit',
-    '#value'        => t('Update'),
-    '#weight'       => 5,
-    '#executes_submit_callback' => TRUE,
-  );
-  $form['delete'] = array(
-    '#type'         => 'submit',
-    '#value'        => t('Delete'),
-    '#weight'       => 6,
-    '#executes_submit_callback' => TRUE,
-  );
-
-  $form['#redirect'] = 'admin/tripal/tripal_cv';
-
-
-  return $form;
-}
-
-/**
- * Purpose: The submit function of the update/delete controlled vocabulary form
- *
- * @ingroup tripal_cv
- */
-function tripal_cv_edit_form_submit($form, &$form_state) {
-
-  $name =  $form_state['values']['name'];
-  $desc =  $form_state['values']['definition'];
-  $cvid =  $form_state['values']['cvid'];
-  $op   =  $form_state['values']['op'];
-
-  if (strcmp($op, 'Update') == 0) {
-    $sql = "
-       UPDATE {cv} SET
-         name = '%s',
-         definition = '%s'
-       WHERE cv_id = %d
-    ";
-    $db = chado_query($sql, $name, $desc, $cvid);
-    if ($db) {
-      drupal_set_message(t("Controlled vocabulary updated"));
-    }
-    else {
-      drupal_set_message(t("Failed to update controlled vocabulary."), 'error');
-    }
-  }
-  if (strcmp($op, 'Delete')==0) {
-    $sql = "
-       DELETE FROM {cv}
-       WHERE cv_id = %d
-    ";
-    $db = chado_query($sql, $cvid);
-    if ($db) {
-      drupal_set_message(t("Controlled vocabulary deleted"));
-    }
-    else {
-      drupal_set_message(t("Failed to delete controlled vocabulary."), 'error');
-    }
-  }
-
-}
-
-
-/**
- * Purpose: Provides the Add controlled vocabulary form
- *
-  * @ingroup tripal_cv
- */
-function tripal_cv_add_form(&$form_state = NULL) {
-
-  $form['cvid'] = array(
-    '#type' => 'hidden',
-    '#value' => $cvid
-  );
-
-  $form['name']= array(
-    '#type'          => 'textfield',
-    '#title'         => t("Controlled Vocabulary name"),
-    '#description'   => t('Please enter the name for this vocabulary.  This field will be ignored if an OBO file or URL is provided above'),
-    '#required'      => FALSE,
-    '#default_value' => $default_cv,
-    '#weight'        => 1
-  );
-
-  $form['definition']= array(
-    '#type'          => 'textarea',
-    '#title'         => t('Description'),
-    '#description'   => t('Please enter a description for this vocabulary'),
-    '#default_value' => $default_desc,
-    '#weight'        => 2
-  );
-
-  $form['add'] = array(
-    '#type'         => 'submit',
-    '#value'        => t('Add'),
-    '#weight'       => 5,
-    '#executes_submit_callback' => TRUE,
-  );
-
-  $form['#redirect'] = 'admin/tripal/tripal_cv';
-
-  return $form;
-}
-
-/**
- * Purpose: The submit function for the add controlled vocabulary form
- *
- * @ingroup tripal_cv
- */
-function tripal_cv_add_form_submit($form, &$form_state) {
-
-  $name =  $form_state['values']['name'];
-  $desc =  $form_state['values']['definition'];
-
-  $sql = "
-    INSERT INTO {cv}
-     (name,definition)
-    VALUES
-     ('%s','%s')
-  ";
-  $db = chado_query($sql, $name, $desc);
-  if ($db) {
-    drupal_set_message(t("Controlled vocabulary added"));
-  }
-  else {
-    drupal_set_message(t("Failed to add controlled vocabulary."), 'error');
-  }
-
-}
-
-/**
- * Purpose: Provides the form that allows adding of terms to an existing
- *   controlled vocabulary
- *
-  * @ingroup tripal_cv
- */
-function tripal_cv_cvterm_form(&$form_state, $action = 'add') {
-  tripal_core_ahah_init_form();
-  
-  $form = array();
-
-  // get defaults  
-  $cv_id = $form_state['values']['cv_id'] ? $form_state['values']['cv_id'] : FALSE;
-  $name  = $form_state['values']['name'] ? $form_state['values']['name'] : '';
-  
-  // if we have a cv_id and a term name then get the rest of the term details
-  if ($cv_id and $name) {
-     $values = array(
-       'cv_id' => $cv_id,
-       'name' => $name,
-     );     
-     $results = tripal_core_chado_select('cvterm', array('*'), $values);
-     if (!$results or count($results) == 0) {
-       // we can't find the cvterm so reset the name to blank
-       $name = '';
-     }
-     else {
-       $cvterm = $results[0];
-       $definition = $cvterm->definition;
-       $is_relationshiptype = $cvterm->is_relationshiptype;
-       $is_obsolete = $cvterm->is_obsolete;
-       
-       // now get the database
-       $values = array('dbxref_id' => $cvterm->dbxref_id);
-       $results = tripal_core_chado_select('dbxref', array('*'), $values);
-       $dbxref = $results[0];
-       $accession = $dbxref->accession;
-       $db_id = $dbxref->db_id;
-     }
-  }
-  
-  $values = array();
-  $columns = array('cv_id', 'name');
-  $options = array('order_by' => array('name' => 'ASC'));
-  $results = tripal_core_chado_select('cv', $columns, $values, $options);
-  $cvs = array();
-  $cvs[] = '';
-  foreach ($results as $cv) {
-    $cvs[$cv->cv_id] = $cv->name;
-  }
-
-  $form['wrapper-top'] = array(
-    '#type' => 'markup',
-    '#value' => '<div id="cvterm-form">', 
-  );
-  
-  $form['form_action'] = array(
-    '#type' => 'hidden',
-    '#value' => $action, 
-  );   
-  
-  $form['cv_id'] = array(
-    '#title' => t('Controlled Vocabulary (Ontology) Name'),
-    '#type' => 'select',
-    '#options' => $cvs,
-    '#required' => TRUE,
-    '#default_value' => $cv_id,
-    '#ahah' => array(
-       'path'    => 'admin/tripal/tripal_cv/cvterm/ahah',
-       'wrapper' => 'cvterm-form',
-       'event'   => 'change',
-       'method'  => 'replace',
-    ),    
-  );
-  
-  if ($cv_id) {
-    $form['add_cvterm'] = array(
-      '#type'           => 'fieldset',
-      '#title'          => t('Term Details'),
-      '#prefix'         => '<div id="cvterm-add-div">',
-      '#suffix'         => '</div>'
-    );
-    $description = t('Please enter the name for this vocabulary term.');
-    if ($action == 'edit') {
-      $description = t('Enter the name of the term to edit.  This field will update automatically as you type. Click outside of the box after entering the term.');
-    }
-    $form['add_cvterm']['name']= array(
-      '#type'          => 'textfield',
-      '#title'         => t("Term Name"),
-      '#description'   => $description,
-      '#default_value' => $name,
-      '#required'      => TRUE,
-    );
-    if ($action == 'edit') {
-      if ($name) {
-        $form['add_cvterm']['name']['#attributes'] = array('readonly' => 'readonly');
-        $form['add_cvterm']['name']['#description'] = 'The term name cannot be changed. If the name is incorrect, please create a new term and make this one as obsolete.';
-      } else {
-        $form['add_cvterm']['name']['#autocomplete_path'] = "admin/tripal/tripal_cv/cvterm/auto_name/$cv_id";
-        $form['add_cvterm']['name']['#ahah'] = array(
-           'path'    => 'admin/tripal/tripal_cv/cvterm/ahah',
-           'wrapper' => 'cvterm-form',
-           'method'  => 'replace',
-        );
-      }
-    }   
-
-    if ($action == 'add' or $name) { 
-      
-      
-      $form['add_cvterm']['definition']= array(
-        '#type'          => 'textarea',
-        '#title'         => t('Description'),
-        '#description'   => t('Please enter a description for this term'),
-        '#default_value' => $definition,
-      );
-  
-      $form['add_cvterm']['is_relationshiptype'] = array(
-        '#type'          => 'checkbox',
-        '#title'         => t('This term describes a relationship?'),
-        '#default_value' => $is_relationshiptype,
-      );
-  
-      $form['add_cvterm']['is_obsolete'] = array(
-        '#type'          => 'checkbox',
-        '#title'         => t('This term is obsolete?'),
-        '#default_value' => $is_obsolete,
-      );
-  
-      $values = array();
-      $columns = array('db_id', 'name');
-      $options = array('order_by' => array('name' => 'ASC'));
-      $results = tripal_core_chado_select('db', $columns, $values, $options);
-      $dbs = array();
-      $dbs[] = '';
-      foreach ($results as $db) {
-        $dbs[$db->db_id] = $db->name;
-      }
-      $form['add_cvterm']['db_id'] = array(
-        '#type'         => 'select',
-        '#title'         => t('Database'),
-        '#description'   => t('All terms must be assocated with an external database.
-                            Please select the external database to associate with
-                            this term'),
-        '#options'      => $dbs,
-        '#default_value' => $db_id,
-        '#required' => TRUE,
-      );
-      if ($action == 'edit') {
-        // we don't want to allow the user to change the database on an edit.
-        $form['add_cvterm']['db_id']['#disabled'] = TRUE;
-        $form['add_cvterm']['db_id']['#description'] = 'The database to which this term belongs cannot be changed.';
-      }
-      
-      $form['add_cvterm']['accession']= array(
-        '#type'          => 'textfield',
-        '#title'         => t("Accession"),
-        '#description'   => t('If this term has an existing accession (unique identifier) in the database 
-           please enter that here.  If the accession is numeric with a database prefix (e.g. GO:003023), please
-           enter just the numeric value.  The database prefix will be appended whenever the term is displayed. 
-           If the accession is not numeric then enter it as is.  If no value is provied, the term name 
-           provided above will be used as the accession.'),
-        '#required'      => FALSE,
-        '#default_value' => $accession,
-      );
-      if ($action == 'edit') {
-        $form['add_cvterm']['accession']['#attributes'] = array('readonly' => 'readonly');
-        $form['add_cvterm']['accession']['#description'] = 'Cannot change the term accession.';
-      }
-      $button_text = 'Add Term';
-      if ($action == 'edit') {
-        $button_text = 'Update Term';
-      }
-      $form['add_cvterm']['submit'] = array(
-        '#type'  => 'submit',
-        '#value' => $button_text,
-      );
-    } // end if name selected (or action == 'add')
-  } //end of if cv selected
-    
-  
-  $form['wrapper-bottom'] = array(
-    '#type' => 'markup',
-    '#value' => '</div>',
-  );
-
-  return $form;
-}
-
-/*
- * @ingroup tripal_cv
- */
-function tripal_cv_cvterm_name_autocomplete($cv_id, $string = '') {
-  $sql = "
-    SELECT cvterm_id, name 
-    FROM {cvterm} 
-    WHERE cv_id = %d and name like '%s%%' 
-    ORDER by name
-  ";
-  $results = chado_query($sql, $cv_id, $string);
-  $items = array();
-  while($term = db_fetch_object($results)) {
-     $items[$term->name] = $term->name;
-  }  
-  drupal_json($items);   
-}
-/**
- * Purpose: Validates the input for adding a cvterm
- *
-  * @ingroup tripal_cv
- */
-function tripal_cv_cvterm_form_validate($form, &$form_state) {
-
-  // Ensure that submit does not get called unless the AHAH in the form was called
-  if (!empty($form_state['ahah_submission'])) {
-    return;
-  }
-
-}
-
-/**
- * Purpose: Adds terms to an existing controlled vocabulary
- *
- * @ingroup tripal_cv
- */
-function tripal_cv_cvterm_form_submit($form, &$form_state) {
-
-  // Ensure the AHAH in the form was called
-  if (!empty($form_state['ahah_submission'])) {
-    return;
-  }
-    
-  // get the database
-  $values = array('db_id' => $form_state['values']['db_id']);
-  $results = tripal_core_chado_select('db', array('name'), $values);
-  if (!$results or count($results) == 0){
-    drupal_set_message(t('Unable to add term.  Cannot find the database.'), 'error');
-    return;
-  }
-  $db = $results[0];
-  
-  // get the cv
-  $values = array('cv_id' => $form_state['values']['cv_id']);
-  $results = tripal_core_chado_select('cv', array('name'), $values);
-  if (!$results or count($results) == 0){
-    drupal_set_message(t('Unable to add term.  Cannot find the vocabulary.'), 'error');
-    return;
-  }
-  $cv = $results[0];
-  
-  // get the accession for this term
-  $accession = $form_state['values']['accession'];
-  if (!$accession) {
-    $accession = $form_state['values']['name'];
-  }  
-  if (is_numeric($accession)) {
-    $accession = $db->name . ":" . $accession;
-  }
-  
-  
-  $update = 0;
-  if ($form_state['values']['form_action'] == 'edit') {
-    $update = 1;
-  }
-  
-  // now add the term
-  $term = array(    
-    'name' => $form_state['values']['name'],
-    'namespace' => $cv->name,
-    'id' => $accession,
-    'def' => $form_state['values']['definition'],
-    'is_obsolete' => $form_state['values']['is_obsolete'],
-  );  
-  
-  $is_relationship = $form_state['values']['is_relationshiptype'];
-  $cvterm = tripal_cv_add_cvterm($term, $cv->name, $is_relationship, $update, $db->name);
-  if ($cvterm) {
-    if (!$update) {
-      drupal_set_message('Term added successfully.');
-    }
-    else {
-      drupal_set_message('Term updated successfully.');
-    }
-  } 
-  else {
-    drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');  
-  }
-
-}
-
-/**
- * Purpose: This function gets called when the selecting of a cv from
- *   the select list triggers it. This function simply rebuilds the form
- *   with new information. No elements are created here
- *
- * @ingroup tripal_cv
- */
-function tripal_cv_cvterm_callback() {
-  $status = TRUE;
-
-  // prepare and render the form
-  $form = tripal_core_ahah_prepare_form();   
-  $data = drupal_render($form);  
-
-  // bind javascript events to the new objects that will be returned 
-  // so that AHAH enabled elements will work.
-  $settings = tripal_core_ahah_bind_events();
-   
-  // return the updated JSON
-  drupal_json(
-    array(
-      'status'   => $status, 
-      'data'     => $data,
-      'settings' => $settings,
-    )  
-  );
-}
-
-/**
- * Cvterm path form submit
- *
- * @ingroup tripal_cv
- */
-function tripal_cv_cvtermpath_form_submit($form, &$form_state) {
-  global $user;
-
-  $cvid =  $form_state['values']['cvid'];
-
-  // first get the controlled vocabulary name:
-  $cv = db_fetch_object(chado_query("SELECT * FROM {cv} WHERE cv_id = %d", $cvid));
-
-  // Submit a job to update cvtermpath
-  $job_args = array($cvid);
-  if ($form_state['values']['op'] == t('Update cvtermpath')) {
-    tripal_add_job("Update cvtermpath: $cv->name", 'tripal_cv',
-       'tripal_cv_update_cvtermpath', $job_args, $user->uid);
-  }
-}

+ 4 - 4
tripal_cv/theme/tripal_cv_admin.tpl.php

@@ -1,10 +1,10 @@
 <br><h3>Tripal Controlled Vocabulary Administrative Tools Quick Links</h3>
     <ul>
-      <li><?php print l('Add a Vocabulary', 'admin/tripal/tripal_cv/cv/add') ?></li>
-      <li><?php print l('Edit a Vocabulary', 'admin/tripal/tripal_cv/cv/edit') ?></li>
+      <li><?php print l('Add a vocabulary', 'admin/tripal/tripal_cv/add_cv') ?></li>
+      <li><?php print l('Edit a vocabulary', 'admin/tripal/tripal_cv/edit_cv') ?></li>
       <li><?php print l('Load ontology with OBO file', 'admin/tripal/tripal_cv/obo_loader') ?></li>
-      <li><?php print l('Add a Term', 'admin/tripal/tripal_cv/cvterm/add') ?></li>
-      <li><?php print l('Edit a Term', 'admin/tripal/tripal_cv/cvterm/edit') ?></li>      
+      <li><?php print l('Add a term to a vocabulary', 'admin/tripal/tripal_cv/cvterm/add') ?></li>
+      <li><?php print l('Edit a term in a vocabulary', 'admin/tripal/tripal_cv/cvterm/edit') ?></li>      
       <li><?php print l('Update cvtermpath', 'admin/tripal/tripal_cv/cvtermpath') ?></li>
     </ul>
 

+ 29 - 31
tripal_cv/tripal_cv.module

@@ -3,9 +3,13 @@
 require_once "includes/charts.inc";
 require_once "includes/trees.inc";
 require_once "includes/obo_loader.inc";
-require_once "includes/tripal_cv_admin.inc";
 require_once "api/tripal_cv.api.inc";
 
+require_once "includes/cv_form.inc";
+require_once "includes/cvterm_form.inc";
+require_once "includes/cvtermpath_form.inc";
+
+
 /**
  * @defgroup tripal_cv CV Module
  * @ingroup tripal_modules
@@ -58,6 +62,10 @@ function tripal_cv_menu() {
     'type' => MENU_NORMAL_ITEM,
   );
   
+  /*
+   * Menu for updating the cvtermpath
+   */
+  
   $items['admin/tripal/tripal_cv/cvtermpath'] = array(
     'title' => 'Update Chado cvtermpath table',
     'description' => 'The Chado cvtermpath table provides lineage for terms and is useful for quickly finding any ancestor parent of a term.  However, this table must be populated.  This page allows for populating of this table one vocabulary at a time',
@@ -70,44 +78,43 @@ function tripal_cv_menu() {
   /*
    * Menu items for adding and editing CVs
    */
-  $items['admin/tripal/tripal_cv/cv/add'] = array(
-    'title' => 'Add a Vocabulary',
+  $items['admin/tripal/tripal_cv/edit_cv'] = array(
+    'title' => 'Edit a Database',
+    'description' => 'Manage Databases ',
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('tripal_cv_add_form'),
+    'page arguments' => array('tripal_cv_cv_edit_form'),
+    'access callback' => 'user_access',
     'access arguments' => array('administer controlled vocabularies'),
     'type' => MENU_NORMAL_ITEM,
   );
-  
-  $items['admin/tripal/tripal_cv/cv/edit'] = array(
-    'title' => 'Edit a Vocabulary',
-    'description' => 'Edit a controlled vocabularies/ontolgoies in Chado ',
-    'page callback' => 'tripal_cv_edit_page',
+
+  $items['admin/tripal/tripal_cv/add_cv'] = array(
+    'title' => 'Add a Database',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('tripal_cv_cv_add_form'),
+    'access callback' => 'user_access',
     'access arguments' => array('administer controlled vocabularies'),
     'type' => MENU_NORMAL_ITEM,
   );
-      
-  $items['admin/tripal/tripal_cv/cv/edit/js'] = array(
-    'page callback' => 'tripal_ajax_cv_edit',
-    'access arguments' => array('administer controlled vocabularies'),
-    'type' => MENU_CALLBACK,
-  );
 
   /*
-   * Menu items for adding and editing CV terms
+   * Menu items for adding and editing CVterms
    */
   $items['admin/tripal/tripal_cv/cvterm/add'] = array(
     'title' => 'Add a Term',
     'description' => 'Manage controlled vocabulary/ontology terms in Chado ',
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('tripal_cv_cvterm_form', 'add'),
+    'page arguments' => array('tripal_cv_cvterm_add_form'),
     'access arguments' => array('administer controlled vocabularies'),
     'type' => MENU_NORMAL_ITEM,
-  );
-  
-  $items['admin/tripal/tripal_cv/cvterm/ahah'] = array(
-    'page callback' => 'tripal_cv_cvterm_callback',
+  );  
+  $items['admin/tripal/tripal_cv/cvterm/edit'] = array(
+    'title' => 'Edit a Term',
+    'description' => 'Edit an existing controlled vocabulary/ontology terms in Chado ',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('tripal_cv_cvterm_edit_form'),
     'access arguments' => array('administer controlled vocabularies'),
-    'type' => MENU_CALLBACK,
+    'type' => MENU_NORMAL_ITEM,
   );
   $items['admin/tripal/tripal_cv/cvterm/auto_name/%/%'] = array(
     'page callback' => 'tripal_cv_cvterm_name_autocomplete',
@@ -116,15 +123,6 @@ function tripal_cv_menu() {
     'type' => MENU_CALLBACK,
   );
   
-  $items['admin/tripal/tripal_cv/cvterm/edit'] = array(
-    'title' => 'Edit a Term',
-    'description' => 'Edit an existing controlled vocabulary/ontology terms in Chado ',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('tripal_cv_cvterm_form', 'edit'),
-    'access arguments' => array('administer controlled vocabularies'),
-    'type' => MENU_NORMAL_ITEM,
-  );
-  
   /*
    * Charts
    */  

+ 1 - 2
tripal_db/includes/tripal_db.admin.inc

@@ -66,12 +66,11 @@ function tripal_db_db_edit_form($form, &$form_state) {
 	  $form['update'] = array(
 	    '#type'         => 'submit',
 	    '#value'        => t('Update'),
-	    '#weight'       => 5,
 	  );
 	  $form['delete'] = array(
 	    '#type'         => 'submit',
 	    '#value'        => t('Delete'),
-      '#weight'       => 6,
+	    '#attributes'   => array('onclick' => 'if(!confirm("Really Delete?")){return false;}'), 
     );
   }
   else {