<?php
/**
 * Retreives the default vocabulary for a given table and field.
 *
 * Each table in Chado that has a 'type_id' (or foreign key constraint to
 * the cvterm table) will have a default vocabulary assigned. This indicates to
 * Tripal that terms in that vocabulary are used to set the type_id for that
 * table. An example where this is used is the
 * tripal_get_cvterm_select_options() function which generates a list of options
 * for a select box used in a Drupal form.  The select box will list the terms
 * from the default vocabulary in the drop down.
 *
 * This function uses the Chado table and field name (e.g. 'type_id') to
 * retreive the vocabulary assgined.
 *
 * @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
 *
 * @return
 *   The cv object of the default vocabulary or an empty array if not
 *   available.
 */
function tripal_get_default_cv($table, $field) {
  $sql = "
    SELECT cv_id
    FROM {tripal_cv_defaults}
    WHERE table_name = :table and field_name = :field
  ";
  $cv_id = db_query($sql, array(':table' => $table, ':field' => $field))->fetchField();

  return tripal_get_cv(array('cv_id' => $cv_id));
}

/**
 * Retrieves the Chado table to which a vocbulary is set as default.
 *
 * Each table in Chado that has a 'type_id' (or foreign key constraint to
 * the cvterm table) will have a default vocabulary assigned. This indicates to
 * Tripal that terms in that vocabulary are used to set the type_id for that
 * table. An example where this is used is the
 * tripal_get_cvterm_select_options() function which generates a list of options
 * for a select box used in a Drupal form.  The select box will list the terms
 * from the default vocabulary in the drop down.
 *
 * This function uses the vocabulary ID to get the Chado table to which it
 * is assigned.
 *
 * @param $cv_id
 *  The ID of the vocabulary.
 *
 * @return
 *   If an assignment is present, an object containing the 'table_name' and
 *   'field_name' is returned.
 */
function tripal_get_default_cv_table($cv_id) {
  $default = db_select('tripal_cv_defaults', 't')
    ->fields('t', array('table_name', 'field_name'))
    ->condition('cv_id', $cv_id)
    ->execute()
    ->fetchObject();
  return $default;
}

/**
 * Create an options array to be used in a form element
 * which provides a list of all chado cvterms. Unlike the
 * tripal_get_cvterm_select_option, this function retreives the cvterms using
 * the default vocabulary set for a given table and field.  It will also
 * notify the administrative user if a default vocabulary is missing for the
 * field and if the vocabulary is empty.
 *
 * @param $table
 *   The name of the table that contains the 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 $field_desc
 *   A human readable descriptive name for the field
 *
 * @return
 *   An array(cvterm_id => name)
 *   for each cvterm in the chado cvterm table where cv_id=that supplied
 */
function tripal_get_cvterm_default_select_options($table, $field, $field_desc) {

  $default_cv = tripal_get_default_cv($table, $field);
  $options = array();

  if ($default_cv) {
    $options = tripal_get_cvterm_select_options($default_cv->cv_id);

    if (count($options) == 0) {
      tripal_set_message('There are no ' . $field_desc . '. Please ' .
          l('add terms',
              'admin/tripal/loaders/chado_cv/' .$default_cv->cv_id. '/cvterm/add',
              array('attributes' => array('target' => '_blank'))) . ' to the ' .
          $default_cv->name .' vocabulary.',
          TRIPAL_WARNING);
    }

  }
  else {
    tripal_set_message('There is not a default vocabulary set for ' . $field_desc . '. '.
        'Please set one using the ' .
        l('vocabulary defaults configuration page',
            'admin/tripal/vocab/defaults',
            array('attributes' => array('target' => '_blank'))) . '.',
        TRIPAL_WARNING);
  }

  return $options;
}