Skip to content

Commit

Permalink
Merge pull request #120 from lara-zeus/deletes
Browse files Browse the repository at this point in the history
allow to force delete and restore posts, and finally green phpstan
  • Loading branch information
atmonshi authored Jul 2, 2023
2 parents 6a380c1 + 38981eb commit 850c691
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 26 deletions.
3 changes: 3 additions & 0 deletions src/Classes/TipTapEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
class TipTapEditor implements ContentEditor
{
/**
* @phpstan-ignore-next-line
*
* @throws InvalidOutputFormatException
*/
public static function component(): Component
Expand All @@ -27,6 +29,7 @@ public static function component(): Component
public static function render(string $content): string
{
if (class_exists(TipTapEditorAlias::class)) {
// @phpstan-ignore-next-line
return tiptap_converter()->asHTML($content);
// return tiptap_converter()->asJSON($content);
// return tiptap_converter()->asText($content);
Expand Down
7 changes: 3 additions & 4 deletions src/Filament/Resources/LibraryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Filament\Tables\Columns\SpatieTagsColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use LaraZeus\Sky\Filament\Resources\LibraryResource\Pages;
use LaraZeus\Sky\Models\Library;
Expand Down Expand Up @@ -58,7 +57,7 @@ public static function form(Form $form): Form
}),

TextInput::make('slug')
->unique(ignorable: fn (?Model $record): ?Model => $record)
->unique(ignorable: fn (?Library $record): ?Library => $record)
->required()
->maxLength(255)
->label(__('Library Slug')),
Expand All @@ -75,7 +74,7 @@ public static function form(Form $form): Form
Select::make('type')
->label(__('Library Type'))
->visible(config('zeus-sky.library_types') !== null)
->options(config('zeus-sky.library_types', null)),
->options(config('zeus-sky.library_types', [])),

Section::make(__('Library File'))
->schema([
Expand Down Expand Up @@ -128,7 +127,7 @@ public static function table(Table $table): Table
->color('warning')
->icon('heroicon-o-external-link')
->label(__('Open'))
->url(fn (Model $record): string => route('library.item', ['slug' => $record->slug]))
->url(fn (Library $record): string => route('library.item', ['slug' => $record->slug]))
->openUrlInNewTab(),
DeleteAction::make('delete')
->label(__('Delete')),
Expand Down
29 changes: 27 additions & 2 deletions src/Filament/Resources/PageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\ActionGroup;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\DeleteBulkAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Actions\ForceDeleteAction;
use Filament\Tables\Actions\ForceDeleteBulkAction;
use Filament\Tables\Actions\RestoreAction;
use Filament\Tables\Actions\RestoreBulkAction;
use Filament\Tables\Columns\ViewColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Str;
use LaraZeus\Sky\Filament\Resources\PageResource\Pages;
use LaraZeus\Sky\Models\Post;
Expand All @@ -42,6 +49,17 @@ protected static function getNavigationBadge(): ?string
return (string) config('zeus-sky.models.post')::query()->page()->count();
}

/**
* @return Builder<Post>
*/
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}

public static function form(Form $form): Form
{
return $form
Expand Down Expand Up @@ -156,11 +174,18 @@ public static function table(Table $table): Table
->label(__('Open'))
->url(fn (Post $record): string => route('page', ['slug' => $record]))
->openUrlInNewTab(),
DeleteAction::make('delete')
->label(__('Delete')),
DeleteAction::make('delete'),
ForceDeleteAction::make(),
RestoreAction::make(),
]),
])
->bulkActions([
DeleteBulkAction::make(),
ForceDeleteBulkAction::make(),
RestoreBulkAction::make(),
])
->filters([
TrashedFilter::make(),
SelectFilter::make('status')
->multiple()
->label(__('Status'))
Expand Down
7 changes: 6 additions & 1 deletion src/Filament/Resources/PageResource/Pages/ListPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use LaraZeus\Sky\Filament\Resources\PageResource;

class ListPosts extends ListRecords
Expand All @@ -14,6 +15,10 @@ class ListPosts extends ListRecords

protected function getTableQuery(): Builder
{
return config('zeus-sky.models.post')::where('post_type', 'page');
return config('zeus-sky.models.post')::query()
->where('post_type', 'page')
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
}
20 changes: 18 additions & 2 deletions src/Filament/Resources/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,24 @@
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\ActionGroup;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\DeleteBulkAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Actions\ForceDeleteAction;
use Filament\Tables\Actions\ForceDeleteBulkAction;
use Filament\Tables\Actions\RestoreAction;
use Filament\Tables\Actions\RestoreBulkAction;
use Filament\Tables\Columns\SpatieTagsColumn;
use Filament\Tables\Columns\ViewColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
use LaraZeus\Sky\Filament\Resources\PostResource\Pages;
use LaraZeus\Sky\Models\Post;
use LaraZeus\Sky\Models\PostScope;

// @mixin Builder<PostScope>
class PostResource extends SkyResource
{
public static function getModel(): string
Expand Down Expand Up @@ -199,11 +207,18 @@ public static function table(Table $table): Table
->label(__('Open'))
->url(fn (Post $record): string => route('post', ['slug' => $record]))
->openUrlInNewTab(),
DeleteAction::make('delete')
->label(__('Delete')),
DeleteAction::make('delete'),
ForceDeleteAction::make(),
RestoreAction::make(),
]),
])
->bulkActions([
DeleteBulkAction::make(),
ForceDeleteBulkAction::make(),
RestoreBulkAction::make(),
])
->filters([
TrashedFilter::make(),
SelectFilter::make('status')
->multiple()
->label(__('Status'))
Expand All @@ -215,6 +230,7 @@ public static function table(Table $table): Table

Filter::make('sticky')
->label(__('Still Sticky'))
// @phpstan-ignore-next-line
->query(fn (Builder $query): Builder => $query->sticky()),

Filter::make('not_sticky')
Expand Down
7 changes: 6 additions & 1 deletion src/Filament/Resources/PostResource/Pages/ListPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use LaraZeus\Sky\Filament\Resources\PostResource;

class ListPosts extends ListRecords
Expand All @@ -14,6 +15,10 @@ class ListPosts extends ListRecords

protected function getTableQuery(): Builder
{
return config('zeus-sky.models.post')::where('post_type', 'post');
return config('zeus-sky.models.post')::query()
->where('post_type', 'post')
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
}
8 changes: 8 additions & 0 deletions src/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace LaraZeus\Sky\Models;

use Database\Factories\PostFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Blade;
use Spatie\MediaLibrary\HasMedia;
Expand All @@ -24,9 +26,15 @@
* @property int $user_id
* @property bool $require_password
* @property string $password
*
* @method Builder|static sticky()
* @method Builder|static published()
* @method Builder|static query()
* @method Builder|static withAnyTags()
*/
class Post extends Model implements HasMedia
{
use SoftDeletes;
use HasFactory;
use HasTags;
use InteractsWithMedia;
Expand Down
61 changes: 45 additions & 16 deletions src/Models/PostScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,91 @@

trait PostScope
{
public function scopeSticky(Builder $query): void
/**
* @param Builder<Post> $query
*/
public function scopeSticky(Builder $query): Builder
{
$query->where('post_type', 'post')
return $query->where('post_type', 'post')
->whereNotNull('sticky_until')
->whereDate('sticky_until', '>=', now())
->whereDate('published_at', '<=', now());
}

public function scopeNotSticky(Builder $query): void
/**
* @param Builder<Post> $query
*/
public function scopeNotSticky(Builder $query): Builder
{
$query->where('post_type', 'post')->where(function ($q) {
return $query->where('post_type', 'post')->where(function ($q) {
return $q->whereDate('sticky_until', '<=', now())->orWhereNull('sticky_until');
})
->whereDate('published_at', '<=', now());
}

public function scopePublished(Builder $query): void
/**
* @param Builder<Post> $query
*/
public function scopePublished(Builder $query): Builder
{
$query->where('post_type', 'post')
return $query->where('post_type', 'post')
->where('status', 'publish')
->whereDate('published_at', '<=', now());
}

public function scopeRelated(Builder $query, Post $post): void
/**
* @param Builder<Post> $query
*/
public function scopeRelated(Builder $query, Post $post): Builder
{
$query->where('post_type', 'post')
return $query->where('post_type', 'post')
->withAnyTags($post->tags->pluck('name')->toArray(), 'category');
}

public function scopePage(Builder $query): void
/**
* @param Builder<Post> $query
*/
public function scopePage(Builder $query): Builder
{
$query->where('post_type', 'page')
return $query->where('post_type', 'page')
->whereDate('published_at', '<=', now());
}

public function scopePosts(Builder $query): void
/**
* @param Builder<Post> $query
*/
public function scopePosts(Builder $query): Builder
{
$query->where('post_type', 'post')
return $query->where('post_type', 'post')
->whereDate('published_at', '<=', now());
}

public function scopeForCategory(Builder $query, $category = null): void
/**
* @param Builder<Post> $query
* @param ?string $category
*/
public function scopeForCategory(Builder $query, string $category = null): Builder
{
if ($category !== null) {
$query->where(
return $query->where(
function ($query) use ($category) {
$query->withAnyTags([$category], 'category');

return $query;
}
);
}

return $query;
}

public function scopeSearch(Builder $query, $term): void
/**
* @param Builder<Post> $query
*/
public function scopeSearch(Builder $query, $term): Builder
{
if ($term !== null) {
$query->where(
return $query->where(
function ($query) use ($term) {
foreach (['title', 'slug', 'content', 'description'] as $attribute) {
$query->orWhere(DB::raw("lower({$attribute})"), 'like', "%{$term}%");
Expand All @@ -74,5 +101,7 @@ function ($query) use ($term) {
}
);
}

return $query;
}
}
5 changes: 5 additions & 0 deletions src/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace LaraZeus\Sky\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

/**
* @property string $slug
* @property string $type
* @property string $name
*
* @method Builder|static published()
*/
class Tag extends \Spatie\Tags\Tag
{
Expand All @@ -32,8 +35,10 @@ public function tag(): MorphToMany
return $this->morphedByMany(config('zeus-sky.models.post'), 'taggable');
}

/** @return MorphToMany<Post> */
public function postsPublished(): MorphToMany
{
// @phpstan-ignore-next-line
return $this->morphedByMany(config('zeus-sky.models.post'), 'taggable')->published();
}

Expand Down

0 comments on commit 850c691

Please sign in to comment.