Created
February 20, 2015 13:55
-
-
Save rettal/0409f5f4b7b08a1fd4a7 to your computer and use it in GitHub Desktop.
Download CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function downloadCsvAction() | |
{ | |
$oldTimeLimit = ini_get('max_execution_time'); | |
ini_set('max_execution_time', 60); | |
$lang = $this->params('lang', "en"); | |
$this->setRatingsViewData($this->viewData, $lang); | |
$headers = array( | |
'location_name', | |
'breakdown_period', | |
'overall_average', | |
'no_of_ratings', | |
'value_for_money', | |
'friendliness_of_counter_staff', | |
'time_to_collect', | |
'time_took_to_drop_off', | |
'cleanliness', | |
'condition', | |
'ease_of_locationg_supplier', | |
'likely_to_rent_again' | |
); | |
$filename = "data_export_" . date("Y-m-d") . ".csv"; | |
$now = gmdate("D, d M Y H:i:s"); | |
ob_start(); | |
$df = fopen("php://output", 'w'); | |
// deal with stupid windows line endings | |
stream_filter_register('crlf', 'StreamNewLines'); | |
$key = 'locationRatings'; | |
if (isset($this->viewData['countryRatings'])) { | |
$key = 'countryRatings'; | |
} elseif (isset($this->viewData['countryGroupRatings'])) { | |
$key = 'countryGroupRatings'; | |
} | |
$row = []; | |
fputcsv($df, $headers); | |
foreach ($this->viewData[$key] as $locations) { | |
foreach($locations as $location) { | |
$row[] = $locName = $location['name']; | |
$breakdowns = $location['breakdowns']; | |
foreach ($breakdowns as $breakdown) { | |
$row[] = $breakdown['name']; | |
$row[] = $breakdown['average']; | |
$row[] = $breakdown['no_of_ratings']; | |
$questionAverages = $breakdown['question_averages']; | |
foreach ($questionAverages as $question) { | |
$row[] = (isset($question['value']) ? $question['value'] : null); | |
} | |
fputcsv($df, $row); | |
$row = []; | |
$row[] = $locName; | |
} | |
$row = []; | |
} | |
} | |
fclose($df); | |
$file = ob_get_clean(); | |
$response = $this->getResponse(); | |
$response->setContent($file); | |
$response | |
->getHeaders() | |
->addHeaderLine('Expires: Tue, 03 Jul 2001 06:00:00 GMT') | |
->addHeaderLine('Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate') | |
->addHeaderLine("Last-Modified: {$now} GMT") | |
->addHeaderLine("Content-Type: application/force-download") | |
->addHeaderLine("Content-Type: application/octet-stream") | |
->addHeaderLine("Content-Type: application/download") | |
->addHeaderLine("Content-Disposition: attachment;filename={$filename}") | |
->addHeaderLine("Content-Transfer-Encoding: binary"); | |
ini_set('max_execution_time', $oldTimeLimit); | |
return $response; | |
} | |
class StreamNewLines extends \php_user_filter | |
{ | |
function filter($in, $out, &$consumed, $closing) | |
{ | |
while ($bucket = stream_bucket_make_writeable($in)) { | |
$bucket->data = preg_replace("/(?<!\r)\n/", "\r\n", $bucket->data); | |
$consumed += $bucket->datalen; | |
stream_bucket_append($out, $bucket); | |
} | |
return PSFS_PASS_ON; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment