Jelajahi Sumber

Fix for issue #117

Stephen Ficklin 6 tahun lalu
induk
melakukan
dcdf5bafe0

+ 40 - 8
tripal_chado/api/tripal_chado.custom_tables.api.inc

@@ -26,10 +26,14 @@
  *   Set as TRUE to skip dropping and re-creation of the table.  This is
  *   useful if the table was already created through another means and you
  *   simply want to make Tripal aware of the table schema.
- *
+ * @param $is_base
+ *   Set as TRUE if the table should be considered a base table. Base tables
+ *   are those which can be used to create content types and publish records.
+ *   Default is FALSE.
  * @ingroup tripal_custom_tables_api
  */
-function chado_edit_custom_table($table_id, $table_name, $schema, $skip_if_exists = 1) {
+function chado_edit_custom_table($table_id, $table_name, $schema,
+    $skip_if_exists = 1, $is_base = FALSE) {
 
   $transaction = db_transaction();
   try {
@@ -38,6 +42,7 @@ function chado_edit_custom_table($table_id, $table_name, $schema, $skip_if_exist
     $record->table_id = $table_id;
     $record->table_name = $table_name;
     $record->schema = serialize($schema);
+    $record->is_base = $is_base;
 
     // Get the current custom table record.
     $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id";
@@ -68,12 +73,13 @@ function chado_edit_custom_table($table_id, $table_name, $schema, $skip_if_exist
 
     // Update the custom table record and run the create custom table function.
     drupal_write_record('tripal_custom_tables', $record, 'table_id');
-    $success = chado_create_custom_table($table_name, $schema, $skip_if_exists);
+    $success = chado_create_custom_table($table_name, $schema, $skip_if_exists, NULL, FALSE, $is_base);
 
     // Re-add the custom table to the semantic web interface to pick up any
     // changes in fields.
     chado_add_semweb_table($table_name);
-  } catch (Exception $e) {
+  }
+  catch (Exception $e) {
     $transaction->rollback();
     watchdog_exception('tripal_chado', $e);
     $error = _drupal_decode_exception($e);
@@ -122,6 +128,9 @@ function chado_edit_custom_table($table_id, $table_name, $schema, $skip_if_exist
  *   Optional (default: TRUE). By default this function redirects back to
  *   admin pages. However, when called by Drush we don't want to redirect. This
  *   parameter allows this to be used as a true API function.
+ * @param $is_base
+ *   Optional (default: 0).  Indicates if this custom view is a base table.
+ *   Base tables can be used to create content types and publish records.
  *
  * @return
  *   TRUE on success, FALSE on failure.
@@ -129,7 +138,7 @@ function chado_edit_custom_table($table_id, $table_name, $schema, $skip_if_exist
  * @ingroup tripal_custom_tables_api
  */
 function chado_create_custom_table($table, $schema, $skip_if_exists = TRUE,
-                                   $mview_id = NULL, $redirect = TRUE) {
+    $mview_id = NULL, $redirect = TRUE, $is_base = 0) {
 
   if (!$table) {
     tripal_report_error('trp_ctables', TRIPAL_ERROR,
@@ -178,7 +187,7 @@ function chado_create_custom_table($table, $schema, $skip_if_exists = TRUE,
 
       // Drop the table we'll recreate it with the new schema.
       chado_query('DROP TABLE {' . $table . '}');
-      // Remove any 'referring_tables' from the array as the 
+      // Remove any 'referring_tables' from the array as the
       // db_create_table doesn't use that.
       $new_schema = $schema;
       if (array_key_exists('referring_tables', $new_schema)) {
@@ -188,13 +197,14 @@ function chado_create_custom_table($table, $schema, $skip_if_exists = TRUE,
       $recreated = 1;
     }
 
-    // Add an entry in the tripal_custom_table.
+    // Add an entry in the tripal_custom_tables.
     $record = new stdClass();
     $record->table_name = $table;
     $record->schema = serialize($schema);
     if ($mview_id) {
       $record->mview_id = $mview_id;
     }
+    $record->is_base = $is_base;
 
     // If an entry already exists then remove it.
     if ($centry) {
@@ -224,7 +234,8 @@ function chado_create_custom_table($table, $schema, $skip_if_exists = TRUE,
 
     // Add the custom table to the semantic web interface.
     chado_add_semweb_table($table);
-  } catch (Exception $e) {
+  }
+  catch (Exception $e) {
     $transaction->rollback();
     $error = $e->getMessage();
     watchdog_exception('tripal_chado', $e);
@@ -333,6 +344,27 @@ function chado_get_custom_table_id($table_name) {
   return FALSE;
 }
 
+/**
+ * Retrieves the list of custom tables that are base tables.
+ *
+ * @return
+ *   An associative array where the key and value pairs are the table names.
+ *
+ * @ingroup tripal_custom_tables_api
+ */
+function chado_get_base_custom_tables() {
+
+  $sql = "SELECT table_name FROM {tripal_custom_tables} WHERE is_base = 1";
+  $resource = db_query($sql);
+
+  foreach ($resource as $r) {
+    $tables[$r->table_name] = $r->table_name;
+  }
+
+  asort($tables);
+  return $tables;
+}
+
 /**
  * Retrieves the list of custom tables in this site.
  *

+ 7 - 1
tripal_chado/api/tripal_chado.schema.api.inc

@@ -633,7 +633,7 @@ function chado_get_base_tables() {
   // whose foreign key constraints link to two or more base table.
   $final_list = [];
   foreach ($base_tables as $i => $tablename) {
-    // A few tables break our rule and seems to look 
+    // A few tables break our rule and seems to look
     // like a linking table, but we want to keep it as a base table.
     if ($tablename == 'biomaterial' or $tablename == 'assay' or $tablename == 'arraydesign') {
       $final_list[] = $tablename;
@@ -659,6 +659,12 @@ function chado_get_base_tables() {
     }
   }
 
+  // Check for custom tables that are listed as base tables.
+  $custom_tables = array_keys(chado_get_base_custom_tables());
+  foreach ($custom_tables as $custom_table) {
+    $final_list[] = $custom_table;
+  }
+
   // Now add in the cvterm table to the list.
   $final_list[] = 'cvterm';
 

+ 30 - 2
tripal_chado/includes/tripal_chado.custom_tables.inc

@@ -131,6 +131,7 @@ function tripal_custom_tables_form($form, &$form_state = NULL, $table_id = NULL)
   // get this requested table
   $default_schema = '';
   $default_force_drop = 0;
+  $default_is_base = 0;
   if (strcmp($action, 'Edit') == 0) {
     $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id ";
     $results = db_query($sql, [':table_id' => $table_id]);
@@ -149,6 +150,10 @@ function tripal_custom_tables_form($form, &$form_state = NULL, $table_id = NULL)
     if (array_key_exists('values', $form_state)) {
       $default_schema = $form_state['values']['schema'];
       $default_force_drop = $form_state['values']['force_drop'];
+      $default_is_base = $form_state['values']['is_base'];
+    }
+    else {
+      $default_is_base = $custom_table->is_base;
     }
 
     if (!$default_schema) {
@@ -244,6 +249,17 @@ array (
     '#description' => t('Check this box if your table already exists and you would like to drop it and recreate it.'),
     '#default_value' => $default_force_drop,
   ];
+  $form['is_base'] = [
+      '#type' => 'checkbox',
+      '#title' => t('Is this a base table?'),
+      '#description' => t('Check this box if this custom table will contain records '.
+          'that can be published. For example, tables such as "feature", ' .
+          '"stock", "analysis" are all tables in Chado that contain "base" ' .
+          'records that can be published and anciallary data attached via '.
+          'fields.  If selected, site admins can create new content types ' .
+          'that resolve to this table.'),
+      '#default_value' => $default_is_base,
+  ];
   $form['schema'] = [
     '#type' => 'textarea',
     '#title' => t('Schema Array'),
@@ -341,6 +357,7 @@ function tripal_custom_tables_form_submit($form, &$form_state) {
   $table_id = $form_state['values']['table_id'];
   $schema = $form_state['values']['schema'];
   $force_drop = $form_state['values']['force_drop'];
+  $is_base = $form_state['values']['is_base'];
 
   $skip_creation = 1;
   if ($force_drop) {
@@ -353,15 +370,26 @@ function tripal_custom_tables_form_submit($form, &$form_state) {
 
 
   if (strcmp($action, 'Edit') == 0) {
-    chado_edit_custom_table($table_id, $schema_arr['table'], $schema_arr, $skip_creation);
+    chado_edit_custom_table($table_id, $schema_arr['table'], $schema_arr, $skip_creation, $is_base);
   }
   elseif (strcmp($action, 'Add') == 0) {
-    chado_create_custom_table($schema_arr['table'], $schema_arr, $skip_creation);
+    chado_create_custom_table($schema_arr['table'], $schema_arr, $skip_creation,
+        NULL, FALSE, $is_base);
   }
   else {
     drupal_set_message(t("No action performed."));
   }
 
+  if ($is_base == 1) {
+    drupal_set_message(t("The custom table is a base table. Before you can use this
+      table for content publication you must 1) be sure to map
+      the controlled vocabulary terms for the columns of this table prior
+      to creating a new content type for this table. You can set the
+      terms at !link",
+        ['!link' => l('Admin > Tripal > Data Storage > Chado > Semantic Web', 'admin/tripal/storage/chado/semweb/' . $schema_arr['table'])]
+     ));
+  }
+
   drupal_goto("admin/tripal/storage/chado/custom_tables");
 }
 

+ 26 - 1
tripal_chado/includes/tripal_chado.fields.inc

@@ -30,7 +30,7 @@ function tripal_chado_bundle_fields_info($entity_type, $bundle) {
   // Create the fields for each column in the table.
   tripal_chado_bundle_fields_info_base($info, $details, $entity_type, $bundle);
 
-  // Create custom fields.
+  // Create custom fields for all tables except custom tables.
   tripal_chado_bundle_fields_info_custom($info, $details, $entity_type, $bundle);
 
   // Create fields for linking tables.
@@ -107,7 +107,17 @@ function tripal_chado_bundle_fields_info_base(&$info, $details, $entity_type, $b
         ]), 'error');
       continue;
     }
+
     $field_name = strtolower($cvterm->dbxref_id->db_id->name . '__' . preg_replace('/[^\w]/', '_', $cvterm->name));
+
+    // For custom tables we need to prefix with a 'c_' for custom, because we
+    // our fields built for Chado may not work for custom tables.
+    $custom_tables = chado_get_base_custom_tables();
+    if (in_array($table_name, $custom_tables)) {
+      $field_name = 'c_' . $field_name;
+    }
+
+    // Stip the name down to 32 characaters because that's all Drupal allows.
     $field_name = substr($field_name, 0, 32);
 
     // Skip the primary key field.
@@ -873,8 +883,14 @@ function tripal_chado_bundle_instances_info($entity_type, $bundle) {
   ];
 
   $info = [];
+
+  // Create the field instances for each column in the table.
   tripal_chado_bundle_instances_info_base($info, $entity_type, $bundle, $details);
+
+  // Create custom field instances for all tables except custom tables.
   tripal_chado_bundle_instances_info_custom($info, $entity_type, $bundle, $details);
+
+  // Create field instances for linking tables.
   tripal_chado_bundle_instances_info_linker($info, $entity_type, $bundle, $details);
 
   return $info;
@@ -950,6 +966,15 @@ function tripal_chado_bundle_instances_info_base(&$info, $entity_type, $bundle,
       continue;
     }
     $field_name = strtolower($cvterm->dbxref_id->db_id->name . '__' . preg_replace('/[^\w]/', '_', $cvterm->name));
+
+    // For custom tables we need to prefix with a 'c_' for custom, because we
+    // our fields built for Chado may not work for custom tables.
+    $custom_tables = chado_get_base_custom_tables();
+    if (in_array($table_name, $custom_tables)) {
+      $field_name = 'c_' . $field_name;
+    }
+
+    // Stip the name down to 32 characaters because that's all Drupal allows.
     $field_name = substr($field_name, 0, 32);
 
     // Skip the primary key field.

+ 2 - 2
tripal_chado/includes/tripal_chado.semweb.inc

@@ -2214,8 +2214,8 @@ function tripal_chado_semweb_form($form, &$form_state, $chado_table = NULL) {
     ];
     $rows = [];
     foreach ($columns AS $column => $detail) {
-      // Do not show column if it's the primary key or default cv
-      if ($column != $pk && $column != $cv_default) {
+      // Do not show column if it's the primary key
+      if ($column != $pk) {
         $cvterm_id =
           db_select('chado_semweb', 'cs')
             ->fields('cs', ['cvterm_id'])

+ 28 - 1
tripal_chado/tripal_chado.install

@@ -471,7 +471,13 @@ function tripal_chado_tripal_custom_tables_schema() {
       'mview_id' => array(
         'type' => 'int',
         'not NULL' => FALSE
-      )
+      ),
+      'is_base' => array(
+        'type' => 'int',
+        'not NULL' => FALSE,
+        'size' => 'tiny',
+        'default' => 0,
+      ),
     ),
     'indexes' => array(
       'table_id' => array('table_id'),
@@ -1868,3 +1874,24 @@ function tripal_chado_update_7337(){
     'urlprefix' => 'https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id={accession}',
   ));
 }
+
+/**
+ * Adds a "is_base" column to the "tripal_custom_tables" table.
+ */
+function tripal_chado_update_7339(){
+  try {
+    $spec = [
+      'type' => 'int',
+      'not NULL' => FALSE,
+      'size' => 'tiny',
+      'default' => 0,
+    ];
+    if (!db_field_exists('tripal_custom_tables', 'is_base')) {
+      db_add_field('tripal_custom_tables', 'is_base', $spec);
+    }
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Could not perform update: '. $error);
+  }
+}

+ 16 - 1
tripal_chado/tripal_chado.views.inc

@@ -96,7 +96,7 @@ function tripal_chado_views_data_custom_tables($data) {
     ],
   ];
 
-  // Table ID
+  // MView ID
   $data['tripal_custom_tables']['mview_id'] = [
     'title' => t('Materialized View ID'),
     'help' => t('Foreign key to tripal_mviews table for the materialized view.'),
@@ -112,6 +112,21 @@ function tripal_chado_views_data_custom_tables($data) {
     ],
   ];
 
+  // Is Base
+  $data['tripal_custom_tables']['is_base'] = [
+    'title' => t('Is Base?'),
+    'help' => t('Indicates if this custom table can be used as a base table for content creation.'),
+    'field' => [
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+    ],
+    'filter' => [
+      'handler' => 'views_handler_filter_numeric',
+    ],
+    'sort' => [
+      'handler' => 'views_handler_sort',
+    ],
+  ];
 
   return $data;
 }

+ 34 - 23
tripal_chado/tripal_chado.views_default.inc

@@ -78,22 +78,22 @@ function tripal_chado_defaultview_admin_custom_tables() {
   $handler->display->display_options['header']['action_links_area']['field'] = 'action_links_area';
   $handler->display->display_options['header']['action_links_area']['label'] = 'Action Links';
   $handler->display->display_options['header']['action_links_area']['empty'] = TRUE;
-  $handler->display->display_options['header']['action_links_area']['link-1'] = [
-    'label-1' => 'Add Custom Table',
-    'path-1' => 'admin/tripal/storage/chado/custom_tables/new',
-  ];
-  $handler->display->display_options['header']['action_links_area']['link-2'] = [
-    'label-2' => '',
-    'path-2' => '',
-  ];
-  $handler->display->display_options['header']['action_links_area']['link-3'] = [
-    'label-3' => '',
-    'path-3' => '',
-  ];
-  $handler->display->display_options['header']['action_links_area']['link-4'] = [
-    'label-4' => '',
-    'path-4' => '',
-  ];
+  $handler->display->display_options['header']['action_links_area']['link-1'] = array(
+      'label-1' => 'Add Custom Table',
+      'path-1' => 'admin/tripal/storage/chado/custom_tables/new',
+  );
+  $handler->display->display_options['header']['action_links_area']['link-2'] = array(
+      'label-2' => '',
+      'path-2' => '',
+  );
+  $handler->display->display_options['header']['action_links_area']['link-3'] = array(
+      'label-3' => '',
+      'path-3' => '',
+  );
+  $handler->display->display_options['header']['action_links_area']['link-4'] = array(
+      'label-4' => '',
+      'path-4' => '',
+  );
   /* Field: Tripal Custom Tables: Custom Table ID */
   $handler->display->display_options['fields']['table_id']['id'] = 'table_id';
   $handler->display->display_options['fields']['table_id']['table'] = 'tripal_custom_tables';
@@ -107,18 +107,29 @@ function tripal_chado_defaultview_admin_custom_tables() {
   $handler->display->display_options['fields']['table_name']['id'] = 'table_name';
   $handler->display->display_options['fields']['table_name']['table'] = 'tripal_custom_tables';
   $handler->display->display_options['fields']['table_name']['field'] = 'table_name';
-  $handler->display->display_options['fields']['table_name']['label'] = 'Name';
   $handler->display->display_options['fields']['table_name']['alter']['make_link'] = TRUE;
   $handler->display->display_options['fields']['table_name']['alter']['path'] = 'admin/tripal/storage/chado/custom_tables/view/[table_id]';
+  /* Field: Tripal Custom Tables: Is Base? */
+  $handler->display->display_options['fields']['is_base']['id'] = 'is_base';
+  $handler->display->display_options['fields']['is_base']['table'] = 'tripal_custom_tables';
+  $handler->display->display_options['fields']['is_base']['field'] = 'is_base';
+  $handler->display->display_options['fields']['is_base']['alter']['alter_text'] = TRUE;
+  $handler->display->display_options['fields']['is_base']['alter']['text'] = 'Yes';
+  $handler->display->display_options['fields']['is_base']['element_label_colon'] = FALSE;
+  $handler->display->display_options['fields']['is_base']['hide_empty'] = TRUE;
+  $handler->display->display_options['fields']['is_base']['empty_zero'] = TRUE;
+  $handler->display->display_options['fields']['is_base']['separator'] = '';
   /* Field: Tripal Custom Tables: Materialized View ID */
   $handler->display->display_options['fields']['mview_id']['id'] = 'mview_id';
   $handler->display->display_options['fields']['mview_id']['table'] = 'tripal_custom_tables';
   $handler->display->display_options['fields']['mview_id']['field'] = 'mview_id';
-  $handler->display->display_options['fields']['mview_id']['label'] = 'Is MView';
+  $handler->display->display_options['fields']['mview_id']['label'] = 'Is Materialized View?';
   $handler->display->display_options['fields']['mview_id']['alter']['alter_text'] = TRUE;
   $handler->display->display_options['fields']['mview_id']['alter']['text'] = 'Yes';
+  $handler->display->display_options['fields']['mview_id']['element_label_colon'] = FALSE;
   $handler->display->display_options['fields']['mview_id']['hide_empty'] = TRUE;
   $handler->display->display_options['fields']['mview_id']['empty_zero'] = TRUE;
+  $handler->display->display_options['fields']['mview_id']['separator'] = '';
   /* Field: Global: Custom text */
   $handler->display->display_options['fields']['nothing']['id'] = 'nothing';
   $handler->display->display_options['fields']['nothing']['table'] = 'views';
@@ -153,11 +164,11 @@ function tripal_chado_defaultview_admin_custom_tables() {
   $handler->display->display_options['filters']['table_name']['expose']['label'] = 'Table Name';
   $handler->display->display_options['filters']['table_name']['expose']['operator'] = 'table_name_op';
   $handler->display->display_options['filters']['table_name']['expose']['identifier'] = 'table_name';
-  $handler->display->display_options['filters']['table_name']['expose']['remember_roles'] = [
-    2 => '2',
-    1 => 0,
-    3 => 0,
-  ];
+  $handler->display->display_options['filters']['table_name']['expose']['remember_roles'] = array(
+      2 => '2',
+      1 => 0,
+      3 => 0,
+  );
 
   /* Display: Page */
   $handler = $view->new_display('page', 'Page', 'page');