feat: anggota dapat menulis artikel dengan workflow approval sebelum diterbitkan
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MyPosts;
|
||||
|
||||
use App\Filament\Resources\MyPosts\Pages\CreateMyPost;
|
||||
use App\Filament\Resources\MyPosts\Pages\EditMyPost;
|
||||
use App\Filament\Resources\MyPosts\Pages\ListMyPosts;
|
||||
use App\Models\Post;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class MyPostResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Post::class;
|
||||
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-pencil-square';
|
||||
protected static string|\UnitEnum|null $navigationGroup = 'Konten';
|
||||
protected static ?string $modelLabel = 'Artikel Saya';
|
||||
protected static ?string $slug = 'my-posts';
|
||||
|
||||
// Hanya tampilkan artikel milik user yang login
|
||||
public static function getEloquentQuery(): Builder
|
||||
{
|
||||
return parent::getEloquentQuery()->where('author_id', auth()->id());
|
||||
}
|
||||
|
||||
public static function form(Schema $form): Schema
|
||||
{
|
||||
return \App\Filament\Resources\MyPosts\Schemas\MyPostForm::configure($form);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return \App\Filament\Resources\MyPosts\Tables\MyPostsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListMyPosts::route('/'),
|
||||
'create' => CreateMyPost::route('/create'),
|
||||
'edit' => EditMyPost::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MyPosts\Pages;
|
||||
|
||||
use App\Filament\Resources\MyPosts\MyPostResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateMyPost extends CreateRecord
|
||||
{
|
||||
protected static string $resource = MyPostResource::class;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MyPosts\Pages;
|
||||
|
||||
use App\Filament\Resources\MyPosts\MyPostResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditMyPost extends EditRecord
|
||||
{
|
||||
protected static string $resource = MyPostResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MyPosts\Pages;
|
||||
|
||||
use App\Filament\Resources\MyPosts\MyPostResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListMyPosts extends ListRecords
|
||||
{
|
||||
protected static string $resource = MyPostResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MyPosts\Schemas;
|
||||
|
||||
use Filament\Forms\Components\RichEditor;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Schemas\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class MyPostForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema->components([
|
||||
TextInput::make('title')->label('Judul')->required()
|
||||
->live(onBlur: true)
|
||||
->afterStateUpdated(fn ($state, $set) => $set('slug', Str::slug($state))),
|
||||
TextInput::make('slug')->required()->unique(ignoreRecord: true),
|
||||
Select::make('category')->label('Kategori')
|
||||
->options([
|
||||
'umum' => 'Umum',
|
||||
'pengumuman' => 'Pengumuman',
|
||||
'berita' => 'Berita',
|
||||
])
|
||||
->default('umum')->required(),
|
||||
RichEditor::make('content')->label('Konten')->required()->columnSpanFull(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MyPosts\Tables;
|
||||
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class MyPostsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('title')->label('Judul')->searchable()->sortable(),
|
||||
TextColumn::make('category')->label('Kategori')->badge(),
|
||||
TextColumn::make('status')->badge()
|
||||
->color(fn ($state) => match ($state) {
|
||||
'published' => 'success',
|
||||
'pending' => 'warning',
|
||||
'rejected' => 'danger',
|
||||
default => 'gray',
|
||||
}),
|
||||
TextColumn::make('rejection_reason')->label('Alasan Penolakan')
|
||||
->visible(fn ($record) => $record?->status === 'rejected')
|
||||
->limit(40)->default('-'),
|
||||
TextColumn::make('created_at')->label('Dibuat')->date('d M Y')->sortable(),
|
||||
])
|
||||
->recordActions([
|
||||
// Hanya bisa edit jika masih draft atau rejected
|
||||
EditAction::make()
|
||||
->visible(fn ($record) => in_array($record->status, ['draft', 'rejected'])),
|
||||
// Ajukan untuk review
|
||||
Action::make('submit')
|
||||
->label('Ajukan')
|
||||
->icon('heroicon-o-paper-airplane')
|
||||
->color('info')
|
||||
->requiresConfirmation()
|
||||
->visible(fn ($record) => in_array($record->status, ['draft', 'rejected']))
|
||||
->action(fn ($record) => $record->update(['status' => 'pending', 'rejection_reason' => null])),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make()
|
||||
->before(function ($records) {
|
||||
// Tidak bisa hapus yang sudah published
|
||||
$records->each(function ($record) {
|
||||
if ($record->status === 'published') {
|
||||
throw new \Exception("Artikel yang sudah diterbitkan tidak dapat dihapus.");
|
||||
}
|
||||
});
|
||||
}),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace App\Filament\Resources\Posts\Tables;
|
||||
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
@@ -22,18 +24,52 @@ class PostsTable
|
||||
'berita' => 'info',
|
||||
default => 'gray',
|
||||
}),
|
||||
TextColumn::make('status')->badge()
|
||||
->color(fn ($state) => match ($state) {
|
||||
'published' => 'success',
|
||||
'pending' => 'warning',
|
||||
'rejected' => 'danger',
|
||||
default => 'gray',
|
||||
}),
|
||||
TextColumn::make('author.name')->label('Penulis'),
|
||||
TextColumn::make('published_at')->label('Dipublikasi')
|
||||
->dateTime('d M Y')->default('Draft')->sortable(),
|
||||
->dateTime('d M Y')->default('-')->sortable(),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('category')->options([
|
||||
'umum' => 'Umum',
|
||||
'pengumuman' => 'Pengumuman',
|
||||
'berita' => 'Berita',
|
||||
SelectFilter::make('status')->options([
|
||||
'draft' => 'Draft',
|
||||
'pending' => 'Menunggu',
|
||||
'published' => 'Diterbitkan',
|
||||
'rejected' => 'Ditolak',
|
||||
]),
|
||||
])
|
||||
->recordActions([EditAction::make()])
|
||||
->recordActions([
|
||||
Action::make('publish')
|
||||
->label('Terbitkan')
|
||||
->icon('heroicon-o-check-circle')
|
||||
->color('success')
|
||||
->requiresConfirmation()
|
||||
->visible(fn ($record) => $record->status === 'pending')
|
||||
->action(fn ($record) => $record->update([
|
||||
'status' => 'published',
|
||||
'published_at' => now(),
|
||||
'reviewed_by' => auth()->id(),
|
||||
])),
|
||||
Action::make('reject')
|
||||
->label('Tolak')
|
||||
->icon('heroicon-o-x-circle')
|
||||
->color('danger')
|
||||
->visible(fn ($record) => $record->status === 'pending')
|
||||
->form([
|
||||
Textarea::make('rejection_reason')->label('Alasan Penolakan')->required(),
|
||||
])
|
||||
->action(fn ($record, array $data) => $record->update([
|
||||
'status' => 'rejected',
|
||||
'reviewed_by' => auth()->id(),
|
||||
'rejection_reason' => $data['rejection_reason'],
|
||||
])),
|
||||
EditAction::make(),
|
||||
])
|
||||
->toolbarActions([BulkActionGroup::make([DeleteBulkAction::make()])]);
|
||||
}
|
||||
}
|
||||
|
||||
+10
-2
@@ -8,7 +8,7 @@ use Illuminate\Support\Str;
|
||||
|
||||
class Post extends Model
|
||||
{
|
||||
protected $fillable = ['title', 'slug', 'category', 'content', 'author_id', 'published_at'];
|
||||
protected $fillable = ['title', 'slug', 'category', 'content', 'author_id', 'published_at', 'status', 'reviewed_by', 'rejection_reason'];
|
||||
|
||||
protected $casts = ['published_at' => 'datetime'];
|
||||
|
||||
@@ -16,6 +16,7 @@ class Post extends Model
|
||||
{
|
||||
static::creating(function (Post $post) {
|
||||
$post->slug ??= Str::slug($post->title);
|
||||
$post->author_id ??= auth()->id();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -24,8 +25,15 @@ class Post extends Model
|
||||
return $this->belongsTo(User::class, 'author_id');
|
||||
}
|
||||
|
||||
public function reviewer(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'reviewed_by');
|
||||
}
|
||||
|
||||
public function scopePublished($query)
|
||||
{
|
||||
return $query->whereNotNull('published_at')->where('published_at', '<=', now());
|
||||
return $query->where('status', 'published')
|
||||
->whereNotNull('published_at')
|
||||
->where('published_at', '<=', now());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use Illuminate\Foundation\Auth\User as AuthUser;
|
||||
use App\Models\ContactMessage;
|
||||
use Illuminate\Auth\Access\HandlesAuthorization;
|
||||
|
||||
class ContactMessagePolicy
|
||||
{
|
||||
use HandlesAuthorization;
|
||||
|
||||
public function viewAny(AuthUser $authUser): bool
|
||||
{
|
||||
return $authUser->can('ViewAny:ContactMessage');
|
||||
}
|
||||
|
||||
public function view(AuthUser $authUser, ContactMessage $contactMessage): bool
|
||||
{
|
||||
return $authUser->can('View:ContactMessage');
|
||||
}
|
||||
|
||||
public function create(AuthUser $authUser): bool
|
||||
{
|
||||
return $authUser->can('Create:ContactMessage');
|
||||
}
|
||||
|
||||
public function update(AuthUser $authUser, ContactMessage $contactMessage): bool
|
||||
{
|
||||
return $authUser->can('Update:ContactMessage');
|
||||
}
|
||||
|
||||
public function delete(AuthUser $authUser, ContactMessage $contactMessage): bool
|
||||
{
|
||||
return $authUser->can('Delete:ContactMessage');
|
||||
}
|
||||
|
||||
public function deleteAny(AuthUser $authUser): bool
|
||||
{
|
||||
return $authUser->can('DeleteAny:ContactMessage');
|
||||
}
|
||||
|
||||
public function restore(AuthUser $authUser, ContactMessage $contactMessage): bool
|
||||
{
|
||||
return $authUser->can('Restore:ContactMessage');
|
||||
}
|
||||
|
||||
public function forceDelete(AuthUser $authUser, ContactMessage $contactMessage): bool
|
||||
{
|
||||
return $authUser->can('ForceDelete:ContactMessage');
|
||||
}
|
||||
|
||||
public function forceDeleteAny(AuthUser $authUser): bool
|
||||
{
|
||||
return $authUser->can('ForceDeleteAny:ContactMessage');
|
||||
}
|
||||
|
||||
public function restoreAny(AuthUser $authUser): bool
|
||||
{
|
||||
return $authUser->can('RestoreAny:ContactMessage');
|
||||
}
|
||||
|
||||
public function replicate(AuthUser $authUser, ContactMessage $contactMessage): bool
|
||||
{
|
||||
return $authUser->can('Replicate:ContactMessage');
|
||||
}
|
||||
|
||||
public function reorder(AuthUser $authUser): bool
|
||||
{
|
||||
return $authUser->can('Reorder:ContactMessage');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user