开发者

Drupal6 - Outputting a CSV with a MENU_CALLBACK

I would like to output a csv file with a drupal 6 module. This is the code I have, but it's hacked together with some of it in my custom theme and some of it in my module. Is there anyway I can move all of it into my module?

///////////////////////////
// csv.module <- a custom module
///////////////////////////
function csv_menu() {

    $items = array();

    $items['csv/results'] = array (
        'page callback' => 'csv_results_page',
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
    );

    return $items;
}

function csv_theme() {
    $items = array();

    $items['csv_results'] = array(
        'arguments' => array(),
    );

    return开发者_如何学C $items;
}

function csv_results_page() {
    return generate_csv_results();
}

function theme_csv_results() {
    return generate_csv_results();
}

function generate_csv_results() {
    return "header1,header2,header3,header4\nval1,val2,val3,val4\n";
}


//////////////////////////////
// page-csv-results.tpl.php <- in my theme. I would like it all contained in the module.
//////////////////////////////
<?php
    //!TODO: Change Content Type Header
    print theme('csv_results');

EDIT

Below is an updated version for anyone with a similar question. Thanks goes to chx!

///////////////////////////
// csv.module <- a custom module
///////////////////////////
function csv_menu() {

    $items = array();

    $items['csv/results'] = array (
        'page callback' => 'csv_results_page',
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
    );

    return $items;
}

function csv_results_page() {
    //Take a look at the Nikit's response on header stuff. This might be incorrect.
    header('Content-Type: text/x-comma-separated-values');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Cache-Control: private',false); // required for certain browser
    print generate_csv_results();
}

function generate_csv_results() {
    return "header1,header2,header3,header4\nval1,val2,val3,val4\n";
}


This is my simplified version of exporting csv file, add this code into csv_results_page(). Here used query, but can be other thing (like arrays, file listing, etc):

/**
 * Export to cvs
 */
function _YOURMODULENAME_csv_export() {
  $delimiter = "\t"; // This is tab delimiter, can be other

  $temp = file_directory_temp();

  $file = tempnam(realpath($temp), 'csv');
  if (!$fp = fopen($file, 'a')) {
    drupal_set_message(t('The file for exported could not be created. Refer to the site administrator'), 'error');
    return;
  }

  $title = "Some title for csv\n"; // you can remove it, if don't want title in csv

  $title .= implode($delimiter, array('Column name 1','Column name 2')). "\n"; // Add all columns titles here, it should mutch query result fields below

  fwrite($fp, $title);

  $query = 'WRITE HERE SOME CODE THAT SHOULD RESULT QUERY AND  RETURN fields in order described in $title above';
  $result = db_query($query);
  while ($data = db_fetch_array($result)) {
    $rows = implode($delimiter, $data);
    fwrite($fp, $rows."\n");
  }

  fclose($fp);

  $header = array();

  $header[] = 'Pragma: no-cache';
  $header[] = 'Cache-Control: no-cache, must-revalidate';
  $header[] = 'Content-Disposition: attachment; filename="exort_'.date('Ymd',time()).'.csv";'; // This is file name
  $header[] = 'Content-Type: text/x-comma-separated-values';
  $header[] = 'Content-Length: '.filesize($file);
  $header[] = 'Connection: close';

  file_transfer($file, $header);
  file_delete($file);

  return;
}


Outputting CSV should be done by just printing stuff in the page callback, if you return as you do it will be in the current theme, surrounded by HTML and you dont need that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜