You are here

function biblio_csv_export_2 in Bibliography Module 7

Same name and namespace in other branches
  1. 6.2 includes/biblio.import.export.inc \biblio_csv_export_2()
  2. 6 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 709
Functions that are used to import and export biblio data.

Code

function biblio_csv_export_2($result, $bfields) {

  // @code
  // $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;
  // }
  // @endcode
  $bfields = biblio_get_db_fields('all');
  $query_biblio_types = 'SELECT tid, name FROM {biblio_types}';
  $res_biblio_types = db_query($query_biblio_types);
  foreach ($res_biblio_types as $rec) {
    $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;
  }

  // Or 'col_name'.
  $label = variable_get('biblio_csv_col_head', 'label') == 'label' ? 1 : 0;
  $linebreak = variable_get('biblio_linebreak_exp', 1);
  foreach ($result as $rec) {
    $node_id = $rec->nid;

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

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

  //End while

  // Head line containing column names.
  if ($label) {
    $csv = $textsep . join("{$textsep}{$filedsep}{$textsep}", array_values($col_array)) . "{$textsep}\n";
  }
  else {
    $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;
    }

    // Cut off leading fieldsep and append EOL.
    $csv .= substr($csv_line, 1) . "\n";
  }
  drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
  drupal_add_http_header('Content-Disposition', 'attachment; filename=biblio_export.csv');
  return $csv;
}