function tripal_views_integration_add_entry

2.x tripal_views.DEPRECATED.inc tripal_views_integration_add_entry($defn_array, $setup_id = FALSE)
1.x tripal_views.api.inc tripal_views_integration_add_entry($defn_array)

Add views integration records into the tripal_views* tables

 $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( ... ),
         ...
       ),
       'join' => array( //describe a table that joins to this one via this field
         'table' => 'featureprop', //table to join to
         'field' => 'feature_id', //field in above table (featureprop)
         'handler' => 'views_handler_join_chado_aggregator', //handler to use
       ),
     )
   ),
 );
 tripal_views_integration_add_entry($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

3 calls to tripal_views_integration_add_entry()
tripal_views_clone_integration in tripal_views/api/tripal_views.api.inc
Clone an integration
tripal_views_integrate_all_chado_tables in tripal_views/api/tripal_views.api.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_import_form_submit in tripal_views/includes/tripal_views_integration_port.inc
Imports a tripal views integration

File

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

Code

function tripal_views_integration_add_entry($defn_array) {
  $no_errors = TRUE;

  if (empty($defn_array['table'])) {
    watchdog('tripal_views', 'Recieved integration with no tablename: %defn', array('%defn' => print_r($defn_array, TRUE)), WATCHDOG_WARNING);
    $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 ($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']) {  # SPF: commented out 9/24/2012 .. It's not required on the form
    if ($defn_array['additional_content']) {
      $setup = db_fetch_object(db_query("SELECT * FROM {tripal_views} WHERE table_name='%s' AND priority=%d", $view_record['table_name'], $view_record['priority']));
      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!
    db_query(
    "UPDATE {tripal_views} SET base_table=%d WHERE table_name='%s' AND priority=%d", 
    $defn_array['base_table'], 
    $defn_array['table'], 
    $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 ($defn_array['additional_content']) {
          $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']));
          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
        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_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;
}