Tripal v1.0 (6.x-1.0)
tripal_views.api.inc
Go to the documentation of this file.
00001 <?php
00002 
00022 function tripal_views_get_table_lightest_priority($table_name) {
00023 
00024   $sql = "SELECT priority FROM {tripal_views} WHERE table_name='%s' ORDER BY priority ASC";
00025   $setup = db_fetch_object(db_query($sql, $table_name));
00026   if ($setup) {
00027     return $setup->priority;
00028   }
00029   else {
00030     // default priority is 10
00031     return 10;
00032   }
00033 }
00034 
00048 function tripal_views_get_lightest_priority_setup($table_name) {
00049 
00050   $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name='%s' ORDER BY priority ASC";
00051   $setup = db_fetch_object(db_query($sql, $table_name));
00052   if ($setup) {
00053     return $setup->setup_id;
00054   }
00055   else {
00056     return FALSE;
00057   }
00058 }
00059 
00073 function tripal_views_is_integrated($table_name, $priority = NULL) {
00074 
00075   if ($priority) {
00076     $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name='%s' AND priority=%d";
00077     $setup = db_fetch_object(db_query($sql, $table_name, $priority));
00078   }
00079   else {
00080     $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name='%s' ORDER BY priority ASC";
00081     $setup = db_fetch_object(db_query($sql, $table_name));
00082   }
00083   if ($setup) {
00084     return $setup->setup_id;
00085   }
00086   else {
00087     return FALSE;
00088   }
00089 }
00090 
00101 function tripal_views_is_lightest_priority_setup($setup_id, $table_name) {
00102 
00103   $lightest_priority_setup_id = tripal_views_get_lightest_priority_setup($table_name);
00104   if ($lightest_priority_setup_id == $setup_id) {
00105     return TRUE;
00106   }
00107   else {
00108     return FALSE;
00109   }
00110 
00111 }
00112 
00155 function tripal_views_integration_add_entry($defn_array) {
00156   $no_errors = TRUE;
00157 
00158   if (empty($defn_array['table'])) {
00159     watchdog('tripal_views','Recieved integration with no tablename: %defn', array('%defn' => print_r($defn_array,TRUE)), WATCHDOG_WARNING);
00160     $no_errors = FALSE;
00161     return $no_errors;
00162   }
00163 
00164   // First insert into tripal_views
00165   $view_record = array(
00166     'table_name' => $defn_array['table'],
00167     'name' => $defn_array['name'],
00168     'comment' => $defn_array['description'],
00169     'priority' => $defn_array['priority'],
00170     'base_table' => $defn_array['base_table'],
00171   );
00172   if ($defn_array['type'] == 'mview') {
00173     $mview = db_fetch_object(db_query("SELECT mview_id FROM {tripal_mviews} WHERE mv_table='%s'", $defn_array['table']));
00174     $view_record['mview_id'] = $mview->mview_id;
00175     if (!$mview->mview_id) {
00176       return FALSE;
00177     }
00178   }
00179   if ($view_record['name']) { // && $view_record['comment']) {  # SPF: commented out 9/24/2012 .. It's not required on the form
00180     if ($defn_array['additional_content']) {
00181       $setup = db_fetch_object(db_query("SELECT * FROM {tripal_views} WHERE table_name='%s' AND priority=%d", $view_record['table_name'], $view_record['priority']));
00182       if (empty($setup->setup_id)) {
00183         $status = drupal_write_record('tripal_views', $view_record);
00184       }
00185       else {
00186         $view_record['setup_id'] = $setup->setup_id;
00187         $status = drupal_write_record('tripal_views', $view_record, 'setup_id');
00188       }
00189     }
00190     else {
00191       $status = drupal_write_record('tripal_views', $view_record);
00192     }
00193   }
00194   else {
00195     $status = FALSE;
00196     drupal_set_message(t('Unable to integrate "%table" table due to a missing name field.', array('%table' => $defn_array['table'])), 'error');
00197   }
00198 
00199   if ($status) {
00200 
00201     // Need to update the tripal_views record so base_table can be false
00202     // this is a fix because drupal_write_record() puts in defaults if !isset()
00203     // and a variable is considered not set if it's null!
00204     db_query(
00205       "UPDATE {tripal_views} SET base_table=%d WHERE table_name='%s' AND priority=%d",
00206       $defn_array['base_table'],
00207       $defn_array['table'],
00208       $defn_array['priority']
00209     );
00210 
00211     // Insert Field Definitions
00212     foreach ($defn_array['fields'] as $field) {
00213       $field_record = array(
00214         'setup_id' => $view_record['setup_id'],
00215         'column_name' => $field['name'],
00216         'name' => $field['title'],
00217         'description' => $field['description'],
00218         'type' => $field['type'],
00219       );
00220       if ($view_record['setup_id'] && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
00221         if ($defn_array['additional_content']) {
00222           $is = db_fetch_object(db_query("SELECT true as present FROM {tripal_views_field} WHERE column_name='%s' AND setup_id=%d", $field_record['column_name'], $field_record['setup_id']));
00223           if (!$is->present) {
00224             $status = drupal_write_record('tripal_views_field', $field_record);
00225           }
00226           else {
00227             $status = drupal_write_record('tripal_views_field', $field_record, array('setup_id', 'column_name'));
00228           }
00229         }
00230         else {
00231           $status = drupal_write_record('tripal_views_field', $field_record);
00232         }
00233       }
00234       else {
00235         drupal_set_message(t('Unable to integrate %name field due to missing required fields.', array('%name' => $field['name'])), 'error');
00236         $status = FALSE;
00237       }
00238 
00239       if ($status) {
00240 
00241         // Insert Handler Definitions
00242         foreach ($field['handlers'] as $handler_type => $handler) {
00243           $handler_record = array(
00244             'setup_id' => $view_record['setup_id'],
00245             'column_name' => $field['name'],
00246             'handler_type' => $handler_type,
00247             'handler_name' => $handler['name'],
00248             'arguments' => serialize($handler)
00249           );
00250           if ($view_record['setup_id'] && $field['name'] && $handler_type && $handler['name'] && $handler) {
00251             $status = drupal_write_record('tripal_views_handlers', $handler_record);
00252           }
00253           else {
00254             $status = FALSE;
00255           }
00256           if (!$status) {
00257             drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
00258             $no_errors = FALSE;
00259           }
00260         }
00261 
00262         // Insert Joins
00263         if (!is_array($field['joins'])) {
00264           $field['joins'] = array();
00265         }
00266         foreach ($field['joins'] as $join) {
00267           $join_record = array(
00268             'setup_id' => $view_record['setup_id'],
00269             'base_table' => $defn_array['table'],
00270             'base_field' => $field['name'],
00271             'left_table' => $join['table'],
00272             'left_field' => $join['field'],
00273           );
00274 
00275           if (!empty($join['handler'])) {
00276             $join_record['handler'] = $join['handler'];
00277           }
00278           else {
00279             $join_record['handler'] = 'views_join';
00280           }
00281 
00282           if ($view_record['setup_id'] && $defn_array['table'] && $field['name'] && $join['table'] && $join['field']) {
00283             $status = drupal_write_record('tripal_views_join', $join_record);
00284           }
00285           else {
00286             $status = FALSE;
00287           }
00288           if (!$status) {
00289             drupal_set_message(
00290               t(
00291                 'Unable to join %left_table.%left_field with %table.%field',
00292                 array(
00293                   '%left_table' => $join['table'],
00294                   '%left_field' => $join['field'],
00295                   '%table' => $defn_array['table'],
00296                   '%field' => $field['name']
00297                 )
00298               ),
00299               'error'
00300             );
00301             $no_errors = FALSE;
00302           }
00303         }
00304 
00305       }
00306       else {
00307         drupal_set_message(t('Unable to integrate %field_name field', array('%field_name' => $field['name'])), 'error');
00308         $no_errors = FALSE;
00309       }
00310     }
00311 
00312   }
00313   else {
00314     drupal_set_message(t('Unable to set default tripal views integration'), 'error');
00315     $no_errors = FALSE;
00316   }
00317 
00318   return $no_errors;
00319 }
00320 
00330 function tripal_views_integration_export_entry($setup_id) {
00331 
00332   // Main setup details
00333   $r = db_fetch_object(db_query("SELECT * FROM {tripal_views} WHERE setup_id=%d", $setup_id));
00334   $defn_array = array(
00335     'table' => $r->table_name,
00336     'name' => $r->name,
00337     'type' => ($r->mview_id) ? 'mview' : 'chado',
00338     'description' => $r->comment,
00339     'priority' => $r->priority,
00340     'base_table' => $r->base_table,
00341     'fields' => array(),
00342   );
00343 
00344   // Add fields
00345   $resource = db_query("SELECT * FROM {tripal_views_field} WHERE setup_id=%d", $setup_id);
00346   while ($r = db_fetch_object($resource)) {
00347     $defn_array['fields'][ $r->column_name ] = array(
00348         'name' => $r->column_name,
00349         'title' => $r->name,
00350         'description' => $r->description,
00351         'type' => $r->type,
00352         'handlers' => array(),
00353         'joins' => array()
00354     );
00355   }
00356 
00357   // Add handlers
00358   $resource = db_query("SELECT * FROM {tripal_views_handlers} WHERE setup_id=%d", $setup_id);
00359   while ($r = db_fetch_object($resource)) {
00360     $defn_array['fields'][ $r->column_name ]['handlers'][ $r->handler_type ] = array(
00361       'name' => $r->handler_name
00362     );
00363   }
00364 
00365   // Add joins
00366   $resource = db_query("SELECT * FROM {tripal_views_join} WHERE setup_id=%d", $setup_id);
00367   while ($r = db_fetch_object($resource)) {
00368     $defn_array['fields'][ $r->base_field ]['joins'][ $r->left_table ] = array(
00369       'table' => $r->left_table,
00370       'field' => $r->left_field,
00371       'handler' => $r->handler,
00372     );
00373   }
00374 
00375   return $defn_array;
00376 }
00377 
00389 function tripal_views_integration_remove_entry_by_table_name($table_name, $priority) {
00390 
00391   $views = db_fetch_object(db_query(
00392     "SELECT * FROM {tripal_views} WHERE table_name='%s' AND priority=%d",
00393     $table_name,
00394     $priority
00395   ));
00396   if ($views->setup_id) {
00397     tripal_views_integration_remove_entry_by_setup_id($views->setup_id);
00398     return TRUE;
00399   }
00400   else {
00401     return FALSE;
00402   }
00403 
00404 }
00405 
00412 function tripal_views_integration_remove_entry_by_setup_id($setup_id) {
00413 
00414     db_query('DELETE FROM {tripal_views} WHERE setup_id=%d', $setup_id);
00415     db_query('DELETE FROM {tripal_views_field} WHERE setup_id=%d', $setup_id);
00416     db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=%d', $setup_id);
00417     db_query('DELETE FROM {tripal_views_join} WHERE setup_id=%d', $setup_id);
00418 
00419 }
00420 
00425 function tripal_views_integrate_all_chado_tables() {
00426 
00427   $tables = tripal_core_get_chado_tables(TRUE);
00428   foreach ($tables as $tablename) {
00429 
00430     $priority = 10;
00431     if (!tripal_views_is_integrated($tablename, $priority)) {
00432       $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
00433       if ($table_integration_array) {
00434         tripal_views_integration_add_entry($table_integration_array);
00435       }
00436     }
00437   }
00438 }
00439 
00449 function tripal_views_get_integration_array_for_chado_table($table_name, $base_table = TRUE, $priority = 9) {
00450 
00451   // Get the schema for this table (via the chado schema api)
00452   $schema = tripal_core_get_chado_table_schema($table_name);
00453 
00454   // Base definition array
00455   $defn_array = array(
00456     'table' => $table_name,
00457     'type' => 'chado',
00458     'name' => 'Chado ' . ucwords(str_replace('_', ' ', $table_name)),
00459     'description' => (!empty($schema['description'])) ? $schema['description'] : ' ',
00460     'priority' => $priority,
00461     'base_table' => $base_table,
00462     'fields' => array(),
00463   );
00464   // Add fields
00465   if (!isset($schema['fields'])) {
00466     watchdog('tripal_views', 'There are no fields defined for %table in the Chado Schema API.', array('%table' => $table_name), WATCHDOG_NOTICE);
00467     return FALSE;
00468   }
00469   foreach ($schema['fields'] as $field_name => $field_schema) {
00470 
00471     // Base field definition
00472     if (!empty($field_name) && !empty($field_schema['type'])) {
00473       $defn_array['fields'][$field_name] = array(
00474         'name' => $field_name,
00475         'title' => ucwords(str_replace('_', ' ', $field_name)),
00476         'type' => $field_schema['type'],
00477         'description' => ($field_schema['description']) ? $field_schema['description'] : ucwords(str_replace('_', ' ', $field_name)),
00478         'handlers' => array(),
00479         'joins' => array()
00480       );
00481 
00482       // Add handlers based on type
00483       if (preg_match('/^int/', $field_schema['type'])) {
00484         $defn_array['fields'][$field_name]['handlers'] = array(
00485           'field' => array('name' => 'chado_views_handler_field_numeric'),
00486           'filter' => array('name' => 'chado_views_handler_filter_numeric'),
00487           'sort' => array('name' => 'chado_views_handler_sort'),
00488         );
00489       }
00490       elseif (preg_match('/^serial/', $field_schema['type'])) {
00491         $defn_array['fields'][$field_name]['handlers'] = array(
00492           'field' => array('name' => 'chado_views_handler_field_numeric'),
00493           'filter' => array('name' => 'chado_views_handler_filter_numeric'),
00494           'sort' => array('name' => 'chado_views_handler_sort'),
00495         );
00496         $defn_array['fields'][$field_name]['type'] = 'int';
00497       }
00498       elseif (preg_match('/^varchar/', $field_schema['type'])) {
00499         $defn_array['fields'][$field_name]['handlers'] = array(
00500           'field' => array('name' => 'chado_views_handler_field'),
00501           'filter' => array('name' => 'chado_views_handler_filter_string'),
00502           'sort' => array('name' => 'chado_views_handler_sort'),
00503         );
00504       }
00505       elseif (preg_match('/^text/', $field_schema['type'])) {
00506         $defn_array['fields'][$field_name]['handlers'] = array(
00507           'field' => array('name' => 'chado_views_handler_field'),
00508           'filter' => array('name' => 'chado_views_handler_filter_string'),
00509           'sort' => array('name' => 'chado_views_handler_sort'),
00510         );
00511       }
00512       elseif (preg_match('/^boolean/', $field_schema['type'])) {
00513         $defn_array['fields'][$field_name]['handlers'] = array(
00514           'field' => array('name' => 'chado_views_handler_field_boolean'),
00515           'filter' => array('name' => 'chado_views_handler_filter_boolean_operator'),
00516           'sort' => array('name' => 'chado_views_handler_sort'),
00517         );
00518       }
00519       elseif (preg_match('/^datetime/', $field_schema['type'])) {
00520         $defn_array['fields'][$field_name]['handlers'] = array(
00521           'field' => array('name' => 'chado_views_handler_field_date'),
00522           'filter' => array('name' => 'chado_views_handler_filter_date'),
00523           'sort' => array('name' => 'views_handler_sort_date'),
00524         );
00525       }
00526       else {
00527         $defn_array['fields'][$field_name]['handlers'] = array(
00528           'field' => array('name' => 'chado_views_handler_field'),
00529           'filter' => array('name' => 'chado_views_handler_filter_string'),
00530           'sort' => array('name' => 'chado_views_handler_sort'),
00531         );
00532       }
00533 
00534       // Specify specialty handlers
00535       if ($field_name == 'type_id' OR $field_name == 'cvterm_id') {
00536         $defn_array['fields'][$field_name]['handlers']['filter']['name'] = 'tripal_views_handler_filter_select_cvterm';
00537       }
00538     }
00539   }
00540 
00541   // Add Joins & Relationship Handlers to fields
00542   if (!isset($schema['foreign keys'])) {
00543     $schema['foreign keys'] = array();
00544     watchdog('tripal_views', 'There are no foreign keys defined for %table in the Chado Schema API.', array('%table' => $table_name), WATCHDOG_WARNING);
00545   }
00546   foreach ($schema['foreign keys'] as $foreign_key_schema) {
00547     foreach ($foreign_key_schema['columns'] as $left_field => $right_field) {
00548       // Join
00549       $defn_array['fields'][$left_field]['joins'][ $foreign_key_schema['table'] ] = array(
00550         'table' => $foreign_key_schema['table'],
00551         'field' => $right_field,
00552         'handler' => 'views_handler_join_chado_aggregator'
00553       );
00554 
00555       // Relationship Handler
00556       $defn_array['fields'][$left_field]['handlers']['relationship'] = array(
00557         'name' => 'chado_views_handler_relationship',
00558         'base' => $foreign_key_schema['table'],
00559         'base field' => $right_field,
00560         'label' => $table_name . ' ' . $left_field . ' to ' . $foreign_key_schema['table'] . ' ' . $right_field
00561       );
00562     }
00563   }
00564 
00565   return $defn_array;
00566 }
00567 
00574 function tripal_views_add_node_relationship_to_chado_table_integration($defn_array) {
00575   $integrations[$defn_array['table']] = $defn_array;
00576   $primary_key = $defn_array['table'] . '_id';
00577   $chado_linking = 'chado_' . $defn_array['table'];
00578 
00579   if (empty($defn_array['table'])) {
00580     watchdog('tripal_views','Tried to add a node=>chado relationship for an empty table defn: %defn',
00581       array('%defn' => print_r($defn_array,TRUE)),WATCHDOG_WARNING);
00582     return FALSE;
00583   }
00584 
00585   // Add table.primary_key => chado_table.primary key join to $defn_array
00586   $integrations[$defn_array['table']]['fields'][$primary_key]['joins'][$chado_linking] = array(
00587     'table' => $chado_linking,
00588     'field' => $primary_key,
00589   );
00590 
00591   // Create chado_table defn_array
00592   $integrations[$chado_linking] = array(
00593     'table' => $chado_linking,
00594     'type' => 'drupal',
00595     'name' => 'Chado ' . $defn_array['table'] . ' Node',
00596     'description' => 'Links chado content to its drupal node counterpart',
00597     'priority' => $defn_array['priority'],
00598     'base_table' => FALSE,
00599     'fields' => array(
00600       $primary_key => array(
00601         'name' => $primary_key,
00602         'title' => ucwords(str_replace('_', ' ', $primary_key)),
00603         'type' => 'int',
00604         'description' => 'The primary key of the chado ' . $defn_array['table'] . ' table',
00605         'handlers' => array(),
00606         'joins' => array(
00607           $defn_array['table'] => array(
00608             'table' => $defn_array['table'],
00609             'field' => $primary_key,
00610           )
00611         ),
00612       ),
00613       'nid' => array(
00614         'name' => 'nid',
00615         'title' => 'Node ID',
00616         'type' => 'int',
00617         'description' => 'Link ' . ucfirst($defn_array['table']) . ' to it\'s node',
00618         'handlers' => array(
00619           'relationship' => array(
00620             'name' => 'chado_views_handler_relationship_to_node',
00621             'title' => ucfirst($defn_array['table']) . ' to Node',
00622             'label' => ucfirst($defn_array['table']) . ' to Node',
00623             'base table' => $defn_array['table'],
00624             'base field' => $primary_key
00625           )
00626         ),
00627         'joins' => array(
00628           'node' => array(
00629             'table' => 'node',
00630             'field' => 'nid',
00631           ),
00632         ),
00633       )
00634     ),
00635   );
00636 
00637   // Create node defn_array
00638   $integrations['node'] = array(
00639     'table' => 'node',
00640     'name' => 'Node',
00641     'description' => 'Primary Drupal Content',
00642     'priority' => $defn_array['priority'],
00643     'additional_content' => TRUE, // Allows multiple modules to add to the node setup
00644     'fields' => array(
00645       'nid' => array(
00646         'name' => 'nid',
00647         'title' => 'Node ID',
00648         'type' => 'int',
00649         'description' => 'the primary key of the drupal node table',
00650         'handlers' => array(),
00651         'joins' => array(
00652           $defn_array['table'] => array(
00653             'table' => $defn_array['table'],
00654             'field' => 'nid',
00655           ),
00656           $chado_linking => array(
00657             'table' => $chado_linking,
00658             'field' => 'nid',
00659           ),
00660         ),
00661       ),
00662     ),
00663   );
00664 
00665   return $integrations;
00666 }
 All Classes Files Functions Variables