function tripal_add_field_to_views_integration

2.x tripal_views.api.inc tripal_add_field_to_views_integration($table_name, $priority, $field)

Adds the given field to an existing or cloned integration. In the case of a cloned integration, the lightest integration is used as the template for the clone.

NOTE: If that field already exists in the specified integration then it will first be deleted and the new one added.

Parameters

$table_name: The name of the table the integration is for

$priority: The priority of the integration to use; pass NULL to use the lightest integration

$field: An array describing the field ot add; uses the same format as the $defn_array

Return value

TRUE if the field was added successfully; FALSE otherwise

Related topics

1 call to tripal_add_field_to_views_integration()
1 string reference to 'tripal_add_field_to_views_integration'

File

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

Code

function tripal_add_field_to_views_integration($table_name, $priority, $field) {
  $no_errors = TRUE;

  // If no priority is supplied then add the field to the lightest integration
  if (empty($priority)) {
    $priority = tripal_get_lightest_views_integration_priority($table_name);
  }

  // First get the setup_id
  // D7 TODO: Check DBTNG changes work
  $setup_id = db_query(
  "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority", 
  array(
    ':table' => $table_name,
    ':priority' => $priority
  )
  );
  $setup_id = $setup_id->fetchObject();

  // If there isn't an integration matching that table/priority combination
  // then clone the lightest priority integration
  if (empty($setup_id)) {
    $setup_id = tripal_clone_views_integration($table_name, $priority);
  }

  // Now delete any existing field
  db_query("DELETE FROM {tripal_views_field} WHERE setup_id=:setup AND column_name=:column", 
  array(':setup' => $setup_id, 'column' => $field['name'])
  );
  db_query("DELETE FROM {tripal_views_handlers} WHERE setup_id=:setup AND column_name=:column", 
  array(':setup' => $setup_id, 'column' => $field['name'])
  );
  db_query("DELETE FROM {tripal_views_join} WHERE setup_id=:setup AND base_table=:table AND base_field=:field", 
  array(':setup' => $setup_id, ':table' => $table_name, ':field' => $field['name'])
  );

  // Now we need to add/update the field
  $field_record = array(
    'setup_id' => $setup_id,
    'column_name' => $field['name'],
    'name' => $field['title'],
    'description' => $field['description'],
    'type' => $field['type'],
  );
  if ($setup_id && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
    if ($defn_array['additional_content']) {
      // D7 TODO: Check DBTNG changes work
      $is = 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 = $is->fetchObject();
      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' => $setup_id,
        'column_name' => $field['name'],
        'handler_type' => $handler_type,
        'handler_name' => $handler['name'],
        'arguments' => serialize($handler)
      );
      if ($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' => $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 ($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;
  }

  return $no_errors;
}