|
2 | 2 |
|
3 | 3 | namespace Relaticle\Flowforge\Filament\Pages; |
4 | 4 |
|
| 5 | +use App\Models\Task; |
| 6 | +use Exception; |
5 | 7 | use Filament\Pages\Page; |
6 | 8 | use Relaticle\Flowforge\Contracts\IKanbanAdapter; |
7 | 9 |
|
8 | | -class KanbanBoardPage extends Page |
| 10 | +abstract class KanbanBoardPage extends Page |
9 | 11 | { |
10 | 12 | protected static string $view = 'flowforge::filament.pages.kanban-board-page'; |
11 | 13 |
|
12 | 14 | /** |
13 | | - * @var IKanbanAdapter |
| 15 | + * @var string |
14 | 16 | */ |
15 | | - protected IKanbanAdapter $adapter; |
| 17 | + protected string $statusField = 'status'; |
16 | 18 |
|
17 | 19 | /** |
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. |
19 | 179 | * |
20 | 180 | * @param IKanbanAdapter $adapter |
21 | 181 | * @return static |
22 | 182 | */ |
23 | 183 | public function adapter(IKanbanAdapter $adapter): static |
24 | 184 | { |
25 | 185 | $this->adapter = $adapter; |
26 | | - |
| 186 | + |
27 | 187 | return $this; |
28 | 188 | } |
29 | 189 |
|
30 | 190 | /** |
31 | | - * Get the Kanban adapter. |
| 191 | + * Set the singular record label for the Kanban items. |
32 | 192 | * |
33 | | - * @return IKanbanAdapter |
| 193 | + * @param string $label |
| 194 | + * @return static |
34 | 195 | */ |
35 | | - public function getAdapter(): IKanbanAdapter |
| 196 | + public function recordLabel(string $label): static |
36 | 197 | { |
37 | | - return $this->adapter; |
| 198 | + $this->recordLabel = $label; |
| 199 | + |
| 200 | + return $this; |
38 | 201 | } |
39 | 202 |
|
40 | 203 | /** |
41 | | - * Mount the page. |
| 204 | + * Set the plural record label for the Kanban items. |
42 | 205 | * |
43 | | - * @return void |
| 206 | + * @param string $label |
| 207 | + * @return static |
44 | 208 | */ |
45 | | - public function mount(): void |
| 209 | + public function pluralRecordLabel(string $label): static |
46 | 210 | { |
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; |
49 | 226 | } |
| 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.'); |
50 | 287 | } |
51 | 288 | } |
0 commit comments