Skip to content

Commit

Permalink
Organization relations managers (#286)
Browse files Browse the repository at this point in the history
* wip

* refresh widgets on update

* wip

* wip

* wip relation managers

* wip

* wip

---------

Co-authored-by: Andrei Ioniță <[email protected]>
  • Loading branch information
gheorghelupu17 and andreiio authored Sep 5, 2023
1 parent 1ccaa19 commit 396df3d
Show file tree
Hide file tree
Showing 27 changed files with 622 additions and 60 deletions.
18 changes: 18 additions & 0 deletions app/Filament/Resources/OrganizationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

use App\Filament\Forms\Components\Download;
use App\Filament\Resources\OrganizationResource\Pages;
use App\Filament\Resources\OrganizationResource\RelationManagers\ActivitiesRelationManager;
use App\Filament\Resources\OrganizationResource\RelationManagers\DonationsRelationManager;
use App\Filament\Resources\OrganizationResource\RelationManagers\ProjectsRelationManager;
use App\Filament\Resources\OrganizationResource\RelationManagers\TicketsRelationManager;
use App\Filament\Resources\OrganizationResource\RelationManagers\UsersRelationManager;
use App\Filament\Resources\OrganizationResource\RelationManagers\VolunteersRelationManager;
use App\Forms\Components\UserLink;
use App\Models\Organization;
use App\Rules\ValidCIF;
Expand Down Expand Up @@ -174,6 +180,18 @@ public static function form(Form $form): Form
]);
}

public static function getRelations(): array
{
return [
ProjectsRelationManager::class,
DonationsRelationManager::class,
UsersRelationManager::class,
VolunteersRelationManager::class,
TicketsRelationManager::class,
ActivitiesRelationManager::class,
];
}

public static function getPages(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Filament\Resources\OrganizationResource\Actions\Pages;

use App\Models\Organization;
use Filament\Forms\Components\Textarea;
use Filament\Pages\Actions\Action;

class RejectOrganizationAction extends Action
Expand Down Expand Up @@ -34,10 +35,18 @@ protected function setUp(): void
])
);

$this->form([
Textarea::make('reason')
->label(__('organization.reject_modal.reason'))
->required(),
]);

$this->modalButton(__('organization.actions.reject'));

$this->action(function (Organization $record) {
$record->markAsRejected();
$this->action(function (Organization $record, array $data) {
$reason = strip_tags($data['reason']);

$record->markAsRejected($reason);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Models\Organization;
use Filament\Tables\Actions\Action;
use Livewire\Component;

class ApproveOrganizationAction extends Action
{
Expand Down Expand Up @@ -39,5 +40,7 @@ protected function setUp(): void
$this->action(function (Organization $record) {
$record->markAsApproved();
});

$this->after(fn (Component $livewire) => $livewire->emit('refreshApprovedOrganizationsWidget'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace App\Filament\Resources\OrganizationResource\Actions\Tables\Organizations;

use App\Models\Organization;
use Filament\Forms\Components\Textarea;
use Filament\Tables\Actions\Action;
use Livewire\Component;

class DeactivateOrganizationAction extends Action
{
Expand Down Expand Up @@ -34,10 +36,20 @@ protected function setUp(): void
])
);

$this->form([
Textarea::make('reason')
->label(__('organization.deactivate_modal.reason'))
->required(),
]);

$this->modalButton(__('organization.actions.deactivate'));

$this->action(function (Organization $record) {
$record->markAsRejected();
$this->action(function (Organization $record, array $data) {
$reason = strip_tags($data['reason']);

$record->markAsRejected($reason);
});

$this->after(fn (Component $livewire) => $livewire->emit('refreshRejectedOrganizationsWidget'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Models\Organization;
use Filament\Tables\Actions\Action;
use Livewire\Component;

class ReactivateOrganizationAction extends Action
{
Expand Down Expand Up @@ -39,5 +40,7 @@ protected function setUp(): void
$this->action(function (Organization $record) {
$record->markAsApproved();
});

$this->after(fn (Component $livewire) => $livewire->emit('refreshApprovedOrganizationsWidget'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace App\Filament\Resources\OrganizationResource\Actions\Tables\Organizations;

use App\Models\Organization;
use Filament\Forms\Components\Textarea;
use Filament\Tables\Actions\Action;
use Livewire\Component;

class RejectOrganizationAction extends Action
{
Expand Down Expand Up @@ -34,10 +36,20 @@ protected function setUp(): void
])
);

$this->form([
Textarea::make('reason')
->label(__('organization.reject_modal.reason'))
->required(),
]);

$this->modalButton(__('organization.actions.reject'));

$this->action(function (Organization $record) {
$record->markAsRejected();
$this->action(function (Organization $record, array $data) {
$reason = strip_tags($data['reason']);

$record->markAsRejected($reason);
});

$this->after(fn (Component $livewire) => $livewire->emit('refreshRejectedOrganizationsWidget'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ protected function getActions(): array
];
}

protected function getFooterWidgets(): array
public function hasCombinedRelationManagerTabsWithForm(): bool
{
return [
OrganizationActivityWidget::class,
];
return true;
}

public function getFormTabLabel(): ?string
{
return __('organization.organization');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\OrganizationResource\RelationManagers;

use App\Filament\Resources\OrganizationResource\Actions\Tables\Activity\ApproveActivityAction;
use App\Filament\Resources\OrganizationResource\Actions\Tables\Activity\RejectActivityAction;
use App\Filament\Resources\OrganizationResource\Actions\Tables\Activity\ViewActivityAction;
use App\Filament\Resources\UserResource;
use App\Models\Activity;
use Carbon\Carbon;
use Filament\Forms;
use Filament\Forms\Components\DatePicker;
use Filament\Resources\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Resources\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\Layout;
use Filament\Tables\Filters\SelectFilter;
use Illuminate\Database\Eloquent\Builder;

class ActivitiesRelationManager extends RelationManager
{
protected static string $relationship = 'activities';

// protected static ?string $recordTitleAttribute = 'description';

public static function getTitle(): string
{
return __('activity.label.plural');
}

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('description')
->required()
->maxLength(255),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('created_at')
->label(__('activity.column.created_at'))
->sortable(),

TextColumn::make('changed_field')
->label(__('activity.column.changed_field'))
->formatStateUsing(
fn ($state) => __('organization.labels.' . $state)
),

TextColumn::make('causer.name')
->label(__('activity.column.causer'))
->description(
fn (Activity $record) => $record->causer?->role->label()
)
->url(
fn (Activity $record) => $record->causer
? UserResource::getUrl('view', $record->causer)
: null
)
->sortable(),

TextColumn::make('status')
->label(__('activity.column.status')),
])
->filters([
Filter::make('created_at')
->columns()
->form([
DatePicker::make('logged_from')
->label(__('activity.filter.logged_from'))
->placeholder(
fn ($state): string => today()
->setDay(17)
->setMonth(11)
->subYear()
->toFormattedDate()
),

DatePicker::make('logged_until')
->label(__('activity.filter.logged_until'))
->placeholder(
fn ($state): string => today()
->toFormattedDate()
),
])
->query(
fn (Builder $query, array $data) => $query->betweenDates(
data_get($data, 'logged_from'),
data_get($data, 'logged_until'),
)
)
->indicateUsing(
fn (array $data) => collect(['logged_from', 'logged_until'])
->mapWithKeys(function (string $filter) use ($data) {
$value = data_get($data, $filter);

if (! \is_null($value)) {
$value = __("activity.indicator.{$filter}", [
'date' => Carbon::parse($value)->toFormattedDate(),
]);
}

return [$filter => $value];
})
->filter()
->all()
),

// SelectFilter::make('causer_id')
// ->relationship('causer', 'name'),

// SelectFilter::make('changed_field'),
])
->filtersLayout(Layout::AboveContent)
->actions([
ViewActivityAction::make(),

ApproveActivityAction::make(),

RejectActivityAction::make(),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Filament\Resources\OrganizationResource\RelationManagers;

use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Resources\Table;
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class DonationsRelationManager extends RelationManager
{
protected static string $relationship = 'donations';

protected static ?string $recordTitleAttribute = 'uuid';

public static function getTitle(): string
{
return __('donation.label.plural');
}

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('uuid')
->required()
->maxLength(255),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('uuid'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
]);
}
}
Loading

0 comments on commit 396df3d

Please sign in to comment.