|
@@ -136,4 +136,63 @@ function tripal_get_cvterm_default_select_options($table, $field, $field_desc) {
|
|
|
}
|
|
|
|
|
|
return $options;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * This function sets the default vocabulary for a given table and field.
|
|
|
+ *
|
|
|
+ * @param $table
|
|
|
+ * The name of the table that contains a field with a foreign key
|
|
|
+ * relationship to the cvterm table
|
|
|
+ * @param $field
|
|
|
+ * The table field name that has the foreign key relationship to the
|
|
|
+ * cvterm table for which the default vocabulary will be set
|
|
|
+ * @param $cv_name
|
|
|
+ * The name of the vocabulary
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * TRUE if set, FALSE if an error occured
|
|
|
+ *
|
|
|
+ * @ingroup tripal_legacy_chado_cv_api
|
|
|
+ */
|
|
|
+function tripal_set_default_cv($table, $field, $cv_name, $cv_id = FALSE) {
|
|
|
+
|
|
|
+ // Get the CV object
|
|
|
+ if ($cv_id) {
|
|
|
+ $cv = tripal_get_cv(array('cv_id' => $cv_id));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $cv = tripal_get_cv(array('name' => $cv_name));
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($cv) {
|
|
|
+ // first delete any entries for this table and field
|
|
|
+ $num_deleted = db_delete('tripal_cv_defaults')
|
|
|
+ ->condition('table_name', $table)
|
|
|
+ ->condition('field_name', $field)
|
|
|
+ ->execute();
|
|
|
+
|
|
|
+ // now add the default value
|
|
|
+ $cv_default_id = db_insert('tripal_cv_defaults')
|
|
|
+ ->fields(array(
|
|
|
+ 'table_name' => $table,
|
|
|
+ 'field_name' => $field,
|
|
|
+ 'cv_id' => $cv->cv_id,
|
|
|
+ ))
|
|
|
+ ->execute();
|
|
|
+
|
|
|
+ if (!$cv_default_id) {
|
|
|
+ tripal_report_error('tripal_chado', TRIPAL_WARNING,
|
|
|
+ "Cannot set default vocabulary for %table.%field. Check the error logs.",
|
|
|
+ array('%table' => $table, '%field' => $field));
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ tripal_report_error('tripal_chado', TRIPAL_WARNING,
|
|
|
+ "Cannot set default vocabulary for %table.%field. The vocabulary name, '%cvname', doesn't exist.",
|
|
|
+ array('%table' => $table, '%field' => $field, '%cvname' => $cv_name));
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
}
|