Example #1
0
 public function generateCsv(View $view)
 {
     $columns = $view->getExportableCols();
     $titles = array_map(function (Column $col) {
         return $col->label;
     }, $columns);
     $colNames = array_map(function (Column $col) {
         return $col->id;
     }, $columns);
     // get rows
     $sql = $view->prepareQuery();
     $rows = \Meta\Core\Db::query($sql)->fetchAll(\PDO::FETCH_ASSOC);
     header("Content-type: text/csv");
     header("Content-Disposition: attachment; filename=export.csv");
     header("Pragma: no-cache");
     header("Expires: 0");
     // write CSV to output
     $stdout = fopen('php://output', 'w');
     fputcsv($stdout, $titles);
     foreach ($rows as $row) {
         $newRow = array();
         foreach ($colNames as $name) {
             $newRow[$name] = $row[$name];
         }
         fputcsv($stdout, $newRow);
     }
     fflush($stdout);
     fclose($stdout);
     exit;
 }