generated from filamentphp/plugin-skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathHasBoardRecords.php
More file actions
270 lines (217 loc) · 8.1 KB
/
HasBoardRecords.php
File metadata and controls
270 lines (217 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
declare(strict_types=1);
namespace Relaticle\Flowforge\Concerns;
use Exception;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
/**
* Enterprise-grade record management for Board with cursor-based pagination.
* Implements position-based ordering for optimal performance at scale.
*/
trait HasBoardRecords
{
protected string $recordTitleAttribute = 'title';
protected int $cardsPerColumn = 20;
protected array $columnExistenceCache = [];
/**
* Set the record title attribute.
*/
public function recordTitleAttribute(string $attribute): static
{
$this->recordTitleAttribute = $attribute;
return $this;
}
/**
* Get the record title attribute.
*/
public function getRecordTitleAttribute(): string
{
return $this->recordTitleAttribute;
}
/**
* Set the number of cards per column.
*/
public function cardsPerColumn(int $count): static
{
$this->cardsPerColumn = $count;
return $this;
}
/**
* Get the number of cards per column.
*/
public function getCardsPerColumn(): int
{
return $this->cardsPerColumn;
}
/**
* Get records for a specific column with cursor-based pagination.
* Uses position field for optimal performance with large datasets.
*/
public function getBoardRecords(string $columnId): Collection
{
$query = $this->getQuery();
if (! $query) {
return new Collection;
}
$statusField = $this->getColumnIdentifierAttribute();
$livewire = $this->getLivewire();
$limit = property_exists($livewire, 'columnCardLimits')
? ($livewire->columnCardLimits[$columnId] ?? $this->getCardsPerColumn())
: $this->getCardsPerColumn();
$queryClone = (clone $query)->where($statusField, $columnId);
// Apply table filters and search using Filament's native system
if ($livewire->getTable()->isFilterable() || $livewire->hasTableSearch()) {
$baseQuery = $livewire->getFilteredTableQuery();
$queryClone = (clone $baseQuery)->where($statusField, $columnId);
}
$positionField = $this->getPositionIdentifierAttribute();
$keyName = $queryClone->getModel()->getKeyName();
if ($positionField && $this->modelHasColumn($queryClone->getModel(), $positionField)) {
$queryClone->orderBy($positionField, 'asc')
->orderBy($keyName, 'asc'); // Tie-breaker for deterministic order
}
return $queryClone->limit($limit)->get();
}
/**
* Get record count for a column (direct query with filters).
*/
public function getBoardRecordCount(string $columnId): int
{
$query = $this->getQuery();
if (! $query) {
return 0;
}
$statusField = $this->getColumnIdentifierAttribute();
$queryClone = (clone $query)->where($statusField, $columnId);
// Apply table filters and search using Filament's native system
$livewire = $this->getLivewire();
if ($livewire->getTable()->isFilterable() || $livewire->hasTableSearch()) {
$baseQuery = $livewire->getFilteredTableQuery();
$queryClone = (clone $baseQuery)->where($statusField, $columnId);
}
return $queryClone->count();
}
/**
* Get record counts for all columns in a single query using GROUP BY.
*/
public function getBatchedBoardRecordCounts(): array
{
$query = $this->getQuery();
if (! $query) {
return [];
}
$statusField = $this->getColumnIdentifierAttribute();
$livewire = $this->getLivewire();
$queryClone = clone $query;
// Apply table filters and search using Filament's native system
if ($livewire->getTable()->isFilterable() || $livewire->hasTableSearch()) {
$baseQuery = $livewire->getFilteredTableQuery();
$queryClone = clone $baseQuery;
}
return $queryClone
->select(DB::raw("{$statusField} as column_value, COUNT(*) as total"))
->groupBy($statusField)
->pluck('total', 'column_value')
->toArray();
}
/**
* Format a record for display with Infolist entries.
*/
public function formatBoardRecord(Model $record): array
{
$formatted = [
'id' => (string) $record->getKey(),
'title' => data_get($record, $this->getRecordTitleAttribute()),
'column' => data_get($record, $this->getColumnIdentifierAttribute()),
'position' => data_get($record, $this->getPositionIdentifierAttribute()),
'model' => $record,
];
// Process card schema if available
$formatted['schema'] = null;
$schema = $this->getCardSchema($record);
if ($schema !== null) {
// The schema is already built and configured
$schema->model($record);
// Store the schema object with record context for proper Livewire rendering
$formatted['schema'] = $schema;
}
return $formatted;
}
/**
* Check if model has the specified column.
*/
protected function modelHasColumn($model, string $columnName): bool
{
$cacheKey = get_class($model) . '::' . $model->getTable() . '::' . $columnName;
if (! isset($this->columnExistenceCache[$cacheKey])) {
try {
$table = $model->getTable();
$schema = $model->getConnection()->getSchemaBuilder();
$this->columnExistenceCache[$cacheKey] = $schema->hasColumn($table, $columnName);
} catch (Exception) {
$this->columnExistenceCache[$cacheKey] = false;
}
}
return $this->columnExistenceCache[$cacheKey];
}
/**
* Get records before a specific position (for inserting cards).
*/
public function getRecordsBeforePosition(string $columnId, string $position, int $limit = 5): Collection
{
$query = $this->getQuery();
if (! $query) {
return new Collection;
}
$statusField = $this->getColumnIdentifierAttribute();
$positionField = $this->getPositionIdentifierAttribute();
$keyName = $query->getModel()->getKeyName();
return (clone $query)
->where($statusField, $columnId)
->where($positionField, '<', $position)
->orderBy($positionField, 'desc')
->orderBy($keyName, 'desc') // Tie-breaker for deterministic order
->limit($limit)
->get();
}
/**
* Get records after a specific position (for inserting cards).
*/
public function getRecordsAfterPosition(string $columnId, string $position, int $limit = 5): Collection
{
$query = $this->getQuery();
if (! $query) {
return new Collection;
}
$statusField = $this->getColumnIdentifierAttribute();
$positionField = $this->getPositionIdentifierAttribute();
$keyName = $query->getModel()->getKeyName();
return (clone $query)
->where($statusField, $columnId)
->where($positionField, '>', $position)
->orderBy($positionField, 'asc')
->orderBy($keyName, 'asc') // Tie-breaker for deterministic order
->limit($limit)
->get();
}
/**
* Get the last position in a column.
*/
public function getLastPositionInColumn(string $columnId): ?string
{
$query = $this->getQuery();
if (! $query) {
return null;
}
$statusField = $this->getColumnIdentifierAttribute();
$positionField = $this->getPositionIdentifierAttribute();
$keyName = $query->getModel()->getKeyName();
$record = (clone $query)
->where($statusField, $columnId)
->orderBy($positionField, 'desc')
->orderBy($keyName, 'desc') // Tie-breaker for deterministic order
->first();
return $record?->getAttribute($positionField);
}
}