Browse Source

Fixed merge conflict

Stephen Ficklin 9 years ago
parent
commit
6191f447f8

+ 23 - 49
tripal_chado/includes/tripal_chado.entity.inc

@@ -1,57 +1,23 @@
 <?php
 
 /**
- *
- * Implements hook_entity_load().
+ * Implements hook_entity_presave().
  */
-function tripal_chado_entity_presave($entity, $type) {
-  // Add the Chado table and column to an entity to which
-  // data of this cvterm will map.
-  if ($type == 'TripalEntity') {
-    $bundle_id = $entity->bundle;
-    $bundle = db_select('tripal_bundle', 'tb')
-      ->fields('tb')
-      ->condition('bundle', $bundle_id)
-      ->execute()
-      ->fetchObject();
-
-    $data = unserialize($bundle->data);
-    $entity->chado_table = $data['data_table'];
-    $entity->chado_field = $data['field'];
-
-    // If we have an ID then this entity has been saved and it will
-    // also therefore have a chado_entity record.  We want to
-    // load this record so it is always part of the entity object.
-    if (property_exists($entity, 'id') and $entity->id) {
-      $details = db_select('chado_entity', 'ce')
-        ->fields('ce')
-        ->condition('entity_id', $entity->id)
-        ->execute()
-        ->fetchObject();
-
-      // Add the chado entity details to the entity in case it's needed
-      // downstream (e.g. in field widget construction).
-      $entity->chado_entity_id = $details->chado_entity_id;
-
-      // Add in the record.
-      $schema = chado_get_schema($entity->chado_table);
-      $pkey = $schema['primary key'][0];
-      $entity->chado_record_id = $details->record_id;
-      $entity->chado_record = chado_generate_var($entity->chado_table, array($pkey =>+ $details->record_id));
-    }
-  }
+function tripal_chado_entity_presave($entity, $type) { }
+
 }
 /**
- *
- * @param $entity
- * @param $type
+ * Implements hook_entity_postsave().
  */
 function tripal_chado_entity_postsave($entity, $type) {
-  // Set the title for this entity using the chado data.
-  $title = chado_get_entity_title($entity);
+  
+  // 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, $title);
+  $ec->setTitle($entity);
+  
 }
+
 /**
  *
  * Implements hook_entity_load().
@@ -60,6 +26,14 @@ function tripal_chado_entity_load($entities, $type) {
 
 }
 
+/**
+ *
+ * Implements hook_entity_insert().
+ */
+function tripal_entities_entity_insert($entity, $type) {
+
+}
+
 /**
  *
  * Implements hook_entity_update().
@@ -72,12 +46,12 @@ function tripal_chado_entity_update($entity, $type) {
  *
  * Implements hook_entity_delete().
  */
-function tripal_chado_entity_delete($entity, $type) {
+function tripal_chaddo_entity_delete($entity, $type) {
   $record = db_select('chado_entity', 'ce')
-    ->fields('ce', array('chado_entity_id', 'data_table', 'record_id'))
-    ->condition('entity_id', $entity->id)
-    ->execute()
-    ->fetchObject();
+   ->fields('ce', array('chado_entity_id', 'data_table', 'record_id'))
+   ->condition('entity_id', $entity->id)
+   ->execute()
+   ->fetchObject();
 
   if ($record && property_exists($record, 'chado_entity_id')) {
     // Delete the corresponding record in Chado

+ 67 - 69
tripal_entities/api/tripal_entities.api.inc

@@ -1,54 +1,5 @@
 <?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).
  *
@@ -106,35 +57,84 @@ function tripal_create_bundle($namespace, $term_id, $term_name, &$error = '') {
   cache_clear_all("entity_info:$langcode", 'cache');
   variable_set('menu_rebuild_needed', TRUE);
 
-
   // Allow modules to now add fields to the bundle
   module_invoke_all('add_bundle_fields', 'TripalEntity', $bundle_id, $term);
 
   return TRUE;
 }
 
-
 /**
- * Allows a module to add key/value pairs about a bundle.
+ * Get Page Title Format for a given Tripal Entity Type.
  *
- * If a module needs to associate variables with a particular TripalEntity
- * type (bundle), it can do so by setting the $bundle_data array passed into
- * this function.  This hook is called prior to creation of a new entity type.
+ * @param TripalBundle $entity
+ *   The Entity object for the Tripal Bundle the title format is for.
+ */
+function tripal_get_title_format($entity) {
+
+  // Title formats are saved as Tripal Bundle Variables.
+  // Therefore, first we need the variable_id for title_formats.
+  $variable_id = db_select('tripal_variables', 'v')
+    ->fields('v', array('variable_id'))
+    ->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'))
+    ->condition('var.bundle_id', $entity->id)
+    ->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.
+  // @TODO: make this chado independant.
+  if (!$title_format) {
+    $title_format = chado_node_get_unique_constraint_format($entity->label);
+    tripal_save_title_format($entity, $title_format);
+  }
+  
+  return $title_format;
+}
+
+/**
+ * Save Page Title Format for a given Tripal Entity Type.
  *
- * @param $bundle_data
- *   An array for key/value pairs to be associated with a bundle.
- * @param $bundle_id
- *   The ID for the bundle.
- * @param $cvterm
- *   The CV term object that the bundle represents.
+ * @param TripalBundle $entity
+ *   The Entity object for the Tripal Bundle the title format is for.
+ * @param string $format
+ *   The pattern to be used when generating entity titles for the above type.
  */
-function hook_tripal_bundle_data_alter(&$bundle_data, $bundle_id, $cvterm) {
-  // Get the cvterm for this entity type.
-  $bundle_id = $entity->bundle;
-  $cvterm_id = preg_replace('/bio-data_/', $bundle_id);
-  $cvterm = tripal_get_cv(array('cvterm_id' => $cvterm_id));
+function tripal_save_title_format($entity, $format) {
+
+  // Title formats are saved as Tripal Bundle Variables.
+  // Thus first we need to grab the variable_id for title_format.
+  $variable_id = db_select('tripal_variables', 'v')->fields('v', array('variable_id'))->condition('name', 'title_format')->execute()->fetchField();
+
+  // And then we need to write the new format to the tripal_bundle_variables table.
+  $record = array(
+    'bundle_id' => $entity->id,
+    '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'))
+    ->condition('var.bundle_id', $record['bundle_id'])
+    ->condition('var.variable_id', $record['variable_id'])
+    ->execute()
+    ->fetchField();
+  if ($bundle_variable_id) {
+    $record['bundle_variable_id'] = $bundle_variable_id;
+    drupal_write_record('tripal_bundle_variables', $record, 'bundle_variable_id');
+  }
+  else {
+    drupal_write_record('tripal_bundle_variables', $record);
+  }
 
-  // Add any key/value pairs to the $bundle_data array as desired.
 }
 
 /**
@@ -198,5 +198,3 @@ function hook_vocab_select_term_form(&$form, &$form_state) {
 function hook_vocab_select_term_form_validate($form, &$form_state) {
 
 }
-
-

+ 17 - 9
tripal_entities/includes/TripalBundleUIController.inc

@@ -88,10 +88,6 @@ function tripal_entities_tripal_bundle_form($form, &$form_state, $entityDataType
       'type' => 'inline',
     ),
   );
-
-  //dpm($form_state, 'form state');
-  //dpm($entity_type, 'entity type');
-  //dpm($cvterm, 'cvterm');
   
   $form['vocab'] = array(
     '#type' => 'select',
@@ -117,8 +113,8 @@ function tripal_entities_tripal_bundle_form($form, &$form_state, $entityDataType
   
   // Set Title Format.
   //-------------------------
-  $title_format = chado_node_get_unique_constraint_format($chado_basetable);
-  
+  $title_format = tripal_get_title_format($entity_type);
+
   $form['set_titles'] = array(
     '#type' => 'fieldset',
     '#title' => t('Page Title options'),
@@ -220,9 +216,21 @@ function tripal_entities_tripal_bundle_form_validate($form, $form_state) {
 /**
  * Submit: Tripal content type edit form.
  */
-function tripal_entities_tripal_bundle_form_submit($form, $form_state) {
-
-  //dpm($form_state, 'form state in submit');
+function tripal_entities_tripal_bundle_form_submit($form, &$form_state) {
+  
+  if ($form_state['triggering_element']['#value'] == 'Save Content Type') {
+    // Save the page title format.
+    tripal_save_title_format(
+      $form_state['build_info']['args'][0], 
+      $form_state['values']['set_titles']['title_format']
+    );
+    
+    $form_state['redirect'] = 'admin/structure/bio-data';
+    drupal_set_message(t('Successfully saved %type content type.', array('%type' => $form_state['build_info']['args'][0]->label)));
+  }
+  else {
+    drupal_set_message(t('%button is not supported at this time.', array('%button' => $form_state['triggering_element']['#value'])), 'warning');
+  }
 }
 
 /**

+ 39 - 7
tripal_entities/includes/TripalEntityController.inc

@@ -81,13 +81,45 @@ class TripalEntityController extends EntityAPIController {
    * @param $entity
    * @param $title
    */
-  public function setTitle($entity, $title) {
-    db_update('tripal_entity')
-      ->fields(array(
-        'title' => $title
-      ))
-      ->condition('id', $entity->id)
-      ->execute();
+  public function setTitle($entity, $title = NULL) {
+
+    // 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]);
+            $value = render($field_render_arr);
+          }
+          $title = str_replace($token, $value, $title);
+        }
+      }
+    }
+    
+    // As long as we were able to determine a title, we should update it ;-).
+    if ($title) {
+      db_update('tripal_entity')
+        ->fields(array(
+          'title' => $title
+        ))
+        ->condition('id', $entity->id)
+        ->execute();
+    }
   }
 
   /**

+ 67 - 0
tripal_entities/tripal_entities.install

@@ -15,6 +15,10 @@ function tripal_entities_schema() {
   $schema['tripal_term'] = tripal_entities_tripal_term_schema();
   $schema['tripal_entity'] = tripal_entities_tripal_entity_schema();
   $schema['tripal_bundle'] = tripal_entities_tripal_bundle_schema();
+  
+  // Adds a table for additional information related to bundles.
+  $schema['tripal_bundle_variables'] = tripal_entities_tripal_bundle_variables_schema();
+
 
   return $schema;
 }
@@ -273,3 +277,66 @@ function tripal_entities_tripal_bundle_schema() {
   );
   return $schema;
 }
+
+/**
+ * Additional Tripal Bundle Information.
+ *
+ * This table is used for storing any additonal information describing
+ * a tripal bundle. For example, this is a good place to store title/url formats.
+ */
+function tripal_entities_tripal_bundle_variables_schema() {
+
+  $schema = array(
+    'description' => 'This table is used for storing any additonal information describing
+      a tripal bundle. For example, this is a good place to store title/url formats.',
+    'fields' => array (
+      'bundle_variable_id' => array (
+        'type' => 'serial',
+        'not null' => TRUE,
+      ),
+      'bundle_id' => array (
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'variable_id' => array (
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'value' => array (
+        'type' => 'text',
+        'not null' => FALSE,
+      ),
+      'rank' => array (
+        'type' => 'int',
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+    ),
+    'primary key' => array (
+      0 => 'bundle_variable_id',
+    ),
+    'unique keys' => array (
+      'tripal_bundle_variables_c1' => array (
+        0 => 'bundle_id',
+        1 => 'variable_id',
+        2 => 'rank',
+      ),
+    ),
+    'indexes' => array (
+      'tripal_bundle_variables_idx1' => array (
+        0 => 'variable_id',
+      ),
+    ),
+    'foreign keys' => array (
+      'tripal_variables' => array (
+        'table' => 'tripal_variables',
+        'columns' => array (
+          'variable_id' => 'variable_id',
+        ),
+      ),
+    ),
+  );
+  
+  return $schema;
+}
+