Skip to content

Commit

Permalink
wip on volunteer request admin
Browse files Browse the repository at this point in the history
  • Loading branch information
gheorghelupu17 committed Oct 20, 2023
1 parent 843b260 commit b6d6170
Show file tree
Hide file tree
Showing 17 changed files with 238 additions and 87 deletions.
33 changes: 32 additions & 1 deletion app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Filament\Resources\UserResource\RelationManagers\BadgesRelationManager;
use App\Filament\Resources\UserResource\RelationManagers\DonationsRelationManager;
use App\Filament\Resources\UserResource\RelationManagers\VolunteersRelationManager;
use App\Forms\Components\Link;
use App\Models\User;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
Expand Down Expand Up @@ -51,6 +52,15 @@ public static function form(Form $form): Form
return $form
->columns(1)
->schema([
Link::make('organizatii')->type('organization')
->label(__('user.labels.organization'))
->inlineLabel()
->hidden(
fn (callable $get) => UserRole::SUPERMANAGER->is($get('role')) ||
UserRole::SUPERADMIN->is($get('role')) ||
UserRole::USER->is($get('role'))
)
->columnSpanFull(),
TextInput::make('name')
->label(__('user.name'))
->inlineLabel()
Expand All @@ -75,7 +85,11 @@ public static function form(Form $form): Form
Select::make('organization')
->label(__('user.organization'))
->relationship('organization', 'name')
->hidden(fn (callable $get) => UserRole::ADMIN->is($get('role')))
->hidden(
fn (callable $get) => UserRole::SUPERMANAGER->is($get('role')) ||
UserRole::SUPERADMIN->is($get('role')) ||
UserRole::USER->is($get('role'))
)
->searchable()
->inlineLabel()
->preload()
Expand All @@ -87,6 +101,12 @@ public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('id')
->formatStateUsing(function (User $record) {
return sprintf('#%d', $record->id);
})
->label(__('project.labels.id'))
->sortable(),
TextColumn::make('name')
->label(__('user.name'))
->searchable()
Expand Down Expand Up @@ -123,6 +143,12 @@ public static function table(Table $table): Table
true: fn (Builder $query) => $query->has('volunteer'),
false: fn (Builder $query) => $query->doesntHave('volunteer'),
),
TernaryFilter::make('has_verified_email')
->label(__('user.filters.has_verified_email'))
->queries(
true: fn (Builder $query) => $query->whereHasVerifiedEmail(),
false: fn (Builder $query) => $query->whereDoesntHaveVerifiedEmail(),
),

])
->actions([
Expand Down Expand Up @@ -152,4 +178,9 @@ public static function getPages(): array
'edit' => Pages\EditUser::route('/{record}/edit'),
];
}

protected static function getNavigationBadge(): ?string
{
return (string) static::$model::whereHasVerifiedEmail()->count();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Filament\Tables;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Database\Eloquent\Model;

class BadgesRelationManager extends RelationManager
{
Expand All @@ -29,11 +28,6 @@ protected function getTableHeading(): string
return __('user.relations.heading.badges', ['count' => $this->getTableQuery()->count()]);
}

public static function canViewForRecord(Model $record): bool
{
return $record->isDonor();
}

public static function form(Form $form): Form
{
return $form
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;

class DonationsRelationManager extends RelationManager
{
Expand All @@ -31,11 +30,6 @@ protected function getTableHeading(): string | Htmlable | Closure | null
return __('user.relations.heading.donations', ['count' => $this->getTableQuery()->count(), 'total' => $this->getTableQuery()->sum('amount')]);
}

public static function canViewForRecord(Model $record): bool
{
return $record->isDonor();
}

public static function form(Form $form): Form
{
return $form
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@
use Filament\Resources\Table;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Database\Eloquent\Model;

class VolunteersRelationManager extends RelationManager
{
protected static string $relationship = 'volunteerRequest';

protected static ?string $recordTitleAttribute = 'name';

public static function canViewForRecord(Model $record): bool
{
return $record->isDonor();
}

public static function getTitle(): string
{
return __('user.relations.volunteer');
Expand Down
177 changes: 162 additions & 15 deletions app/Filament/Resources/VolunteerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,148 @@

namespace App\Filament\Resources;

use App\Enums\VolunteerStatus;
use App\Filament\Resources\VolunteerResource\Pages;
use App\Models\Volunteer;
use App\Models\VolunteerRequest;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Actions\ViewAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;

class VolunteerResource extends Resource
{
protected static ?string $model = Volunteer::class;

protected static ?string $navigationGroup = 'Administrează';
protected static ?string $model = VolunteerRequest::class;

protected static ?int $navigationSort = 6;

protected static ?string $navigationIcon = 'heroicon-o-collection';

public static function getPluralLabel(): ?string
{
return __('volunteer.label.plural');
}

public static function getModelLabel(): string
{
return __('volunteer.label.singular');
}

protected static function getNavigationGroup(): ?string
{
return __('navigation.group.manage');
}

protected static function getNavigationBadge(): ?string
{
return (string) VolunteerRequest::where('status', VolunteerStatus::APPROVED)->count();
}

public static function form(Form $form): Form
{
return $form
->schema([
//
TextInput::make('name')
->label(__('volunteer.form.name'))
->formatStateUsing(fn ($record) => $record->volunteer->name)
->disabled(),
TextInput::make('email')
->label(__('volunteer.form.email'))
->formatStateUsing(fn ($record) => $record->volunteer->email)
->disabled(),
TextInput::make('volunteer.phone')
->label(__('volunteer.form.phone'))
->formatStateUsing(fn ($record) => $record->volunteer->phone)
->disabled(),
Select::make('status')
->label(__('volunteer.form.status'))
->options(VolunteerStatus::options())
->required(),
Select::make('model_type')
->label(__('volunteer.form.model_type'))
->options([
'App\Models\Project' => 'Project',
'App\Models\Organization' => 'Organization',
])
->disabled(),

]);
}

public static function table(Table $table): Table
{
return $table
->columns([
//
TextColumn::make('id')
->formatStateUsing(function (VolunteerRequest $record) {
return sprintf('#%d', $record->id);
})
->label(__('project.labels.id'))
->sortable(),
TextColumn::make('volunteer.name')
->label(__('volunteer.column.name'))
->searchable(),
TextColumn::make('volunteer.email')
->label(__('volunteer.column.email'))
->searchable(),
TextColumn::make('project')
->label(__('volunteer.column.project'))
->formatStateUsing(fn ($record) => $record->model_type === 'App\Models\Project' ? self::getResourceLink($record, 'project') : 'General')
->searchable(),
TextColumn::make('organization_name')
->label(__('volunteer.column.organization'))
->formatStateUsing(fn ($record) => self::getResourceLink($record, 'organization'))
->searchable(),
IconColumn::make('status')
->label(__('volunteer.column.status'))
->options([
'heroicon-o-clock' => 'pending',
'heroicon-o-check-circle' => 'approved',
'heroicon-o-x-circle' => 'rejected',
])->colors([
'warning' => 'pending',
'success' => 'approved',
'danger' => 'rejected',
]),
TextColumn::make('has_user')
->label(__('volunteer.column.has_user'))
->formatStateUsing(fn ($record) => self::getResourceLink($record, 'user')),

])
->filters([
//
SelectFilter::make('name')
->multiple()
->relationship('volunteer', 'name')
->label(__('volunteer.filters.user')),
TernaryFilter::make('has_user')
->label(__('volunteer.filters.has_user'))
->queries(
true: fn (Builder $query) => $query->whereHas(
'volunteer',
fn (Builder $query) => $query->whereNotNull('user_id')
),
false: fn (Builder $query) => $query->whereHas(
'volunteer',
fn (Builder $query) => $query->whereNull('user_id')
),
),

])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
]);
ViewAction::make()->iconButton(),
EditAction::make()->iconButton(),
DeleteAction::make()->iconButton(),
])->defaultSort('id', 'desc');
}

public static function getRelations(): array
Expand All @@ -57,8 +159,53 @@ public static function getPages(): array
{
return [
'index' => Pages\ListVolunteers::route('/'),
'create' => Pages\CreateVolunteer::route('/create'),
'edit' => Pages\EditVolunteer::route('/{record}/edit'),
];
}

private static function getResourceLink(VolunteerRequest $record, string $type): HtmlString
{
return match ($type) {
'project' => self::getProjectLink($record),
'organization' => self::getOrganizationLink($record),
'user' => self::getUserLink($record),
};
}

private static function getProjectLink(VolunteerRequest $record): HtmlString
{
if ($record->model_type === 'App\Models\Project') {
$url = route('filament.resources.projects.view', $record->model_id);
$name = Str::words($record->model->name, 3, '...');

return new HtmlString(sprintf('<a href="%s">%s</a>', $url, $name));
}

return new HtmlString('General');
}

private static function getOrganizationLink(VolunteerRequest $record)
{
if ($record->model_type === 'App\Models\Organization') {
$url = route('filament.resources.organizations.view', $record->model->id);
$name = Str::words($record->model->name, 3, '...');

return new HtmlString(sprintf('<a href="%s">%s</a>', $url, $name));
}

$url = route('filament.resources.organizations.view', $record->model->organization->id);
$name = Str::words($record->model->organization->name, 3, '...');

return new HtmlString(sprintf('<a href="%s">%s</a>', $url, $name));
}

private static function getUserLink(VolunteerRequest $record): HtmlString
{
if ($record->volunteer->user_id === null) {
return new HtmlString('Nu');
}
$url = route('filament.resources.users.view', $record->volunteer->user_id);
$name = Str::words($record->volunteer->name, 3, '...');

return new HtmlString(sprintf('<a href="%s">%s</a>', $url, $name));
}
}
13 changes: 0 additions & 13 deletions app/Filament/Resources/VolunteerResource/Pages/CreateVolunteer.php

This file was deleted.

21 changes: 0 additions & 21 deletions app/Filament/Resources/VolunteerResource/Pages/EditVolunteer.php

This file was deleted.

Loading

0 comments on commit b6d6170

Please sign in to comment.