mokhosh / filament-kanban

Add kanban boards to your Filament pages
https://filamentphp.com/plugins/mokhosh-kanban
MIT License
228 stars 32 forks source link

Cannot access protected property App\Filament\Pages\TasksKanbanBoard::$title #45

Closed monzer15 closed 1 month ago

monzer15 commented 1 month ago

What happened?

Here's the resource:

<?php

namespace App\Filament\Pages;

use App\Models\Project;
use App\Models\Task;
use App\Models\TaskAssignee;
use App\Models\TaskStatus;
use App\Models\TaskType;
use Filament\Actions\Action;
use Filament\Facades\Filament;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\Fieldset;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Form;
use Filament\Tables\Grouping\Group;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Mokhosh\FilamentKanban\Pages\KanbanBoard;

class TasksKanbanBoard extends KanbanBoard
{
    use InteractsWithForms;

    protected static string $model = Task::class;
    protected static string $recordStatusAttribute = 'task_status_id';

    protected static ?string $title = "tasks";

    protected static ?string $slug = "tasks";

    protected static ?int $navigationSort = 3;

    public function mount(): void
    {
        parent::mount();
        session()->put('project', request()->query('project'));
    }

    public function getHeading(): \Illuminate\Contracts\Support\Htmlable|string
    {
        if (session()->has('project')) {
            return Project::firstWhere('no', session('project'))->name;
        }
        return parent::getHeading(); // TODO: Change the autogenerated stub
    }

    public static function getNavigationBadge(): ?string
    {
        return Task::count();
    }

    public function form(Form $form): Form
    {
        return $form
            ->schema([
                Select::make('project'),
            ]);
    }

    protected function statuses(): Collection
    {
        return collect(TaskStatus::select(['id', 'title'])->get()->toArray());
    }

    protected function records(): Collection
    {
        if (session()->has('project'))
            return Task::with(['team', 'owner.media', 'assignees.media', 'project'])
                ->whereHas('project', function ($query) {
                    $query->where('no', session('project'));
                })
                ->ordered()
                ->get();

        return Task::with(['team', 'owner.media', 'assignees.media', 'project'])->ordered()->get();
    }

    public function onStatusChanged(int $recordId, string $status, array $fromOrderedIds, array $toOrderedIds): void
    {
        Task::find($recordId)->update(['task_status_id' => $status]);
        Task::setNewOrder($toOrderedIds);
    }

    public function onSortChanged(int $recordId, string $status, array $orderedIds): void
    {
        Task::setNewOrder($orderedIds);
    }

    protected function getEditModalFormSchema(null|int $recordId): array
    {
        return [
            Section::make()
                ->columns(3)
                ->schema([
                    \Filament\Forms\Components\Group::make()->schema([

                        Hidden::make('project_id')->default(function () {
                            if (session()->has('project')) {
                                return Project::firstWhere('no', session('project'))->id;
                            }
                            return null;
                        }),
                        Section::make(__("Task details"))
                            ->schema([
                                TextInput::make('title')
                                    ->autofocus()
                                    ->maxLength(255)
                                    ->required(),

                                Textarea::make('description')
                                    ->maxLength(1400)
                                    ->nullable(),
                            ])
                            ->columns(1),

                    ])->columnSpan(['lg' => 2]),

                    \Filament\Forms\Components\Group::make()->schema([

                        Select::make('task_status_id')
                            ->label(__('Task status'))
                            ->options(TaskStatus::pluck('title', 'id'))
                            ->required(),

                        Select::make('task_type_id')
                            ->label(__('Task type'))
                            ->options(TaskType::pluck('name', 'id'))
                            ->required(),

                        Select::make('priority')
                            ->label(__('Priority'))
                            ->required()
                            ->default('normal')
                            ->options([
                                'low' => 'Low',
                                'normal' => 'Normal',
                                'high' => 'High',
                            ]),

                        Select::make('assignees')
                            ->label(__('Assignees'))
                            ->relationship('assignees')
                            ->options(get_team_members()->pluck('username', 'id'))
                            ->searchable()
                            ->multiple()
                            ->required(),

                        Checkbox::make('urgent'),

                    ])
                        ->columnSpan(['lg' => 1]),
                ])
        ];
    }

    protected function editRecord($recordId, array $data, array $state): void
    {
        Task::find($recordId)->update([
            'title' => $data['title'],
            'description' => $data['description'],
            'urgent' => $data['urgent'] ?? false,
        ]);
    }

    protected function getHeaderActions(): array
    {
        return [
            Action::make('create')
                ->label(__('Create Task'))
                ->model(Task::class)
                ->form([
                    Section::make()
                        ->columns(3)
                        ->schema([
                            \Filament\Forms\Components\Group::make()->schema([

                                Hidden::make('project_id')->default(function () {
                                    if (session()->has('project')) {
                                        return Project::firstWhere('no', session('project'))->id;
                                    }
                                    return null;
                                }),
                                Section::make(__("Task details"))
                                    ->schema([
                                        TextInput::make('title')
                                            ->autofocus()
                                            ->maxLength(255)
                                            ->required(),

                                        Textarea::make('description')
                                            ->maxLength(1400)
                                            ->nullable(),
                                    ])
                                    ->columns(1),

                            ])->columnSpan(['lg' => 2]),

                            \Filament\Forms\Components\Group::make()->schema([

                                Select::make('task_status_id')
                                    ->label(__('Task status'))
                                    ->default(TaskStatus::first()?->id)
                                    ->options(TaskStatus::pluck('title', 'id'))
                                    ->required(),

                                Select::make('task_type_id')
                                    ->label(__('Task type'))
                                    ->default(TaskType::first()?->id)
                                    ->options(TaskType::pluck('name', 'id'))
                                    ->required(),

                                Select::make('priority')
                                    ->label(__('Priority'))
                                    ->required()
                                    ->default('normal')
                                    ->options([
                                        'low' => 'Low',
                                        'normal' => 'Normal',
                                        'high' => 'High',
                                    ]),

                                Select::make('assignees')
                                    ->label(__('Assignees'))
                                    ->relationship('assignees')
                                    ->options(get_team_members()->pluck('username', 'id'))
                                    ->searchable()
                                    ->multiple()
                                    ->required(),

                                Checkbox::make('urgent'),

                            ])
                                ->columnSpan(['lg' => 1]),
                        ])

                ])
                ->action(function (array $data): void {
                    $data['team_id'] = get_team()->id;
                    $data['user_id'] = Filament::auth()->id();
                    $task = Task::create($data);

                    foreach ($data['assignees'] ?? [] as $user_id) {
                        TaskAssignee::create([
                            'task_id' => $task->id,
                            'user_id' => $user_id,
                        ]);
                    }
                }),
        ];
    }

    protected function additionalRecordData(Model $record): Collection
    {
        return collect(['test' => 'ok']);
    }
}

How to reproduce the bug

use the resource above

Package Version

2.8.0

PHP Version

8.2

Laravel Version

11.7.0

Which operating systems does with happen with?

No response

Which browsers does with happen with?

No response

Notes

.

mokhosh commented 1 month ago

You understand there are so many classes and functions in this code that I don't have, so I can't reproduce your issue.

Please either provide a reproduction repo, or try to narrow down where the issue happens so I can help you.

monzer15 commented 1 month ago

The issue was

use InteractsWithForms;