Quellcode durchsuchen

Created 7.x-3.x branch, added missing file

Stephen Ficklin vor 9 Jahren
Ursprung
Commit
b84eeb8ec5

+ 4 - 3
tripal_chado/includes/tripal_chado.entity.inc

@@ -3,19 +3,20 @@
 /**
  * Implements hook_entity_presave().
  */
-function tripal_chado_entity_presave($entity, $type) { }
+function tripal_chado_entity_presave($entity, $type) {
 
 }
+
 /**
  * Implements hook_entity_postsave().
  */
 function tripal_chado_entity_postsave($entity, $type) {
-  
+
   // Set the title for this entity.
   // This needs to be done post save because it uses the saved data and a format.
   $ec = new TripalEntityController($entity->type);
   $ec->setTitle($entity);
-  
+
 }
 
 /**

+ 131 - 0
tripal_chado/includes/tripal_chado.term_storage.inc

@@ -0,0 +1,131 @@
+<?php
+/**
+ * Implements hook_vocab_storage_info().
+ */
+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_select_term_form().
+ */
+function tripal_chado_vocab_select_term_form($form, &$form_state) {
+  $term_name = '';
+  $num_terms = 0;
+  $cv_id = '';
+  $terms = array();
+
+  // Set defaults using the form state.
+  if (array_key_exists('storage', $form_state)) {
+    if (array_key_exists('terms', $form_state['storage'])) {
+      $terms = $form_state['storage']['terms'];
+    }
+  }
+  $num_terms = count($terms);
+
+  // If no term has been selected yet then provide the auto complete field.
+  if ($num_terms == 0) {
+    $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/vocab/cvterm/auto_name/$cv_id/",
+    );
+  }
+
+  // If the term belongs to more than one vocabulary then add additional fields
+  // to let the user select the vocabulary.
+  if ($num_terms > 1) {
+    $form['term_name'] = array(
+      '#type' => 'hidden',
+      '#value' => $term_name,
+    );
+
+    $cvs = array();
+    foreach ($terms as $term) {
+      $cvs[$term->cv_id->cv_id] = 'Vocabulary: <b>' . $term->cv_id->name . '</b> (' . $term->cv_id->definition . ')<br>' . $term->name . ': ' . $term->definition;
+    }
+    $form['cv_id'] = array(
+      '#type' => 'radios',
+      '#title' => t('Select the appropriate vocabulary'),
+      '#options' => $cvs,
+    );
+  }
+
+  // Add in the button for the cases of no terms or too many.
+  $form['select_button'] = array(
+    '#type' => 'submit',
+    '#value' => t('Use this term'),
+    '#name' => 'select_cvterm'
+  );
+  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'] =='select_cvterm') {
+
+    // First, make sure the term is unique. If not then we can't check it.
+    $term_name = NULL;
+    $cv_id = NULL;
+    $cvterm = NULL;
+    $term_id = NULL;
+    if (array_key_exists('term_name', $form_state['values'])) {
+      $term_name = $form_state['input']['term_name'];
+    }
+    if (array_key_exists('cv_id', $form_state['input'])) {
+      $cv_id = $form_state['input']['cv_id'];
+    }
+
+    // If a term and $cv_id are provided then we can look for the term using
+    // both and we should find a unique term. If only ther term is provided
+    // we can still look for a unique term but there must only be one.
+    if ($term_name and !$cv_id) {
+      $match = array(
+        'name' => $term_name,
+      );
+    }
+    else {
+      $match = array(
+        'name' => $term_name,
+        'cv_id' => $cv_id,
+      );
+    }
+    $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
+    $form_state['storage']['terms'] = $terms;
+
+    // If we do not have any terms then the term provided by the user does not
+    // exists and we need to provide an error message.
+    if (count($terms) == 0) {
+      form_set_error('term_name', t('The term does not exist in this database.'));
+    }
+    // If we have more than one term then we need to set an error so that the
+    // form can provide a list of vocabularies to select from.
+    if (count($terms) > 1) {
+      form_set_error('term_name', t('The term is not unique. A list of vocabularies
+        that contain this term. Please select the most appropriate vocabulary.'));
+    }
+    // If we have a unique term then set the namespace, term_id and name.
+    if (count($terms) == 1) {
+      $form_state['storage']['namespace'] = $terms[0]->dbxref_id->db_id->name;
+      $form_state['storage']['term_id']   = $terms[0]->dbxref_id->accession;
+      $form_state['storage']['term_name'] = $terms[0]->name;
+    }
+  }
+}

+ 51 - 4
tripal_entities/api/tripal_entities.api.inc

@@ -1,5 +1,52 @@
 <?php
+/**
+ * Retrieves a TripalTerm entity that matches the given arguments.
+ *
+ * @param $namespace
+ *   The namespace for the vocabulary
+ * @param $term_id
+ *   The ID (accession) of the term in the vocabulary.
+ *
+ * @return
+ *   A TripalTerm entity object or NULL if not found.
+ */
+function tripal_load_term_entity($namespace, $term_id) {
+  $query = db_select('tripal_term', 'tt');
+  $query->join('tripal_vocab' ,'tv', 'tv.id = tt.vocab_id');
+  $query->fields('tt', array('id', 'term_id'))
+    ->fields('tv', array('namespace'))
+    ->condition('tv.namespace', $namespace)
+    ->condition('tt.term_id', $term_id);
+  $term = $query->execute()->fetchObject();
+
+  if ($term) {
+    $entity = entity_load('TripalTerm', array($term->id));
+    return reset($entity);
+  }
+  return NULL;
+}
 
+/**
+ * Retrieves a TripalVocab entity that maches the given arguments.
+ *
+ * @param $namespace
+ *
+ * @return
+ * A TripalVocab entity object or NULL if not found.
+ */
+function tripal_load_vocab_entity($namespace) {
+  $vocab = db_select('tripal_vocab', 'tv')
+    ->fields('tv')
+    ->condition('tv.namespace', $namespace)
+    ->execute()
+    ->fetchObject();
+
+  if ($vocab) {
+    $entity = entity_load('TripalVocab', array($vocab->id));
+    return reset($entity);
+  }
+  return NULL;
+}
 /**
  * Creates a new Tripal Entity type (i.e. bundle).
  *
@@ -78,7 +125,7 @@ function tripal_get_title_format($entity) {
     ->condition('name', 'title_format')
     ->execute()
     ->fetchField();
-  
+
   // Then we can check if there is already a title format for this bundle/type.
   $title_format = db_select('tripal_bundle_variables', 'var')
     ->fields('var', array('value'))
@@ -86,7 +133,7 @@ function tripal_get_title_format($entity) {
     ->condition('var.variable_id', $variable_id)
     ->execute()
     ->fetchField();
-    
+
   // If there isn't yet a title format for this bundle/type then we should
   // determine the default based on the table unique constraint and save it.
   // @TODO: Replace the label with the base table name.
@@ -95,7 +142,7 @@ function tripal_get_title_format($entity) {
     $title_format = chado_node_get_unique_constraint_format($entity->label);
     tripal_save_title_format($entity, $title_format);
   }
-  
+
   return $title_format;
 }
 
@@ -119,7 +166,7 @@ function tripal_save_title_format($entity, $format) {
     'variable_id' => $variable_id,
     'value' => $format,
   );
-  
+
   // Check whether there is already a format saved.
   $bundle_variable_id = db_select('tripal_bundle_variables', 'var')
     ->fields('var', array('bundle_variable_id'))

+ 6 - 6
tripal_entities/includes/TripalEntityController.inc

@@ -86,21 +86,21 @@ class TripalEntityController extends EntityAPIController {
     // If no title was supplied then we should try to generate one using the
     // default format set by admins.
     if (!$title) {
-      
+
       // First get the format for the title based on the bundle of the entity.
       $bundle_entity = tripal_bundle_load($entity->bundle);
       $title = tripal_get_title_format($bundle_entity);
-    
+
       // Determine which tokens were used in the format string
       if (preg_match_all('/\[\w+\.\w+\]/', $title, $matches)) {
         $used_tokens = $matches[0];
-        
+
         foreach($used_tokens as $token) {
           $field = str_replace(array('.','[',']'),array('__','',''),$token);
-          
+
           $value = '';
           if (isset($entity->{$field})) {
-            
+
             // Render the value from the field.
             $field_value = field_get_items('TripalEntity', $entity, $field);
             $field_render_arr = field_view_value('TripalEntity', $entity, $field, $field_value[0]);
@@ -110,7 +110,7 @@ class TripalEntityController extends EntityAPIController {
         }
       }
     }
-    
+
     // As long as we were able to determine a title, we should update it ;-).
     if ($title) {
       db_update('tripal_entity')