<?php

//
// APPLICATION PROGRAMMER INTERFACE
//

// Generic Chado API functions
require_once "api/tripal_chado.api.inc";
require_once 'api/tripal_chado.property.api.inc';
require_once 'api/tripal_chado.query.api.inc';
require_once 'api/tripal_chado.entity.api.inc';
require_once 'api/tripal_chado.variables.api.inc';
require_once 'api/tripal_chado.schema.api.inc';
require_once 'api/tripal_chado.custom_tables.api.inc';
require_once 'api/tripal_chado.mviews.api.inc';
require_once 'api/tripal_chado.schema_v1.3.api.inc';
require_once 'api/tripal_chado.schema_v1.2.api.inc';
require_once 'api/tripal_chado.schema_v1.11.api.inc';
require_once 'api/tripal_chado.semweb.api.inc';

// Chado module specific API functions
require_once 'api/modules/tripal_chado.analysis.api.inc';
require_once 'api/modules/tripal_chado.contact.api.inc';
require_once 'api/modules/tripal_chado.cv.api.inc';
require_once 'api/modules/tripal_chado.db.api.inc';
require_once 'api/modules/tripal_chado.feature.api.inc';
require_once 'api/modules/tripal_chado.organism.api.inc';
require_once 'api/modules/tripal_chado.pub.api.inc';
require_once 'api/modules/tripal_chado.stock.api.inc';
require_once 'api/modules/tripal_chado.phylotree.api.inc';


//
// REQUIRED INCLUDE FILES
//
// These require files implement hooks and therefore must
// ways be included when the module is interpreted.
require_once "includes/tripal_chado.entity.inc";
require_once "includes/tripal_chado.schema.inc";
require_once "includes/tripal_chado.vocab_storage.inc";
require_once "includes/tripal_chado.field_storage.inc";
require_once "includes/tripal_chado.bundle.inc";
require_once "includes/tripal_chado.fields.inc";
require_once "includes/tripal_chado.mapping.inc";

require_once "includes/TripalFields/ChadoField.inc";
require_once "includes/TripalFields/ChadoFieldWidget.inc";
require_once "includes/TripalFields/ChadoFieldFormatter.inc";

require_once "includes/ChadoDatabaseConnection.inc";

tripal_chado_set_globals();

/**
 * This function is used to set the global Chado variables
 *
 * @ingroup tripal_chado
 */
function tripal_chado_set_globals() {
  // these global variables are meant to be accessed by all Tripal
  // modules to find the chado version installed and if Chado is local.
  // these variables are stored as globals rather than using the drupal_set_variable
  // functions because the Drupal functions make databaes queries and for long
  // running loaders we don't want those queries repeatedly.
  $GLOBALS["chado_is_installed"]  = chado_is_installed();
  if ($GLOBALS["chado_is_installed"]) {
    $GLOBALS["chado_is_local"]      = chado_is_local();
    $GLOBALS["chado_version"]       = chado_get_version();
    $GLOBALS["exact_chado_version"] = chado_get_version(TRUE);
  }
}

/**
 * Implements hook_init().
 *
 * @ingroup tripal_chado
 */
function tripal_chado_init() {
  if (user_access('administer tripal')) {
    if ($GLOBALS["chado_is_installed"]) {

      // Check to see if the Chado and Drupal have been prepared
      if (!variable_get('tripal_chado_is_prepared', FALSE)) {
        drupal_set_message('Chado is installed but Tripal has not yet prepared Drupal and Chado. Please ' .
            l('prepare both Drupal and Chado', 'admin/tripal/storage/chado/prepare') .
            ' before continuing.', 'warning');
      }
    }
    else {
      drupal_set_message('Tripal cannot find a Chado installation. Please ' .
          l('install Chado', 'admin/tripal/storage/chado/install') .
          ' before continuing.', 'warning');
    }
  }
}

/**
 * Implements hook_views_api().
 *
 * Essentially this hook tells drupal that there is views support for
 *  for this module which then includes tripal_db.views.inc where all the
 *  views integration code is
 *
 * @ingroup tripal_feature
 */
function tripal_chado_views_api() {
  return array(
    'api' => 3.0,
  );
}
/**
 * Implements hook_menu().
 */
function tripal_chado_menu() {
  $items = array();

  //////////////////////////////////////////////////////////////////////////////
  //                            Chado Storage Backend
  //////////////////////////////////////////////////////////////////////////////
  $items['admin/tripal/storage/chado'] = array(
    'title' => 'Chado',
    'description' => t("Integrates Chado with Tripal and includes tools to
        load data, and extend the chado schema through custom tables &
        materialized views."),
    'weight' => -100,
    'access arguments' => array('administer tripal'),
  );

  $items['admin/tripal/storage/chado/install'] = array(
    'title' => 'Install Chado',
    'description' => t('Installs the Chado database tables, views, etc., inside the current Drupal database'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_chado_load_form'),
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array('install chado'),
    'file' => 'includes/tripal_chado.install.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'weight' => -100
  );
  $items['admin/tripal/storage/chado/prepare'] = array(
    'title' => 'Prepare Chado',
    'description' => t('Prepares Drupal to use Chado.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_chado_prepare_form'),
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array('install chado'),
    'file' => 'includes/setup/tripal_chado.setup.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'weight' => -99
  );
  $items['admin/tripal/storage/chado/publish'] = array(
    'title' => 'Publish',
    'description' => t('Publish data that is present in Chado but which does
        not yet have a page on this site for viewing. In Tripal v2.0 or
        earlier this was refered to as "syncing".'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_chado_publish_form'),
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array('administer tripal'),
    'file' => 'includes/tripal_chado.publish.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'weight' => -99
  );

  // Adds a +Publish Chado Content link on the 'Tripal Content Types' page.
  $items['admin/structure/bio_data/publish'] = array(
    'title' => 'Publish Chado Content',
    'description' => t('Publish data that is present in Chado but which does
        not yet have a page on this site for viewing. In Tripal v2.0 or
        earlier this was refered to as "syncing".'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_chado_publish_form'),
    'access arguments' => array('administer tripal'),
    'file' => 'includes/tripal_chado.publish.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_LOCAL_ACTION,
    'weight' => 2
  );


  //////////////////////////////////////////////////////////////////////////////
  //                       Materialized Views
  //////////////////////////////////////////////////////////////////////////////
  $items['admin/tripal/storage/chado/mviews'] = array(
    'title' => 'Materialized Views',
    'description' => t('Materialized views are used to improve speed of large or complex queries.'),
    'page callback' => 'tripal_mview_admin_view',
    'access arguments' => array('administer tripal'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'includes/tripal_chado.mviews.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'weight' => -10
  );
  $items['admin/tripal/storage/chado/mviews/help'] = array(
    'title' => 'Help',
    'description' => t('Help for the materialized views management system'),
    'page callback' => 'theme',
    'page arguments' => array('tripal_mviews_help'),
    'access arguments' => array('administer tripal'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/tripal_chado.mviews.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'weight' => 10
  );
  $items['admin/tripal/storage/chado/mviews/report/%'] = array(
    'title' => 'Materialized View',
    'description' => t('Materialized views are used to improve speed of large or complex queries. These are database views as compared to Drupal views.'),
    'page callback' => 'tripal_mview_report',
    'page arguments' => array(6),
    'access arguments' => array('administer tripal'),
    'type' => MENU_CALLBACK,
    'file' => 'includes/tripal_chado.mviews.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );
  $items['admin/tripal/storage/chado/mviews/new'] = array(
    'title' => 'Create Materialized View',
    'description' => t('Create a new materialized view.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_mviews_form'),
    'access arguments' => array('administer tripal'),
    'type' => MENU_CALLBACK,
    'file' => 'includes/tripal_chado.mviews.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );
  $items['admin/tripal/storage/chado/mviews/edit/%'] = array(
    'title' => 'Edit Materialized View',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_mviews_form', 6),
    'access arguments' => array('administer tripal'),
    'type' => MENU_CALLBACK,
    'file' => 'includes/tripal_chado.mviews.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );
  $items['admin/tripal/storage/chado/mviews/update/%'] = array(
    'title' => 'Create Materialized View',
    'description' => t('Materialized views are used to improve speed of large or complex queries.'),
    'page callback' => 'tripal_mviews_add_populate_job',
    'page arguments' => array(6),
    'access arguments' => array('administer tripal'),
    'type' => MENU_CALLBACK,
    'file' => 'includes/tripal_chado.mviews.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );
  $items['admin/tripal/storage/chado/mviews/delete/%'] = array(
    'title' => 'Create Materialized View',
    'description' => t('Materialized views are used to improve speed of large or complex queries.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_mviews_delete_form', 5),
    'access arguments' => array('administer tripal'),
    'type' => MENU_CALLBACK,
    'file' => 'includes/tripal_chado.mviews.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );
  // TODO: complete the code for exporting and importing of MViews.
  // Need to address security issues of sharing SQL.
  $items['admin/tripal/storage/chado/mviews/import'] = array(
    'title' => 'Import MView',
    'description' => 'Import a materialized view from another Tripal instance.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_mviews_import_form'),
    'access arguments' => array('administer tripal'),
    'type' => MENU_CALLBACK,
    'file' => 'includes/tripal_chado.mviews.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );
  $items['admin/tripal/storage/chado/mviews/%tblid/export'] = array(
    'title' => 'Export MView',
    'description' => 'Export a materialized view for use by another Tripal instance.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_mviews_export_form', 5),
    'access arguments' => array('administer tripal'),
    'type' => MENU_CALLBACK,
    'file' => 'includes/tripal_chado.mviews.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );

  //////////////////////////////////////////////////////////////////////////////
  //                           Custom Tables
  //////////////////////////////////////////////////////////////////////////////
  $items['admin/tripal/storage/chado/custom_tables'] = array(
    'title' => 'Custom Tables',
    'description' => t('Creation of custom tables that are added to Chado database.'),
    'page callback' => 'tripal_custom_table_admin_view',
    'access arguments' => array('administer tripal'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'includes/tripal_chado.custom_tables.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'weight' => -10
  );
  $items['admin/tripal/storage/chado/custom_tables/help'] = array(
    'title' => 'Help',
    'description' => t('Help for the tripal job management system'),
    'page callback' => 'theme',
    'page arguments' => array('tripal_job_help'),
    'access arguments' => array('administer tripal'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/tripal_chado.custom_tables.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'weight' => 10
  );
  $items['admin/tripal/storage/chado/custom_tables/view/%'] = array(
    'title' => 'Custom Tables',
    'description' => t('Custom tables are added to Chado.'),
    'page callback' => 'tripal_custom_table_view',
    'page arguments' => array(6),
    'access arguments' => array('administer tripal'),
    'file' => 'includes/tripal_chado.custom_tables.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/tripal/storage/chado/custom_tables/new'] = array(
    'title' => 'Create Custom Table',
    'description' => t('An interface for creating your own custom tables.'),
    'page callback' => 'tripal_custom_table_new_page',
    'access arguments' => array('administer tripal'),
    'file' => 'includes/tripal_chado.custom_tables.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/tripal/storage/chado/custom_tables/edit/%'] = array(
    'title' => 'Edit Custom Table',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_custom_tables_form', 6),
    'access arguments' => array('administer tripal'),
    'file' => 'includes/tripal_chado.custom_tables.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/tripal/storage/chado/custom_tables/delete/%'] = array(
    'title' => 'Create Custom Table',
    'description' => t('Custom tables are added to Chado.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_custom_tables_delete_form', 6),
    'access arguments' => array('administer tripal'),
    'file' => 'includes/tripal_chado.custom_tables.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/tripal/storage/chado/custom_tables/views/tables/enable'] = array(
    'title' => 'Enable Custom Tables Administrative View',
    'page callback' => 'tripal_enable_view',
    'page arguments' => array('tripal_admin_custom_table', 'admin/tripal/storage/chado/custom_tables'),
    'access arguments' => array('administer tripal'),
    'file' => 'includes/tripal_chado.custom_tables.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );


  //////////////////////////////////////////////////////////////////////////////
  //                           Data Loaders
  //////////////////////////////////////////////////////////////////////////////
  $items['admin/tripal/loaders'] = array(
    'title' => 'Data Loaders',
    'description' => t('Tools facilitating data import.'),
    'access arguments' => array('administer tripal'),
    'type' => MENU_NORMAL_ITEM,
    'weight' => 6
  );
//   $items['admin/tripal/loaders/fasta_loader'] = array(
//     'title' => 'Chado FASTA File Loader',
//     'description' => 'Load sequences from a multi-FASTA file into Chado',
//     'page callback' => 'drupal_get_form',
//     'page arguments' => array('tripal_feature_fasta_load_form'),
//     'access arguments' => array('administer tripal'),
//     'file' => 'includes/loaders/tripal_chado.fasta_loader.inc',
//     'file path' => drupal_get_path('module', 'tripal_chado'),
//     'type' => MENU_NORMAL_ITEM,
//   );
//   $items['admin/tripal/loaders/gff3_load'] = array(
//     'title' => 'Chado GFF3 File Loader',
//     'description' => 'Import a GFF3 file into Chado',
//     'page callback' => 'drupal_get_form',
//     'page arguments' => array('tripal_feature_gff3_load_form'),
//     'access arguments' => array('administer tripal'),
//     'file' => 'includes/loaders/tripal_chado.gff_loader.inc',
//     'file path' => drupal_get_path('module', 'tripal_chado'),
//     'type' => MENU_NORMAL_ITEM,
//   );
  $items['admin/tripal/loaders/ncbi_taxonomy_loader'] = array(
    'title' => 'Chado NCBI Taxonomy Loader',
    'description' => 'Loads taxonomic details about installed organisms.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_chado_taxonomy_load_form'),
    'access arguments' => array('administer tripal'),
    'file' => 'includes/loaders/tripal_chado.taxonomy_importer.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_NORMAL_ITEM,
  );

  // Data Loaders
  $items['admin/tripal/loaders/newic_phylotree_loader'] = array(
    'title' => 'Chado Phylogenetic Trees (Newic format)',
    'description' => 'Loads phylogenetic trees in Newic format.',
    'page callback' => 'drupal_goto',
    'page arguments' => array('node/add/chado-phylotree'),
    'access arguments' => array('administer tripal'),
    'type' => MENU_NORMAL_ITEM,
  );

  $items['admin/tripal/loaders/pub'] = array(
    'title' => t('Chado Publication Importers'),
    'description' => t('Create and modify importers that can connect to and retreive publications from remote databases.'),
    'page callback' => 'tripal_pub_importers_list',
    'access arguments' => array('administer tripal'),
    'file' => 'includes/loaders/tripal_chado.pub_importers.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_NORMAL_ITEM,
    'weight' => 0
  );

  $items['admin/tripal/loaders/pub/new'] = array(
    'title' => t('Add an Importer'),
    'description' => t('Add a new publication importer.'),
    'page callback' => 'tripal_pub_importer_setup_page',
    'access arguments' => array('administer tripal'),
    'type ' => MENU_CALLBACK,
    'file' => 'includes/loaders/tripal_chado.pub_importers.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );
  $items['admin/tripal/loaders/pub/edit/%'] = array(
    'page callback' => 'tripal_pub_importer_setup_page',
    'page arguments' => array(5),
    'access arguments' => array('administer tripal'),
    'type ' => MENU_CALLBACK,
    'file' => 'includes/loaders/tripal_chado.pub_importers.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );
  $items['admin/tripal/loaders/pub/raw/%'] = array(
    'page callback' => 'tripal_get_remote_pub_raw_page',
    'page arguments' => array(5),
    'access arguments' => array('administer tripal'),
    'type ' => MENU_CALLBACK,
    'file' => 'includes/loaders/tripal_chado.pub_importers.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );

  // add a second link for the importer on the data loaders page
  $items['admin/tripal/loaders/pub/import'] = array(
    'page callback' => 'tripal_pub_importers_list',
    'access arguments' => array('administer tripal'),
    'type' => MENU_CALLBACK,
    'file' => 'includes/loaders/tripal_chado.pub_importers.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );

  $items['admin/tripal/loaders/pub/submit/%'] = array(
    'page callback' => 'tripal_pub_importer_submit_job',
    'page arguments' => array(5),
    'access arguments' => array('administer tripal'),
    'type ' => MENU_CALLBACK,
    'file' => 'includes/loaders/tripal_chado.pub_importers.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );

  $items['admin/tripal/loaders/pub/delete/%'] = array(
    'page callback' => 'tripal_pub_importer_delete',
    'page arguments' => array(5),
    'access arguments' => array('administer tripal'),
    'type ' => MENU_CALLBACK,
    'file' => 'includes/loaders/tripal_chado.pub_importers.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );
  $items['admin/tripal/loaders/pub/changedb'] = array(
    'page callback' => 'tripal_pub_importer_setup_page_update_remotedb',
    'page arguments' => array(),
    'access arguments' => array('administer tripal'),
    'type ' => MENU_CALLBACK,
    'file' => 'includes/loaders/tripal_chado.pub_importers.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );

  $items['admin/tripal/loaders/pub/criteria/%/%'] = array(
    'page callback' => 'tripal_pub_importer_setup_page_update_criteria',
    'page arguments' => array(5, 6),
    'access arguments' => array('administer tripal'),
    'type ' => MENU_CALLBACK,
    'file' => 'includes/loaders/tripal_chado.pub_importers.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );


  //////////////////////////////////////////////////////////////////////////////
  //                           Migrate Content
  //////////////////////////////////////////////////////////////////////////////
  $items['admin/tripal/storage/chado/migrate'] = array(
    'title' => 'Migrate',
    'description' => t('Migrate Tripal v2 content to Tripal v3'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_chado_migrate_form'),
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array('administer tripal'),
    'file' => 'includes/tripal_chado.migrate.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'weight' => 10
  );

  //////////////////////////////////////////////////////////////////////////////
  //                           Semantic Web Settings
  //////////////////////////////////////////////////////////////////////////////
  $items['admin/tripal/storage/chado/semweb'] = array(
    'title' => 'Semantic Web',
    'description' => t('Semantic web settings. Set terms used for the web services.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_chado_semweb_form'),
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array('administer tripal'),
    'file' => 'includes/tripal_chado.semweb.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'weight' => 10
  );
  $items['admin/tripal/storage/chado/semweb/edit/%/%'] = array(
    'title' => 'Edit Semantic Web Term',
    'description' => t('Edit terms used for the web services.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_chado_semweb_edit_form', 6, 7),
    'type' => MENU_CALLBACK,
    'access arguments' => array('administer tripal'),
    'file' => 'includes/tripal_chado.semweb.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );
  $items['admin/tripal/storage/chado/semweb/reset/%/%'] = array(
    'title' => 'Reset Semantic Web Term',
    'description' => t('Edit terms used for the web services.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_chado_semweb_reset_form', 6, 7),
    'type' => MENU_CALLBACK,
    'access arguments' => array('administer tripal'),
    'file' => 'includes/tripal_chado.semweb.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
  );

  //////////////////////////////////////////////////////////////////////////////
  //                           Auto Completes
  //////////////////////////////////////////////////////////////////////////////
  $items['admin/tripal/storage/chado/auto_name/dbxref/%/%'] = array(
    'page callback' => 'tripal_autocomplete_dbxref',
    'page arguments' => array(6, 7),
    'access arguments' => array('access content'),
    'file' => 'api/modules/tripal_chado.db.api.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/tripal/storage/chado/auto_name/cvterm/%/%'] = array(
    'page callback' => 'tripal_autocomplete_cvterm',
    'page arguments' => array(6, 7),
    'access arguments' => array('access content'),
    'file' => 'api/modules/tripal_chado.cv.api.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );

  $items['admin/tripal/storage/chado/auto_name/pub/%'] = array(
    'page callback' => 'tripal_autocomplete_pub',
    'page arguments' => array(6),
    'access arguments' => array('access content'),
    'file' => 'api/modules/tripal_chado.pub.api.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/tripal/storage/chado/auto_name/contact/%'] = array(
    'page callback' => 'tripal_autocomplete_contact',
    'page arguments' => array(6),
    'access arguments' => array('access content'),
    'file' => 'api/modules/tripal_chado.contact.api.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/tripal/storage/chado/auto_name/feature/%'] = array(
    'page callback' => 'tripal_autocomplete_feature',
    'page arguments' => array(6),
    'access arguments' => array('access content'),
    'file' => 'api/modules/tripal_chado.feature.api.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/tripal/storage/chado/auto_name/organism/%'] = array(
    'page callback' => 'tripal_autocomplete_organism',
    'page arguments' => array(6),
    'access arguments' => array('access content'),
    'file' => 'api/modules/tripal_chado.organism.api.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );

  //////////////////////////////////////////////////////////////////////////////
  //                          Controlled Vocabularies
  //////////////////////////////////////////////////////////////////////////////
  $items['admin/tripal/loaders/chado_cv'] = array(
    'title' => 'Chado Controlled Vocabularies',
    'description' => 'Controlled Vocabularies control the terms available for various chado fields.',
    'page callback' => 'tripal_cv_admin_cv_listing',
    'access arguments' => array('administer controlled vocabularies'),
    'file' => 'includes/tripal_chado.cv.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/tripal/loaders/chado_cv/edit/%'] = array(
    'title' => 'Edit a Controlled Vocabulary',
    'description' => 'Edit the details such as name and description for an existing controlled vocabulary.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_cv_cv_edit_form', 5),
    'access callback' => 'user_access',
    'access arguments' => array('administer controlled vocabularies'),
    'file' => 'includes/tripal_chado.cv.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/tripal/loaders/chado_cv/add'] = array(
    'title' => 'Add a Controlled Vocabulary',
    'description' => 'Manually a new controlled vocabulary.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_cv_cv_add_form'),
    'access callback' => 'user_access',
    'access arguments' => array('administer controlled vocabularies'),
    'file' => 'includes/tripal_chado.cv.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/tripal/loaders/chado_cv/%/cvterm/add'] = array(
    'title' => 'Add a Controlled Vocabulary Term',
    'description' => 'Add a new controlled vocabulary term.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_cv_cvterm_add_form', 4),
    'access arguments' => array('administer controlled vocabularies'),
    'file' => 'includes/tripal_chado.cv.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/tripal/loaders/chado_cv/cvterm/add'] = array(
    'title' => 'Add a Controlled Vocabulary Term',
    'description' => 'Add a new controlled vocabulary term.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_cv_cvterm_add_form'),
    'access arguments' => array('administer controlled vocabularies'),
    'file' => 'includes/tripal_chado.cv.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/tripal/loaders/chado_cv/%/cvterm/edit/%'] = array(
    'title' => 'Edit a Controlled Vocabulary Term',
    'description' => 'Edit an existing controlled vocabulary term.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_cv_cvterm_edit_form', 4, 7),
    'access arguments' => array('administer controlled vocabularies'),
    'file' => 'includes/tripal_chado.cv.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );

  //////////////////////////////////////////////////////////////////////////////
  //                           Databases
  //////////////////////////////////////////////////////////////////////////////
  $items['admin/tripal/loaders/chado_db'] = array(
    'title' => 'Chado Databases',
    'description' => 'References to External Database sites such as NCBI',
    'page callback' => 'tripal_chado_admin_db_listing',
    'access arguments' => array('administer db cross-references'),
    'file' => 'includes/tripal_chado.db.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/tripal/loaders/chado_db/edit/%'] = array(
    'title' => 'Edit a Database Reference',
    'description' => 'Edit existing Database References.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_chado_db_edit_form',6),
    'access callback' => 'user_access',
    'access arguments' => array('administer db cross-references'),
    'file' => 'includes/tripal_chado.db.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );

  $items['admin/tripal/loaders/chado_db/add'] = array(
    'title' => 'Create a Database Reference',
    'description' => 'Create a new reference to an External Database.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_chado_db_add_form'),
    'access callback' => 'user_access',
    'access arguments' => array('administer db cross-references'),
    'file' => 'includes/tripal_chado.db.inc',
    'file path' => drupal_get_path('module', 'tripal_chado'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Implements hook_permission().
 *
 * Set the permission types that the chado module uses.  Essentially we
 *  want permissionis that protect creation, editing and deleting of chado
 *  data objects
 *
 * @ingroup tripal
 */
function tripal_chado_permission() {
  return array(
    'install chado' => array(
      'title' => t('Install Chado'),
      'description' => t('Allow the user to install or upgrade a Chado database in the existing Drupal database.')
    ),
    'view chado_ids' => array(
      'title' => t('View Internal IDs'),
      'description' => t('On content pages Tripal will typically provide
          a table of information pulled from the Chado database but the
          primary key IDs for that data is typically not shown.  The
          default Tripal templates can show the primary key ID inside of a
          blue shaded table row if this permission is enabled. This can
          be useful for site developers who might want these IDs when working
          with the underlying database.'),
      'restrict access' => TRUE,
    )
  );
}

/**
 * Implements hook_theme().
 */
function tripal_chado_theme($existing, $type, $theme, $path) {
  $themes = array(

    'tripal_chado_date_combo' => array(
      'render element' => 'element',
      'file' => 'theme/tripal_chado.theme.inc',
    ),

    // Themed forms
    'tripal_pub_importer_setup_form_elements' => array(
      'render element' => 'form',
      'file' => 'includes/loaders/tripal_chado.pub_importers.inc',
    ),
    'tripal_pub_search_setup_form_elements' => array(
      'render element' => 'form',
      'file' => 'includes/loaders/tripal_chado.pub_importers.inc',
    ),
  );

  // Override the theme for each entity to use the legacy modules
  // templates.
  if (module_exists('tripal_core')) {
    $core_path = drupal_get_path('module', 'tripal_core');
    // Get the list of node types that have legacy templates.
    $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates', array());
    // Get the list of TripalEntity bundle.
    $bundles = db_select('tripal_bundle', 'tb')
      ->fields('tb')
      ->execute();

    // Iterate through all of the TripalEntity bundles and see which ones
    // map to tables that used to have Tripal v2 nodes.  For those, if the
    // legacy support is turned on then we want to use the legacy template.
    while ($bundle = $bundles->fetchObject()) {
      $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
      $vocab = $term->vocab;
      $params = array(
        'vocabulary' => $vocab->vocabulary,
        'accession' => $term->accession,
      );
      $mapped_table = chado_get_cvterm_mapping($params);

      // Do not proceed if there is not a mapped_table.
      if (!$mapped_table) {
        continue;
      }

      $chado_table = $mapped_table->chado_table;
      $legacy_template = 'legacy_template--chado_' . $chado_table;
      if (key_exists($legacy_template, $enabled_templates) && $enabled_templates[$legacy_template]) {
        $themes['TripalEntity__' . $bundle->name] = array(
          'template' => 'node--chado-generic',
          'render element' => 'entity',
          'base hook' => 'entity',
          'path' => "$core_path/theme/templates",
        );
      }
    }
  }

  return $themes;
}

/**
 * Implements hook_preprocess().
 *
 * This function is used to support legacy Tripal v2 templates
 * for use with Tripal v3 entities.
 */
function tripal_chado_preprocess(&$variables, $hook) {
  if ($hook == 'entity' and array_key_exists('TripalEntity', $variables)) {
    // The node--chado-generic template expets there to be a
    // teaser and node variables.  So, we'll add them.
    $variables['teaser'] = FALSE;
    $variables['node'] = $variables['TripalEntity'];
  }
}

/**
 * Implements hook_exclude_type_by_default()
 *
 * This hooks allows fields of a specified type that match a specified criteria
 * to be excluded by default from any table when chado_generate_var() is called.
 * Keep in mind that if fields are excluded by default they can always be
 * expanded at a later date using chado_expand_var().
 *
 * Criteria are php strings that evaluate to either TRUE or FALSE. These
 * strings are evaluated using drupal_eval() which suppresses syntax errors and
 * throws watchdog entries of type php. There are also watchdog entries of type
 * tripal stating the exact criteria evaluated. Criteria can
 * contain the following tokens:
 *   - <field_name>
 *       Replaced by the name of the field to be excluded
 *   - <field_value>
 *       Replaced by the value of the field in the current record
 * Also keep in mind that if your criteria doesn't contain the
 * &gt;field_value&lt;  token then it will be evaluated before the query is
 * executed and if the field is excluded it won't be included in the
 * query.
 *
 * @return
 *   An array of type => criteria where the type is excluded if the criteria
 *   evaluates to TRUE
 *
 * @ingroup tripal
 */
function tripal_chado_exclude_type_by_default() {
  return array('text' => 'strlen("<field_value> ") > 250');
}

/**
 * Implements hook_job_describe_args().
 *
 * Describes the arguements for the tripal_populate_mview job to allow for
 * greater readability in the jobs details pages.
 *
 * @param $callback
 *   The callback of the current tripal job (this is the function that will be
 *   executed when tripal_launch_jobs.php is run.
 * @param $args
 *   An array of arguments passed in when the job was registered.
 *
 * @return
 *   A more readable $args array
 *
 * @ingroup tripal
 */
function tripal_chado_job_describe_args($callback, $args) {
  $new_args = array();
  if ($callback == 'tripal_populate_mview') {
    // get this mview details
    $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id ";
    $results = db_query($sql, array(':mview_id' => $args[0]));
    $mview = $results->fetchObject();
    $new_args['View Name'] = $mview->name;
  }

  elseif ($callback == 'tripal_install_chado') {
    $new_args['Action'] = $args[0];
  }
  return $new_args;
}



/**
 * Remove the nid from chado_entity if it exists when the node is deleted
 */
function tripal_chado_node_delete($node) {
  $nid = $node->nid;
  $sql = "UPDATE chado_entity SET nid = NULL WHERE nid = :nid";
  //db_query($sql, array('nid' => $nid));
}


/**
 *
 * Implements hook_form_FORM_ID_alter().
 *
 * The field_ui_field_edit_form is used for customizing the settings of
 * a field attached to an entity.
 *
 * This alter function disables some of the form widgets when the storage
 * backend indicates they are not appropriate.
 */
function tripal_chado_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  // For entity fields added by Tripal Entities we don't want the
  // the end-user to change the cardinality and the required fields
  // such that record can't be saved in Chado.

  // TODO: we shouldn't check for specific field types here
  // (e.g. chaod_linker_prop). That should be done via the TripalField
  // functions.
  if ($form['#instance']['entity_type'] == 'TripalEntity') {
    if ($form['#field']['storage']['type'] == 'field_chado_storage' and
        $form['#field']['type'] != 'chado_linker__prop') {
      $form['field']['cardinality']['#access'] = FALSE;
      $form['instance']['required']['#access'] = FALSE;
    }
  }

  // TODO: don't the the maximum length be larger than the field size.
}