Browse Source

Added new field for the featuremap.unittype_id column

Stephen Ficklin 8 years ago
parent
commit
f66a8a4fb4

+ 3 - 0
tripal_chado/includes/TripalFields/obi__organism/obi__organism.inc

@@ -85,9 +85,12 @@ class obi__organism extends ChadoField {
     // Set some defaults for the empty record.
     $entity->{$field_name}['und'][0] = array(
       'value' => array(
+        /*
+        // Types of elements that will appear in the value array.
         $label_term => '',
         $genus_term => '',
         $species_term => '',
+        */
       ),
     );
 

+ 67 - 0
tripal_chado/includes/TripalFields/uo__unit/uo__unit.inc

@@ -0,0 +1,67 @@
+<?php
+
+class uo__unit extends ChadoField {
+
+  // The default lable for this field.
+  public static $default_label = 'Unit';
+
+  // The default description for this field.
+  public static $description = 'The unit of measurement.';
+
+  // Provide a list of instance specific settings. These can be access within
+  // the instanceSettingsForm.  When the instanceSettingsForm is submitted
+  // then Drupal with automatically change these settings for the instnace.
+  // It is recommended to put settings at the instance level whenever possible.
+  // If you override this variable in a child class be sure to replicate the
+  // term_name, term_vocab, term_accession and term_fixed keys as these are
+  // required for all TripalFields.
+  public static $default_instance_settings  = array(
+    // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
+    'term_vocabulary' => 'UO',
+    // The name of the term.
+    'term_name' => 'unit',
+    // The unique ID (i.e. accession) of the term.
+    'term_accession' => '0000000',
+    // Set to TRUE if the site admin is allowed to change the term
+    // type. This will create form elements when editing the field instance
+    // to allow the site admin to change the term settings above.
+    'term_fixed' => FALSE,
+  );
+
+  // The default widget for this field.
+  public static $default_widget = 'UO__unit_widget';
+
+  // The default formatter for this field.
+  public static $default_formatter = 'UO__unit_formatter';
+
+
+  /**
+   * @see TripalField::validate()
+   */
+  public function validate($entity_type, $entity, $field, $items, &$errors) {
+  }
+
+  /**
+   * @see TripalField::load()
+   */
+  public function load($entity, $details = array()) {
+
+    $record = $details['record'];
+    $settings = $this->instance['settings'];
+
+    $field_name = $this->field['field_name'];
+    $field_type = $this->field['type'];
+    $field_table = $this->instance['settings']['chado_table'];
+    $field_column = $this->instance['settings']['chado_column'];
+
+
+    // Set some defaults for the empty record.
+    $entity->{$field_name}['und'][0] = array(
+      'value' => '',
+    );
+
+    if ($record) {
+      $entity->{$field_name}['und'][0]['value'] = $record->unittype_id->name;
+    }
+  }
+}

+ 22 - 0
tripal_chado/includes/TripalFields/uo__unit/uo__unit_formatter.inc

@@ -0,0 +1,22 @@
+<?php
+
+class uo__unit_formatter extends ChadoFieldFormatter {
+
+  // The default lable for this field.
+  public static $default_label = 'Unit';
+
+  // The list of field types for which this formatter is appropriate.
+  public static $field_types = array('uo__unit');
+
+  /**
+   * @see TripalFieldFormatter::view()
+  */
+  public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
+    if (count($items) > 0) {
+      $element[0] = array(
+        '#type' => 'markup',
+        '#markup' => $items[0]['value'],
+      );
+    }
+  }
+}

+ 21 - 0
tripal_chado/includes/TripalFields/uo__unit/uo__unit_widget.inc

@@ -0,0 +1,21 @@
+<?php
+
+class uo__unit_widget extends ChadoFieldWidget {
+
+  // The default lable for this field.
+  public static $default_label = 'Unit';
+
+  // The list of field types for which this formatter is appropriate.
+  public static $field_types = array('uo__unit');
+
+
+  /**
+   * @see TripalFieldWidget::form()
+   */
+  public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
+
+    parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
+
+    // TODO: add a form for changing the unit types.
+  }
+}

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

@@ -57,10 +57,12 @@ function tripal_chado_entity_load($entities, $type) {
           ->condition('cb.bundle_id', $bundle->id)
           ->execute()
           ->fetchObject();
-        $bundle->data_table = $chado_bundle->data_table;
-        $bundle->type_linker_table = $chado_bundle->type_linker_table;
-        $bundle->type_column = $chado_bundle->type_column;
-        $bundle->type_id = $chado_bundle->type_id;
+        if ($chado_bundle) {
+          $bundle->data_table = $chado_bundle->data_table;
+          $bundle->type_linker_table = $chado_bundle->type_linker_table;
+          $bundle->type_column = $chado_bundle->type_column;
+          $bundle->type_id = $chado_bundle->type_id;
+        }
       }
     }
   }
@@ -184,6 +186,12 @@ function tripal_chado_tripal_default_title_format($bundle, $available_tokens) {
       'weight' => -5
     );
   }
+  if ($table == 'featuremap') {
+    $format[] = array(
+      'format' => '[schema__name]',
+      'weight' => -5
+    );
+  }
   if ($table == 'stock') {
     $format[] = array(
       'format' => '[stock__name]',

+ 56 - 3
tripal_chado/includes/tripal_chado.fields.inc

@@ -292,6 +292,21 @@ function tripal_chado_bundle_create_fields_custom(&$info, $details, $entity_type
 //       ),
 //     );
 //   }
+
+  // FEATUREMAP UNITTYPE_ID
+  if ($table_name == 'featuremap') {
+    $field_name = 'uo__unit';
+    $field_type = 'uo__unit';
+    $info[$field_name] = array(
+      'field_name' => $field_name,
+      'type' => $field_type,
+      'cardinality' => 1,
+      'locked' => TRUE,
+      'storage' => array(
+        'type' => 'field_chado_storage',
+      ),
+    );
+  }
 }
 
 /**
@@ -971,6 +986,39 @@ function tripal_chado_bundle_create_instances_custom(&$info, $entity_type, $bund
 //       ),
 //     );
 //   }
+
+
+  // FEATURE SEQLEN
+  if ($table_name == 'featuremap') {
+    $field_name = 'uo__unit';
+    $info[$field_name] = array(
+      'field_name' => $field_name,
+      'entity_type' => $entity_type,
+      'bundle' => $bundle->name,
+      'label' => 'Units',
+      'description' => 'The map\'s unit type.',
+      'required' => TRUE,
+      'settings' => array(
+        'auto_attach' => TRUE,
+        'chado_table' => $table_name,
+        'chado_column' => 'unittype_id',
+        'base_table' => $table_name,
+      ),
+      'widget' => array(
+        'type' => 'uo__unit_widget',
+        'settings' => array(
+          'display_label' => 1,
+        ),
+      ),
+      'display' => array(
+        'default' => array(
+          'label' => 'inline',
+          'type' => 'uo__unit_formatter',
+          'settings' => array(),
+        ),
+      ),
+    );
+  }
 }
 
 /**
@@ -1236,13 +1284,18 @@ function tripal_chado_bundle_create_instances_linker(&$info, $entity_type, $bund
      $tpkey = $tschema['primary key'][0];
      $pkey = $schema['primary key'][0];
      // Get the list of existing property types for this table.
+     $args = array();
      $sql = "
        SELECT DISTINCT P.type_id
        FROM {" . $prop_table . "} P
-         INNER JOIN {" . $table_name . "} T on T.$tpkey = P.$tpkey
-       WHERE T.type_id = :cvterm_id
      ";
-     $args = array(':cvterm_id' => $cvterm_id);
+     if ($type_field) {
+       $sql .= "
+           INNER JOIN {" . $table_name . "} T on T.$tpkey = P.$tpkey
+         WHERE T.type_id = :cvterm_id
+       ";
+       $args[':cvterm_id'] = $cvterm_id;
+     }
      $props = chado_query($sql, $args);
      while ($prop = $props->fetchObject()) {
        $term = chado_generate_var('cvterm', array('cvterm_id' => $prop->type_id));

+ 1 - 2
tripal_ws/includes/tripal_ws.rest_v0.1.inc

@@ -694,7 +694,7 @@ function tripal_ws_services_v0_1_get_content($api_url, &$response, $ws_path, $ct
       FIELD_LOAD_CURRENT, array('field_id' => $field['id']));
 
     tripal_ws_services_v0_1_get_content_add_field($key_adj, $entity, $field, $instance, $api_url, $response, TRUE);
-
+    tripal_ws_services_v0_1_write_context($response, $ctype);
     return;
   }
 
@@ -728,7 +728,6 @@ function tripal_ws_services_v0_1_get_content($api_url, &$response, $ws_path, $ct
   $response['itemPage'] = url('/bio_data/' . $entity->id, array('absolute' => TRUE));
 
   tripal_ws_services_v0_1_get_content_add_fields($entity, $bundle, $api_url, $response, $ws_path, $ctype, $entity_id, $params);
-
   tripal_ws_services_v0_1_write_context($response, $ctype);
 }