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;
|
namespace App\Filament\Resources\Posts\Tables;
|
||||||
|
|
||||||
|
use Filament\Actions\Action;
|
||||||
use Filament\Actions\BulkActionGroup;
|
use Filament\Actions\BulkActionGroup;
|
||||||
use Filament\Actions\DeleteBulkAction;
|
use Filament\Actions\DeleteBulkAction;
|
||||||
use Filament\Actions\EditAction;
|
use Filament\Actions\EditAction;
|
||||||
|
use Filament\Forms\Components\Textarea;
|
||||||
use Filament\Tables\Columns\TextColumn;
|
use Filament\Tables\Columns\TextColumn;
|
||||||
use Filament\Tables\Filters\SelectFilter;
|
use Filament\Tables\Filters\SelectFilter;
|
||||||
use Filament\Tables\Table;
|
use Filament\Tables\Table;
|
||||||
@@ -22,18 +24,52 @@ class PostsTable
|
|||||||
'berita' => 'info',
|
'berita' => 'info',
|
||||||
default => 'gray',
|
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('author.name')->label('Penulis'),
|
||||||
TextColumn::make('published_at')->label('Dipublikasi')
|
TextColumn::make('published_at')->label('Dipublikasi')
|
||||||
->dateTime('d M Y')->default('Draft')->sortable(),
|
->dateTime('d M Y')->default('-')->sortable(),
|
||||||
])
|
])
|
||||||
->filters([
|
->filters([
|
||||||
SelectFilter::make('category')->options([
|
SelectFilter::make('status')->options([
|
||||||
'umum' => 'Umum',
|
'draft' => 'Draft',
|
||||||
'pengumuman' => 'Pengumuman',
|
'pending' => 'Menunggu',
|
||||||
'berita' => 'Berita',
|
'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()])]);
|
->toolbarActions([BulkActionGroup::make([DeleteBulkAction::make()])]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-2
@@ -8,7 +8,7 @@ use Illuminate\Support\Str;
|
|||||||
|
|
||||||
class Post extends Model
|
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'];
|
protected $casts = ['published_at' => 'datetime'];
|
||||||
|
|
||||||
@@ -16,6 +16,7 @@ class Post extends Model
|
|||||||
{
|
{
|
||||||
static::creating(function (Post $post) {
|
static::creating(function (Post $post) {
|
||||||
$post->slug ??= Str::slug($post->title);
|
$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');
|
return $this->belongsTo(User::class, 'author_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function reviewer(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, 'reviewed_by');
|
||||||
|
}
|
||||||
|
|
||||||
public function scopePublished($query)
|
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');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('posts', function (Blueprint $table) {
|
||||||
|
$table->enum('status', ['draft', 'pending', 'published', 'rejected'])
|
||||||
|
->default('draft')->after('author_id');
|
||||||
|
$table->foreignId('reviewed_by')->nullable()->constrained('users')->after('status');
|
||||||
|
$table->text('rejection_reason')->nullable()->after('reviewed_by');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('posts', function (Blueprint $table) {
|
||||||
|
$table->dropColumn(['status', 'reviewed_by', 'rejection_reason']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -36,10 +36,11 @@ class PermissionSeeder extends Seeder
|
|||||||
->orWhere('name', 'like', 'View:Division')
|
->orWhere('name', 'like', 'View:Division')
|
||||||
->get());
|
->get());
|
||||||
|
|
||||||
// Anggota: hanya lihat kegiatan & voting
|
// Anggota: lihat kegiatan & voting + kelola artikel sendiri
|
||||||
$anggota->syncPermissions(Permission::whereIn('name', [
|
$anggota->syncPermissions(Permission::whereIn('name', [
|
||||||
'ViewAny:Activity', 'View:Activity',
|
'ViewAny:Activity', 'View:Activity',
|
||||||
'ViewAny:Vote', 'View:Vote',
|
'ViewAny:Vote', 'View:Vote',
|
||||||
|
'ViewAny:MyPost', 'View:MyPost', 'Create:MyPost', 'Update:MyPost', 'Delete:MyPost',
|
||||||
])->get());
|
])->get());
|
||||||
|
|
||||||
// Auditor: read-only semua + akses audit
|
// Auditor: read-only semua + akses audit
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ class PostSeeder extends Seeder
|
|||||||
foreach ($posts as $data) {
|
foreach ($posts as $data) {
|
||||||
Post::firstOrCreate(
|
Post::firstOrCreate(
|
||||||
['slug' => \Illuminate\Support\Str::slug($data['title'])],
|
['slug' => \Illuminate\Support\Str::slug($data['title'])],
|
||||||
array_merge($data, ['author_id' => $author->id])
|
array_merge($data, ['author_id' => $author->id, 'status' => 'published'])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user