You are here

function _add_custom_field_data in Bibliography Module 6

Same name and namespace in other branches
  1. 7 biblio.install \_add_custom_field_data()
  2. 7.2 biblio.install \_add_custom_field_data()
3 calls to _add_custom_field_data()
biblio_install in ./biblio.install
@file Install file for biblio module
biblio_reset_types in ./biblio.install
biblio_update_6000 in ./biblio.install

File

./biblio.install, line 1171
Install file for biblio module

Code

function _add_custom_field_data() {
  $next_ctdid = 10;

  //first contributor_type_data id
  $schema = biblio_schema();
  $fieldnames = array_keys($schema['biblio_field_type_data']['fields']);
  $query = "SELECT fid, name FROM {biblio_fields} ";
  $res = db_query($query);
  while ($row = db_fetch_object($res)) {
    $fieldmap[$row->name] = $row->fid;
  }
  $csv_file = drupal_get_path('module', 'biblio') . '/biblio.field.type.data.csv';
  if ($handle = fopen($csv_file, "r")) {
    $header = fgetcsv($handle, 10000, ",");

    // the first line has the field names
    $generic = fgetcsv($handle, 10000, ",");

    // the second line has the default titles if none given
    // build cache lookups
    _id_by_name(NULL, NULL, NULL, array(
      'tablename' => 'biblio_field_type_data',
      'name_column' => 'title',
      'id_column' => 'ftdid',
    ));
    _id_by_name(NULL, NULL, NULL, array(
      'tablename' => 'biblio_contributor_type_data',
      'name_column' => 'title',
      'id_column' => 'auth_type',
    ));

    // map contributor field titles to field ids
    $res = db_query("SELECT fid,name FROM {biblio_fields} WHERE type='contrib_widget'");
    $contributor_categories = array();
    while ($row = db_fetch_object($res)) {
      $contributor_categories[$row->name] = $row->fid;
    }

    // process all rows of the 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') {
          if (!empty($row[$column]) && $row[$column] != "~" && isset($fieldmap[$field_name])) {
            $ftd[0] = ($existing_id = _id_by_name('biblio_field_type_data', $row[$column])) ? $existing_id : variable_get('biblio_last_ftdid', 100);

            // ftdid
            $ftd[1] = trim($row[$column]);

            // title
            $ftd[2] = "";

            // hint
            db_query("UPDATE {biblio_field_type}\n                      SET ftdid = %d, cust_tdid = %d, visible = %d\n                      WHERE tid = %d AND fid = %d ", $ftd[0], $ftd[0], 1, $row[1], $fieldmap[$field_name]);
            if (!$existing_id) {

              // if this title doesn't alreay exist, then insert it into the table
              db_query("INSERT INTO {biblio_field_type_data} (" . implode(", ", $fieldnames) . ")\n                        VALUES (%d, '%s', '%s')", $ftd);
              _id_by_name('biblio_field_type_data', $row[$column], $ftd[0]);

              // cache the new id value for future use
              variable_set('biblio_last_ftdid', $ftd[0] + 1);

              //increment the field type data id by one.
            }

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

                // cache the new id value for future use
                $next_ctdid++;
              }
            }
          }
          elseif ($row[$column] == "~" && isset($fieldmap[$field_name])) {

            // turn the visibility off for this (~) type
            db_query("UPDATE {biblio_field_type}\n                      SET visible = 0\n                      WHERE tid = %d AND fid = %d ", $row[1], $fieldmap[$field_name]);
          }
          elseif (empty($row[$column]) && isset($fieldmap[$field_name])) {

            // use the default field title when the title is blank
            db_query("UPDATE {biblio_field_type}\n                      SET visible = 1\n                      WHERE tid = %d AND fid = %d ", $row[1], $fieldmap[$field_name]);
          }
        }
        $column++;
      }
    }
    fclose($handle);
    $result = array(
      'success' => TRUE,
      'query' => 'Added type specific field titles',
    );
  }
  else {
    $result = array(
      'success' => FALSE,
      'query' => 'Could not open ' . $csv_file,
    );
  }
  return $result;
}