123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- <?php
- function tripal_views_get_lightest_priority_setup($table_name) {
- $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name='%s' ORDER BY priority ASC";
- $setup = db_fetch_object(db_query($sql, $table_name));
- if ($setup) {
- return $setup->setup_id;
- }
- else {
- return FALSE;
- }
- }
- function tripal_views_is_integrated($table_name, $priority = NULL) {
- if ($priority) {
- $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name='%s' AND priority=%d";
- $setup = db_fetch_object(db_query($sql, $table_name, $priority));
- }
- else {
- $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name='%s' ORDER BY priority ASC";
- $setup = db_fetch_object(db_query($sql, $table_name));
- }
- if ($setup) {
- return $setup->setup_id;
- }
- else {
- return FALSE;
- }
- }
- function tripal_views_is_lightest_priority_setup($setup_id, $table_name) {
- $lightest_priority_setup_id = tripal_views_get_lightest_priority_setup($table_name);
- if ($lightest_priority_setup_id == $setup_id) {
- return TRUE;
- }
- else {
- return FALSE;
- }
- }
- function tripal_views_integration_add_entry($defn_array) {
- $no_errors = TRUE;
-
- $view_record = array(
- 'table_name' => $defn_array['table'],
- 'name' => $defn_array['name'],
- 'comment' => $defn_array['description'],
- 'priority' => $defn_array['priority'],
- 'base_table' => ($defn_array['base_table']) ? 1 : 0,
- );
- if ($defn_array['type'] == 'mview') {
- $mview = db_fetch_object(db_query("SELECT mview_id FROM {tripal_mviews} WHERE mv_table='%s'", $defn_array['table']));
- $view_record['mview_id'] = $mview->mview_id;
- if (!$mview->mview_id) {
- return FALSE;
- }
- }
- if ($view_record['name'] && $view_record['comment']) {
- $status = drupal_write_record('tripal_views', $view_record);
- }
- else {
- $status = FALSE;
- drupal_set_message(t('Unable to integrate %table table due to a missing name or comment field.', array('%table' => $defn_array['table'])), 'error');
- }
- if ($status) {
-
- foreach ($defn_array['fields'] as $field) {
- $field_record = array(
- 'setup_id' => $view_record['setup_id'],
- 'column_name' => $field['name'],
- 'name' => $field['title'],
- 'description' => $field['description'],
- 'type' => $field['type'],
- );
- if ($view_record['setup_id'] && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
- $status = drupal_write_record('tripal_views_field', $field_record);
- }
- else {
- drupal_set_message(t('Unable to integrate %name field due to a missing required fields.', array('%name' => $field['name'])), 'error');
- $status = FALSE;
- }
- if ($status) {
-
- foreach ($field['handlers'] as $handler_type => $handler) {
- $handler_record = array(
- 'setup_id' => $view_record['setup_id'],
- 'column_name' => $field['name'],
- 'handler_type' => $handler_type,
- 'handler_name' => $handler['name'],
- 'arguments' => serialize($handler)
- );
- if ($view_record['setup_id'] && $field['name'] && $handler_type && $handler['name'] && $handler) {
- $status = drupal_write_record('tripal_views_handlers', $handler_record);
- }
- else {
- $status = FALSE;
- }
- if (!$status) {
- drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
- $no_errors = FALSE;
- }
- }
-
- if (!is_array($field['joins'])) {
- $field['joins'] = array();
- }
- foreach ($field['joins'] as $join) {
- $join_record = array(
- 'setup_id' => $view_record['setup_id'],
- 'base_table' => $defn_array['table'],
- 'base_field' => $field['name'],
- 'left_table' => $join['table'],
- 'left_field' => $join['field'],
- );
- if (!empty($join['handler'])) {
- $join_record['handler'] = $join['handler'];
- }
- else {
- $join_record['handler'] = 'views_join';
- }
- if ($view_record['setup_id'] && $defn_array['table'] && $field['name'] && $join['table'] && $join['field']) {
- $status = drupal_write_record('tripal_views_join', $join_record);
- }
- else {
- $status = FALSE;
- }
- if (!$status) {
- drupal_set_message(
- t(
- 'Unable to join %left_table.%left_field with %table.%field',
- array(
- '%left_table' => $join['table'],
- '%left_field' => $join['field'],
- '%table' => $defn_array['table'],
- '%field' => $field['name']
- )
- ),
- 'error'
- );
- $no_errors = FALSE;
- }
- }
- }
- else {
- drupal_set_message(t('Unable to integrate field: %field_name', array('%field_name' => $field['name'])), 'error');
- $no_errors = FALSE;
- }
- }
- }
- else {
- drupal_set_message(t('Unable to set default views integration'), 'error');
- $no_errors = FALSE;
- }
- return $no_errors;
- }
- function tripal_views_integration_remove_entry_by_table_name($table_name, $priority) {
- $views = db_fetch_object(db_query(
- "SELECT * FROM {tripal_views} WHERE table_name='%s' AND priority=%d",
- $table_name,
- $priority
- ));
- if ($views->setup_id) {
- tripal_views_integration_remove_entry_by_setup_id($views->setup_id);
- return TRUE;
- }
- else {
- return FALSE;
- }
- }
- function tripal_views_integration_remove_entry_by_setup_id($setup_id) {
- db_query('DELETE FROM {tripal_views} WHERE setup_id=%d', $setup_id);
- db_query('DELETE FROM {tripal_views_field} WHERE setup_id=%d', $setup_id);
- db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=%d', $setup_id);
- db_query('DELETE FROM {tripal_views_join} WHERE setup_id=%d', $setup_id);
- }
- function tripal_views_get_integration_array_for_chado_table($table_name, $base_table = TRUE) {
-
- $schema = module_invoke_all('chado_' . $table_name . '_schema');
-
- $defn_array = array(
- 'table' => $table_name,
- 'type' => 'chado',
- 'name' => ucwords(str_replace('_', ' ', $table_name)),
- 'description' => ($schema['description']) ? $schema['description'] : ' ',
- 'priority' => 10,
- 'base_table' => $base_table,
- 'fields' => array(),
- );
-
- if (!isset($schema['fields'])) {
- $schema['fields'] = array();
- drupal_set_message(t('There are no fields defined for %table in the Chado Schema API.', array('%table' => $table_name)), 'warning');
- }
- foreach ($schema['fields'] as $field_name => $field_schema) {
-
- if (!empty($field_name) && !empty($field_schema['type'])) {
- $defn_array['fields'][$field_name] = array(
- 'name' => $field_name,
- 'title' => ucwords(str_replace('_', ' ', $field_name)),
- 'type' => $field_schema['type'],
- 'description' => ($field_schema['description']) ? $field_schema['description'] : ' ',
- 'handlers' => array(),
- 'joins' => array()
- );
-
- if (preg_match('/^int/', $field_schema['type'])) {
- $defn_array['fields'][$field_name]['handlers'] = array(
- 'field' => array('name' => 'chado_views_handler_field_numeric'),
- 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
- 'sort' => array('name' => 'chado_views_handler_sort'),
- );
- }
- elseif (preg_match('/^serial/', $field_schema['type'])) {
- $defn_array['fields'][$field_name]['handlers'] = array(
- 'field' => array('name' => 'chado_views_handler_field_numeric'),
- 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
- 'sort' => array('name' => 'chado_views_handler_sort'),
- );
- $defn_array['fields'][$field_name]['type'] = 'int';
- }
- elseif (preg_match('/^varchar/', $field_schema['type'])) {
- $defn_array['fields'][$field_name]['handlers'] = array(
- 'field' => array('name' => 'chado_views_handler_field'),
- 'filter' => array('name' => 'chado_views_handler_filter_string'),
- 'sort' => array('name' => 'chado_views_handler_sort'),
- );
- }
- elseif (preg_match('/^text/', $field_schema['type'])) {
- $defn_array['fields'][$field_name]['handlers'] = array(
- 'field' => array('name' => 'chado_views_handler_field'),
- 'filter' => array('name' => 'chado_views_handler_filter_string'),
- 'sort' => array('name' => 'chado_views_handler_sort'),
- );
- }
- else {
- $defn_array['fields'][$field_name]['handlers'] = array(
- 'field' => array('name' => 'chado_views_handler_field'),
- 'filter' => array('name' => 'chado_views_handler_filter_string'),
- 'sort' => array('name' => 'chado_views_handler_sort'),
- );
- }
- }
- }
-
- if (!isset($schema['foreign keys'])) {
- $schema['foreign keys'] = array();
- drupal_set_message(t('There are no foreign keys defined for %table in the Chado Schema API.', array('%table' => $table_name)), 'warning');
- }
- foreach ($schema['foreign keys'] as $foreign_key_schema) {
- foreach ($foreign_key_schema['columns'] as $left_field => $right_field) {
- $defn_array['fields'][$left_field]['joins'][ $foreign_key_schema['table'] ] = array(
- 'table' => $foreign_key_schema['table'],
- 'field' => $right_field,
- 'handler' => 'views_handler_join_chado_aggregtor'
- );
- }
- }
- return $defn_array;
- }
|