Explorar el Código

CV Defaults: Added the ability to add in new cv defaults through the web UI

Lacey Sanderson hace 11 años
padre
commit
d33cf2fcde
Se han modificado 1 ficheros con 106 adiciones y 6 borrados
  1. 106 6
      tripal_cv/includes/tripal_cv.admin.inc

+ 106 - 6
tripal_cv/includes/tripal_cv.admin.inc

@@ -148,27 +148,127 @@ function tripal_cv_admin_set_defaults_form($form, $form_state) {
 
   }
 
-  $form['submit'] = array(
+  $form['settings']['submit'] = array(
     '#type' => 'submit',
     '#value' => 'Update Defaults'
   );
 
+  // Adding new CV Defaults
+  $form['new'] = array(
+    '#type' => 'fieldset',
+    '#title' => 'Add New Defaults',
+    '#description' => 'You can use the form below to add a default controlled vocabulary
+      for a table/field combination not available in the "Configured Defaults" section above.',
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+    '#tree' => TRUE,
+    '#prefix' => '<div id="new-default">',
+    '#suffix' => '</div>',
+  );
+
+  $tripal_msg = tripal_set_message(
+    'If you are developing a custom module and would like to use the Default Controlled
+      Vocabulary API to flexibly set the controlled vocabulary, then it is better to set
+      the default programatically rather than through this interface. To do this <ol>
+        <li>Tell Tripal about the table/field you would like to set the default for. This
+          is done by implementing hook_install() in your modules .install file and adding
+          a call to <code>tripal_set_default_cv([table name], [field name], [cv name])</code> which
+          will set the default for <code>[table name].[field name]</code> to the <code>[cv name]</code> controlled
+          vocabulary. This vocabulary must already exist.</li>
+        <li>Then everywhere in your module that you need to know the controlled vocabulary,
+          you call <code>tripal_get_default_cv([table name], [field name])</code> which will return
+          an object describing the set default controlled vocabulary or call
+          <code>tripal_get_cvterm_default_select_options([table name], [field name], [field friendly name])</code>
+          if you would like an array of options for use in a select or autocomplete form element.</li></ol>
+      ',
+    TRIPAL_NOTICE,
+    array('return_html' => TRUE)
+  );
+
+  $form['new']['instructions'] = array(
+    '#type' => 'markup',
+    '#markup' => $tripal_msg
+  );
+
+  $chado_tables = chado_get_table_names(TRUE);
+  $chado_tables[0] = 'Select a Table';
+  $form['new']['table'] = array(
+    '#type' => 'select',
+    '#title' => 'Table Name',
+    '#description' => 'The name of the table you would like to set a controlled vocabulary default for.',
+    '#options' => $chado_tables,
+    '#default_value' => 0,
+    '#ajax' => array(
+      'callback' => 'tripal_cv_admin_ajax_new_default_field_callback',
+      'wrapper' => 'new-default',
+    )
+  );
+
+  $table = (isset($form_state['values']['new']['table']))? $form_state['values']['new']['table'] : FALSE;
+  $columns = array('Select a Field');
+  if ($table) {
+    // get the table description
+    $table_desc = chado_get_schema($table);
+    if (isset($table_desc['foreign keys']['cvterm'])) {
+      foreach ($table_desc['foreign keys']['cvterm']['columns'] as $left_column => $right_column) {
+        $columns[$left_column] = $left_column;
+      }
+    }
+  }
+  $form['new']['field'] = array(
+    '#type' => 'select',
+    '#title' => 'Field Name',
+    '#description' => 'The name of the field you would like to set a controlled vocabulary default for.',
+    '#options' => $columns,
+    '#default_value' => 0
+  );
+
+  $cvs[0] = 'Select a Vocabulary';
+  $form['new']['vocabulary'] = array(
+    '#type' => 'select',
+    '#title' => 'Vocabulary',
+    '#description' => 'The default controlled vocabulary you would like to set for this field.',
+    '#options' => $cvs,
+    '#default_value' => 0
+  );
+
+  $form['new']['add_new'] = array(
+    '#type' => 'submit',
+    '#value' => 'Set New Default'
+  );
+
   return $form;
 }
 
 function tripal_cv_admin_set_defaults_form_submit($form, $form_state) {
 
-  foreach ($form_state['values']['settings']['existing'] as $default_cv) {
-    if (!empty($default_cv['vocabulary'])) {
+  if ($form_state['triggering_element']['#value'] == 'Update Defaults') {
+    foreach ($form_state['values']['settings']['existing'] as $default_cv) {
+      if (!empty($default_cv['vocabulary'])) {
+        tripal_set_default_cv(
+          $default_cv['table_name'],
+          $default_cv['field_name'],
+          '', // We are passing in the cv_id so we don't need the name
+          $default_cv['vocabulary']
+        );
+      }
+    }
+  }
+
+  if ($form_state['triggering_element']['#value'] == 'Set New Default') {
+    if (!empty($form_state['values']['new']['vocabulary'])) {
       tripal_set_default_cv(
-        $default_cv['table_name'],
-        $default_cv['field_name'],
+        $form_state['values']['new']['table'],
+        $form_state['values']['new']['field'],
         '', // We are passing in the cv_id so we don't need the name
-        $default_cv['vocabulary']
+        $form_state['values']['new']['vocabulary']
       );
     }
   }
+}
 
+function tripal_cv_admin_ajax_new_default_field_callback($form, $form_state) {
+  return $form['new'];
 }
 
 /**