Skip to content

Commit 23b0caf

Browse files
committed
UP
1 parent 5b93258 commit 23b0caf

File tree

3 files changed

+256
-316
lines changed

3 files changed

+256
-316
lines changed

README.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ Create a new page that extends the `KanbanBoard` class and configure it with all
6464
```php
6565
namespace App\Filament\Resources\TaskResource\Pages;
6666

67-
use App\Filament\Resources\TaskResource;
68-
use Relaticle\Flowforge\Filament\Resources\Pages\KanbanBoard;
67+
use App\Filament\Resources\TaskResource;use Relaticle\Flowforge\Filament\Pages\KanbanBoardPage;
6968

70-
class TaskKanbanBoard extends KanbanBoard
69+
class TaskKanbanBoard extends KanbanBoardPage
7170
{
7271
protected static string $resource = TaskResource::class;
7372

@@ -339,15 +338,9 @@ While it's possible to define configuration in the model, we recommend keeping a
339338
You can customize the forms used for creating and editing cards by implementing custom form methods in your Kanban board page:
340339

341340
```php
342-
use App\Filament\Resources\TaskResource;
343-
use Relaticle\Flowforge\Filament\Resources\Pages\KanbanBoard;
344-
use Filament\Forms\Components\TextInput;
345-
use Filament\Forms\Components\Select;
346-
use Filament\Forms\Components\DatePicker;
347-
use Filament\Forms\Components\RichEditor;
348-
use Filament\Forms\Form;
349-
350-
class TaskKanbanBoard extends KanbanBoard
341+
use Filament\Forms\Components\DatePicker;use Filament\Forms\Components\RichEditor;use Filament\Forms\Components\Select;use Filament\Forms\Components\TextInput;use Filament\Forms\Form;use Relaticle\Flowforge\Filament\Pages\KanbanBoardPage;
342+
343+
class TaskKanbanBoard extends KanbanBoardPage
351344
{
352345
// ... other configurations
353346

src/Filament/Pages/KanbanBoardPage.php

Lines changed: 251 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,287 @@
22

33
namespace Relaticle\Flowforge\Filament\Pages;
44

5+
use App\Models\Task;
6+
use Exception;
57
use Filament\Pages\Page;
68
use Relaticle\Flowforge\Contracts\IKanbanAdapter;
79

8-
class KanbanBoardPage extends Page
10+
abstract class KanbanBoardPage extends Page
911
{
1012
protected static string $view = 'flowforge::filament.pages.kanban-board-page';
1113

1214
/**
13-
* @var IKanbanAdapter
15+
* @var string
1416
*/
15-
protected IKanbanAdapter $adapter;
17+
protected string $statusField = 'status';
1618

1719
/**
18-
* Set the Kanban adapter for this page.
20+
* @var array<string, string>
21+
*/
22+
protected array $statusValues = [];
23+
24+
/**
25+
* @var string
26+
*/
27+
protected string $titleAttribute = 'name';
28+
29+
/**
30+
* @var string|null
31+
*/
32+
protected ?string $descriptionAttribute = null;
33+
34+
/**
35+
* @var array<string, string>
36+
*/
37+
protected array $cardAttributes = [];
38+
39+
/**
40+
* @var array<string, string>|null
41+
*/
42+
protected ?array $statusColors = null;
43+
44+
/**
45+
* @var string|null
46+
*/
47+
protected ?string $orderField = null;
48+
49+
/**
50+
* @var callable|null
51+
*/
52+
protected mixed $createFormCallback = null;
53+
54+
/**
55+
* @var IKanbanAdapter|null
56+
*/
57+
protected ?IKanbanAdapter $adapter = null;
58+
59+
/**
60+
* @var string|null
61+
*/
62+
protected ?string $recordLabel = null;
63+
64+
/**
65+
* @var string|null
66+
*/
67+
protected ?string $pluralRecordLabel = null;
68+
69+
/**
70+
* Mount the page.
71+
*
72+
* @return void
73+
*/
74+
public function mount(): void
75+
{
76+
// This method can be overridden by child classes
77+
}
78+
79+
/**
80+
* Set the status field for the Kanban board.
81+
*
82+
* @param string $field
83+
* @return static
84+
*/
85+
public function statusField(string $field): static
86+
{
87+
$this->statusField = $field;
88+
89+
return $this;
90+
}
91+
92+
/**
93+
* Set the status values for the Kanban board.
94+
*
95+
* @param array<string, string> $values
96+
* @return static
97+
*/
98+
public function statusValues(array $values): static
99+
{
100+
$this->statusValues = $values;
101+
102+
return $this;
103+
}
104+
105+
/**
106+
* Set the title attribute for the Kanban cards.
107+
*
108+
* @param string $attribute
109+
* @return static
110+
*/
111+
public function titleAttribute(string $attribute): static
112+
{
113+
$this->titleAttribute = $attribute;
114+
115+
return $this;
116+
}
117+
118+
/**
119+
* Set the description attribute for the Kanban cards.
120+
*
121+
* @param string $attribute
122+
* @return static
123+
*/
124+
public function descriptionAttribute(string $attribute): static
125+
{
126+
$this->descriptionAttribute = $attribute;
127+
128+
return $this;
129+
}
130+
131+
/**
132+
* Set the card attributes for the Kanban cards.
133+
*
134+
* @param array<string, string> $attributes
135+
* @return static
136+
*/
137+
public function cardAttributes(array $attributes): static
138+
{
139+
$this->cardAttributes = $attributes;
140+
141+
return $this;
142+
}
143+
144+
/**
145+
* Set the status colors for the Kanban board columns.
146+
*
147+
* @param array<string, string> $colors
148+
* @return static
149+
*/
150+
public function statusColors(array $colors): static
151+
{
152+
$this->statusColors = $colors;
153+
154+
return $this;
155+
}
156+
157+
/**
158+
* Set the order field for the Kanban board.
159+
*
160+
* @param string $field
161+
* @return static
162+
*/
163+
public function orderField(string $field): static
164+
{
165+
$this->orderField = $field;
166+
167+
return $this;
168+
}
169+
170+
public function createForm(callable $callback): static
171+
{
172+
$this->createFormCallback = $callback;
173+
174+
return $this;
175+
}
176+
177+
/**
178+
* Set a custom adapter for the Kanban board.
19179
*
20180
* @param IKanbanAdapter $adapter
21181
* @return static
22182
*/
23183
public function adapter(IKanbanAdapter $adapter): static
24184
{
25185
$this->adapter = $adapter;
26-
186+
27187
return $this;
28188
}
29189

30190
/**
31-
* Get the Kanban adapter.
191+
* Set the singular record label for the Kanban items.
32192
*
33-
* @return IKanbanAdapter
193+
* @param string $label
194+
* @return static
34195
*/
35-
public function getAdapter(): IKanbanAdapter
196+
public function recordLabel(string $label): static
36197
{
37-
return $this->adapter;
198+
$this->recordLabel = $label;
199+
200+
return $this;
38201
}
39202

40203
/**
41-
* Mount the page.
204+
* Set the plural record label for the Kanban items.
42205
*
43-
* @return void
206+
* @param string $label
207+
* @return static
44208
*/
45-
public function mount(): void
209+
public function pluralRecordLabel(string $label): static
46210
{
47-
if (!isset($this->adapter)) {
48-
throw new \Exception('Kanban adapter not set. Use the adapter() method to set the adapter.');
211+
$this->pluralRecordLabel = $label;
212+
213+
return $this;
214+
}
215+
216+
/**
217+
* Get the Kanban adapter.
218+
*
219+
* @return IKanbanAdapter
220+
* @throws Exception
221+
*/
222+
public function getAdapter(): IKanbanAdapter
223+
{
224+
if ($this->adapter) {
225+
return $this->adapter;
49226
}
227+
228+
$model = $this->getModel();
229+
230+
// Check if the model uses the HasKanbanBoard trait
231+
if (method_exists($model, 'getKanbanAdapter')) {
232+
// Create an instance and configure it
233+
$instance = new $model();
234+
235+
// Override default values if they are provided
236+
if (method_exists($instance, 'setKanbanStatusField') && $this->statusField) {
237+
$instance->setKanbanStatusField($this->statusField);
238+
}
239+
240+
if (method_exists($instance, 'setKanbanStatusValues') && $this->statusValues) {
241+
$instance->setKanbanStatusValues($this->statusValues);
242+
}
243+
244+
if (method_exists($instance, 'setKanbanTitleAttribute') && $this->titleAttribute) {
245+
$instance->setKanbanTitleAttribute($this->titleAttribute);
246+
}
247+
248+
if (method_exists($instance, 'setKanbanDescriptionAttribute') && $this->descriptionAttribute) {
249+
$instance->setKanbanDescriptionAttribute($this->descriptionAttribute);
250+
}
251+
252+
if (method_exists($instance, 'setKanbanCardAttributes') && $this->cardAttributes) {
253+
$instance->setKanbanCardAttributes($this->cardAttributes);
254+
}
255+
256+
if (method_exists($instance, 'setKanbanStatusColors') && $this->statusColors) {
257+
$instance->setKanbanStatusColors($this->statusColors);
258+
}
259+
260+
if (method_exists($instance, 'setKanbanOrderField') && $this->orderField) {
261+
$instance->setKanbanOrderField($this->orderField);
262+
}
263+
264+
if (method_exists($instance, 'setKanbanCreateFormCallback') && $this->createFormCallback) {
265+
$instance->setKanbanCreateFormCallback($this->createFormCallback);
266+
}
267+
268+
if (method_exists($instance, 'setKanbanRecordLabel') && $this->recordLabel) {
269+
$instance->setKanbanRecordLabel($this->recordLabel);
270+
}
271+
272+
if (method_exists($instance, 'setKanbanPluralRecordLabel') && $this->pluralRecordLabel) {
273+
$instance->setKanbanPluralRecordLabel($this->pluralRecordLabel);
274+
}
275+
276+
return $instance->getKanbanAdapter();
277+
}
278+
279+
throw new Exception('Model does not use the HasKanbanBoard trait.');
280+
}
281+
282+
/**
283+
* @throws Exception
284+
*/
285+
private function getModel(): string {
286+
throw new Exception('getModel() method not implemented.');
50287
}
51288
}

0 commit comments

Comments
 (0)