You are here

function _add_custom_field_data in Bibliography Module 7.2

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

File

./biblio.install, line 1350

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);
  foreach ($res as $row) {
    $fieldmap[$row->name] = $row->fid;
  }
  $csv_file = drupal_get_path('module', 'biblio') . '/misc/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();
    foreach ($res as $row) {
      $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_update('biblio_field_type')
              ->fields(array(
              'ftdid' => $ftd[0],
              'cust_tdid' => $ftd[0],
              'visible' => 1,
            ))
              ->condition(db_and()
              ->condition('tid', $row[1])
              ->condition('fid', $fieldmap[$field_name]))
              ->execute();
            if (!$existing_id) {

              // if this title doesn't alreay exist, then insert it into the table
              db_insert('biblio_field_type_data')
                ->fields(array(
                'ftdid' => $ftd[0],
                'title' => $ftd[1],
                'hint' => $ftd[2],
              ))
                ->execute();
              _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_update('biblio_contributor_type')
                ->fields(array(
                'auth_type' => $ctdid,
              ))
                ->condition(db_and()
                ->condition('auth_category', $type)
                ->condition('biblio_type', $biblio_type))
                ->execute();
              if (!$eid) {
                db_insert('biblio_contributor_type_data')
                  ->fields(array(
                  'auth_type' => $ctdid,
                  'title' => $title,
                ))
                  ->execute();
                _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_update('biblio_field_type')
              ->fields(array(
              'visible' => 0,
            ))
              ->condition(db_and()
              ->condition('tid', $row[1])
              ->condition('fid', $fieldmap[$field_name]))
              ->execute();
          }
          elseif (empty($row[$column]) && isset($fieldmap[$field_name])) {

            // use the default field title when the title is blank
            db_update('biblio_field_type')
              ->fields(array(
              'visible' => 1,
            ))
              ->condition(db_and()
              ->condition('tid', $row[1])
              ->condition('fid', $fieldmap[$field_name]))
              ->execute();
          }
        }
        $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;
}