Эх сурвалжийг харах

Added a new API function to add the node relationship to the views integration array and made the changes to the chado relationship to node handler and the views integration code to deal with node relationships

Lacey Sanderson 12 жил өмнө
parent
commit
6a4dce745f

+ 92 - 0
tripal_views/api/tripal_views.api.inc

@@ -505,3 +505,95 @@ function tripal_views_get_integration_array_for_chado_table($table_name, $base_t
 
   return $defn_array;
 }
+
+/**
+ * Adds the joins necessary to link a chado table to it's node counterpart
+ *
+ * @param &$defn_array
+ *   The current definition array for a given table
+ */
+function tripal_views_add_node_relationship_to_chado_table_integration($defn_array) {
+  $integrations[$defn_array['table']] = $defn_array;
+  $primary_key = $defn_array['table'] . '_id';
+  $chado_linking = 'chado_' . $defn_array['table'];
+
+  // Add table.primary_key => chado_table.primary key join to $defn_array
+  $integrations[$defn_array['table']]['fields'][$primary_key]['joins'][$chado_linking] = array(
+    'table' => $chado_linking,
+    'field' => $primary_key,
+  );
+
+  // Create chado_table defn_array
+  $integrations[$chado_linking] = array(
+    'table' => $chado_linking,
+    'type' => 'drupal',
+    'name' => 'Chado ' . $defn_array['table'] . ' to Node',
+    'description' => 'Links chado content to its drupal node counterpart',
+    'priority' => $defn_array['priority'],
+    'base_table' => FALSE,
+    'fields' => array(
+      $primary_key => array(
+        'name' => $primary_key,
+        'title' => ucwords(str_replace('_', ' ', $primary_key)),
+        'type' => 'int',
+        'description' => 'The primary key of the chado ' . $defn_array['table'] . ' table',
+        'handlers' => array(),
+        'joins' => array(
+          $defn_array['table'] => array(
+            'table' => $defn_array['table'],
+            'field' => $primary_key,
+          )
+        ),
+      ),
+      'nid' => array(
+        'name' => 'nid',
+        'title' => 'Node ID',
+        'type' => 'int',
+        'description' => 'The primary key of the drupal node table',
+        'handlers' => array(),
+        'joins' => array(
+          'node' => array(
+            'table' => 'node',
+            'field' => 'nid',
+          ),
+        ),
+      )
+    ),
+  );
+
+  // Create node defn_array
+  $integrations['node'] = array(
+    'table' => 'node',
+    'name' => 'Node',
+    'description' => 'Primary Drupal Content',
+    'priority' => $defn_array['priority'],
+    'fields' => array(
+      'nid' => array(
+        'name' => 'nid',
+        'title' => 'Node ID',
+        'type' => 'int',
+        'description' => 'the primary key of the drupal node table',
+        'handlers' => array(
+          'relationship' => array(
+            'name' => 'chado_views_handler_relationship_to_node',
+            'label' => 'Node => Chado ' . ucwords($defn_array['table']),
+            'base table' => $defn_array['table'],
+            'base field' => $primary_key
+          )
+        ),
+        'joins' => array(
+          $defn_array['table'] => array(
+            'table' => $defn_array['table'],
+            'field' => 'nid',
+          ),
+          $chado_linking => array(
+            'table' => $chado_linking,
+            'field' => 'nid',
+          ),
+        ),
+      ),
+    ),
+  );
+
+  return $integrations;
+}

+ 39 - 35
tripal_views/tripal_views.views.inc

@@ -288,6 +288,44 @@ function tripal_views_views_data() {
     else {
       $base_table = $tvi_row->table_name;
 
+      // Check if we are trying to integrate the node table
+      // if we are we want to add to a current integration
+      // as compared to create a whole new one
+      if ($base_table == 'node') {
+        // Add any joins between the node table and other tables
+        $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
+        $joins = db_query($sql, $setup_id);
+        while ($join = db_fetch_object($joins)) {
+          $left_table = $join->left_table;
+          $left_field = $join->left_field;
+          $base_field = $join->base_field;
+          $handler = $join->handler;
+
+          // add join entry
+          $data[$base_table]['table']['join'][$left_table] = array(
+            'left_field' => $left_field,
+            'field' => $base_field,
+          );
+          if ($handler) {
+            $data[$base_table]['table']['join'][$left_table]['handler'] = $handler;
+          }
+        }
+
+        // Add in any relationships for node fields
+        $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id=%d";
+        $query = db_query($sql, $setup_id);
+        while ($handler = db_fetch_object($query)) {
+          $data[$base_table][$handler->column_name][$handler->handler_type]['handler'] = $handler->handler_name;
+
+          // Add in any additional arguments
+          // This should be a serialized array including (at a minimum) name => <handler name>
+          if ($handler->arguments) {
+            $data[$base_table][$handler->column_name][$handler->handler_type] = array_merge($data[$base_table][$handler->column_name][$handler->handler_type], unserialize($handler->arguments));
+          }
+        }
+        continue;
+      }
+
       // The chado table could be a regular Chado table or a custom table
       // in the chado database.  Therefore we'll check both
       $table_desc = tripal_core_get_chado_table_schema($base_table);
@@ -378,23 +416,12 @@ function tripal_views_views_data() {
       if ($handler) {
         $data[$base_table]['table']['join'][$left_table]['handler'] = $handler;
       }
-
-      // check to see if the join table is one that correlates with Drupal nodes
-      // if so, there will be a chado_<table_name> table in the Drupal database
-      // if there is, then we need to add the linking join information
-      if (db_table_exists("chado_$left_table")) {
-        // join the mview to the linker table
-        $data[$base_table]['table']['join']["chado_$left_table"] = array(
-          'left_field' => $left_field,
-          'field' => $base_field,
-        );
-      }
     }
   }
 
   // TEMPORARY: needed to join chado base tables to node linking tables
   // currently done using old-style data arrays
-  $data = tripal_views_TEMPORARY_chado_linking_data($data);
+  //$data = tripal_views_TEMPORARY_chado_linking_data($data);
 
   return $data;
 }
@@ -430,29 +457,6 @@ function tripal_views_views_data_alter(&$data) {
             'field' => $left_field,
           );
         }
-
-        // check to see if this table is one that correlates with Drupal nodes
-        // if so, there will be a chado_<table_name> table in the Drupal database
-        // if there is, then we need to add the linking join information.  We did
-        // this step in the hook_views_data function above, but now we need
-        // to add the reciprical joins
-        if (db_table_exists("chado_$left_table")) {
-
-          // join the linker table to the mview
-          if (array_key_exists("chado_$left_table", $data)) {
-            $data["chado_$left_table"]['table']['join'][$base_table] = array(
-              'left_field' => $base_field,
-              'field' => $left_field,
-            );
-
-            // Join the node table to the view by way of the chado linker table
-            $data['node']['table']['join'][$base_table] = array(
-              'left_table' => "chado_$left_table",
-              'left_field' => 'nid',
-              'field' => 'nid',
-            );
-          }
-        }
       }
     }
 

+ 24 - 6
tripal_views/views/handlers/chado_views_handler_relationship_to_node.inc

@@ -23,12 +23,23 @@ class chado_views_handler_relationship_to_node extends views_handler_relationshi
 
     $this->ensure_my_table();
 
+    // The base table in chado
+    $base_table = $this->definition['base table'];
+    $base_field = $this->definition['base field'];
+
+    // The drupal linking table
+    $chado_table = 'chado_' . $base_table;
+
+    // Need to remove the incorrectly added node join
+    unset($this->query->table_queue['node'],
+      $this->query->tables['stock']['node']);
+
     // First add base => chado_base join
     $def = array();
-    $def['table'] = 'chado_' . $this->table;
-    $def['field'] = $this->field;
-    $def['left_table'] = $this->table;
-    $def['left_field'] = $this->field;
+    $def['table'] = $chado_table;
+    $def['field'] = $base_field;
+    $def['left_table'] = $base_table;
+    $def['left_field'] = $base_field;
     //$def['handler'] = 'views_handler_join_chado_aggregator';
 
     $join = new views_join();
@@ -37,7 +48,8 @@ class chado_views_handler_relationship_to_node extends views_handler_relationshi
     $join->construct();
     $join->adjusted = TRUE;
 
-    $this->linker_alias = $this->query->add_relationship('stock_chado_stock', $join, $this->table);
+    $alias = $base_table . '_' . $chado_table;
+    $this->linker_alias = $this->query->add_relationship($alias, $join, $this->table);
 
     // Now add chado_base => node join
     $def = array();
@@ -53,6 +65,12 @@ class chado_views_handler_relationship_to_node extends views_handler_relationshi
     $join->construct();
     $join->adjusted = TRUE;
 
-    $this->alias = $this->query->add_relationship('chado_stock_node', $join, 'node');
+    if ($base_table == $this->view->base_table) {
+      $alias = 'node';
+    }
+    else {
+      $alias = $chado_table . '_node';
+    }
+    $this->alias = $this->query->add_relationship($alias, $join, 'node');
   }
 }