You are here

function _add_custom_field_data in Bibliography Module 7

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

File

./biblio.install, line 1683

Code

function _add_custom_field_data() {

  // First contributor_type_data id.
  $next_ctdid = 10;
  $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")) {

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

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

    // 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])) {

            // Ftdid.
            $ftd[0] = ($existing_id = _id_by_name('biblio_field_type_data', $row[$column])) ? $existing_id : variable_get('biblio_last_ftdid', 100);

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

            // hint.
            $ftd[2] = "";
            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();

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

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

            // 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();

                // Cache the new id value for future use.
                _id_by_name('biblio_contributor_type_data', $title, $ctdid);
                $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;
}