2023-03-14 23:55:50 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Member\Actions;
|
|
|
|
|
|
|
|
use App\Member\FilterScope;
|
|
|
|
use App\Member\Member;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use League\Csv\Writer;
|
|
|
|
use Lorisleiva\Actions\ActionRequest;
|
|
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
|
|
|
|
class ExportAction
|
|
|
|
{
|
|
|
|
use AsAction;
|
|
|
|
|
|
|
|
/**
|
2023-03-15 00:08:20 +01:00
|
|
|
* @param Collection<int, Member> $members
|
2023-03-14 23:55:50 +01:00
|
|
|
*/
|
|
|
|
public function handle(Collection $members): string
|
|
|
|
{
|
|
|
|
$csv = Writer::createFromString();
|
|
|
|
|
2023-05-15 15:12:12 +02:00
|
|
|
$csv->insertOne(['Nachname', 'Vorname', 'Adresse', 'PLZ', 'Ort', 'Haupt-Telefon', 'Mobiltelefon', 'Arbeits-Telefon', 'E-Mail-Adresse']);
|
2023-03-14 23:55:50 +01:00
|
|
|
|
|
|
|
foreach ($members as $member) {
|
|
|
|
$csv->insertOne([
|
|
|
|
$member->lastname,
|
|
|
|
$member->firstname,
|
|
|
|
$member->address,
|
|
|
|
$member->zip,
|
|
|
|
$member->location,
|
2023-03-15 00:08:20 +01:00
|
|
|
$member->main_phone,
|
|
|
|
$member->mobile_phone,
|
|
|
|
$member->work_phone,
|
2023-05-15 15:12:12 +02:00
|
|
|
$member->email,
|
2023-03-14 23:55:50 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $csv->toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function asController(ActionRequest $request): StreamedResponse
|
|
|
|
{
|
2024-05-09 02:07:31 +02:00
|
|
|
$members = FilterScope::fromRequest($request->input('filter'))->withOptions(['hitsPerPage' => 20000])->getQuery()->get();
|
2024-01-28 11:42:32 +01:00
|
|
|
$contents = $this->handle($members);
|
2023-03-14 23:55:50 +01:00
|
|
|
|
|
|
|
Storage::disk('temp')->put('mitglieder.csv', $contents);
|
|
|
|
|
|
|
|
return Storage::disk('temp')->download('mitglieder.csv');
|
|
|
|
}
|
|
|
|
}
|