|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Usamamuneerchaudhary\Commentify\Filament\Resources; |
|
4
|
|
|
|
|
5
|
|
|
use Filament\Actions\Action; |
|
|
|
|
|
|
6
|
|
|
use Filament\Actions\BulkAction; |
|
|
|
|
|
|
7
|
|
|
use Filament\Actions\BulkActionGroup; |
|
|
|
|
|
|
8
|
|
|
use Filament\Actions\DeleteAction; |
|
|
|
|
|
|
9
|
|
|
use Filament\Actions\DeleteBulkAction; |
|
|
|
|
|
|
10
|
|
|
use Filament\Actions\EditAction; |
|
|
|
|
|
|
11
|
|
|
use Filament\Actions\ForceDeleteBulkAction; |
|
|
|
|
|
|
12
|
|
|
use Filament\Actions\RestoreAction; |
|
|
|
|
|
|
13
|
|
|
use Filament\Actions\RestoreBulkAction; |
|
|
|
|
|
|
14
|
|
|
use Filament\Actions\ViewAction; |
|
|
|
|
|
|
15
|
|
|
use Filament\Forms; |
|
|
|
|
|
|
16
|
|
|
use Filament\Resources\Resource; |
|
|
|
|
|
|
17
|
|
|
use Filament\Schemas\Schema; |
|
|
|
|
|
|
18
|
|
|
use Filament\Tables; |
|
|
|
|
|
|
19
|
|
|
use Filament\Tables\Table; |
|
|
|
|
|
|
20
|
|
|
use Usamamuneerchaudhary\Commentify\Filament\Resources\CommentResource\Pages; |
|
21
|
|
|
use Usamamuneerchaudhary\Commentify\Models\Comment; |
|
22
|
|
|
|
|
23
|
|
|
class CommentResource extends Resource |
|
24
|
|
|
{ |
|
25
|
|
|
protected static ?string $model = Comment::class; |
|
26
|
|
|
|
|
27
|
|
|
protected static string|null|\BackedEnum $navigationIcon = 'heroicon-o-chat-bubble-left-right'; |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
protected static string|null|\UnitEnum $navigationGroup = 'Commentify'; |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
protected static ?int $navigationSort = 2; |
|
32
|
|
|
|
|
33
|
|
|
public static function form(Schema $schema): Schema |
|
34
|
|
|
{ |
|
35
|
|
|
return $schema |
|
36
|
|
|
->schema([ |
|
37
|
|
|
Forms\Components\Select::make('user_id') |
|
|
|
|
|
|
38
|
|
|
->relationship('user', 'name') |
|
39
|
|
|
->searchable() |
|
40
|
|
|
->required() |
|
41
|
|
|
->disabled(), |
|
42
|
|
|
Forms\Components\Select::make('parent_id') |
|
43
|
|
|
->relationship('parent', 'id') |
|
44
|
|
|
->searchable() |
|
45
|
|
|
->nullable() |
|
46
|
|
|
->label('Parent Comment') |
|
47
|
|
|
->disabled(), |
|
48
|
|
|
Forms\Components\Textarea::make('body') |
|
|
|
|
|
|
49
|
|
|
->required() |
|
50
|
|
|
->maxLength(5000) |
|
51
|
|
|
->rows(6) |
|
52
|
|
|
->columnSpanFull() |
|
53
|
|
|
->helperText('Supports Markdown formatting'), |
|
54
|
|
|
Forms\Components\Select::make('commentable_type') |
|
55
|
|
|
->label('Commentable Type') |
|
56
|
|
|
->options(function (Comment $record) { |
|
57
|
|
|
if ($record->commentable_type) { |
|
58
|
|
|
return [$record->commentable_type => class_basename($record->commentable_type)]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return []; |
|
62
|
|
|
}) |
|
63
|
|
|
->disabled(), |
|
64
|
|
|
Forms\Components\TextInput::make('commentable_id') |
|
|
|
|
|
|
65
|
|
|
->label('Commentable ID') |
|
66
|
|
|
->disabled(), |
|
67
|
|
|
Forms\Components\Toggle::make('is_approved') |
|
|
|
|
|
|
68
|
|
|
->label('Approved') |
|
69
|
|
|
->default(false), |
|
70
|
|
|
]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public static function table(Table $table): Table |
|
74
|
|
|
{ |
|
75
|
|
|
return $table |
|
76
|
|
|
->modifyQueryUsing(function ($query) { |
|
77
|
|
|
return $query->withCount(['likes', 'children', 'reports']); |
|
78
|
|
|
}) |
|
79
|
|
|
->columns([ |
|
80
|
|
|
Tables\Columns\TextColumn::make('id') |
|
|
|
|
|
|
81
|
|
|
->label('ID') |
|
82
|
|
|
->sortable() |
|
83
|
|
|
->searchable(), |
|
84
|
|
|
Tables\Columns\TextColumn::make('user.name') |
|
85
|
|
|
->label('User') |
|
86
|
|
|
->sortable() |
|
87
|
|
|
->searchable() |
|
88
|
|
|
->default('Unknown'), |
|
89
|
|
|
Tables\Columns\TextColumn::make('body') |
|
90
|
|
|
->label('Comment') |
|
91
|
|
|
->limit(50) |
|
92
|
|
|
->wrap() |
|
93
|
|
|
->searchable() |
|
94
|
|
|
->formatStateUsing(function (Comment $record) { |
|
95
|
|
|
return strip_tags($record->presenter()->markdownBody()); |
|
96
|
|
|
}), |
|
97
|
|
|
Tables\Columns\TextColumn::make('commentable_type') |
|
98
|
|
|
->label('Type') |
|
99
|
|
|
->formatStateUsing(fn ($state) => $state ? class_basename($state) : '-') |
|
100
|
|
|
->sortable() |
|
101
|
|
|
->toggleable(), |
|
102
|
|
|
Tables\Columns\TextColumn::make('commentable_id') |
|
103
|
|
|
->label('Item ID') |
|
104
|
|
|
->sortable() |
|
105
|
|
|
->toggleable(), |
|
106
|
|
|
Tables\Columns\IconColumn::make('isParent') |
|
|
|
|
|
|
107
|
|
|
->label('Is Parent') |
|
108
|
|
|
->boolean() |
|
109
|
|
|
->getStateUsing(fn (Comment $record) => $record->isParent()) |
|
110
|
|
|
->toggleable(), |
|
111
|
|
|
Tables\Columns\TextColumn::make('parent_id') |
|
112
|
|
|
->label('Parent ID') |
|
113
|
|
|
->sortable() |
|
114
|
|
|
->toggleable(isToggledHiddenByDefault: true), |
|
115
|
|
|
Tables\Columns\TextColumn::make('likes_count') |
|
116
|
|
|
->label('Likes') |
|
117
|
|
|
->sortable() |
|
118
|
|
|
->badge() |
|
119
|
|
|
->color('success') |
|
120
|
|
|
->default(0), |
|
121
|
|
|
Tables\Columns\TextColumn::make('children_count') |
|
122
|
|
|
->label('Replies') |
|
123
|
|
|
->sortable() |
|
124
|
|
|
->badge() |
|
125
|
|
|
->color('info') |
|
126
|
|
|
->default(0), |
|
127
|
|
|
Tables\Columns\TextColumn::make('reports_count') |
|
128
|
|
|
->label('Reports') |
|
129
|
|
|
->sortable() |
|
130
|
|
|
->badge() |
|
131
|
|
|
->color(fn ($state) => ($state ?? 0) > 0 ? 'danger' : 'gray') |
|
132
|
|
|
->default(0) |
|
133
|
|
|
->toggleable(), |
|
134
|
|
|
Tables\Columns\IconColumn::make('is_approved') |
|
135
|
|
|
->label('Approved') |
|
136
|
|
|
->boolean() |
|
137
|
|
|
->sortable() |
|
138
|
|
|
->toggleable(), |
|
139
|
|
|
Tables\Columns\TextColumn::make('created_at') |
|
140
|
|
|
->dateTime() |
|
141
|
|
|
->sortable() |
|
142
|
|
|
->toggleable(isToggledHiddenByDefault: true), |
|
143
|
|
|
Tables\Columns\TextColumn::make('updated_at') |
|
144
|
|
|
->dateTime() |
|
145
|
|
|
->sortable() |
|
146
|
|
|
->toggleable(isToggledHiddenByDefault: true), |
|
147
|
|
|
Tables\Columns\TextColumn::make('deleted_at') |
|
148
|
|
|
->dateTime() |
|
149
|
|
|
->sortable() |
|
150
|
|
|
->toggleable(isToggledHiddenByDefault: true) |
|
151
|
|
|
->label('Deleted At'), |
|
152
|
|
|
]) |
|
153
|
|
|
->filters([ |
|
154
|
|
|
Tables\Filters\SelectFilter::make('user_id') |
|
|
|
|
|
|
155
|
|
|
->relationship('user', 'name') |
|
156
|
|
|
->label('User') |
|
157
|
|
|
->searchable(), |
|
158
|
|
|
Tables\Filters\TernaryFilter::make('isParent') |
|
|
|
|
|
|
159
|
|
|
->label('Parent Comments Only') |
|
160
|
|
|
->queries( |
|
161
|
|
|
true: fn ($query) => $query->whereNull('parent_id'), |
|
162
|
|
|
false: fn ($query) => $query->whereNotNull('parent_id'), |
|
163
|
|
|
), |
|
164
|
|
|
Tables\Filters\TernaryFilter::make('is_approved') |
|
165
|
|
|
->label('Approval Status') |
|
166
|
|
|
->placeholder('All Comments') |
|
167
|
|
|
->queries( |
|
168
|
|
|
true: fn ($query) => $query->where('is_approved', true), |
|
169
|
|
|
false: fn ($query) => $query->where('is_approved', false), |
|
170
|
|
|
), |
|
171
|
|
|
Tables\Filters\TrashedFilter::make(), |
|
|
|
|
|
|
172
|
|
|
]) |
|
173
|
|
|
->recordActions([ |
|
174
|
|
|
ViewAction::make(), |
|
175
|
|
|
EditAction::make(), |
|
176
|
|
|
Action::make('approve') |
|
177
|
|
|
->label('Approve') |
|
178
|
|
|
->icon('heroicon-o-check-circle') |
|
179
|
|
|
->color('success') |
|
180
|
|
|
->requiresConfirmation() |
|
181
|
|
|
->visible(fn (Comment $record) => ! $record->is_approved) |
|
182
|
|
|
->action(function (Comment $record) { |
|
183
|
|
|
$record->update(['is_approved' => true]); |
|
184
|
|
|
}), |
|
185
|
|
|
Action::make('disapprove') |
|
186
|
|
|
->label('Disapprove') |
|
187
|
|
|
->icon('heroicon-o-x-circle') |
|
188
|
|
|
->color('danger') |
|
189
|
|
|
->requiresConfirmation() |
|
190
|
|
|
->visible(fn (Comment $record) => $record->is_approved) |
|
191
|
|
|
->action(function (Comment $record) { |
|
192
|
|
|
$record->update(['is_approved' => false]); |
|
193
|
|
|
}), |
|
194
|
|
|
DeleteAction::make(), |
|
195
|
|
|
RestoreAction::make(), |
|
196
|
|
|
]) |
|
197
|
|
|
->toolbarActions([ |
|
198
|
|
|
BulkActionGroup::make([ |
|
199
|
|
|
BulkAction::make('approve') |
|
200
|
|
|
->label('Approve Selected') |
|
201
|
|
|
->icon('heroicon-o-check-circle') |
|
202
|
|
|
->color('success') |
|
203
|
|
|
->requiresConfirmation() |
|
204
|
|
|
->action(function ($records) { |
|
205
|
|
|
$records->each(function (Comment $record) { |
|
206
|
|
|
$record->update(['is_approved' => true]); |
|
207
|
|
|
}); |
|
208
|
|
|
}) |
|
209
|
|
|
->deselectRecordsAfterCompletion(), |
|
210
|
|
|
BulkAction::make('disapprove') |
|
211
|
|
|
->label('Disapprove Selected') |
|
212
|
|
|
->icon('heroicon-o-x-circle') |
|
213
|
|
|
->color('danger') |
|
214
|
|
|
->requiresConfirmation() |
|
215
|
|
|
->action(function ($records) { |
|
216
|
|
|
$records->each(function (Comment $record) { |
|
217
|
|
|
$record->update(['is_approved' => false]); |
|
218
|
|
|
}); |
|
219
|
|
|
}) |
|
220
|
|
|
->deselectRecordsAfterCompletion(), |
|
221
|
|
|
DeleteBulkAction::make(), |
|
222
|
|
|
RestoreBulkAction::make(), |
|
223
|
|
|
ForceDeleteBulkAction::make(), |
|
224
|
|
|
]), |
|
225
|
|
|
]) |
|
226
|
|
|
->defaultSort('created_at', 'desc'); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
public static function getRelations(): array |
|
230
|
|
|
{ |
|
231
|
|
|
return [ |
|
232
|
|
|
CommentResource\RelationManagers\RepliesRelationManager::class, |
|
233
|
|
|
CommentResource\RelationManagers\ReportsRelationManager::class, |
|
234
|
|
|
]; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public static function getPages(): array |
|
238
|
|
|
{ |
|
239
|
|
|
return [ |
|
240
|
|
|
'index' => Pages\ListComments::route('/'), |
|
241
|
|
|
'create' => Pages\CreateComment::route('/create'), |
|
242
|
|
|
'view' => Pages\ViewComment::route('/{record}'), |
|
243
|
|
|
'edit' => Pages\EditComment::route('/{record}/edit'), |
|
244
|
|
|
]; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths