Skip to content

Instantly share code, notes, and snippets.

@oojacoboo
Created October 10, 2012 03:24
Show Gist options
  • Save oojacoboo/3862989 to your computer and use it in GitHub Desktop.
Save oojacoboo/3862989 to your computer and use it in GitHub Desktop.
array_walk closure :P
/**
* Outputs a CSV file for download
* @param sfRequest $request
* @return string (.csv file)
*/
public function executeExportcsv(sfRequest $request) {
$companies = $this->getCompaniesData($request);
$outstream = fopen("php://output", "w");
$companiesArray = array();
foreach($companies as $company) {
$admins = $company->getColleagues(array("admin"));
foreach($admins as $admin) {
$companiesArray[$admin->getId()][] = $company->getDisplayName();
$companiesArray[$admin->getId()][] = $admin->getFirstName();
$companiesArray[$admin->getId()][] = $admin->getLastName();
$companiesArray[$admin->getId()][] = $admin->getEmail();
}
}
array_walk(&$companiesArray, function($adminArray, $companyId, $outstream) {
fputcsv($outstream, $adminArray);
}, $outstream);
fclose($outstream);
$this->getResponse()->clearHttpHeaders();
$this->getResponse()->setStatusCode(200);
$this->getResponse()->setContentType('text/csv');
$this->getResponse()->setHttpHeader('Pragma', 'public'); //optional cache header
$this->getResponse()->setHttpHeader('Expires', 0); //optional cache header
$this->getResponse()->setHttpHeader('Content-Disposition', "attachment; filename=companies_export_" . date("m-d-Y") . ".csv");
$this->getResponse()->setHttpHeader('Content-Transfer-Encoding', 'binary');
$this->getResponse()->setHttpHeader('Content-Length', filesize($outstream));
return $this->renderText(file_get_contents($outstream));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment