You are here

function _biblio_add_field_definitions in Bibliography Module 6.2

Adds biblio field definitions to fields tables based upon data in a CSV file.

This function reads data from the CSV file biblio.field.link.data.csv and uses that information to create field type definitions as well as contributor type definition records in the appropriate field-related tables for the biblio module.

Return value

array An array of data summarizing attempts to add custom field data to tables with the following keys:

  • success: A boolean indicating success in this operation.
  • query: A message string with further information about operation success.
3 calls to _biblio_add_field_definitions()
biblio_install in ./biblio.install
Implements hook_install().
biblio_reset_types in ./biblio.install
Helper function to reset all field defintions to their defaults.
biblio_update_6000 in ./biblio.install
Update ...

File

./biblio.install, line 1468
Install, update, and uninstall functions for the biblio module.

Code

function _biblio_add_field_definitions() {
  global $db_type;
  $csv_file = drupal_get_path('module', 'biblio') . '/misc/biblio.field.link.data.csv';

  // Immediately return if handle to CSV file cannot be obtained.
  if (($handle = fopen($csv_file, "r")) === FALSE) {
    $result = array(
      'success' => FALSE,
      'query' => 'Could not open CSV file ' . $csv_file,
    );
    return $result;
  }

  // Get the column names of all three biblio tables used for defining fields.
  $schema = biblio_schema();
  $fields_columns = array_keys($schema['biblio_fields']['fields']);
  $field_type_columns = array_keys($schema['biblio_field_type']['fields']);
  $field_type_data_columns = array_keys($schema['biblio_field_type_data']['fields']);

  // @todo: Add comment explaining these non-obvious queries.
  if ($db_type == 'mysql' or $db_type == 'mysqli') {
    db_query("/*!40000 ALTER TABLE {biblio_field_type_data} DISABLE KEYS */;");
    db_query("/*!40000 ALTER TABLE {biblio_fields} DISABLE KEYS */;");
  }

  // Prepare array keyed by IDs of bibliographic types.
  $biblio_types = array();
  foreach (_biblio_data_bibliographic_types() as $key => $type) {
    $biblio_types[$type[0]] = $type[1];
  }

  // The first line of CSV files contains the field names.
  $header = fgetcsv($handle, 10000, ",");
  while (($row = fgetcsv($handle, 10000, ",")) !== FALSE) {
    $column = 0;

    // Add data for both the default biblio type (0) and all other biblio types
    // defined in _biblio_data_bibliographic_types().
    foreach (array_merge(array(
      0,
    ), array_keys($biblio_types)) as $t) {
      $field_type_values = array(
        $t,
        // tid: ID of the bibliographic type.
        $row[0],
        // fid: {biblio_fields}.fid of the node.
        $row[0],
        // ftdid: {biblio_field_type_data}.ftdid of the node, points to the current data, default or custom.
        $row[0],
        // cust_tdid: This always points to the custom data for this field. Stored so we can switch back an forth between default and custom.
        $row[3],
        // common:
        $row[4],
        // autocomplete:
        $row[5],
        // required: Is input required for this field?
        $row[6],
        // weight: The weight (location) of the field on the input form.
        $row[7],
      );
      db_query("INSERT INTO {biblio_field_type} (" . implode(", ", $field_type_columns) . ") " . "VALUES ('" . implode("', '", $field_type_values) . "')");
    }
    $field_type_data_values = array(
      $row[0],
      // ftdid: ID of this type of field.
      $row[1],
      // title: The title, which will be displayed on the form, for a given field.
      $row[2],
    );
    db_query("INSERT INTO {biblio_field_type_data} (" . implode(", ", $field_type_data_columns) . ") " . "VALUES('" . implode("', '", $field_type_data_values) . "')");
    $fields_values = array(
      $row[0],
      // fid: ID of this type of field.
      $row[8],
      // name: Name of this type of field.
      $row[9],
      // type: Type of form element for entering data for this field.
      $row[10],
      // size: Default size of this form element.
      $row[11],
    );
    db_query("INSERT INTO {biblio_fields} (" . implode(", ", $fields_columns) . ") " . "VALUES('" . implode("', '", $fields_values) . "')");

    // If appropriate, add contributor_type to tables.
    if ($row[9] == 'contrib_widget') {

      // Use field name without trailing 's' as initial guess for author type.
      $auth_type = substr($row[1], -1, 1) == 's' ? substr($row[1], 0, -1) : $row[1];
      db_query("INSERT INTO {biblio_contributor_type_data} (auth_type, title) VALUES (%d, '%s')", $row[0], $auth_type);
      db_query("INSERT INTO {biblio_contributor_type} (auth_category, biblio_type, auth_type) VALUES (%d, %d, %d)", $row[0], 0, $row[0]);
    }
  }
  fclose($handle);

  // @todo: Add comment explaining this non-obvious step about keys.
  if ($db_type == 'mysql' or $db_type == 'mysqli') {
    db_query("/*!40000 ALTER TABLE {biblio_field_type_data} ENABLE KEYS */;");
    db_query("/*!40000 ALTER TABLE {biblio_fields} ENABLE KEYS */;");
  }
  $result = array(
    'success' => TRUE,
    'query' => 'Added field titles and default values',
  );
  return $result;
}