Tripal v1.0 (6.x-1.0)
tripal_views.views.inc
Go to the documentation of this file.
00001 <?php
00002 
00003 include('views/handlers/views_handler_join_chado_through_linking.inc');
00004 include('views/handlers/views_handler_join_chado_aggregator.inc');
00005 include('api/tripal_views.api.inc');
00006 
00007 
00008 // TEMPORARY
00009 include('views/chado_linking.TMP.inc');
00010 
00064 function tripal_views_views_handlers() {
00065 
00066   return array(
00067     'info' => array(
00068       'path' => drupal_get_path('module', 'tripal_views') . '/views/handlers',
00069     ),
00070     'handlers' => array(
00071 
00072       // Custom Tripal Filter Handlers
00073       'tripal_views_handler_filter_file_upload' => array(
00074         'parent' => 'views_handler_filter',
00075       ),
00076       'tripal_views_handler_filter_textarea' => array(
00077         'parent' => 'views_handler_filter',
00078       ),
00079       'tripal_views_handler_filter_no_results' => array(
00080         'parent' => 'views_handler_filter'
00081       ),
00082       'tripal_views_handler_filter_select_cvterm' => array(
00083         'parent' => 'views_handler_filter_string',
00084       ),
00085       'tripal_views_handler_filter_select_string' => array(
00086         'parent' => 'chado_views_handler_filter_string',
00087       ),
00088       'tripal_views_handler_filter_sequence' => array(
00089         'parent' => 'chado_views_handler_filter_string',
00090       ),
00091 
00092       // Custom Tripal Field Handlers
00093       'tripal_views_handler_field_aggregate' => array(
00094         'parent' => 'chado_views_handler_field',
00095       ),
00096       'tripal_views_handler_field_sequence' => array(
00097         'parent' => 'chado_views_handler_field',
00098       ),
00099 
00100       // Join Handlers
00101       'views_handler_join_chado_aggregator' => array(
00102         'parent' => 'views_join',
00103       ),
00104       'views_handler_join_chado_through_linking' => array(
00105         'parent' => 'views_join',
00106       ),
00107 
00108       // Relationship Handlers
00109       'chado_views_handler_relationship' => array(
00110         'parent' => 'views_handler_relationship'
00111       ),
00112       'chado_views_handler_relationship_to_node' => array(
00113         'parent' => 'views_handler_relationship'
00114       ),
00115 
00116       // Wrappers for Default Views Handlers-----
00117       // Field Handlers
00118       'chado_views_handler_field' => array(
00119         'parent' => 'views_handler_field'
00120       ),
00121       'chado_views_handler_field_boolean' => array(
00122         'parent' => 'views_handler_field_boolean'
00123       ),
00124       'chado_views_handler_field_counter' => array(
00125         'parent' => 'views_handler_field_counter'
00126       ),
00127       'chado_views_handler_field_custom' => array(
00128         'parent' => 'views_handler_field_custom'
00129       ),
00130       'chado_views_handler_field_date' => array(
00131         'parent' => 'views_handler_field_date'
00132       ),
00133       'chado_views_handler_field_markup' => array(
00134         'parent' => 'views_handler_field_markup'
00135       ),
00136       'chado_views_handler_field_math' => array(
00137         'parent' => 'views_handler_field_math'
00138       ),
00139       'chado_views_handler_field_numeric' => array(
00140         'parent' => 'views_handler_field_numeric'
00141       ),
00142       // Filter Handlers
00143       'chado_views_handler_filter_string' => array(
00144         'parent' => 'views_handler_filter_string',
00145       ),
00146       'chado_views_handler_filter_boolean_operator_string' => array(
00147         'parent' => 'views_handler_filter_boolean_operator_string',
00148       ),
00149       'chado_views_handler_filter_boolean_operator' => array(
00150         'parent' => 'views_handler_filter_boolean_operator',
00151       ),
00152       'chado_views_handler_filter_date' => array(
00153         'parent' => 'views_handler_filter_date',
00154       ),
00155       'chado_views_handler_filter_equality' => array(
00156         'parent' => 'views_handler_filter_equality',
00157       ),
00158       'chado_views_handler_filter_float' => array(
00159         'parent' => 'views_handler_filter_float',
00160       ),
00161       'chado_views_handler_filter_numeric' => array(
00162         'parent' => 'views_handler_filter_numeric',
00163       ),
00164       // Sort Handlers
00165       'chado_views_handler_sort' => array(
00166         'parent' => 'views_handler_sort'
00167       ),
00168       'chado_views_handler_sort_date' => array(
00169         'parent' => 'views_handler_sort_date'
00170       ),
00171       'chado_views_handler_sort_formula' => array(
00172         'parent' => 'views_handler_sort_formula'
00173       ),
00174       'chado_views_handler_sort_menu_hierarchy' => array(
00175         'parent' => 'views_handler_sort_menu_hierarchy'
00176       ),
00177       'chado_views_handler_sort_random' => array(
00178         'parent' => 'views_handler_sort_random'
00179       ),
00180     ),
00181   );
00182 }
00183 
00196 function tripal_views_views_pre_render(&$view) {
00197 
00198   // We need to unset the exposed_input for the view so we can repopulate that
00199   // variable. This is necessary if we're using the file_upload_combo
00200   // custom form element which adds the file_path variable to the $_GET after the
00201   // view has populated the $view->exposed_input variable
00202   unset($view->exposed_input);
00203 
00204   // we want to add to the bottom of the views the form for downloading
00205   // results in other formats (e.g. Excel, FASTA, CSV, etc.).  The Views Data
00206   // Export module provides small images at the bottom, but we want to provide
00207   // a more intutitive interface for getting different file formats
00208   $form = drupal_get_form('tripal_views_data_export_download_form', $view, $display_id, $args);
00209   $view->attachment_after = $form;
00210 }
00211 
00225 function tripal_views_views_data() {
00226 
00227   // Make sure all chado tables are integrated
00228   tripal_views_integrate_all_chado_tables();
00229 
00230   // Define Global Fields
00231   // Filter handler that lets the admin say:
00232   // "Show no results until they enter search parameters"
00233   $data['views']['search_results'] = array(
00234     'title' => t('Search Results'),
00235     'help' => t('Delay results until Apply/Search is clicked by the user.'),
00236     'filter' => array(
00237       'handler' => 'tripal_views_handler_filter_no_results',
00238     ),
00239   );
00240 
00241   $tvi_query = db_query('SELECT * FROM {tripal_views}');
00242 
00243   while ($tvi_row = db_fetch_object($tvi_query)) {
00244 
00245     // check to see if this is the lightest (drupal-style) priority setup for this table
00246     // if not then don't use this definition
00247     $lightest_priority_setup = tripal_views_is_lightest_priority_setup($tvi_row->setup_id, $tvi_row->table_name);
00248     if (!$lightest_priority_setup) {
00249       continue;
00250     }
00251 
00252     // ids we'll use for queries
00253     $setup_id = $tvi_row->setup_id;
00254     $mview_id = $tvi_row->mview_id;
00255 
00256     // holds the base table name and fields
00257     $base_table = '';
00258     $base_fields = array();
00259 
00260     // indicated whether the current table is a base table or not
00261     $is_base_table = $tvi_row->base_table;
00262 
00263     // populate the base table name and fields.  If an $mview_id is given
00264     // then get the materialized view info, otherwise get the Chado table info
00265     if ($mview_id) {
00266 
00267       // get the base table name from the materialized view
00268       $sql = "SELECT name, mv_specs FROM {tripal_mviews} WHERE mview_id = %d";
00269       $mview_table = db_fetch_object(db_query($sql, $mview_id));
00270       $base_table = $mview_table->name;
00271 
00272       // get the columns in this materialized view.  They are separated by commas
00273       // where the first word is the column name and the rest is the type
00274       $columns = explode(",", $mview_table->mv_specs);
00275       foreach ($columns as $column) {
00276         $column = trim($column);  // trim trailing and leading spaces
00277         preg_match("/^(.*?)\ (.*?)$/", $column, $matches);
00278         $column_name = $matches[1];
00279         $column_type = $matches[2];
00280         $base_fields[$column_name] = array(
00281           'column_name' => $column_name,
00282           'type' => $column_type,
00283         );
00284       }
00285 
00286       // get the field name and descriptions
00287       $sql = "SELECT * FROM {tripal_views_field} WHERE setup_id=%d";
00288       $query = db_query($sql, $setup_id);
00289       while ($field = db_fetch_object($query)) {
00290         $base_fields[$field->column_name]['name'] = $field->name;
00291         $base_fields[$field->column_name]['help'] = $field->description;
00292       }
00293     }
00294     // if this is not a legacy materialized view
00295     else {
00296       $base_table = $tvi_row->table_name;
00297 
00298       // Check if we are trying to integrate the node table
00299       // if we are we want to add to a current integration
00300       // as compared to create a whole new one
00301       if ($base_table == 'node') {
00302         // Add any joins between the node table and other tables
00303         $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
00304         $joins = db_query($sql, $setup_id);
00305         while ($join = db_fetch_object($joins)) {
00306           $left_table = $join->left_table;
00307           $left_field = $join->left_field;
00308           $base_field = $join->base_field;
00309           $handler = $join->handler;
00310 
00311           // add join entry
00312           $data[$base_table]['table']['join'][$left_table] = array(
00313             'left_field' => $left_field,
00314             'field' => $base_field,
00315           );
00316           if ($handler) {
00317             $data[$base_table]['table']['join'][$left_table]['handler'] = $handler;
00318           }
00319         }
00320         
00321 
00322         // Add in any handlers for node fields
00323         $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id=%d";
00324         $query = db_query($sql, $setup_id);
00325         while ($handler = db_fetch_object($query)) {
00326           $data[$base_table][$handler->column_name][$handler->handler_type]['handler'] = $handler->handler_name;
00327 
00328           // Add in any additional arguments
00329           // This should be a serialized array including (at a minimum) name => <handler name>
00330           if ($handler->arguments) {
00331             $data[$base_table][$handler->column_name][$handler->handler_type] = array_merge($data[$base_table][$handler->column_name][$handler->handler_type], unserialize($handler->arguments));
00332           }
00333         }
00334         continue;
00335       }
00336 
00337       // get the table description
00338       $table_desc = tripal_core_get_chado_table_schema($base_table);
00339             
00340       $fields = $table_desc['fields'];
00341       if (!is_array($fields)) {
00342         $fields = array();
00343         watchdog('tripal_views', 'No fields were described for this table (%table) through the Tripal Schema API.', array('%table' => $base_table), WATCHDOG_ERROR);
00344       }
00345       foreach ($fields as $column => $attrs) {
00346         $base_fields[$column] = array(
00347           'column_name' => $column,
00348           'type' => $attrs['type'],
00349         );
00350       }
00351 
00352       // get the field name and descriptions
00353       $sql = "SELECT * FROM {tripal_views_field} WHERE setup_id=%d";
00354       $query = db_query($sql, $setup_id);
00355       while ($field = db_fetch_object($query)) {
00356         $base_fields[$field->column_name]['name'] = $field->name;
00357         $base_fields[$field->column_name]['help'] = $field->description;
00358       }
00359     }
00360 
00361       // Setup the base table info in the data array
00362       $data[$base_table]['table']['group'] = t("$tvi_row->name");
00363 
00364       if ($is_base_table) {
00365         $data[$base_table]['table']['base'] = array(
00366           'group' => "$tvi_row->name",
00367           'title' => "$tvi_row->name",
00368           'help'  => $tvi_row->comment,
00369         );
00370       }
00371       else {
00372         $data[$base_table]['table'] = array(
00373           'group' => "$tvi_row->name",
00374           'title' => "$tvi_row->name",
00375           'help'  => $tvi_row->comment,
00376         );
00377       }
00378 
00379       // first add the fields
00380       foreach ($base_fields as $column_name => $base_field) {
00381         $data[$base_table][$column_name] = array(
00382           'title' => t($base_field['name']),
00383           'help' => t($base_field['help']),
00384           'field' => array(
00385             'click sortable' => TRUE,
00386           ),
00387         );
00388 
00389 
00390         // now add the handlers
00391         $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id = %d AND column_name = '%s'";
00392         $handlers = db_query($sql, $setup_id, $column_name);
00393         while ($handler = db_fetch_object($handlers)) {
00394           $data[$base_table][$column_name][$handler->handler_type]['handler'] = $handler->handler_name;
00395 
00396           // Add in any additional arguments
00397           // This should be a serialized array including (at a minimum) name => <handler name>
00398           if ($handler->arguments) {
00399             $data[$base_table][$column_name][$handler->handler_type] = array_merge($data[$base_table][$column_name][$handler->handler_type], unserialize($handler->arguments));
00400           }
00401         };
00402     }
00403 
00404     // now add the joins
00405     $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
00406     $joins = db_query($sql, $setup_id);
00407     if (!isset($joins)) {
00408       $joins = array();
00409     }
00410     while ($join = db_fetch_object($joins)) {
00411       $left_table = $join->left_table;
00412       $left_field = $join->left_field;
00413       $base_field = $join->base_field;
00414       $handler = $join->handler;
00415 
00416       // add join entry
00417       $data[$base_table]['table']['join'][$left_table] = array(
00418         'left_field' => $left_field,
00419         'field' => $base_field,
00420       );
00421       if ($handler) {
00422         $data[$base_table]['table']['join'][$left_table]['handler'] = $handler;
00423       }
00424     }
00425   }
00426 
00427   // TEMPORARY: needed to join chado base tables to node linking tables
00428   // currently done using old-style data arrays
00429   //$data = tripal_views_TEMPORARY_chado_linking_data($data);
00430   return $data;
00431 }
00432 
00437 function tripal_views_views_data_alter(&$data) {
00438     $tvi_query = db_query('SELECT * FROM {tripal_views}');
00439 
00440     // iterate through the views that we manage
00441     while ($tvi_row = db_fetch_object($tvi_query)) {
00442 
00443       //ids we'll use for queries
00444       $mview_id = $tvi_row->mview_id;
00445       $setup_id = $tvi_row->setup_id;
00446 
00447       // iterate through the columns and alter the existing data array for
00448       // joins to other tables
00449       $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
00450       $joins = db_query($sql, $setup_id);
00451       while ($join = db_fetch_object($joins)) {
00452         $left_table = $join->left_table;
00453         $left_field = $join->left_field;
00454         $base_field = $join->base_field;
00455         $base_table = $join->base_table;
00456 
00457         // add the recipricol join entries for each column
00458         if (array_key_exists($left_table, $data)) {
00459           $data[$left_table]['table']['join'][$base_table] = array(
00460             'left_field' => $base_field,
00461             'field' => $left_field,
00462           );
00463         }
00464       }
00465     }
00466 
00467     return $data;
00468 }
00469 
00473 function tripal_views_views_plugins() {
00474   $tc_path = drupal_get_path('module', 'tripal_views');
00475 
00476   $style_defaults = array(
00477     'path' => $tc_path . '/views_data_export/plugins',
00478     'parent' => 'views_data_export',
00479     'theme' => 'views_data_export',
00480     'theme path' => $tc_path . '/views_data_export/theme',
00481     'theme file' => 'tripal_views_data_export.theme.inc',
00482     'uses row plugin' => FALSE,
00483     'uses fields' => TRUE,
00484     'uses options' => TRUE,
00485     'type' => 'data_export',
00486   );
00487 
00488   // add FASTA format as a new style for the existing views_export_data Display
00489   return array(
00490     'style' => array(
00491       'views_data_export_fasta' => array(
00492         'title' => t('FASTA file'),
00493         'help' => t('Display results in FASTA format. All fields in results are on the definition line while the feature.residues field must be present .'),
00494         'handler' => 'tripal_views_plugin_style_export_fasta',
00495         // Views Data Export element that will be used to set additional headers when serving the feed.
00496         'export headers' => array('Content-type: text/plain; charset=utf-8'),
00497         // Views Data Export element mostly used for creating some additional classes and template names.
00498         'export feed type' => 'fasta',
00499         'export feed text' => 'FASTA',
00500         'export feed file' => '%view.fna',
00501         'export feed icon' => $tc_path . '/views_data_export/images/fasta.png',
00502         'additional themes' => array(
00503           'views_data_export_fasta_header' => 'style',
00504           'views_data_export_fasta_body' => 'style',
00505           'views_data_export_fasta_footer' => 'style',
00506         ),
00507         'additional themes base' => 'views_data_export_fasta',
00508       ) + $style_defaults,
00509     ),
00510   );
00511 }
00512 
00516 function tripal_views_views_pre_view(&$view, &$display_id, &$args) {
00517 
00518   // merge the $_GET and $_POST into the $_GET. This is because
00519   // Views and Views Data Export modules only uses the $_GET variable but
00520   // file uploads require $_POST. We need to make sure these two modules
00521   // have access to everything needed for this view to work properlys
00522   $_GET = array_merge($_GET, $_POST);
00523 }
00524 
00528 /* function tripal_views_views_pre_render(&$view, &$display_id, &$args){
00529   // we want to add to the bottom of the views the form for downloading
00530   // results in other formats (e.g. Excel, FASTA, CSV, etc.).  The Views Data
00531   // Export module provides small images at the bottom, but we want to provide
00532   // a more intutitive interface for getting different file formats
00533   $form = drupal_get_form('tripal_views_data_export_download_form',$view,$display_id,$args);
00534   $view->attachment_after = $form;
00535 }*/
00536 
00537 /*
00538  * 
00539  
00540 function tripal_views_views_query_alter(&$view, &$query){
00541   // iterate through the tables and see if they are chado tables.
00542   // if they are then prefix them with a "chado." prefix
00543   dpm($query);
00544   $tables = $query->tables;
00545   foreach ($tables as $base => $subtables) {
00546     $desc = tripal_core_get_chado_table_schema($base); 
00547     if ($desc) {       
00548       $query->tables["chado." . $base] = $subtables;
00549       unset($query->tables[$base]);
00550       $base = "chado." . $base;
00551     }
00552     foreach ($subtables as $subtable => $values) {
00553       $desc = tripal_core_get_chado_table_schema($subtable);     
00554       if ($desc) {
00555         $query->tables[$base]["chado." . $subtable] = $values;
00556         unset($query->tables[$base][$subtable]);
00557       }  
00558     }
00559   }
00560     
00561   dpm($query);
00562 } */
00566 function tripal_views_views_pre_execute(&$view) {
00567   // if the base table is a chado table then we want to set the 
00568   // search path so it can find all of the tables.
00569   // this will break views that use tables that have the same name
00570   // as chado tables (e.g. contact).
00571   $desc = tripal_core_get_chado_table_schema($view->base_table);
00572   if ($desc) {
00573     tripal_db_set_chado_search_path('chado');
00574   }
00575 }
00579 function tripal_views_views_post_render(&$view, &$output, &$cache) {
00580   // if the base table and the query is completed we want to set
00581   // the search path back to the default.
00582   $desc = tripal_core_get_chado_table_schema($view->base_table);
00583   if ($desc) {
00584     tripal_db_set_default_search_path();
00585   }
00586 }
00587 
00591 function tripal_views_data_export_download_form(&$form_state, $view, $display_id, $args) {
00592   $form = array();
00593   $urls = array();
00594 
00595   // get any export_data_export displays
00596   $displays = $view->display;
00597   $options = array();
00598   $default = '';
00599   $current_display = $view->current_display;
00600   foreach ($displays as $name => $display) {
00601     if (preg_match("/^views_data_export/", $name)) {
00602 
00603       // only add this display to the form if it is attached
00604       $display_options = $display->display_options;
00605       if (strcmp($display_options['displays'][$current_display], $current_display) != 0) {
00606          continue;
00607       }
00608 
00609       // set the first item as default
00610       if (!$default) {
00611         $default = $display->id;
00612       }
00613 
00614       $path = $display->display_options['path'];
00615       $query = $view->get_exposed_input();  // retrieves elements in $_GET array
00616 
00617       $urls[$display->id]['path'] = $path;
00618       $urls[$display->id]['query'] = $query;
00619 
00620       // add the new item to the options array
00621       $options[$display->id] = $display->display_title;
00622     }
00623   }
00624 
00625   // only generate the form if we have views_data_export displays attached
00626   // to this view
00627   if (count($options) > 0) {
00628 
00629     $form_state['storage']['urls'] = $urls;
00630     $form['#cache'] = TRUE;
00631 
00632     // we want the form to redirect to a new window
00633     $form['#attributes']['target'] = "_blank";
00634 
00635     // now build the form elements
00636     $form['downloads'] = array(
00637       '#type' => 'fieldset',
00638       '#title' => 'Download Results',
00639       '#collapsible' => TRUE,
00640       '#collapsed' => FALSE
00641     );
00642     $form['downloads']['file_type'] = array(
00643       '#title' => t('File format'),
00644       '#type' => 'radios',
00645       '#options' => $options,
00646       '#required' => TRUE,
00647       '#default_value' => $default,
00648       '#description' => t('Please select a file format to download'),
00649     );
00650     $form['downloads']['submit'] = array(
00651       '#value' => t('Download Results'),
00652       '#type' => 'submit',
00653     );
00654   }
00655   return $form;
00656 }
00657 
00661 function tripal_views_data_export_download_form_submit($form, &$form_state) {
00662   $urls = $form_state['storage']['urls'];
00663   $display_id = $form_state['values']['file_type'];
00664   drupal_goto($urls[$display_id]['path'], $urls[$display_id]['query']);
00665 }
00666 
00667 
00668 
00669 
 All Classes Files Functions Variables