-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d723bea
commit ecd3f75
Showing
17 changed files
with
511 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Enums; | ||
|
||
enum ArticleCategoryEnum: string | ||
{ | ||
case News = 'news'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources; | ||
|
||
use App\Enums\ArticleCategoryEnum; | ||
use App\Filament\Resources\ArticleResource\Pages\CreateArticle; | ||
use App\Filament\Resources\ArticleResource\Pages\EditArticle; | ||
use App\Filament\Resources\ArticleResource\Pages\ListArticles; | ||
use App\Filament\Resources\ArticleResource\Pages\ViewArticle; | ||
use App\Models\Article; | ||
use App\Models\User; | ||
use Filament\Forms\Components\DatePicker; | ||
use Filament\Forms\Components\Select; | ||
use Filament\Forms\Components\Textarea; | ||
use Filament\Forms\Components\TextInput; | ||
use Filament\Forms\Form; | ||
use Filament\Resources\Resource; | ||
use Filament\Tables\Actions\CreateAction; | ||
use Filament\Tables\Actions\ViewAction; | ||
use Filament\Tables\Columns\TextColumn; | ||
use Filament\Tables\Table; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\SoftDeletingScope; | ||
use Illuminate\Support\Str; | ||
|
||
class ArticleResource extends Resource | ||
{ | ||
protected static ?string $model = Article::class; | ||
|
||
protected static ?string $navigationIcon = 'heroicon-o-document-text'; | ||
|
||
public static function form(Form $form): Form | ||
{ | ||
return $form | ||
->schema([ | ||
TextInput::make('title')->required()->columnSpan('full'), | ||
Select::make('category') | ||
->options([ | ||
ArticleCategoryEnum::News->value => Str::title(ArticleCategoryEnum::News->value), | ||
]) | ||
->default(ArticleCategoryEnum::News->value) | ||
->required(), | ||
Textarea::make('meta_description')->nullable()->autosize()->columnSpan('full'), | ||
Textarea::make('content')->required()->autosize()->columnSpan('full'), | ||
Select::make('user_id') | ||
->relationship( | ||
name: 'user', | ||
modifyQueryUsing: fn ($query) => $query->managers()->orderBy('username')->orderBy('email') | ||
) | ||
->getOptionLabelFromRecordUsing(fn (User $user) => $user->username ?? $user->email ?? 'ID '.$user->id) | ||
->required(), | ||
DatePicker::make('published_at')->nullable(), | ||
]); | ||
} | ||
|
||
public static function table(Table $table): Table | ||
{ | ||
return $table | ||
->columns([ | ||
TextColumn::make('title') | ||
->label('Title') | ||
->sortable() | ||
->searchable(), | ||
TextColumn::make('category') | ||
->label('Category') | ||
->sortable() | ||
->searchable(), | ||
|
||
TextColumn::make('published_at') | ||
->label('Date Published') | ||
->date() | ||
->sortable(), | ||
|
||
TextColumn::make('created_at') | ||
->label('Date Created') | ||
->dateTime() | ||
->sortable(), | ||
|
||
]) | ||
->filters([ | ||
// | ||
]) | ||
->recordUrl(fn (Article $article) => ArticleResource::getUrl('view', ['record' => $article])) | ||
->actions([ | ||
ViewAction::make(), | ||
]) | ||
->emptyStateActions([ | ||
CreateAction::make(), | ||
]); | ||
} | ||
|
||
public static function getRelations(): array | ||
{ | ||
return [ | ||
// | ||
]; | ||
} | ||
|
||
public static function getPages(): array | ||
{ | ||
return [ | ||
'index' => ListArticles::route('/'), | ||
'create' => CreateArticle::route('/create'), | ||
'view' => ViewArticle::route('/{record}'), | ||
'edit' => EditArticle::route('/{record}/edit'), | ||
]; | ||
} | ||
|
||
/** | ||
* @return Builder<Article> | ||
*/ | ||
public static function getEloquentQuery(): Builder | ||
{ | ||
return parent::getEloquentQuery() | ||
->withoutGlobalScopes([ | ||
SoftDeletingScope::class, | ||
]); | ||
} | ||
|
||
public static function shouldSkipAuthorization(): bool | ||
{ | ||
return app()->isLocal(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
app/Filament/Resources/ArticleResource/Pages/CreateArticle.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources\ArticleResource\Pages; | ||
|
||
use App\Filament\Resources\ArticleResource; | ||
use Filament\Resources\Pages\CreateRecord; | ||
|
||
class CreateArticle extends CreateRecord | ||
{ | ||
protected static string $resource = ArticleResource::class; | ||
} |
25 changes: 25 additions & 0 deletions
25
app/Filament/Resources/ArticleResource/Pages/EditArticle.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources\ArticleResource\Pages; | ||
|
||
use App\Filament\Resources\ArticleResource; | ||
use Filament\Actions\DeleteAction; | ||
use Filament\Actions\ForceDeleteAction; | ||
use Filament\Actions\RestoreAction; | ||
use Filament\Resources\Pages\EditRecord; | ||
|
||
class EditArticle extends EditRecord | ||
{ | ||
protected static string $resource = ArticleResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
DeleteAction::make(), | ||
ForceDeleteAction::make(), | ||
RestoreAction::make(), | ||
]; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/Filament/Resources/ArticleResource/Pages/ListArticles.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources\ArticleResource\Pages; | ||
|
||
use App\Filament\Resources\ArticleResource; | ||
use Filament\Actions\CreateAction; | ||
use Filament\Resources\Pages\ListRecords; | ||
|
||
class ListArticles extends ListRecords | ||
{ | ||
protected static string $resource = ArticleResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
CreateAction::make(), | ||
]; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/Filament/Resources/ArticleResource/Pages/ViewArticle.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources\ArticleResource\Pages; | ||
|
||
use App\Filament\Resources\ArticleResource; | ||
use Filament\Actions\DeleteAction; | ||
use Filament\Actions\EditAction; | ||
use Filament\Actions\ForceDeleteAction; | ||
use Filament\Actions\RestoreAction; | ||
use Filament\Resources\Pages\ViewRecord; | ||
|
||
class ViewArticle extends ViewRecord | ||
{ | ||
protected static string $resource = ArticleResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
EditAction::make(), | ||
DeleteAction::make(), | ||
ForceDeleteAction::make(), | ||
RestoreAction::make(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Policies; | ||
|
||
use App\Models\Article; | ||
use App\Models\User; | ||
|
||
final class ArticlePolicy | ||
{ | ||
public function viewAny(User $user): bool | ||
{ | ||
return $user->hasPermissionTo('article:viewAny', 'admin'); | ||
} | ||
|
||
public function view(User $user, Article $article): bool | ||
{ | ||
// If users can view any, can view single article | ||
return $this->viewAny($user); | ||
} | ||
|
||
public function create(User $user): bool | ||
{ | ||
return $user->hasPermissionTo('article:create', 'admin'); | ||
} | ||
|
||
public function update(User $user, Article $article): bool | ||
{ | ||
if ($user->hasPermissionTo('article:updateAny', 'admin')) { | ||
return true; | ||
} | ||
|
||
// If users can create, they can update their own | ||
return $this->create($user) && ($user->is($article->user)); | ||
} | ||
|
||
public function delete(User $user, Article $article): bool | ||
{ | ||
if ($user->hasPermissionTo('article:deleteAny', 'admin')) { | ||
return true; | ||
} | ||
|
||
// If users can create, they can delete their own | ||
return $this->create($user) && ($user->is($article->user)); | ||
} | ||
|
||
public function restore(User $user, Article $article): bool | ||
{ | ||
return $user->hasPermissionTo('article:restore', 'admin'); | ||
} | ||
|
||
public function forceDelete(User $user, Article $article): bool | ||
{ | ||
return $user->hasPermissionTo('article:forceDelete', 'admin'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.