Skip to content

Commit

Permalink
Import volunteers from excel
Browse files Browse the repository at this point in the history
  • Loading branch information
alexPopaCode4 committed Mar 13, 2024
1 parent 4f9e085 commit f0f0d00
Show file tree
Hide file tree
Showing 4 changed files with 883 additions and 764 deletions.
107 changes: 107 additions & 0 deletions app/Filament/Resources/VolunteerResource/Pages/ListVolunteers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@

namespace App\Filament\Resources\VolunteerResource\Pages;

use App\Enum\VolunteerRole;
use App\Enum\VolunteerSpecialization;
use App\Filament\Resources\VolunteerResource;
use App\Models\City;
use App\Models\County;
use App\Models\Organisation;
use App\Models\Volunteer;
use Filament\Pages\Actions;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Builder;
use Konnco\FilamentImport\Actions\ImportAction;
use Konnco\FilamentImport\Actions\ImportField;

class ListVolunteers extends ListRecords
{
Expand All @@ -17,6 +25,105 @@ protected function getActions(): array
{
return [
Actions\CreateAction::make(),
ImportAction::make()
->fields([
ImportField::make('organisation_name')
->label(__('volunteer.field.organisation'))
->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')),
])
->handleRecordCreation(function (array $data) {
if (! isset($data['organisation_name']) ||
! isset($data['first_name']) ||
! isset($data['last_name']) ||
! isset($data['email']) ||
! isset($data['role']) ||
! isset($data['specializations'])) {
return new Volunteer();
}

$organisation = Organisation::query()
->where('name', 'like', $data['organisation_name'])
->first();

if (! $organisation) {
return new Volunteer();
}

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

$specializations = explode(',', $data['specializations']);
$allSpecializations = VolunteerSpecialization::options();
$newSpecializations = [];
foreach ($specializations as $specialization) {
$newSpecializations[] = array_search(trim($specialization), $allSpecializations);
}

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

$fields = ['organisation_id' => $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', $data['county'])
->first();

if ($county) {
$fields['county_id'] = $county->id;

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

if ($city) {
$fields['city_id'] = $city->id;
}
}
}
}

return Volunteer::create($fields);
})
->label(__('volunteer.field.import')),
];
}

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.9",
Expand Down
Loading

0 comments on commit f0f0d00

Please sign in to comment.