Quellcode durchsuchen

Trying to rework support of TripalFields and views'

Stephen Ficklin vor 8 Jahren
Ursprung
Commit
0c753c2810

+ 78 - 78
legacy/tripal_views/tripal_views.views.inc

@@ -324,90 +324,90 @@ function tripal_views_views_data() {
     // only add joins if this is a base table
     // this keeps the list of available fields manageable
     // all other tables should be added via relationships
-      $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = :setup";
-      $joins = db_query($sql, array(':setup' => $setup_id));
-      if (!isset($joins)) {
-        $joins = array();
-      }
-      foreach ($joins as $join) {
-        $left_table = $join->left_table;
-        $left_field = $join->left_field;
-        $base_table = $join->base_table;
-        $base_field = $join->base_field;
-        $handler = $join->handler;
-        $base_title = ucwords(str_replace('_', ' ', $base_table));
-        $left_title = ucwords(str_replace('_', ' ', $left_table));
-
-        // if the 'node' table is in our integrated list then
-        // we want to skip it. It shouldn't be there.
-        if ($left_table == 'node') {
-          continue;
-        }
-
-        // add join entry
-        if (!$join->relationship_only) {
-          $data[$left_table]['table']['join'][$base_table] = array(
-            'left_field' => $base_field,
-            'field' => $left_field,
-          );
-          if ($handler) {
-            $data[$left_table]['table']['join'][$base_table]['handler'] = $handler;
-          }
-          if (!empty($join->arguments)) {
-            array_merge($data[$left_table]['table']['join'][$base_table], unserialize($join->arguments));
-          }
-        }
+    $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = :setup";
+    $joins = db_query($sql, array(':setup' => $setup_id));
+    if (!isset($joins)) {
+      $joins = array();
+    }
+    foreach ($joins as $join) {
+      $left_table = $join->left_table;
+      $left_field = $join->left_field;
+      $base_table = $join->base_table;
+      $base_field = $join->base_field;
+      $handler = $join->handler;
+      $base_title = ucwords(str_replace('_', ' ', $base_table));
+      $left_title = ucwords(str_replace('_', ' ', $left_table));
 
-        // warn if deprecated method of relationship addition was used (ie: through handlers)
-        if (isset($data[$base_table][$base_field]['relationship'])) {
-          tripal_report_error('tripal_views', TRIPAL_NOTICE,
-            'DEPRECATED: Currently using tripal_views_handlers to store relationship for %base => %left when you should be using tripal_views_joins.',
-            array('%base' => $base_table, '%left' => $left_table));
-        }
+      // if the 'node' table is in our integrated list then
+      // we want to skip it. It shouldn't be there.
+      if ($left_table == 'node') {
+        continue;
+      }
 
-        // Add relationship entry.
-        // NOTE: we use a fake field name to allow us to have multiple
-        // relationships for the same field (ie: feature.feature_id has many
-        // Many relationships but views only supports a single one).
-        $fake_field = $base_field . '_to_' . $left_table;
-
-        // Bug Fix: The old $fake_field used above doesn't take into account
-        // multiple relationships to the same left table. To keep backwards
-        // compatibility, this old fake_field needs to continue to be used for
-        // the LAST recorded relationship. However, for all previously set
-        // relationships we can use an improved fake name which takes into
-        // account the left field and ensures all relationships for a single
-        // left table are not condensed into a single relationship.
-        if (array_key_exists($fake_field, $data[$base_table])) {
-
-          // Again, note that we can't just change the fake_name after finding
-          // there is more than one relationship because then the FIRST
-          // relationship would keep the old fake_name rather than the LAST
-          // which keeps backwards compatiblity since the old naming caused all
-          // previous relationships be be overridden by the next one.
-          // Thus we first need to determine the left field of the previous
-          // join for this table combination and then use that to form our
-          // improved fake field.
-          $previous_left_field = $data[$base_table][$fake_field]['relationship']['base field'];
-          $improved_fake_field = $base_field . '_to_' . $left_table . "." . $previous_left_field;
-          $data[$base_table][$improved_fake_field] = $data[$base_table][$fake_field];
-        }
-        $data[$base_table][$fake_field] = array(
-          'title' => "$base_title.$base_field => $left_title.$left_field",
-          'help' => t("Joins @base to @left", array('@base' => "$base_title.$base_field", '@left' => "$left_title.$left_field")),
-          'relationship' => array(
-            'handler' => $join->relationship_handler,
-            'title' => t("$base_field => $left_title ($left_field)"),
-            'label' => t("$base_field => $left_title ($left_field)"),
-            'real field' => $base_field,
-            'base' => $left_table,
-            'base field' => $left_field
-          )
+      // add join entry
+      if (!$join->relationship_only) {
+        $data[$left_table]['table']['join'][$base_table] = array(
+          'left_field' => $base_field,
+          'field' => $left_field,
         );
+        if ($handler) {
+          $data[$left_table]['table']['join'][$base_table]['handler'] = $handler;
+        }
         if (!empty($join->arguments)) {
-          array_merge($data[$base_table][$fake_field]['relationship'], unserialize($join->arguments));
+          array_merge($data[$left_table]['table']['join'][$base_table], unserialize($join->arguments));
         }
       }
+
+      // warn if deprecated method of relationship addition was used (ie: through handlers)
+      if (isset($data[$base_table][$base_field]['relationship'])) {
+        tripal_report_error('tripal_views', TRIPAL_NOTICE,
+          'DEPRECATED: Currently using tripal_views_handlers to store relationship for %base => %left when you should be using tripal_views_joins.',
+          array('%base' => $base_table, '%left' => $left_table));
+      }
+
+      // Add relationship entry.
+      // NOTE: we use a fake field name to allow us to have multiple
+      // relationships for the same field (ie: feature.feature_id has many
+      // Many relationships but views only supports a single one).
+      $fake_field = $base_field . '_to_' . $left_table;
+
+      // Bug Fix: The old $fake_field used above doesn't take into account
+      // multiple relationships to the same left table. To keep backwards
+      // compatibility, this old fake_field needs to continue to be used for
+      // the LAST recorded relationship. However, for all previously set
+      // relationships we can use an improved fake name which takes into
+      // account the left field and ensures all relationships for a single
+      // left table are not condensed into a single relationship.
+      if (array_key_exists($fake_field, $data[$base_table])) {
+
+        // Again, note that we can't just change the fake_name after finding
+        // there is more than one relationship because then the FIRST
+        // relationship would keep the old fake_name rather than the LAST
+        // which keeps backwards compatiblity since the old naming caused all
+        // previous relationships be be overridden by the next one.
+        // Thus we first need to determine the left field of the previous
+        // join for this table combination and then use that to form our
+        // improved fake field.
+        $previous_left_field = $data[$base_table][$fake_field]['relationship']['base field'];
+        $improved_fake_field = $base_field . '_to_' . $left_table . "." . $previous_left_field;
+        $data[$base_table][$improved_fake_field] = $data[$base_table][$fake_field];
+      }
+      $data[$base_table][$fake_field] = array(
+        'title' => "$base_title.$base_field => $left_title.$left_field",
+        'help' => t("Joins @base to @left", array('@base' => "$base_title.$base_field", '@left' => "$left_title.$left_field")),
+        'relationship' => array(
+          'handler' => $join->relationship_handler,
+          'title' => t("$base_field => $left_title ($left_field)"),
+          'label' => t("$base_field => $left_title ($left_field)"),
+          'real field' => $base_field,
+          'base' => $left_table,
+          'base field' => $left_field
+        )
+      );
+      if (!empty($join->arguments)) {
+        array_merge($data[$base_table][$fake_field]['relationship'], unserialize($join->arguments));
+      }
+    }
   }
 
   return $data;

+ 10 - 9
tripal/includes/TripalEntityViewsController.inc

@@ -22,7 +22,7 @@ class TripalEntityViewsController extends EntityDefaultViewsController {
     $data['tripal_entity']['changed']['filter']['handler'] = 'views_handler_filter_date';
     $data['tripal_entity']['changed']['help'] = t('The date that the content was last updated.');
 
-    $data['tripal_entity']['id']['title'] = 'Tripal Content ID';
+    $data['tripal_entity']['id']['title'] = 'tid';
     $data['tripal_entity']['id']['help'] = 'The unique numeric ID for the content';
 
 
@@ -35,16 +35,17 @@ class TripalEntityViewsController extends EntityDefaultViewsController {
     $data['tripal_entity']['status']['field']['handler'] = 'tripal_views_handler_field_entity_status';
 
 
-    $data['tripal_entity']['uid']['relationship'] = array(
-      'base' => 'users',
-      'base field' => 'uid',
-      'handler' => 'views_handler_relationship',
-      'label' => t('Users'),
-      'title' => t('Users'),
-      'help' => t('Associates the user information with this record.')
+    // We want to use our own query plugin.
+    $data['tripal_entity']['table']['base']['query class'] = 'tripal_views_query';
+
+    // Join with the User's table.
+    $data['users']['table']['join']['tripal_entity'] = array(
+      'handler' => 'views_join',
+      'left_field' => 'uid',
+      'field' => 'uid',
     );
 
-    // It is not intended that the following fields will every be used by the
+    // It is not intended that the following fields will ever be used by the
     // end-user within Views.
     unset($data['tripal_entity']['bundle']);
     unset($data['tripal_entity']['term_id']);

+ 4 - 4
tripal/includes/TripalTermViewsController.inc

@@ -10,13 +10,13 @@ class TripalTermViewsController extends EntityDefaultViewsController {
   public function views_data() {
     $data = parent::views_data();
 
-    $data['tripal_term']['table']['group'] = 'Tripal Content Type';
+    $data['tripal_term']['table']['group'] = 'Tripal Term';
 
-    $data['tripal_term']['accession']['title'] = 'Term ID';
+    $data['tripal_term']['accession']['title'] = 'Term Accession';
     $data['tripal_term']['accession']['help'] = t('The unique term ID (accession) of the vocabulary term to which a Tripal content type is asscoiated.');
 
-    $data['tripal_term']['name']['title'] = 'Content Type';
-    $data['tripal_term']['name']['help'] = t('The name of a Tripal content type.');
+    $data['tripal_term']['name']['title'] = 'Term Name';
+    $data['tripal_term']['name']['help'] = t('The term that a content type is associated with.');
     $data['tripal_term']['name']['filter']['handler'] = 'tripal_views_handler_filter_string_selectbox';
 
     $data['tripal_term']['vocab_id']['title'] = t('Vocabulary ID');

+ 8 - 1
tripal/includes/TripalVocabViewsController.inc

@@ -10,12 +10,19 @@ class TripalVocabViewsController extends EntityDefaultViewsController {
   public function views_data() {
     $data = parent::views_data();
 
-    $data['tripal_vocab']['table']['group'] = 'Tripal Content Vocabulary';
+    $data['tripal_vocab']['table']['group'] = 'Tripal Term Vocabulary';
 
     $data['tripal_vocab']['vocabulary']['title'] = 'Vocabulary';
     $data['tripal_vocab']['vocabulary']['help'] = t('The short name of the vocabulary.');
 
 
+    // Join the term to it's entity type.
+    $data['tripal_vocab']['table']['join']['tripal_term'] = array(
+      'handler' => 'views_join',
+      'left_field' => 'vocab_id',
+      'field' => 'id',
+    );
+
     // It is not intended that the following fields will every be used by the
     // end-user.
     unset($data['tripal_vocab']['id']);

+ 1 - 0
tripal/includes/tripal.entity.inc

@@ -200,6 +200,7 @@ function tripal_entity_info() {
     'label' => 'Tripal Content Type',
     'entity class' => 'TripalBundle',
     'controller class' => 'TripalBundleController',
+    'views controller class' => 'TripalBundleViewsController',
     'base table' => 'tripal_bundle',
     'fieldable' => FALSE,
     'bundle of' => 'TripalEntity',

+ 1 - 0
tripal/tripal.module

@@ -24,6 +24,7 @@ require_once "includes/TripalEntityViewsController.inc";
 require_once "includes/TripalBundle.inc";
 require_once "includes/TripalBundleController.inc";
 require_once "includes/TripalBundleUIController.inc";
+require_once "includes/TripalBundleViewsController.inc";
 require_once "includes/TripalFields/TripalField.inc";
 require_once "includes/TripalFields/TripalFieldWidget.inc";
 require_once "includes/TripalFields/TripalFieldFormatter.inc";

+ 11 - 9
tripal/tripal.views.inc

@@ -23,7 +23,6 @@ function tripal_views_plugins() {
 
 /**
  * Describe various Tripal Core systems to Views
- *   for the creation of administrative views.
  *
  * @ingroup tripal
  */
@@ -53,12 +52,12 @@ function tripal_entity_views_data() {
   while ($bundle = $bundles->fetchObject()) {
 
     // Each bundle gets it's own "table".
-    $data[$bundle->name]['table']['group'] = t('Tripal ' . $bundle->label . ' page');
-    $data[$bundle->name]['table']['base'] = array(
-      'query class' => 'tripal_views_query',
-      'title' => t('Tripal ' . $bundle->label . ' page'),
-      'help' => t('Searches Tripal ' . $bundle->label . ' pages'),
-    );
+//     $data['tripal_entity']['table']['group'] = t($bundle->label);
+//     $data[$bundle->name]['table']['base'] = array(
+//       'query class' => 'tripal_views_query',
+//       'title' => t($bundle->label),
+//       'help' => t('Tripal ' . $bundle->label . ' pages'),
+//     );
 
     // Now add the fields to the bundle.
     $instances = field_info_instances('TripalEntity', $bundle->name);
@@ -79,9 +78,11 @@ function tripal_entity_views_data() {
         $field_handler = 'views_handler_field';
       }
 
-      $data[$bundle->name][$field_name] = array(
+      $data['tripal_entity'][$field_name] = array(
+        'group' => $bundle->label,
         'title' => $instance['label'],
-        'help' => $instance['description'],
+        'help' => t(' Appears in: @bundles.', array('@bundles' => $bundle->label)) . $instance['description'],
+        'handler' => '',
         'field' => array(
           'handler' => $field_handler,
           'click sortable' => $click_sortable,
@@ -95,6 +96,7 @@ function tripal_entity_views_data() {
       );
     }
   }
+
   return $data;
 }
 

+ 44 - 16
tripal/tripal_views_query.inc

@@ -1,40 +1,68 @@
 <?php
-/**
- * @file
- * Views query plugin for Apache Solr Views.
- * Gets its data not from the database, but from a Solr server.
- */
 
-class tripal_views_query extends views_plugin_query {
+class tripal_views_query extends views_plugin_query_default {
 
-  public function add_field($table_alias, $field, $alias = '', $params = array()) {
-    // Make sure an alias is assigned.
-    $alias = $alias ? $alias : $field;
-    return $alias;
-  }
+  private $attach_fields = array();
 
   /**
-   * We don't support construction of SQL queries.
-   * @param $get_count
+   *
    */
-  function query($get_count = FALSE) {
+  public function init($base_table = 'tripal_entity', $base_field = 'id', $options) {
+    parent::init($base_table, $base_field, $options);
+
+    // Always add the TripaEntity id and the bundle name so we can keep
+    // track of which rows are in the SQL results
+    $this->add_field('tripal_entity', 'id', 'tripal_entity_id');
+    $this->add_field('tripal_bundle', 'name', 'tripal_bundle_name');
 
   }
+  /**
+   *
+   */
+  public function add_field($table_alias, $field, $alias = '', $params = array()) {
+    // If this is not a TripalField then allow the add_field to work as usual.
+    $f = field_info_field($field);
+    if (!$f) {
+      return parent::add_field($table_alias, $field, $alias, $params);
+    }
+    else {
+      $this->attach_fields[] = $f['id'];
+    }
+  }
+
   /**
    *
    * @param  $view
    */
   function execute(&$view) {
+
+    // First execute any SQL that needs to run.
+    parent::execute($view);
+
+    // Add in the entity object to the results.
+    for ($i = 0; $i < count($view->result); $i++) {
+      $entity_id = $view->result[$i]->tripal_entity_id;
+      $bundle_name = $view->result[$i]->tripal_bundle_name;
+      $ids = array($entity_id, 0, $bundle_name);
+      $entity = entity_create_stub_entity('TripalEntity', $ids);
+      foreach ($this->attach_fields as $fid) {
+        $options = array('field_id' => $fid);
+        field_attach_load('TripalEntity', array($entity_id => $entity), FIELD_LOAD_CURRENT, $options);
+      }
+      $view->result[$i]->entity = $entity;
+    }
+    return;
+
     // The base table indicates our content type.
     $base_table = $view->base_table;
 
     // Get the bundle that the view base table represents.
-    $bundle = tripal_load_bundle_entity(array('name' => $view->base_table));
+//    $bundle = tripal_load_bundle_entity(array('name' => $view->base_table));
 
     // The base table for the view is a bundle therefore the first condition
     // must be with the content_type field.
     $query = new TripalFieldQuery();
-    $query->entityCondition('entity_type', 'TripalEntity');
+//    $query->entityCondition('entity_type', 'TripalEntity');
     $query->entityCondition('bundle', $bundle->name);
 
     // Apply filters

+ 6 - 4
tripal/views_handlers/tripal_views_handler_field_entity_default_formatter.inc

@@ -17,9 +17,11 @@ class tripal_views_handler_field_entity_default_formatter extends  views_handler
     $field_name = $this->field;
     $row_index = $this->view->row_index;
     $result = $this->view->result[$row_index];
-    $entity = $result->entity;
-    $format = field_view_field('TripalEntity', $entity, $field_name);
-    $format['#label_display'] = 'hidden';
-    return $format;
+    if (property_exists($result, entity)) {
+      $entity = $result->entity;
+      $format = field_view_field('TripalEntity', $entity, $field_name);
+      $format['#label_display'] = 'hidden';
+      return $format;
+    }
   }
 }

+ 2 - 2
tripal_chado/includes/TripalFields/sbo__relationship/sbo__relationship.inc

@@ -278,8 +278,8 @@ class sbo__relationship extends ChadoField {
         $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $object_id_key] = $relationship->$object_id_key->$object_pkey;
 
         $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
-        $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->$subject_id_key->name . ' [id: ' . $relationship->$subject_id_key->$fkey_rcolumn . ']';
-        $entity->{$field_name}['und'][$i]['object_name'] = $relationship->$object_id_key->name  . ' [id: ' . $relationship->$object_id_key->$fkey_rcolumn . ']';
+        $entity->{$field_name}['und'][$i]['subject_name'] = $subject_name . ' [id: ' . $relationship->$subject_id_key->$fkey_rcolumn . ']';
+        $entity->{$field_name}['und'][$i]['object_name'] = $object_name  . ' [id: ' . $relationship->$object_id_key->$fkey_rcolumn . ']';
         if (array_key_exists('value', $schema['fields'])) {
           $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__value'] = $relationship->value;
         }

+ 2 - 3
tripal_chado/includes/TripalFields/sbo__relationship/sbo__relationship_formatter.inc

@@ -22,7 +22,7 @@ class sbo__relationship_formatter extends ChadoFieldFormatter {
     $element = array();
     $element['title'] = array(
       '#type' => 'textfield',
-      '#title' => 'Relationship Title',
+      '#title' => 'Table Header',
       '#default_value' => array_key_exists('title', $settings) ? $settings['title'] : 'Relationship',
     );
     $element['empty'] = array(
@@ -104,8 +104,7 @@ class sbo__relationship_formatter extends ChadoFieldFormatter {
       'header' => $headers,
       'rows' => $rows,
       'attributes' => array(
-        'id' => 'chado-linker--relationship-table',
-        'class' => 'tripal-data-table'
+        'id' => 'sbo--relationship-table',
       ),
       'sticky' => FALSE,
       'caption' => '',

+ 9 - 6
tripal_chado/includes/TripalFields/schema__publication/schema__publication_formatter.inc

@@ -37,12 +37,15 @@ class schema__publication_formatter extends ChadoFieldFormatter {
 
     krsort($list_items, SORT_NUMERIC);
 
-    $list = array(
-      'title' => '',
-      'items' => $list_items,
-      'type' => 'ol',
-      'attributes' => array(),
-    );
+    $list = '';
+    if (count($list_items) > 1) {
+      $list = array(
+        'title' => '',
+        'items' => $list_items,
+        'type' => 'ol',
+        'attributes' => array(),
+      );
+    }
 
     if (count($items) > 0) {
       $element[0] = array(

+ 4 - 5
tripal_ws/includes/tripal_ws.rest_v0.1.inc

@@ -831,9 +831,8 @@ function tripal_ws_services_v0_1_get_content_add_field($key, $entity, $field, $i
     $values[$i] = tripal_ws_services_v0_1_rewrite_field_items_keys($items[$i]['value'], $response, $api_url);
   }
 
-  // Add the $values array to the WS response.
-  // If we only have one value then set the response with just the value.
-  if (count($values) == 1) {
+  // If the field cardinality is 1
+  if ($field[cardinality] == 1) {
     // If the value is an array and this is the field page then all of those
     // key/value pairs should be added directly to the response.
     if (is_array($values[0])) {
@@ -853,8 +852,8 @@ function tripal_ws_services_v0_1_get_content_add_field($key, $entity, $field, $i
     }
   }
 
-  // If we have more than one value then set the response to be a collection.
-  if (count($values) > 1) {
+  // If the field cardinality is > 1
+  if ($field[cardinality] != 1) {
 
     // If this is the field page then the Collection is added directly to the
     // response, otherwise, it's added under the field $key.