Created
October 10, 2012 03:24
-
-
Save oojacoboo/3862989 to your computer and use it in GitHub Desktop.
array_walk closure :P
This file contains hidden or 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
/** | |
* 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