You are here

function _uc_reports_get_csv in Ubercart 6.2

Same name and namespace in other branches
  1. 5 uc_reports/uc_reports.module \_uc_reports_get_csv()

Retrieves a cached CSV report & send its data.

Parameters

$report_id: A unique string that identifies the specific report CSV to retrieve

$user_id: The user id to who's retrieving the report

  • (Equals uid for authenticated users)
  • (Equals session_id for anonymous users)
1 string reference to '_uc_reports_get_csv'
uc_reports_menu in uc_reports/uc_reports.module
Implements hook_menu().

File

uc_reports/uc_reports.module, line 247
Displays reports on sales, customers, and products to store admin

Code

function _uc_reports_get_csv($report_id, $user_id) {
  global $user;
  $user_check = empty($user->uid) ? session_id() : $user->uid;
  $csv_data = cache_get('uc_reports_' . $report_id . '_' . $user_id, 'cache');
  if (!$csv_data || $user_id != $user_check) {
    drupal_set_message(t("The CSV data could not be retrieved. It's possible the data might have expired. Refresh the report page and try to retrieve the CSV file again."), 'error');
    drupal_not_found();
    exit;
  }
  else {
    ob_end_clean();
    $http_headers = array(
      'Pragma: private',
      'Expires: 0',
      'Cache-Control: private, must-revalidate',
      'Content-Transfer-Encoding: binary',
      'Content-Length:' . strlen($csv_data->data),
      'Content-Disposition: attachment; filename="' . $report_id . '.csv"',
      'Content-Type: text/csv',
    );
    foreach ($http_headers as $header) {
      $header = preg_replace('/\\r?\\n(?!\\t| )/', '', $header);
      drupal_set_header($header);
    }
    print $csv_data->data;
    exit;
  }
}