function tripal_add_views_integration

2.x tripal_views.api.inc tripal_add_views_integration($defn_array, $setup_id = FALSE)
3.x tripal_chado_views.api.inc tripal_add_views_integration($defn_array, $setup_id = FALSE)

Add views integration records into the tripal_views* tables.

This is the programatic way to add your own integrations to the tripal views integrations list. Keep in mind that the priority set in your $defn_array needs to be lighter than any existing integrations to be used by views and that it should still be below 0 in order to allow site administrators to override it should they need to.

 $defn_array = array(
   'table' => 'feature', //tablename or materialized view name
   'name' => 'Sequence Features', // Human readable name
   'type' => 'chado', //either chado or mview depending on tablename
   'description' => 'Create a listing of features.', //description seen when creating a view of this type
   'priority' => 10, //For Base tripal modules: 10; custom modules: 9 to 0;
   'base_table' => TRUE //either TRUE or FALSE depending on whether the current table should show up in the add view list
   'fields' => array(
     'feature_id' => array(
       'name' => 'feature_id', //field name in database
       'title' => 'Feature ID', //human-readable name -seen in Views UI
       'description' => 'This is the unique identifier for features', //help/description seen in Views UI
       'type' => 'int', // the type of field
       'handlers' => array(  //possible keys are field, filter, sort, argument, relationship
         'field' => array(
           'name' => 'chado_views_handler_numeric' //name of handler
         ),
         'filter' => array( ... ),
         ...
       ),
       // Describe any joins involving this field.
       // Note: you can include both foreign keys (feature.type_id => cvterm.cvterm_id)
       // and referring tables (ie: feature.feature_id <= feature_relationship.subject_id)
       'joins' => array(
         'feature_relationship' => array( //table to join to.
           'subject_id' => array( //field in above table (feature_relationship)
             'table' => 'featureprop', //table to join to
             'field' => 'feature_id', //field in above table (feature_relationship)
             'handler' => 'views_join', //handler to use for joining
             'relationship_handler' => 'views_handler_relationship', //handler to use when a relationship is added.
             'relationship_only' => FALSE, //whether to join automatically (FALSE) or not (TRUE)
           ),
           ...
         ),
         ...
       ),
     )
   ),
 );
 tripal_add_views_integration($defn_array);

Parameters

$defn_array: An array describing the structure and fields of the table

Return value

True/False if completed successfully/not

Example usage (in hook_install()):

Related topics

6 calls to tripal_add_views_integration()
tripal_clone_views_integration in tripal_views/api/tripal_views.api.inc
Clone an integration. This is often a great way to create your own module-specific integration while still benifiting from an existing (or even the lightest priority) integration.
tripal_phylogeny_integrate_view in tripal_phylogeny/tripal_phylogeny.install
tripal_update_views_integration in tripal_views/api/tripal_views.api.inc
Update an existing Views Intregration Entry. This essentially removes and then re-adds the integration.
tripal_views_integrate_all_chado_tables in tripal_views/includes/tripal_views_integration.inc
Integrate all chado tables in the schema api. This integration only occurs once and sets all Chado tables to a priority of 10
tripal_views_integration_add_entry in tripal_views/api/tripal_views.DEPRECATED.inc

... See full list

1 string reference to 'tripal_add_views_integration'

File

tripal_views/api/tripal_views.api.inc, line 632
API functions for Tripal Views Integration

Code

function tripal_add_views_integration($defn_array, $setup_id = FALSE) {
  $no_errors = TRUE;

  if (empty($defn_array['table'])) {
    tripal_report_error('tripal_views', TRIPAL_WARNING, 'Recieved integration with no tablename: %defn', array('%defn' => print_r($defn_array, TRUE)));
    $no_errors = FALSE;
    return $no_errors;
  }

  // First insert into tripal_views
  $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'],
  );
  if ($setup_id) {
    $view_record['setup_id'] = $setup_id;
  }
  if ($defn_array['type'] == 'mview') {
    $mview = db_query("SELECT mview_id FROM {tripal_mviews} WHERE mv_table=:table", array(':table' => $defn_array['table']));
    $mview = $mview->fetchObject();
    $view_record['mview_id'] = $mview->mview_id;
    if (!$mview->mview_id) {
      return FALSE;
    }
  }
  if ($view_record['name']) { // && $view_record['comment']) {  # SPF: commented out 9/24/2012 .. It's not required on the form
    if (isset($defn_array['additional_content'])) {
      // D7 TODO: Check DBTNG changes work
      $setup = db_query(
      "SELECT * FROM {tripal_views} WHERE table_name=:table AND priority=:priority", 
      array(':table' => $view_record['table_name'], ':priority' => $view_record['priority'])
      );
      $setup = $setup->fetchObject();
      if (empty($setup->setup_id)) {
        $status = drupal_write_record('tripal_views', $view_record);
      }
      else {
        $view_record['setup_id'] = $setup->setup_id;
        $status = drupal_write_record('tripal_views', $view_record, 'setup_id');
      }
    }
    else {
      $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 field.', array('%table' => $defn_array['table'])), 'error');
  }

  if ($status) {

    // Need to update the tripal_views record so base_table can be false
    // this is a fix because drupal_write_record() puts in defaults if !isset()
    // and a variable is considered not set if it's null!
    // D7 TODO: Check DBTNG changes work
    db_query(
    "UPDATE {tripal_views} SET base_table=:base WHERE table_name=:table AND priority=:priority", 
    array(
      ':base' => $defn_array['base_table'],
      ':table' => $defn_array['table'],
      ':priority' => $defn_array['priority']
    )
    );

    // Insert Field Definitions
    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']) {
        if (isset($defn_array['additional_content'])) {
          // D7 TODO: Check DBTNG changes work
          $is_present = db_query(
          "SELECT true as present FROM {tripal_views_field} WHERE column_name=:column AND setup_id=:setup", 
          array(
            ':column' => $field_record['column_name'],
            ':setup' => $field_record['setup_id']
          )
          );
          $is_present = $is_present->fetchField();
          if (!$is_present) {
            $status = drupal_write_record('tripal_views_field', $field_record);
          }
          else {
            $status = drupal_write_record('tripal_views_field', $field_record, array('setup_id', 'column_name'));
          }
        }
        else {
          $status = drupal_write_record('tripal_views_field', $field_record);
        }
      }
      else {
        drupal_set_message(t('Unable to integrate %name field due to missing required fields.', array('%name' => $field['name'])), 'error');
        $status = FALSE;
      }

      if ($status) {

        // Insert Handler Definitions
        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;
          }
        }

        // Insert Joins
        // Note: The new defn_array structure accounts for 1+ joins to the same
        // table (ie: feature_relationship => feature : subject_id & object_id)
        // by making $field['joins'] an array of left_field keys.
        if (!is_array($field['joins'])) {
          $field['joins'] = array();
        }
        foreach ($field['joins'] as $joins) {
          // To keep backwards compatibility with the old defn_array which just
          // jumped right into the table definition allowing only a single join,
          // we need to check for old defn_arrays and transform them into the
          // new format.
          if (isset($joins['table'])) {
            $left_field = $joins['field'];
            $joins = array(
              $left_field => $joins
            );
          }

          // Loop on left fields
          foreach ($joins as $left_field => $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' => $left_field,
            );

            $join_record['handler'] = (!empty($join['handler'])) ? $join['handler'] : 'views_join';
            $join_record['relationship_handler'] = (!empty($join['relationship_handler'])) ? $join['relationship_handler'] : 'views_handler_relationship';
            $join_record['relationship_only'] = (!empty($join['relationship_only'])) ? $join['relationship_only'] : 0;

            if ($view_record['setup_id'] && $defn_array['table'] && $field['name'] && $join['table'] && $left_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' => $left_field,
                '%table' => $defn_array['table'],
                '%field' => $field['name']
              )
              ), 
              'error'
              );
              $no_errors = FALSE;
            }
          }
        }

      }
      else {
        drupal_set_message(t('Unable to integrate %field_name field', array('%field_name' => $field['name'])), 'error');
        $no_errors = FALSE;
      }
    }

  }
  else {
    drupal_set_message(t('Unable to set default tripal views integration'), 'error');
    $no_errors = FALSE;
  }

  return $no_errors;
}