You are here

public function Reports::getCSV in Ubercart 8.4

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 'Reports::getCSV'
uc_report.routing.yml in uc_report/uc_report.routing.yml
uc_report/uc_report.routing.yml

File

uc_report/src/Controller/Reports.php, line 961

Class

Reports
Provides reports for Ubercart.

Namespace

Drupal\uc_report\Controller

Code

public function getCSV($report_id, $user_id) {
  $account = $this
    ->currentUser();
  $user_check = $account
    ->isAnonymous() ? session_id() : $account
    ->id();
  $csv_data = \Drupal::cache()
    ->get('uc_report_' . $report_id . '_' . $user_id);
  if (!$csv_data || $user_id != $user_check) {
    $this
      ->messenger()
      ->addError($this
      ->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."));
    throw new NotFoundHttpException();
  }
  else {
    $response = new Response($csv_data->data);
    $http_headers = [
      '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);
      $response->headers
        ->set($header, $value);
    }
    return $response;
  }
}