Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static function table(Table $table): Table
])
->headerActions([
ExportAction::make(),

// VolunteerResource\Actions\ImportVolunteersAction::make(),
Tables\Actions\CreateAction::make()
->requiresConfirmation()
->modalHeading(__('volunteer.modal.heading'))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\VolunteerResource\Actions;

use App\Enum\VolunteerRole;
use App\Enum\VolunteerSpecialization;
use App\Models\Volunteer;
use Illuminate\Support\Collection;
use pxlrbt\FilamentExcel\Actions\Pages\ExportAction;
use pxlrbt\FilamentExcel\Columns\Column;
use pxlrbt\FilamentExcel\Exports\ExcelExport;

class ExportVolunteersExample extends ExportAction
{
protected function setUp(): void
{
parent::setUp();
$this->label(__('volunteer.labels.download_example'));

$this->exports([
ExcelExport::make()
->withColumns([
Column::make('first_name')
->heading(__('volunteer.field.first_name')),
Column::make('last_name')
->heading(__('volunteer.field.last_name')),
Column::make('email')
->heading(__('volunteer.field.email')),
Column::make('phone')
->heading(__('volunteer.field.phone')),
Column::make('cnp')
->heading(__('volunteer.field.cnp')),
Column::make('role')
->heading(__('volunteer.field.role'))
->formatStateUsing(fn (VolunteerRole $state) => $state->label()),
Column::make('specializations')
->heading(__('volunteer.field.specializations'))
->formatStateUsing(fn (Collection $state) => $state
->implode(fn (VolunteerSpecialization $item) => $item->label(), ', ')),
Column::make('has_first_aid_accreditation')
->heading(__('volunteer.field.has_first_aid_accreditation'))
->formatStateUsing(fn ($state) => $state ? __('general.boolean.yes') : __('general.boolean.no')),
Column::make('county')
->heading(__('general.county'))
->formatStateUsing(fn ($state) => $state->name),
Column::make('city')
->heading(__('general.city'))
->formatStateUsing(fn ($state) => $state->name),
])
->withFilename(__('volunteer.file_name.download_example')),
]);
}

public function handleExport(array $data)
{
$examples = Volunteer::query()
->with(['county', 'city'])
->limit(1)
->get();

$exportable = $this->getSelectedExport($data);

return app()->call([$exportable, 'hydrate'], [
'livewire' => $this->getLivewire(),
'records' => $examples,
'formData' => data_get($data, $exportable->getName()),
])->export();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\VolunteerResource\Actions;

use App\Enum\VolunteerRole;
use App\Enum\VolunteerSpecialization;
use App\Models\City;
use App\Models\County;
use App\Models\Organisation;
use App\Models\Volunteer;
use Filament\Forms\Components\Select;
use Illuminate\Validation\Rule;
use Konnco\FilamentImport\Actions\ImportAction;
use Konnco\FilamentImport\Actions\ImportField;

class ImportVolunteersAction extends ImportAction
{
protected function setUp(): void
{
parent::setUp();

$this->label(__('volunteer.labels.import'));
$this->successNotificationTitle(__('volunteer.field.success_import'));
$this->visible(fn () => auth()->user()->isPlatformAdmin() || auth()->user()->isOrgAdmin());
$this->handleBlankRows(true);
$this->fields([
Select::make('organisation_id')
->options(function () {
if (auth()->user()->isOrgAdmin()) {
$organisation = auth()->user()->organisation;

return [$organisation->id => $organisation->name];
}

return Organisation::all()
->pluck('name', 'id');
})
->searchable()
->label(__('organisation.label.singular'))
->required(),
ImportField::make('first_name')
->label(__('volunteer.field.first_name'))
->required(),
ImportField::make('last_name')
->label(__('volunteer.field.last_name'))
->required(),
ImportField::make('email')
->label(__('volunteer.field.email'))
->required(),
ImportField::make('phone')
->label(__('volunteer.field.phone')),
ImportField::make('cnp')
->label(__('volunteer.field.cnp')),
ImportField::make('role')
->label(__('volunteer.field.role'))
->required(),
ImportField::make('specializations')
->label(__('volunteer.field.specializations'))
->required(),
ImportField::make('has_first_aid_accreditation')
->label(__('volunteer.field.has_first_aid_accreditation')),
ImportField::make('county')
->label(__('general.county')),
ImportField::make('city')
->label(__('general.city')),
]);

$this->handleRecordCreation(function (array $data) {
$validator = \Validator::make($data, [
'organisation_id' => [
'required',
Rule::in(Organisation::all()->pluck('id')),
],
'first_name' => [
'required',
'max:255',
'string',
],
'last_name' => [
'required',
'max:255',
'string',
],
'email' => [
'required',
'max:255',
'email',
],
'phone' => [
'numeric',
'min:10',
],
'role' => [
'required',
Rule::in(VolunteerRole::options()),
],
'specializations' => ['required'],
]);

$data = $validator->getData();
$messages = $validator->getMessageBag();
if ($messages->messages()) {
$this->setFailureMsg($messages->messages());

return new Volunteer();
}
$roles = VolunteerRole::options();
$role = array_search($data['role'], $roles);

$specializations = explode(',', $data['specializations']);
$allSpecializations = VolunteerSpecialization::options();
$newSpecializations = [];
foreach ($specializations as $specialization) {
$specializationFromEnum = array_search(trim($specialization), $allSpecializations);
if (! $specializationFromEnum) {
$this->setFailureMsg([
__(
'validation.in_array',
['attribute' => __('volunteer.field.specializations') . ' (' . $specialization . ')',
'other' => implode(', ', $allSpecializations),
]
),
]);

return new Volunteer();
}
$newSpecializations[] = $specializationFromEnum;
}

$firstAID = false;
if (isset($data['has_first_aid_accreditation'])) {
$firstAID = (bool) $data['has_first_aid_accreditation'];
}

$fields = ['organisation_id' => $data['organisation_id'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone' => $data['phone'] ?? null,
'cnp' => $data['cnp'] ?? null,
'role' => $role,
'specializations' => $newSpecializations,
'has_first_aid_accreditation' => $firstAID,
];

if (isset($data['county'])) {
$county = County::query()
->where('name', 'like', trim($data['county']))
->first();

if (! $county) {
$this->setFailureMsg([
__(
'validation.in_array',
[
'attribute' => __('general.county') . ' (' . $data['county'] . ')',
'other' => County::all()
->map(fn ($item) => $item->name)
->implode(', '),
]
),
]);

return new Volunteer();
}
$fields['county_id'] = $county->id;

if (isset($data['city'])) {
$city = City::query()
->search(trim($data['city']))
->where('county_id', $county->id)
->first();

if (! $city) {
$this->setFailureMsg([
__(
'validation.in_array',
[
'attribute' => __('general.city') . ' (' . $data['city'] . ')',
'other' => __(
'general.localities_from_county',
['county' => $data['county']]
),
]
),
]);

return new Volunteer();
}
$fields['city_id'] = $city->id;
}
}

return Volunteer::create($fields);
});
}

public function setFailureMsg(array $messages)
{
foreach ($messages as &$msg) {
$msg = \is_array($msg) ? implode(' ', $msg) : $msg;
}
$msgString = implode(' ', $messages);
$msgString = substr($msgString, 0, 100);
$this->failureNotificationTitle($msgString);
$this->failure();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace App\Filament\Resources\VolunteerResource\Pages;

use App\Filament\Resources\VolunteerResource;
use App\Filament\Resources\VolunteerResource\Actions\ExportVolunteersExample;
use App\Filament\Resources\VolunteerResource\Actions\ImportVolunteersAction;
use Filament\Pages\Actions;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Builder;
Expand All @@ -17,6 +19,8 @@ protected function getActions(): array
{
return [
Actions\CreateAction::make(),
ImportVolunteersAction::make(),
ExportVolunteersExample::make(),
];
}

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"laravel/tinker": "^2.8",
"league/flysystem-aws-s3-v3": "^3.21",
"pxlrbt/filament-excel": "^1.1",
"sentry/sentry-laravel": "^4.1"
"sentry/sentry-laravel": "^4.1",
"konnco/filament-import": "1.6.0"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.13",
Expand Down
Loading