<?php
/**
 * Implements hook_vocab_storage_info().
 *
 * This hook is created by the Tripal module and is not a Drupal hook.
 */
function tripal_chado_vocab_storage_info() {
  return array(
    'term_chado_storage' => array(
      'label' => t('Chado storage'),
      'module' => 'tripal_chado',
      'description' => t('Integrates terms stored in the local Chado database
          with Tripal entities.'),
      'settings' => array(),
    ),
  );
}

/**
 * Implements hook_vocab_get_term().
 *
 * This hook is created by the Tripal module and is not a Drupal hook.
 */
function tripal_chado_vocab_get_term($vocabulary, $accession) {

  // It's possible that Chado is not available (i.e. it gets renamed
  // for copying) but Tripal has already been prepared and the
  // entities exist.  If this is the case we don't want to run the
  // commands below.
  if (!chado_table_exists('cvterm')) {
    return FALSE;
  }
  $match = array(
    'dbxref_id' => array(
      'db_id' => array(
        'name' => $vocabulary,
      ),
      'accession' => $accession,
    ),
  );
  $cvterm = chado_generate_var('cvterm', $match);
  if (!$cvterm) {
    return FALSE;
  }
  $cvterm = chado_expand_var($cvterm, 'field', 'cvterm.definition');
  $term = array(
    'vocabulary' => array(
      'name' => $cvterm->cv_id->name,
      'short_name' => $cvterm->dbxref_id->db_id->name,
      'description' =>  $cvterm->dbxref_id->db_id->description,
      'url' => $cvterm->dbxref_id->db_id->url
    ),
    'accession'  => $cvterm->dbxref_id->accession,
    'name'       => $cvterm->name,
    'url'        => tripal_get_dbxref_url($cvterm->dbxref_id),
    'definition' => (isset($cvterm->definition)) ? $cvterm->definition : '',
  );
  return $term;
}

/**
 * Implements hook_vocab_import_form();
 */
function tripal_chado_vocab_import_form($form, &$form_state) {
  module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  return tripal_cv_obo_form($form, $form_state);
}
/**
 * Implements hook_vocab_import_form_validate();
 */
function tripal_chado_vocab_import_form_validate($form, &$form_state) {
  module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  return tripal_cv_obo_form_validate($form, $form_state);
}
/**
 * Implements hook_vocab_import_form_submit();
 */
function tripal_chado_vocab_import_form_submit($form, &$form_state) {
  module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  return tripal_cv_obo_form_submit($form, $form_state);
}
/**
 * Implements hook_vocab_select_term_form().
 *
 * TODO: can't this be moved over to the tripal module and remove the
 *  Chado specific parts???
 */
function tripal_chado_vocab_select_term_form($form, &$form_state) {
  $term_name = array_key_exists('values', $form_state) ? $form_state['values']['term_name'] : '';

  // If no term has been selected yet then provide the auto complete field.
  $form['term_name'] = array(
    '#title'       => t('Content Type'),
    '#type'        => 'textfield',
    '#description' => t("The content type must be the name of a term in
        a controlled vocabulary and the controlled vocabulary should
        already be loaded into Tripal.  For example, to create a content
        type for storing 'genes', use the 'gene' term from the
        Sequence Ontology (SO)."),
    '#required'    => TRUE,
    '#default_value' => $term_name,
    '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  );
  $form['select_button'] = array(
    '#type' => 'submit',
    '#value' => t('Lookup Term'),
    '#name' => 'select_cvterm',
    '#ajax' => array(
      'callback' => "tripal_chado_vocab_select_term_form_ajax_callback",
      'wrapper' => "tripal-chado-vocab-select-form",
      'effect' => 'fade',
      'method' => 'replace'
    ),
  );

  if ($term_name) {
    $form['terms_list'] = array(
      '#type' => 'fieldset',
      '#title' => t('Matching Terms'),
      '#description' => t('Please select the term the best matches the
          content type you want to create. If the same term exists in
          multiple vocabularies you will see more than one option below.')
    );
    $match = array(
      'name' => $term_name,
    );
    $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
    $terms = chado_expand_var($terms, 'field', 'cvterm.definition');
    $num_terms = 0;
    foreach ($terms as $term) {
      // Save the user a click by setting the default value as 1 if there's
      // only one matching term.
      $default = FALSE;
      $attrs = array();
      if ($num_terms == 0 and count($terms) == 1) {
        $default = TRUE;
        $attrs = array('checked' => 'checked');
      }
       $form['terms_list']['term-' . $term->cvterm_id] = array(
         '#type' => 'checkbox',
         '#title' =>  $term->name,
         '#default_value' => $default,
         '#attributes' => $attrs,
         '#description' => '<b>Vocabulary:</b> ' . $term->cv_id->name . ' (' . $term->dbxref_id->db_id->name . ') ' . $term->cv_id->definition .
             '<br><b>Term: </b> ' . $term->dbxref_id->db_id->name . ':' . $term->dbxref_id->accession . '.  ' .
             '<br><b>Definition:</b>  ' . $term->definition,
       );
       $base_tables = chado_get_base_tables();
       $default_table = 0;
       $options = array(0 => '-- Select table --');
       foreach ($base_tables AS $tablename) {
         $options[$tablename] = $tablename;
       }
       $mapped_table = chado_get_cvterm_mapping(array('cvterm_id' => $term->cvterm_id));
       if ($mapped_table) {
         $default_table = $mapped_table->chado_table;
       }
       $form['terms_list']['default_table-' . $term->cvterm_id] = array(
         '#type' => 'select',
         '#title' => 'Chado table',
         '#options' => $options,
         '#description' => 'Select the table in Chado where data of this type
           will be stored.',
         '#default_value' => $default_table
       );
       $num_terms++;
    }
    if ($num_terms == 0) {
      $form['terms_list']['none'] = array(
        '#type' => 'item',
        '#markup' => '<i>' . t('There is no term that matches the entered text.') . '</i>'
      );
    }
    // Add in the button for the cases of no terms or too many.
    $form['submit_button'] = array(
      '#type' => 'submit',
      '#value' => t('Use this term'),
      '#name' => 'use_cvterm'
    );
  }

  $form['#prefix'] = '<div id = "tripal-chado-vocab-select-form">';
  $form['#suffix'] = '</div>';

  return $form;
}

/**
 * Implements an AJAX callback for the tripal_chado_vocab_select_term_form.
 */
function tripal_chado_vocab_select_term_form_ajax_callback($form, $form_state) {
  return $form;
}

/**
 * Implements hook_vocab_select_term_form_validate().
 */
function tripal_chado_vocab_select_term_form_validate($form, &$form_state) {

  if (array_key_exists('clicked_button', $form_state) and
      $form_state['clicked_button']['#name'] =='use_cvterm') {

    $cvterm_id = NULL;

    // Make sure we have a cvterm selected
    $num_selected = 0;
    foreach ($form_state['values'] as $key => $value) {
      $matches = array();
      if (preg_match("/^term-(\d+)$/", $key, $matches) and
          $form_state['values']['term-' . $matches[1]]) {
        $cvterm_id = $matches[1];
        $term = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
        $num_selected++;
      }
    }
    if ($num_selected == 0) {
      form_set_error('', 'Please select at least one term.');
    }
    else if ($num_selected > 1) {
      form_set_error('term-' . $cvterm_id, 'Please select only one term from the list below.');
    }
    else {
      $form_state['storage']['vocabulary'] = $term->dbxref_id->db_id->name;
      $form_state['storage']['accession'] = $term->dbxref_id->accession;
      $form_state['storage']['term_name'] = $term->name;
      // Make sure a default table is selected
      $default_table = $form_state['values']['default_table-' . $cvterm_id];
      if (!$default_table) {
        form_set_error('default_table-' . $cvterm_id, 'Please select a default table.');
      }
      else {
        $chado_field = NULL;
        $schema = chado_get_schema($default_table);
        if (isset($schema['foreign keys']['cvterm']['columns'])) {
          $fkey = array_keys($schema['foreign keys']['cvterm']['columns']);
          $chado_field = $fkey[0];
        }
        tripal_chado_add_cvterm_mapping($cvterm_id, $default_table, $chado_field);
      }
    }
  }
  // For any other button click it's an AJAX call and we just want to reubild
  // the form.
  else {
    $form_state['rebuild'] = TRUE;
  }
}