fix: gabungkan MyPostResource ke PostResource dengan scope dan UI adaptif per role
This commit is contained in:
@@ -11,13 +11,33 @@ use App\Models\Post;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class PostResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Post::class;
|
||||
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-newspaper';
|
||||
protected static string|\UnitEnum|null $navigationGroup = 'Konten';
|
||||
protected static ?string $modelLabel = 'Artikel';
|
||||
|
||||
// Label dinamis sesuai role
|
||||
public static function getModelLabel(): string
|
||||
{
|
||||
return auth()->user()?->hasAnyRole(['super_admin', 'ketua', 'auditor'])
|
||||
? 'Artikel'
|
||||
: 'Artikel Saya';
|
||||
}
|
||||
|
||||
// Scope: ketua/super_admin/auditor lihat semua, lainnya hanya milik sendiri
|
||||
public static function getEloquentQuery(): Builder
|
||||
{
|
||||
$query = parent::getEloquentQuery();
|
||||
|
||||
if (auth()->user()?->hasAnyRole(['super_admin', 'ketua', 'auditor'])) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
return $query->where('author_id', auth()->id());
|
||||
}
|
||||
|
||||
public static function form(Schema $form): Schema
|
||||
{
|
||||
|
||||
@@ -13,6 +13,8 @@ class PostForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
$isAdmin = auth()->user()?->hasAnyRole(['super_admin', 'ketua']);
|
||||
|
||||
return $schema->components([
|
||||
TextInput::make('title')->label('Judul')->required()
|
||||
->live(onBlur: true)
|
||||
@@ -20,12 +22,13 @@ class PostForm
|
||||
TextInput::make('slug')->required()->unique(ignoreRecord: true),
|
||||
Select::make('category')->label('Kategori')
|
||||
->options([
|
||||
'umum' => 'Umum',
|
||||
'pengumuman' => 'Pengumuman',
|
||||
'berita' => 'Berita',
|
||||
'umum' => 'Umum',
|
||||
'pengumuman' => 'Pengumuman',
|
||||
'berita' => 'Berita',
|
||||
])
|
||||
->default('umum')->required(),
|
||||
DateTimePicker::make('published_at')->label('Tanggal Publikasi')
|
||||
->visible($isAdmin)
|
||||
->helperText('Kosongkan untuk menyimpan sebagai draft'),
|
||||
RichEditor::make('content')->label('Konten')->required()->columnSpanFull(),
|
||||
]);
|
||||
|
||||
@@ -15,6 +15,8 @@ class PostsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
$isAdmin = auth()->user()?->hasAnyRole(['super_admin', 'ketua']);
|
||||
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('title')->label('Judul')->searchable()->sortable(),
|
||||
@@ -31,7 +33,10 @@ class PostsTable
|
||||
'rejected' => 'danger',
|
||||
default => 'gray',
|
||||
}),
|
||||
TextColumn::make('author.name')->label('Penulis'),
|
||||
TextColumn::make('author.name')->label('Penulis')->visible($isAdmin),
|
||||
TextColumn::make('rejection_reason')->label('Alasan Penolakan')
|
||||
->limit(40)->default('-')
|
||||
->visible(fn ($record) => $record?->status === 'rejected'),
|
||||
TextColumn::make('published_at')->label('Dipublikasi')
|
||||
->dateTime('d M Y')->default('-')->sortable(),
|
||||
])
|
||||
@@ -44,22 +49,34 @@ class PostsTable
|
||||
]),
|
||||
])
|
||||
->recordActions([
|
||||
// Untuk anggota/pengurus/bendahara: ajukan artikel
|
||||
Action::make('submit')
|
||||
->label('Ajukan')
|
||||
->icon('heroicon-o-paper-airplane')
|
||||
->color('info')
|
||||
->requiresConfirmation()
|
||||
->visible(fn ($record) => ! $isAdmin && in_array($record->status, ['draft', 'rejected']))
|
||||
->action(fn ($record) => $record->update(['status' => 'pending', 'rejection_reason' => null])),
|
||||
|
||||
// Untuk admin: approve
|
||||
Action::make('publish')
|
||||
->label('Terbitkan')
|
||||
->icon('heroicon-o-check-circle')
|
||||
->color('success')
|
||||
->requiresConfirmation()
|
||||
->visible(fn ($record) => $record->status === 'pending')
|
||||
->visible(fn ($record) => $isAdmin && $record->status === 'pending')
|
||||
->action(fn ($record) => $record->update([
|
||||
'status' => 'published',
|
||||
'status' => 'published',
|
||||
'published_at' => now(),
|
||||
'reviewed_by' => auth()->id(),
|
||||
'reviewed_by' => auth()->id(),
|
||||
])),
|
||||
|
||||
// Untuk admin: tolak
|
||||
Action::make('reject')
|
||||
->label('Tolak')
|
||||
->icon('heroicon-o-x-circle')
|
||||
->color('danger')
|
||||
->visible(fn ($record) => $record->status === 'pending')
|
||||
->visible(fn ($record) => $isAdmin && $record->status === 'pending')
|
||||
->form([
|
||||
Textarea::make('rejection_reason')->label('Alasan Penolakan')->required(),
|
||||
])
|
||||
@@ -68,7 +85,9 @@ class PostsTable
|
||||
'reviewed_by' => auth()->id(),
|
||||
'rejection_reason' => $data['rejection_reason'],
|
||||
])),
|
||||
EditAction::make(),
|
||||
|
||||
EditAction::make()
|
||||
->visible(fn ($record) => $isAdmin || in_array($record->status, ['draft', 'rejected'])),
|
||||
])
|
||||
->toolbarActions([BulkActionGroup::make([DeleteBulkAction::make()])]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user