You are here

function _add_db_field_data in Bibliography Module 7.2

Same name and namespace in other branches
  1. 6 biblio.install \_add_db_field_data()
  2. 7 biblio.install \_add_db_field_data()
1 call to _add_db_field_data()
biblio_reset_types in ./biblio.install

File

./biblio.install, line 1277

Code

function _add_db_field_data() {
  if (db_driver() == 'mysql' or db_driver() == 'mysqli') {
    db_query("/*!40000 ALTER TABLE {biblio_field_type_data} DISABLE KEYS */;");
    db_query("/*!40000 ALTER TABLE {biblio_fields} DISABLE KEYS */;");
  }
  $csv_file = drupal_get_path('module', 'biblio') . '/misc/biblio.field.link.data.csv';
  if ($handle = fopen($csv_file, "r")) {
    $header = fgetcsv($handle, 10000, ",");

    // the first line has the field names
    while (($row = fgetcsv($handle, 10000, ",")) !== FALSE) {
      $column = 0;

      // add link data for default biblio type (0) and all other defined types (100-130)
      foreach (array_merge(array(
        0,
      ), range(100, 136)) as $t) {
        db_insert('biblio_field_type')
          ->fields(array(
          'tid' => $t,
          'fid' => $row[0],
          'ftdid' => $row[0],
          'cust_tdid' => $row[0],
          'common' => $row[3],
          'autocomplete' => $row[4],
          'required' => $row[5],
          'weight' => $row[6],
          'visible' => $row[7],
          'vtab' => $row[12],
        ))
          ->execute();
      }
      db_insert('biblio_field_type_data')
        ->fields(array(
        'ftdid' => $row[0],
        'title' => $row[1],
        'hint' => $row[2],
      ))
        ->execute();
      db_insert('biblio_fields')
        ->fields(array(
        'fid' => $row[0],
        'name' => $row[8],
        'type' => $row[9],
        'size' => $row[10],
        'maxsize' => $row[11],
      ))
        ->execute();

      // add contributor type data
      if (strstr($row[8], 'biblio_authors')) {

        // 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_insert('biblio_contributor_type_data')
          ->fields(array(
          'auth_type' => $row[0],
          'title' => $auth_type,
        ))
          ->execute();
        db_insert('biblio_contributor_type')
          ->fields(array(
          'auth_category' => $row[0],
          'biblio_type' => 0,
          'auth_type' => $row[0],
        ))
          ->execute();
      }
    }
    fclose($handle);
    $result = array(
      'success' => TRUE,
      'query' => 'Added field titles and default values',
    );
  }
  else {
    $result = array(
      'success' => FALSE,
      'query' => 'Could not open ' . $csv_file,
    );
  }
  if (db_driver() == 'mysql' or db_driver() == 'mysqli') {
    db_query("/*!40000 ALTER TABLE {biblio_field_type_data} ENABLE KEYS */;");
    db_query("/*!40000 ALTER TABLE {biblio_fields} ENABLE KEYS */;");
  }
  return $result;
}