Преглед на файлове

More work on Drupal Views Integration. Added support for new handlers, changed table structure, altered submit code and form

spficklin преди 13 години
родител
ревизия
c91f759176
променени са 4 файла, в които са добавени 334 реда и са изтрити 212 реда
  1. 117 90
      base/tripal_core/tripal_core.install
  2. 1 1
      base/tripal_core/tripal_core.module
  3. 81 59
      base/tripal_core/tripal_core.views.inc
  4. 135 62
      base/tripal_core/tripal_views_integration.inc

+ 117 - 90
base/tripal_core/tripal_core.install

@@ -49,10 +49,62 @@ function tripal_core_uninstall(){
 * @ingroup tripal_core
 */
 function tripal_core_get_schemas (){  
-  $schema = array();
+   $schema = array();
 
+   // get all the various schema parts and join them together
+   $temp = tripal_core_jobs_schema();
+   foreach ($temp as $table => $arr){ 
+      $schema[$table] = $arr; 
+   }
+   $temp = tripal_core_mviews_schema();
+   foreach ($temp as $table => $arr){ 
+      $schema[$table] = $arr; 
+   }
+   $temp = tripal_core_views_schema();
+   foreach ($temp as $table => $arr){ 
+      $schema[$table] = $arr; 
+   } 
 
-  $schema['tripal_jobs'] = array(
+	return $schema;
+}
+/************************************************************************
+* 
+*
+* @ingroup tripal_core
+*/
+function tripal_core_mviews_schema(){
+   $schema = array();
+   $schema['tripal_mviews'] = array(
+      'fields' => array(
+         'mview_id'      => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
+         'name'          => array('type' => 'varchar','length' => 255, 'not null' => TRUE),
+         'modulename'    => array('type' => 'varchar','length' => 50, 'not null' => TRUE, 'description' => 'The module name that provides the callback for this job'),
+         'mv_table'      => array('type' => 'varchar','length' => 128, 'not null' => TRUE),
+         'mv_specs'      => array('type' => 'text', 'size' => 'normal', 'not null' => TRUE),
+         'indexed'       => array('type' => 'text', 'size' => 'normal', 'not null' => TRUE),
+         'query'         => array('type' => 'text', 'size' => 'normal', 'not null' => TRUE),
+         'special_index' => array('type' => 'text', 'size' => 'normal', 'not null' => FALSE),
+         'last_update'   => array('type' => 'int', 'not null' => FALSE, 'description' => 'UNIX integer time'),
+      ),
+      'indexes' => array(
+         'mview_id' => array('mview_id')
+      ),
+      'unique keys' => array(
+         'mv_table' => array('mv_table'),
+         'mv_name' => array('name'),
+      ),
+      'primary key' => array('mview_id'),
+  );
+  return $schema;
+}
+/************************************************************************
+* 
+*
+* @ingroup tripal_core
+*/
+function tripal_core_jobs_schema(){
+   $schema = array();
+   $schema['tripal_jobs'] = array(
       'fields' => array(
          'job_id' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
          'uid' => array ('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The Drupal userid of the submitee'),
@@ -76,41 +128,17 @@ function tripal_core_get_schemas (){
          'job_name' => array('job_name')
       ),
       'primary key' => array('job_id'),
-  );
-
-  $schema['tripal_mviews'] = array(
-      'fields' => array(
-         'mview_id' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
-         'name' => array('type' => 'varchar','length' => 255, 'not null' => TRUE),
-         'modulename' => array('type' => 'varchar','length' => 50, 'not null' => TRUE, 'description' => 'The module name that provides the callback for this job'),
-         'mv_table' => array('type' => 'varchar','length' => 128, 'not null' => TRUE),
-         'mv_specs' => array('type' => 'text', 'size' => 'normal', 'not null' => TRUE),
-         'indexed' => array('type' => 'text', 'size' => 'normal', 'not null' => TRUE),
-         'query' => array('type' => 'text', 'size' => 'normal', 'not null' => TRUE),
-         'special_index' => array('type' => 'text', 'size' => 'normal', 'not null' => FALSE),
-         'last_update' => array ('type' => 'int', 'not null' => FALSE, 'description' => 'UNIX integer time'),
-      ),
-      'indexes' => array(
-         'mview_id' => array('mview_id')
-      ),
-      'unique keys' => array(
-         'mv_table' => array('mv_table'),
-         'mv_name' => array('name'),
-      ),
-      'primary key' => array('mview_id'),
-  );
-
-   $schema = tripal_core_views_integration_schema();
-
-	return $schema;
+   );
+   return $schema;
 }
 /************************************************************************
 * 
 *
 * @ingroup tripal_core
 */
-function tripal_core_views_integration_schema(){
-	$schema['tripal_views_integration'] = array(
+function tripal_core_views_schema(){
+   $schema = array();
+	$schema['tripal_views'] = array(
 		'description' => 'contains the setupes, their materialized view id and base table name that was used.',
 		'fields' => array(
 			'setup_id' => array(
@@ -124,8 +152,8 @@ function tripal_core_views_integration_schema(){
 				'type' => 'int',
 				'unsigned' => TRUE,
 			),
-			'chado_table_name' => array(
-				'description' => 'the base table name to be used when using this setup',
+			'table_name' => array(
+				'description' => 'the base table name to be used when using this setup. Use this field when not using a materialized view',
 				'type' => 'varchar',
 				'length' => 255,
 				'not null' => TRUE,
@@ -138,11 +166,11 @@ function tripal_core_views_integration_schema(){
 				'not null' => TRUE,
 				'default' => '',
 			),
-			'description' => array(
-				'description' => 'description of this row',
-				'type' => 'varchar',
-				'length' => 255,
-				'not null' => TRUE,
+			'comment' => array(
+				'description' => 'add notes about this views setup',
+				'type' => 'text',
+				'size' => 'normal',
+				'not null' => FALSE,
 				'default' => '',
 			),
 		),
@@ -152,54 +180,54 @@ function tripal_core_views_integration_schema(){
 		'primary key' => array('setup_id'),
 	);
 
-	$schema['tripal_mviews_join'] = array(
-		'description' => 'which materialzed views and chado tables to join in a given setup',
+	$schema['tripal_views_join'] = array(
+		'description' => 'coordinate the joining of tables',
 		'fields' => array(
-		  'mview_join_id' => array(
+		  'view_join_id' => array(
 				'description' => 'the id of the join',
-					'type' => 'serial',
-					'unsigned' => TRUE,
-					'not null' => TRUE,
+				'type' => 'serial',
+				'unsigned' => TRUE,
+				'not null' => TRUE,
 		  ),
 			'setup_id' => array(
-				'description' => 'tripal setup id from tripal_views_integration table',
+				'description' => 'setup id from tripal_views table',
 				'type' => 'int',
 				'unsigned' => TRUE,
 				'not null'=> TRUE,
 			),
-			'view_table' => array(
-					'description' => 'materialized view table name',
-					'type' => 'varchar',
-					'length' => '255',
-					'not null' => TRUE,
-					'default' => '',
-	    ),
-			'view_column' => array(
-				'description' => 'column of materialized view table (mview_table)',
+			'base_table' => array(
+			   'description' => 'the name of the base table',
+				'type' => 'varchar',
+				'length' => '255',
+				'not null' => TRUE,
+				'default' => '',
+	      ),
+			'base_field' => array(
+				'description' => 'the name of the base table column that will be joined',
 				'type' => 'varchar',
 				'length' => '255',
 				'not null' => TRUE,
 				'default' => '',
 			),
-			'chado_table_join' => array(
-				'description' => 'on which chado table to join to materialized view in this serach',
+			'left_table' => array(
+				'description' => 'the table on which to perform a left join',
 				'type' => 'varchar',
 				'length' => '255',
 				'not null' => TRUE,
 				'default' => '',
 			),
-			'chado_column' => array(
-				'description' => 'chado table (above) column to join on materialized view in this serach',
+			'left_field' => array(
+				'description' => 'the column on which to perform a left join',
 				'type' => 'varchar',
 				'length' => '255',
 				'not null' => TRUE,
 				'default' => '',
-	    ),
+	      ),
 		),
 		'unique_keys' => array(
-			'setup_id' => array('mview_join_id'),
+			'setup_id' => array('view_join_id'),
 		),
-		'primary key' => array('mview_join_id'),
+		'primary key' => array('view_join_id'),
 	);
 
 	$schema['tripal_views_handlers'] = array(
@@ -207,15 +235,15 @@ function tripal_core_views_integration_schema(){
 		'fields' => array(
 			'handler_id' => array(
 				'description' => 'the id of the handler',
-					'type' => 'serial',
-					'unsigned' => TRUE,
-					'not null' => TRUE,
-	    ),
+				'type' => 'serial',
+			   'unsigned' => TRUE,
+			   'not null' => TRUE,
+	      ),
 			'setup_id' => array(
-				'description' => 'which setup this is used by from tripal_views_integration table',
-					'type' => 'int',
-					'unsigned' => TRUE,
-					'not null'=> TRUE,
+				'description' => 'setup id from the tripal_views table',
+				'type' => 'int',
+				'unsigned' => TRUE,
+				'not null'=> TRUE,
 			),
 			'column_name' => array(
 				'description' => '',
@@ -224,24 +252,31 @@ function tripal_core_views_integration_schema(){
 				'not null' => TRUE,
 				'default' => '',
 			),
-			'handler_filter' => array(
-				'description' => 'identifier of the handler filter to be used for this column',
+			'handler_type' => array(
+				'description' => 'identifies the type of hander (e.g. field, filter, sort, argument, relationship, etc.)',
 				'type' => 'varchar',
-				'length' => '255',
+				'length' => '50',
 				'not null' => TRUE,
 				'default' => '',
 			),
-			'handler_field' => array(
-				'description' => 'identifier of the handler field to be used for this column',
+			'handler_name' => array(
+				'description' => 'the name of the handler',
 				'type' => 'varchar',
 				'length' => '255',
 				'not null' => TRUE,
 				'default' => '',
-	    ),
+			),
+			'arguments' => array(
+				'description' => 'arguments that may get passed to the handler',
+				'type' => 'text',
+				'size' => 'normal',
+				'not null' => FALSE,
+				'default' => '',
+			),
 		),
 		'unique_keys' => array(
-				'setup_id' => array('handler_id'),
-	  ),
+		   'setup_id' => array('handler_id'),
+	   ),
 		'primary key' => array('handler_id'),
 	);
 
@@ -253,20 +288,12 @@ function tripal_core_views_integration_schema(){
 * @ingroup tripal_core
 */
 function tripal_core_update_6000(){
-   // recreate the materialized view
-   tripal_feature_add_organism_count_mview();
-   $ret = array(
-      '#finished' => 1,
-   );
-   
+   $schema = tripal_core_views_schema();
+   $ret = array();
+   foreach ($schema as $name => $table) {
+      db_create_table($ret, $name, $table);
+   }   
    return $ret;
 }
-/************************************************************************
-* 
-*
-* @ingroup tripal_core
-*/
-function tripal_core_update_6000_views_integration(){
 
-}
 ?>

+ 1 - 1
base/tripal_core/tripal_core.module

@@ -198,7 +198,7 @@ function tripal_core_menu() {
 
 	//page to actually create setup->mview->chado table relationships
 	$items['admin/tripal/views/integration/mviews/new'] = array(
-     'title' => 'Add an MView',
+     'title' => 'Integrate an MView with Drupal Views',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('tripal_core_views_integration_new_setup_form'),
      'access arguments' => array('manage tripal_views_integration'), //TODO: figure out the proper permissions arguments

+ 81 - 59
base/tripal_core/tripal_core.views.inc

@@ -288,7 +288,7 @@ function tripal_core_add_node_ids_to_view (&$view) {
 
 function tripal_core_views_data(){   
 
-    $tvi_query = db_query('SELECT * FROM tripal_views_integration');
+    $tvi_query = db_query('SELECT * FROM {tripal_views}');
 
     //tvi = tripal_views_integration
     while($tvi_row = db_fetch_object($tvi_query)){
@@ -297,70 +297,85 @@ function tripal_core_views_data(){
       $setup_id = $tvi_row->setup_id;
       $mview_id = $tvi_row->mview_id;
 
-      //let's get the name of the table
-      $mview_table = db_fetch_object(db_query("SELECT name, mv_specs FROM {tripal_mviews} WHERE mview_id = '$mview_id';"));
+      //let's get the name of the materialized view
+      $base_table = '';
+      $base_fields = array();
+      if($mview_id){
+         $mview_table = db_fetch_object(db_query("SELECT name, mv_specs FROM {tripal_mviews} WHERE mview_id = '$mview_id';"));
+         $base_table = $mview_table->name;
+
+         // get the columns in this materialized view.  They are separated by commas
+         // where the first word is the column name and the rest is the type
+         $sql = "SELECT mv_specs FROM {tripal_mviews} WHERE mview_id = $mview_id";
+         $mview = db_fetch_object(db_query($sql));
+         $columns = explode(",",$mview->mv_specs);
+         foreach ($columns as $column){
+            $column = trim($column);  // trim trailing and leading spaces
+            preg_match("/^(.*?)\ (.*?)$/",$column,$matches);
+            $column_name = $matches[1];
+            $column_type = $matches[2];
+            $base_fields[] = $column_name;
+         }
+      }
+      else {
+         // TODO: get the non materialized view info and populate these variables
+         //  1) $base_table
+         //  2) $base_fields (an array of just the table field names)
+      }
 
       //use name from above and description from $tvi_row
-      $data[$mview_table->name]['table']['group'] = t('Mview ' . $tvi_row->name);
-      $data[$mview_table->name]['table']['base'] = array(
+      $data[$base_table]['table']['group'] = t($tvi_row->name);
+      $data[$base_table]['table']['base'] = array(
         'group' => t($tvi_row->name),
         'title' => t($tvi_row->name),
-        'help'  => t($tvi_row->description),
+        'help'  => t($tvi_row->comment),
       );
 
-      //let's add fields
-      //tmj = tripal_mviews_join
-      $tmj_query = db_query("SELECT * FROM tripal_mviews_join WHERE setup_id = '$setup_id'");
-      while($tmj_row = db_fetch_object($tmj_query)){
-
-        $column_name = $tmj_row->view_column;
-        $handlers = db_fetch_object(db_query("SELECT handler_filter, handler_field FROM {tripal_views_handlers} WHERE setup_id = '$setup_id' AND column_name = '$column_name';"));
-        //handlers would be used $handlers->handler_filter, $handlers->handler_field etc, thuogh may need to include new ones in this query or do select *
-
-        //let's use handlers we retrieved from above
-        $data[$mview_table->name][$tmj_row->view_column] = array(
-           'title' => t($tmj_row->view_column),
+      // first add the fields
+      foreach ($base_fields as $base_field){
+        $data[$base_table][$base_field] = array(
+           'title' => t($base_field),
            'help' => t("**"),
            'field' => array(
-              'handler' => $handlers->handler_field,
               'click sortable' => TRUE,
            ),
-           'sort' => array(
-              'handler' => 'views_handler_sort',
-           ),
-           'filter' => array(
-              'handler' => $handlers->handler_filter,
-           ),
-           'argument' => array(
-              'handler' => 'views_handler_argument_string',
-           ),
         );
-               
-        $chado_join_table = $tmj_row->chado_table_join;
-        $chado_join_column = $tmj_row->chado_column;
-        $mview_join_column = $tmj_row->view_column;
-        
-        // add recipricol join entries
-        $data["$mview_table->name"]['table']['join']["$chado_join_table"] = array(
-          'left_field' => $chado_join_column,
-          'field' => $mview_join_column,
+     };
+
+     // now add the handlers
+     $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id = '$setup_id' AND base_field = '$column_name'";
+     $handlers = db_query($sql);
+     while($handler = db_fetch_object($handlers)){
+        $data[$base_table][$column_name][$handler->hander_type]['handler'] = $handler->handler_name;
+     };
+
+     // now add the joins
+     $joins = db_query("SELECT * FROM {tripal_views_join} WHERE setup_id = '$setup_id'");
+     while($join = db_fetch_object($joins)){               
+        $left_table = $join->left_table;
+        $left_field = $join->left_field;
+        $base_field = $join->base_field;  
+      
+        // add join entry
+        $data[$base_table]['table']['join'][$left_table] = array(
+          'left_field' => $left_field,
+          'field' => $base_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
-        $sql = "SELECT tablename FROM pg_tables WHERE tablename = 'chado_$chado_join_table'";
+        $sql = "SELECT tablename FROM pg_tables WHERE tablename = 'chado_$left_table'";
         if(db_fetch_object(db_query($sql))){
 
            // join the mview to the linker table
-           $data["$mview_table->name"]['table']['join']["chado_$chado_join_table"] = array(
-             'left_field' => $chado_join_column,
-             'field' => $mview_join_column,
+           $data[$base_table]['table']['join']["chado_$left_table"] = array(
+             'left_field' => $left_field,
+             'field' => $base_field,
            );
         }
       }
     }
-
     return $data;
 }
 /**
@@ -368,7 +383,7 @@ function tripal_core_views_data(){
  * @ingroup tripal_core
  */
 function tripal_core_views_data_alter(&$data) {
-    $tvi_query = db_query('SELECT * FROM tripal_views_integration');
+    $tvi_query = db_query('SELECT * FROM {tripal_views}');
 
     //tvi = tripal_views_integration
     while($tvi_row = db_fetch_object($tvi_query)){
@@ -378,21 +393,28 @@ function tripal_core_views_data_alter(&$data) {
       $setup_id = $tvi_row->setup_id;
 
       //let's get the name of the table
-      $mview_table = db_fetch_object(db_query("SELECT name, mv_specs FROM {tripal_mviews} WHERE mview_id = '$mview_id';"));
+      $base_table = '';
+      if($mview_id){
+         $mview_table = db_fetch_object(db_query("SELECT name, mv_specs FROM {tripal_mviews} WHERE mview_id = '$mview_id';"));
+         $base_table = $mview_table->name;
+      }
+      else {
+         // TODO: get the non materialized view info and populate these variables
+         //  1) $base_table
+      }
 
       // iterate through the columns and alter the existing data array for
       // joins to other tables
-      $tmj_query = db_query("SELECT * FROM tripal_mviews_join WHERE setup_id = '$setup_id'");
-      while($tmj_row = db_fetch_object($tmj_query)){
-        
-        $chado_join_table = $tmj_row->chado_table_join;
-        $chado_join_column = $tmj_row->chado_column;
-        $mview_join_column = $tmj_row->view_column;      
+      $joins = db_query("SELECT * FROM {tripal_views_join} WHERE setup_id = '$setup_id'");
+      while($join = db_fetch_object($joins)){                       
+        $left_table = $tmj_row->left_join;
+        $left_field = $tmj_row->left_column;
+        $base_field = $tmj_row->base_column;      
  
         // add the recipricol join entries for each column
-        $data["$chado_join_table"]['table']['join']["$mview_table->name"] = array(
-          'left_field' => $mview_join_column,
-          'field' => $chado_join_column,
+        $data[$left_table]['table']['join'][$base_table] = array(
+          'left_field' => $base_field,
+          'field' => $left_field,
         );
 
         // check to see if this table is one that correlates with Drupal nodes
@@ -400,18 +422,18 @@ function tripal_core_views_data_alter(&$data) {
         // 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
-        $sql = "SELECT tablename FROM pg_tables WHERE tablename = 'chado_$chado_join_table'";
+        $sql = "SELECT tablename FROM pg_tables WHERE tablename = 'chado_$left_table'";
         if(db_fetch_object(db_query($sql))){
 
            // join the linker table to the mview
-           $data["chado_$chado_join_table"]['table']['join']["$mview_table->name"] = array(
-             'left_field' => $mview_join_column,
-             'field' => $chado_join_column,
+           $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']["$mview_table->name"] = array(
-           	'left_table' => "chado_$chado_join_table",
+           $data['node']['table']['join'][$base_table] = array(
+           	'left_table' => "chado_$left_table",
            	'left_field' => 'nid',
            	'field' => 'nid',
            );

+ 135 - 62
base/tripal_core/tripal_views_integration.inc

@@ -67,14 +67,14 @@ function tripal_core_views_integration_admin_form(){
 
   $form['#theme'] = 'tripal';
 
-  $query_results = db_query('SELECT * FROM public.tripal_views_integration;');
+  $query_results = db_query('SELECT * FROM tripal_views');
 
-  $header = array('Setup ID', 'Name', 'Materialized View ID', 'Base Table Name', 'Description');
+  $header = array('Setup ID', 'Name', 'MView ID', 'Table Name', 'Comment');
   $rows = array();
 
   $results = array();
   while($result = db_fetch_object($query_results)){
-    $rows[] = array($result->setup_id, $result->name, $result->mview_id, $result->base_table_name, $result->description,);
+    $rows[] = array($result->setup_id, $result->name, $result->mview_id, $result->base_table_name, $result->comment);
     $results[] = $result;
   }
 
@@ -110,9 +110,9 @@ function tripal_core_views_integration_admin_form(){
  */
 function tripal_core_views_integration_admin_form_submit($form, &$form_state){
   $value = $form['existing_rows']['#options'][$form_state['values']['existing_rows']];
-  db_query("DELETE FROM public.tripal_views_integration WHERE setup_id = $value;");
+  db_query("DELETE FROM public.tripal_views WHERE setup_id = $value;");
   db_query("DELETE FROM public.tripal_views_handlers WHERE setup_id = $value;");
-  db_query("DELETE FROM public.tripal_mviews_join WHERE setup_id = $value;");
+  db_query("DELETE FROM public.tripal_views_join WHERE setup_id = $value;");
 }
 /**
  *
@@ -122,9 +122,7 @@ function tripal_core_views_integration_admin_form_submit($form, &$form_state){
 function tripal_core_views_integration_new_setup_form(&$form_state){
 
   $form = array();
-
   $data = array();
-
   $form['#cache'] = TRUE;
 
   //ahah_helper requires this to register the form with it's module
@@ -132,21 +130,19 @@ function tripal_core_views_integration_new_setup_form(&$form_state){
 
   // field for the name of the
   $form['row_name'] = array(
-    '#title' => t('Name'),
+    '#title' => t('View Type Name'),
     '#type' => 'textfield',
     '#size' => 60,
     '#maxlength' => 128,
-    '#description' => 'Name of the Views Setup',
-    '#required' => TRUE,
+    '#description' => 'Provide the view type name.  This is the name that will appear in the Drupal Views interface when adding a new view.',
+    '#required' => TRUE,   
   );
 
   $form['row_description'] = array(
-    '#title' => t('Description'),
-    '#type' => 'textfield',
-    '#size' => 60,
-    '#maxlength' => 255,
-    '#description' => 'Briefly describe in which view this will be used',
-    '#required' => TRUE,
+    '#title' => t('Comment'),
+    '#type' => 'textarea',
+    '#description' => '(Optional). Provide any details regarding this setup you would like.',
+    '#required' => FALSE,
   );
 
   $mview_query = db_query("SELECT mview_id,name FROM {tripal_mviews} ORDER BY name;");
@@ -169,19 +165,9 @@ function tripal_core_views_integration_new_setup_form(&$form_state){
        'effect' => 'fade',
        'event' => 'change',
        'method' => 'replace',
-  ),
+    ),
   );
 
-  // ignore this for now... we'll come back to it later -- spf
-  //   $form['row_base_table_name'] = array(
-  //      '#title' => t('Base Table Name'),
-  //      '#type' => 'select',
-  //      // '#options' => array('stub'),
-  //      '#options' => tripal_core_get_chado_tables(),
-  //      '#description' => 'Select which chado table to use for this view.',
-  //      '#required' => TRUE,
-  //   );
-
   $form['view_setup_table'] = array(
      '#type' => 'item',
        '#prefix' => '<div id="table-rows-div">',
@@ -222,15 +208,35 @@ function tripal_core_views_integration_new_setup_form(&$form_state){
     $all_handlers = tripal_core_views_integration_discover_handlers();
     $handlers_fields = array();
     $handlers_filters = array();
+    $handlers_sort = array();
+    $handlers_arguments = array();
+    $handlers_join = array();
+    $handlers_rel = array();
     
-    $handlers_fields[] = "Select a Field Handler"; 
-    $handlers_filters[] = "Select a Filter Handler";
+    $handlers_fields[0] = "Select a field handler"; 
+    $handlers_filters[0] = "Select a filter handler";
+    $handlers_sort[0] = "Select a sort handler";
+    $handlers_argument[0] = "Select an argument handler";
+    $handlers_join[0] = "Select a join handler";
+    $handlers_rel[0] = "Select a relationship handler";
     foreach($all_handlers as $handler){
        if(preg_match("/views_handler_field/",$handler)){
-          $handlers_fields[] = $handler;
+          $handlers_fields[$handler] = $handler;
        }
        if(preg_match("/views_handler_filter/",$handler)){
-          $handlers_filters[] = $handler;
+          $handlers_filters[$handler] = $handler;
+       }
+       if(preg_match("/views_handler_sort/",$handler)){
+          $handlers_sort[$handler] = $handler;
+       }
+       if(preg_match("/views_handler_argument/",$handler)){
+          $handlers_argument[$handler] = $handler;
+       }
+       if(preg_match("/views_handler_join/",$handler)){
+          $handlers_join[$handler] = $handler;
+       }
+       if(preg_match("/views_handler_relationship/",$handler)){
+          $handlers_rel[$handler] = $handler;
        }
     }
 
@@ -274,9 +280,12 @@ function tripal_core_views_integration_new_setup_form(&$form_state){
          ),
       );
       
+      $columns = array();
       if($table){
         $table_desc = module_invoke_all('chado_'.$table.'_schema');
-        $columns = array_keys($table_desc['fields']);
+        foreach ($table_desc['fields'] as $column => $def){
+           $columns[$column] = $column;
+        }
       } else {
         $columns = array('Select Join Column');
       }
@@ -299,10 +308,38 @@ function tripal_core_views_integration_new_setup_form(&$form_state){
       $form['view_setup_table']["fields_filter_handler_$mview_id-$i"] = array(
         '#type' => 'select',
         '#prefix' => "<div class=\"fields-filter-handler\">",
-        '#suffix' => "</div></div>",
+        '#suffix' => "</div>",
         '#options' => $handlers_filters,
         '#required' => FALSE,
       );
+      $form['view_setup_table']["fields_sort_handler_$mview_id-$i"] = array(
+        '#type' => 'select',
+        '#prefix' => "<div class=\"fields-sort-handler\">",
+        '#suffix' => "</div>",
+        '#options' => $handlers_sort,
+        '#required' => FALSE,
+      );
+      $form['view_setup_table']["fields_argument_handler_$mview_id-$i"] = array(
+        '#type' => 'select',
+        '#prefix' => "<div class=\"fields-argument-handler\">",
+        '#suffix' => "</div>",
+        '#options' => $handlers_argument,
+        '#required' => FALSE,
+      );
+      $form['view_setup_table']["fields_relationship_handler_$mview_id-$i"] = array(
+        '#type' => 'select',
+        '#prefix' => "<div class=\"fields-relationship-handler\">",
+        '#suffix' => "</div>",
+        '#options' => $handlers_rel,
+        '#required' => FALSE,
+      );
+      $form['view_setup_table']["fields_join_handler_$mview_id-$i"] = array(
+        '#type' => 'select',
+        '#prefix' => "<div class=\"fields-join-handler\">",
+        '#suffix' => "</div></div>",
+        '#options' => $handlers_join,
+        '#required' => FALSE,
+      );
       
       $form['view_setup_table']["fields_end_$i"] = array(
         '#type' => 'markup',
@@ -319,7 +356,7 @@ function tripal_core_views_integration_new_setup_form(&$form_state){
   }
 
   //use this to put values into $form_state['values']
-  $form['data'] = array();
+  $form['data'] = array();   
 
   //need to find out if storing $form['data'][$key]['#value'] = $value <- is an issue
   //since it will give me errors if i try to stare an array instead of $value
@@ -351,37 +388,73 @@ function tripal_core_views_integration_new_setup_form_validate($form, &$form_sta
  * @ingroup tripal_views_integration
  */
 function tripal_core_views_integration_new_setup_form_submit($form, &$form_state){
-  $name = $form_state['values']['row_name'];
-  $mview_id = $form_state['values']['mview_id'];
-  $tripal_views_integration_record = array(
-    'mview_id' => $mview_id,
-    'name' => $name,
-  	 'description' => $form_state['values']['row_description'],
-  );
 
-  drupal_write_record('tripal_views_integration', $tripal_views_integration_record);
+   $name = $form_state['values']['row_name'];
+   $mview_id = $form_state['values']['mview_id'];
+   $table_name = $form_state['values']['table_name'];
 
-  $i = 1;
-  foreach ($form_state['values']['field_types'] as $key => $value){
-    $mview_join_record = array(
-        'setup_id' => $tripal_views_integration_record['setup_id'],
-        'view_table' => $form['mview_id']['#options'][$form_state['values']['mview_id']],
-        'view_column' => $key,
-        'chado_table_join' => $form_state['values']["fields_join_$mview_id-$i"],
-        'chado_column' => $form['view_setup_table']["fields_join_column_$mview_id-$i"]['#options'][$form_state['values']["fields_join_column_$mview_id-$i"]],
-    );
-    drupal_write_record('tripal_mviews_join', $mview_join_record);
-    
-    $handlers_record = array(
-      'setup_id' => $tripal_views_integration_record['setup_id'],
-      'column_name' => $key,//TODO: should we change this to an mview_join_id from tripal_view_join?
-      'handler_filter' => $form['view_setup_table']["fields_filter_handler_$mview_id-$i"]['#options'][$form_state['values']["fields_filter_handler_$mview_id-$i"]],
-    	'handler_field' => $form['view_setup_table']["fields_field_handler_$mview_id-$i"]['#options'][$form_state['values']["fields_field_handler_$mview_id-$i"]],
-    );
-    drupal_write_record('tripal_views_handlers', $handlers_record);
-    $i++;
-  }
+   $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = $mview_id";
+   $mview = db_fetch_object(db_query($sql));
+
+   // If this is for a materialized view then we want to add that record
+   $tripal_views_record = array();
+   if($mview_id){
+      $tripal_views_record = array(
+       'mview_id' => $mview_id,
+       'name' => $name,
+       'comment' => $form_state['values']['row_description'],
+      );
+      if(!drupal_write_record('tripal_views', $tripal_views_record)){
+         drupal_set_message("Failed to add setup.");
+         return;
+      }
+   } 
+   // if not a materialized view then just keep track of the table name
+   if($table_name){
+      $tripal_views_record = array(
+       'table_name' => $table_name,
+       'name' => $name,
+       'comment' => $form_state['values']['row_description'],
+      );
+      drupal_write_record('tripal_views', $tripal_views_record);
+   }
 
+   // iterate through the columns of the form and add
+   // the joins if provided, and the handlers
+   $i = 1;
+   foreach ($form_state['values']['field_types'] as $key => $value){
+
+      // first add the join if it exists
+      $left_table = $form_state['values']["fields_join_$mview_id-$i"];
+      $left_column = $form_state['values']["fields_join_column_$mview_id-$i"];
+
+      if($left_column){
+         $view_join_record = array(
+           'setup_id' => $tripal_views_record['setup_id'],
+           'base_table' => $mview->mv_table,
+           'base_field' => $key,
+           'left_table' => $left_table,
+           'left_field' => $left_column,
+         );
+         drupal_write_record('tripal_views_join', $view_join_record);
+      }
+
+      // add the hanlders
+      $handlers = array('filter','field','sort','argument','join','relationship');
+      foreach($handlers as $handler){
+         $handler_name = $form_state['values']["fields_".$handler."_handler_$mview_id-$i"];
+         if($handler_name){
+            $handler_record = array(
+            'setup_id' => $tripal_views_record['setup_id'],
+            'column_name' => $key, 
+            'handler_type' => $handler,
+            'handler_name' => $handler_name,
+            );
+            drupal_write_record('tripal_views_handlers', $handler_record);
+         }
+      }
+      $i++;
+   }
 }
 
 /**