You are here

function uc_reports_get_csv in Ubercart 7.3

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:

  • uid: Equals uid for authenticated users.
  • sid: 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.admin.inc, line 1305
Reports administration menu items.

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 => $value) {
      $value = preg_replace('/\\r?\\n(?!\\t| )/', '', $value);
      drupal_add_http_header($header, $value);
    }
    print $csv_data->data;
    exit;
  }
}