You are here

function _biblio_types_customize_fields in Bibliography Module 6.2

Creates customized fields for bibliographic types based on a CSV data file.

This function reads data from the CSV file biblio.field.type.data.csv and uses that information to create custom fields that can be used by various types of biblio content.

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_types_customize_fields()
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 1570
Install, update, and uninstall functions for the biblio module.

Code

function _biblio_types_customize_fields() {

  // Set the name of the CSV file with biblio field type mapping data.
  $csv_file = drupal_get_path('module', 'biblio') . '/misc/biblio.field.type.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;
  }

  // Default for first contributor_type_data ID.
  $next_ctdid = 10;

  // Determine the column names defined in the {biblio_field_type_data} table.
  $schema = biblio_schema();
  $fieldnames = array_keys($schema['biblio_field_type_data']['fields']);

  // Create array for mapping biblio field machine names to their field IDs.
  $field_map = array();
  $resource = db_query("SELECT fid, name FROM {biblio_fields}");
  while ($row = db_fetch_object($resource)) {
    $field_map[$row->name] = $row->fid;
  }

  // Create array to map contributor field machine names to their field IDs.
  $contributor_map = array();
  $resource = db_query("SELECT fid, name FROM {biblio_fields} WHERE type = 'contrib_widget'");
  while ($row = db_fetch_object($resource)) {
    $contributor_map[$row->name] = $row->fid;
  }

  // Build field ID map array to speed field customization process.
  _biblio_field_id_by_name(NULL, NULL, NULL, array(
    'tablename' => 'biblio_field_type_data',
    'name_column' => 'title',
    'id_column' => 'ftdid',
  ));
  _biblio_field_id_by_name(NULL, NULL, NULL, array(
    'tablename' => 'biblio_contributor_type_data',
    'name_column' => 'title',
    'id_column' => 'auth_type',
  ));

  // First line of the CSV file contains the machine names of field types.
  $header = fgetcsv($handle, 10000, ",");

  // The second line has the default titles if none are set.
  $generic = fgetcsv($handle, 10000, ",");

  // Process all remaining rows in the CSV file.
  while (($row = fgetcsv($handle, 10000, ",")) !== FALSE) {
    $column = 0;
    if (empty($row[1])) {
      continue;
    }
    foreach ($header as $key => $field_name) {
      if (!empty($field_name) && $field_name != 'tid') {

        // @todo: Add comment about what this non-obvious condition this is for
        if (!empty($row[$column]) && $row[$column] != "~" && isset($field_map[$field_name])) {

          // Determine the value for ftdid field.
          $ftd[0] = ($existing_id = _biblio_field_id_by_name('biblio_field_type_data', $row[$column])) ? $existing_id : variable_get('biblio_last_ftdid', 100);

          // Determine the value for the title field.
          $ftd[1] = trim($row[$column]);

          // Default the value of the hint field to empty.
          $ftd[2] = "";
          $sql = "UPDATE {biblio_field_type} " . "SET ftdid = %d, cust_tdid = %d, visible = %d " . "WHERE tid = %d AND fid = %d ";
          db_query($sql, $ftd[0], $ftd[0], 1, $row[1], $field_map[$field_name]);
          if (!$existing_id) {

            // If this title does not already exist, then insert it.
            db_query("INSERT INTO {biblio_field_type_data} (" . implode(", ", $fieldnames) . ") " . "VALUES (%d, '%s', '%s')", $ftd);

            // Cache the new ftd value for future use.
            _biblio_field_id_by_name('biblio_field_type_data', $row[$column], $ftd[0]);

            // Incrment by one the ID for field type data.
            variable_set('biblio_last_ftdid', $ftd[0] + 1);
          }

          // Also populate contributor_type* tables.
          if (substr($field_name, -7, 7) == 'authors' && $row[$column] != '~') {
            $type = $contributor_map[$field_name];
            $title = trim($row[$column]);
            $biblio_type = $row[1];
            $ctdid = ($eid = _biblio_field_id_by_name('biblio_contributor_type_data', $title)) ? $eid : $next_ctdid;
            $sql = "UPDATE {biblio_contributor_type} SET auth_type=%d where auth_category=%d and biblio_type=%d";
            db_query($sql, $ctdid, $type, $biblio_type);
            if (!$eid) {
              $sql = "INSERT INTO {biblio_contributor_type_data} (auth_type, title) VALUES (%d, '%s')";
              db_query($sql, $ctdid, $title);

              // Cache the new contributor_type_data value for future use.
              _biblio_field_id_by_name('biblio_contributor_type_data', $title, $ctdid);

              // Increment the contributor_data_type counter.
              $next_ctdid++;
            }
          }
        }
        elseif ($row[$column] == "~" && isset($field_map[$field_name])) {

          // Turn off the visibility for this (~) field type.
          db_query("UPDATE {biblio_field_type}\n                    SET visible = 0\n                    WHERE tid = %d AND fid = %d ", $row[1], $field_map[$field_name]);
        }
        elseif (empty($row[$column]) && isset($field_map[$field_name])) {
          db_query("UPDATE {biblio_field_type}\n                    SET visible = 1\n                    WHERE tid = %d AND fid = %d ", $row[1], $field_map[$field_name]);
        }
      }
      $column++;
    }
  }
  fclose($handle);
  $result = array(
    'success' => TRUE,
    'query' => 'Added type specific field titles',
  );
  return $result;
}