auditionadmin/app/Http/Controllers/NominationEnsembles/MeobdaNominationExportContr...

57 lines
1.9 KiB
PHP

<?php
namespace App\Http\Controllers\NominationEnsembles;
use App\Http\Controllers\Controller;
use App\Models\NominationEnsembleEntry;
use Illuminate\Support\Facades\Response;
class MeobdaNominationExportController extends Controller implements NominationExportController
{
public function __invoke()
{
$data = $this->getData();
// Create a callback to write the CSV data
$callback = function () use ($data) {
$file = fopen('php://output', 'w');
foreach ($data as $line) {
// Convert the string into an array
$fields = explode(',', $line);
// Write the array to the CSV file
fputcsv($file, $fields);
}
fclose($file);
};
// Return a response with the CSV content
return Response::stream($callback, 200, [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="audition_entries_export.csv"',
]);
dd($this->getData());
}
private function getData()
{
// Room, Audition, Draw Number, Name, School
$exportRows = [
'Ensemble,Split,Instrument,Seat,First Name, Last Name,School',
];
$nominations = NominationEnsembleEntry::with('ensemble')->with('student.school')->get();
foreach ($nominations as $nomination) {
$ensemble = $nomination->ensemble->name;
$split = $nomination->data['split'];
$instrument = $nomination->data['instrument'];
$seat = $nomination->data['seat'];
$firstName = $nomination->student->first_name;
$lastName = $nomination->student->last_name;
$schoolName = $nomination->student->school->name;
$exportRows[] = "$ensemble, $split, $instrument, $seat, $firstName, $lastName, $schoolName";
}
return $exportRows;
}
}