Skip to content

Commit

Permalink
Merge pull request #151 from fsr5-fhaachen/dev
Browse files Browse the repository at this point in the history
merge dev into main
  • Loading branch information
simonostendorf authored Oct 30, 2024
2 parents 53bc5ab + 334b3ad commit 6542d81
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions app/Http/Controllers/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\ArticleActionLog;
use App\Models\Person;
use App\Models\Article;
use Symfony\Component\HttpFoundation\StreamedResponse;

class ExportController extends Controller
Expand All @@ -30,38 +31,29 @@ public function createCsv(): StreamedResponse
$columns = [
'Nachname',
'Vorname',
'Anz_Bier',
'Anz_Radler',
'Anz_Softdrinks',
'Anz_Wasser',
];

$callback = function () use ($persons, $columns) {
$articles = Article::all();
foreach ($articles as $article) {
$columns[] = 'Anz_' . $article->name;
}

$callback = function () use ($persons, $columns, $articles) {
$file = fopen('php://output', 'w');
fputcsv($file, $columns, ';');

foreach ($persons as $person) {
$row['Nachname'] = $person->lastname;
$row['Vorname'] = $person->firstname;
$row['Email'] = $person->email;
$row['Anz_Bier'] = ArticleActionLog::where([
['person_id', '=', $person->id],
['article_id', '=', 1],
])->count();
$row['Anz_Radler'] = ArticleActionLog::where([
['person_id', '=', $person->id],
['article_id', '=', 2],
])->count();
$row['Anz_Softdrink'] = ArticleActionLog::where([
['person_id', '=', $person->id],
['article_id', '=', 3],
])->count();
$row['Anz_Wasser'] = ArticleActionLog::where([
['person_id', '=', $person->id],
['article_id', '=', 4],
])->count();
$array = [$row['Nachname'], $row['Vorname'], $row['Anz_Bier'], $row['Anz_Radler'], $row['Anz_Softdrink'], $row['Anz_Wasser']];
$array = array_map('utf8_decode', $array);

foreach ($articles as $article) {
$row['Anz_' . $article->name] = ArticleActionLog::where([
['person_id', '=', $person->id],
['article_id', '=', $article->id],
])->count();
}

$array = array_map('utf8_decode', array_values($row));
fputcsv($file, $array, ';');
}
fclose($file);
Expand Down

0 comments on commit 6542d81

Please sign in to comment.