I'm trying to create a csv report that calucualtes totals to be downloaded. I can write the file I'm just not sure how to get the button to push the file to the browser.
See bellow:
function downloadAirmilesReport(){
$trainers = DataObject::get("Member", "MemberType = 'Trainer' OR MemberType = 'Supervisor' OR MemberType = 'Manager'");
//CSV HeaderRow
$report = array(array("Offer","Report Date", "Collector Number", "Amount"));
//Open File
$fp = fopen('AirmilesReport.csv', 'w');
foreach ($trainers as $trainer) {
$id = $trainer->ID;
$memberType = $trainer->MemberType;
switch ($memberType) {
case "Trainer":
$clients = DataObject::get("Client", "AirMilesApproved = 1 AND TrainerID = '$id'");
$totalClients = $clients->count();
$totalPoints = $totalClients * 20;
break;
case "Supervisor":
$clients = DataObject::get("Client", "AirMilesApproved = 1 AND SupervisorID = '$id'");
$totalClients = $clients->count();
$totalPoints = $totalClients * 15;
break;
case "Manager":
$clients = DataObject::get("Client", "AirMilesApproved = 1 AND ManagerID = '$id'");
$totalClients = $clients->count();
$totalPoints = $totalClients * 5;
break;
}
if ($clients){
//Add Row to CSV
array_push($report,array("xxx", "01.01.01",$trainer->AirmilesID, $totalPoints)) ;
}
echo "<br>";
}
foreach ($report as $line){
fputcsv($fp, $line);
}
fclose($fp);
return $fp;
}
Thanks