You are here

function biblio_csv_export_2 in Bibliography Module 6.2

Same name and namespace in other branches
  1. 6 biblio.import.export.inc \biblio_csv_export_2()
  2. 7 includes/biblio.import.export.inc \biblio_csv_export_2()
  3. 7.2 includes/biblio.import.export.inc \biblio_csv_export_2()

File

includes/biblio.import.export.inc, line 625
Functions that are used to import and export biblio data.

Code

function biblio_csv_export_2($result, $bfields) {

  //  $query_biblio_fields = 'SELECT name, title FROM {biblio_fields}';
  //  $res_biblio_fields = db_query($query_biblio_fields);
  //  while ($rec = db_fetch_object($res_biblio_fields)){
  //    $bfields[$rec->name] = $rec->title;
  //  }
  $bfields = biblio_get_db_fields('all');
  $query_biblio_types = 'SELECT tid, name FROM {biblio_types}';
  $res_biblio_types = db_query($query_biblio_types);
  while ($rec = db_fetch_object($res_biblio_types)) {
    $btypes[$rec->tid] = $rec->name;
  }
  switch (variable_get('biblio_csv_field_sep', 'tab')) {
    case 'tab':
      $filedsep = "\t";
      break;
    case 'comma':
      $filedsep = ',';
      break;
  }
  switch (variable_get('biblio_csv_text_sep', 'dquote')) {
    case 'dquote':
      $textsep = '"';
      break;
    case 'quote':
      $textsep = '\'';
      break;
  }
  $label = variable_get('biblio_csv_col_head', 'label') == 'label' ? 1 : 0;

  // or 'col_name'
  $linebreak = variable_get('biblio_linebreak_exp', 1);
  while ($rec = db_fetch_object($result)) {
    $node_id = $rec->nid;
    $node_array[$node_id]['type'] = $btypes[$rec->biblio_type];

    // there is no "label" for "type"
    $col_array['type'] = 'Type';
    foreach (array_keys($bfields) as $fieldname) {
      if (!empty($rec->{$fieldname}) && !in_array($fieldname, array(
        'biblio_citekey',
        'biblio_coins',
      ))) {
        $col_array[$fieldname] = $bfields[$fieldname];

        // mark field as in use
        $text = strtr($rec->{$fieldname}, $textsep, "{$textsep}{$textsep}");
        if ($linebreak) {
          $text = strtr($text, ';', "\n");
        }
        $node_array[$node_id][$fieldname] = trim($text);
      }
    }
  }

  //end while
  if ($label) {

    // head line containing column names
    $csv = $textsep . join("{$textsep}{$filedsep}{$textsep}", array_values($col_array)) . "{$textsep}\n";
  }
  else {

    // original DB field names
    $csv = $textsep . join("{$textsep}{$filedsep}{$textsep}", array_keys($col_array)) . "{$textsep}\n";
  }

  // Enclosing text in "<text>" is neccessary to enshure
  // multi line fields (like author) are handled correctly.
  // Therefore existing " must be excaped before.
  $csv = '"' . join("\"\t\"", array_keys($col_array)) . "\"\n";
  foreach ($node_array as $line_array) {
    $csv_line = '';
    foreach (array_keys($col_array) as $col) {
      $csv_line .= "{$filedsep}{$textsep}" . $line_array[$col] . $textsep;
    }
    $csv .= substr($csv_line, 1) . "\n";

    // cut off leading fieldsep and append EOL
  }
  drupal_set_header('Content-Type: text/plain; charset=utf-8');
  drupal_set_header('Content-Disposition: attachment; filename=biblio_export.csv');
  return $csv;
}