Add export action
This commit is contained in:
parent
529bdf32bf
commit
ea6a98af18
|
@ -0,0 +1,53 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* @param Collection<int, Member>
|
||||
*/
|
||||
public function handle(Collection $members): string
|
||||
{
|
||||
$csv = Writer::createFromString();
|
||||
|
||||
$csv->insertOne(['Nachname', 'Vorname', 'Adresse', 'PLZ', 'Ort', 'Haupt-Telefon', 'Mobiltelefon', 'Arbeits-Telefon']);
|
||||
|
||||
foreach ($members as $member) {
|
||||
$csv->insertOne([
|
||||
$member->lastname,
|
||||
$member->firstname,
|
||||
$member->address,
|
||||
$member->zip,
|
||||
$member->location,
|
||||
$member->mainPhone,
|
||||
$member->mobilePhone,
|
||||
$member->workPhone,
|
||||
]);
|
||||
}
|
||||
|
||||
return $csv->toString();
|
||||
}
|
||||
|
||||
public function asController(ActionRequest $request): StreamedResponse
|
||||
{
|
||||
$filter = FilterScope::fromRequest($request->input('filter'));
|
||||
|
||||
$contents = $this->handle(Member::withFilter($filter)->get());
|
||||
|
||||
Storage::disk('temp')->put('mitglieder.csv', $contents);
|
||||
|
||||
return Storage::disk('temp')->download('mitglieder.csv');
|
||||
}
|
||||
}
|
|
@ -36,6 +36,7 @@
|
|||
"laravel/telescope": "^4.13",
|
||||
"laravel/tinker": "^2.0",
|
||||
"laravel/ui": "^3.0",
|
||||
"league/csv": "^9.9",
|
||||
"lorisleiva/laravel-actions": "^2.4",
|
||||
"monicahq/laravel-sabre": "^1.6",
|
||||
"nunomaduro/collision": "^6.1",
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "0b1ef2c8dbb80fed5f32017b3ea53955",
|
||||
"content-hash": "12e35b75b7cd461b7e989e43ccf8d55b",
|
||||
"packages": [
|
||||
{
|
||||
"name": "beyondcode/laravel-dump-server",
|
||||
|
@ -2731,6 +2731,93 @@
|
|||
],
|
||||
"time": "2022-12-11T20:36:23+00:00"
|
||||
},
|
||||
{
|
||||
"name": "league/csv",
|
||||
"version": "9.9.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/thephpleague/csv.git",
|
||||
"reference": "b4418ede47fbd88facc34e40a16c8ce9153b961b"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/thephpleague/csv/zipball/b4418ede47fbd88facc34e40a16c8ce9153b961b",
|
||||
"reference": "b4418ede47fbd88facc34e40a16c8ce9153b961b",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-json": "*",
|
||||
"ext-mbstring": "*",
|
||||
"php": "^8.1.2"
|
||||
},
|
||||
"require-dev": {
|
||||
"doctrine/collections": "^2.1.2",
|
||||
"ext-dom": "*",
|
||||
"ext-xdebug": "*",
|
||||
"friendsofphp/php-cs-fixer": "^v3.14.3",
|
||||
"phpbench/phpbench": "^1.2.8",
|
||||
"phpstan/phpstan": "^1.10.4",
|
||||
"phpstan/phpstan-deprecation-rules": "^1.1.2",
|
||||
"phpstan/phpstan-phpunit": "^1.3.10",
|
||||
"phpstan/phpstan-strict-rules": "^1.5.0",
|
||||
"phpunit/phpunit": "^10.0.14"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-dom": "Required to use the XMLConverter and the HTMLConverter classes",
|
||||
"ext-iconv": "Needed to ease transcoding CSV using iconv stream filters"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "9.x-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"files": [
|
||||
"src/functions_include.php"
|
||||
],
|
||||
"psr-4": {
|
||||
"League\\Csv\\": "src"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Ignace Nyamagana Butera",
|
||||
"email": "nyamsprod@gmail.com",
|
||||
"homepage": "https://github.com/nyamsprod/",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"description": "CSV data manipulation made easy in PHP",
|
||||
"homepage": "https://csv.thephpleague.com",
|
||||
"keywords": [
|
||||
"convert",
|
||||
"csv",
|
||||
"export",
|
||||
"filter",
|
||||
"import",
|
||||
"read",
|
||||
"transform",
|
||||
"write"
|
||||
],
|
||||
"support": {
|
||||
"docs": "https://csv.thephpleague.com",
|
||||
"issues": "https://github.com/thephpleague/csv/issues",
|
||||
"rss": "https://github.com/thephpleague/csv/releases.atom",
|
||||
"source": "https://github.com/thephpleague/csv"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://github.com/sponsors/nyamsprod",
|
||||
"type": "github"
|
||||
}
|
||||
],
|
||||
"time": "2023-03-11T15:57:12+00:00"
|
||||
},
|
||||
{
|
||||
"name": "league/flysystem",
|
||||
"version": "3.12.3",
|
||||
|
@ -11846,14 +11933,7 @@
|
|||
"time": "2023-01-11T23:22:44+00:00"
|
||||
}
|
||||
],
|
||||
"aliases": [
|
||||
{
|
||||
"package": "zoomyboy/tex",
|
||||
"version": "dev-main",
|
||||
"alias": "1.0",
|
||||
"alias_normalized": "1.0.0.0"
|
||||
}
|
||||
],
|
||||
"aliases": [],
|
||||
"minimum-stability": "dev",
|
||||
"stability-flags": {
|
||||
"zoomyboy/laravel-nami": 20,
|
||||
|
|
|
@ -17,6 +17,7 @@ use App\Efz\ShowEfzDocumentAction;
|
|||
use App\Home\Actions\IndexAction as HomeIndexAction;
|
||||
use App\Initialize\Actions\InitializeAction;
|
||||
use App\Initialize\Actions\InitializeFormAction;
|
||||
use App\Member\Actions\ExportAction;
|
||||
use App\Member\Actions\MemberResyncAction;
|
||||
use App\Member\Actions\MemberShowAction;
|
||||
use App\Member\Actions\SearchAction;
|
||||
|
@ -56,6 +57,7 @@ Route::group(['middleware' => 'auth:web'], function (): void {
|
|||
Route::resource('member.course', CourseController::class);
|
||||
Route::get('/member/{member}/efz', ShowEfzDocumentAction::class)->name('efz');
|
||||
Route::get('/member/{member}/resync', MemberResyncAction::class)->name('member.resync');
|
||||
Route::get('member-export', ExportAction::class)->name('member-export');
|
||||
Route::get('/activity', ActivityIndexAction::class)->name('activity.index');
|
||||
Route::get('/activity/{activity}/edit', ActivityEditAction::class)->name('activity.edit');
|
||||
Route::get('/activity/create', ActivityCreateAction::class)->name('activity.create');
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Member;
|
||||
|
||||
use App\Member\Member;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ExportCsvActionTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/**
|
||||
* A basic feature test example.
|
||||
*/
|
||||
public function testItExportsACsvFile(): void
|
||||
{
|
||||
Storage::fake('temp');
|
||||
|
||||
$this->withoutExceptionHandling()->login()->loginNami();
|
||||
Member::factory()->defaults()->postBillKind()->create(['firstname' => 'Jane']);
|
||||
Member::factory()->defaults()->emailBillKind()->create(['firstname' => 'Max']);
|
||||
|
||||
$response = $this->callFilter('member-export', ['bill_kind' => 'Post']);
|
||||
|
||||
$response->assertDownload('mitglieder.csv');
|
||||
$contents = Storage::disk('temp')->get('mitglieder.csv');
|
||||
$this->assertTrue(str_contains($contents, 'Jane'));
|
||||
$this->assertFalse(str_contains($contents, 'Max'));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue