-
Notifications
You must be signed in to change notification settings - Fork 416
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
Showing
13 changed files
with
459 additions
and
102 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,152 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\Blog; | ||
|
||
use App\Filament\Resources\Blog\LinkResource\Pages; | ||
use App\Filament\Resources\Blog\LinkResource\RelationManagers; | ||
use App\Models\Blog\Link; | ||
use Filament\Forms; | ||
use Filament\Forms\Form; | ||
use Filament\Infolists\Components\ColorEntry; | ||
use Filament\Infolists\Components\ImageEntry; | ||
use Filament\Infolists\Components\TextEntry; | ||
use Filament\Infolists\Infolist; | ||
use Filament\Notifications\Notification; | ||
use Filament\Resources\Concerns\Translatable; | ||
use Filament\Resources\Resource; | ||
use Filament\Support\Enums\FontWeight; | ||
use Filament\Tables; | ||
use Filament\Tables\Table; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\SoftDeletingScope; | ||
|
||
class LinkResource extends Resource | ||
{ | ||
use Translatable; | ||
|
||
protected static ?string $model = Link::class; | ||
|
||
protected static ?string $navigationIcon = 'heroicon-o-link'; | ||
|
||
protected static ?string $navigationGroup = 'Blog'; | ||
|
||
protected static ?int $navigationSort = 3; | ||
|
||
public static function form(Form $form): Form | ||
{ | ||
return $form | ||
->schema([ | ||
Forms\Components\TextInput::make('title') | ||
->maxLength(255) | ||
->required(), | ||
Forms\Components\ColorPicker::make('color') | ||
->required() | ||
->hex() | ||
->hexColor(), | ||
Forms\Components\Textarea::make('description') | ||
->maxLength(1024) | ||
->required() | ||
->columnSpanFull(), | ||
Forms\Components\TextInput::make('url') | ||
->label('URL') | ||
->required() | ||
->maxLength(255) | ||
->columnSpanFull(), | ||
Forms\Components\FileUpload::make('image') | ||
->image(), | ||
]); | ||
} | ||
|
||
public static function infolist(Infolist $infolist): Infolist | ||
{ | ||
return $infolist | ||
->schema([ | ||
TextEntry::make('title'), | ||
ColorEntry::make('color'), | ||
TextEntry::make('description') | ||
->columnSpanFull(), | ||
TextEntry::make('url') | ||
->label('URL') | ||
->columnSpanFull() | ||
->url(fn (Link $record): string => '#' . urlencode($record->url)), | ||
ImageEntry::make('image'), | ||
]); | ||
} | ||
|
||
public static function table(Table $table): Table | ||
{ | ||
return $table | ||
->columns([ | ||
Tables\Columns\Layout\Stack::make([ | ||
Tables\Columns\ImageColumn::make('image') | ||
->height('100%') | ||
->width('100%'), | ||
Tables\Columns\Layout\Stack::make([ | ||
Tables\Columns\TextColumn::make('title') | ||
->weight(FontWeight::Bold), | ||
Tables\Columns\TextColumn::make('url') | ||
->formatStateUsing(fn (string $state): string => str($state)->after('://')->ltrim('www.')->trim('/')) | ||
->color('gray') | ||
->limit(30), | ||
]), | ||
])->space(3), | ||
Tables\Columns\Layout\Panel::make([ | ||
Tables\Columns\Layout\Split::make([ | ||
Tables\Columns\ColorColumn::make('color') | ||
->grow(false), | ||
Tables\Columns\TextColumn::make('description') | ||
->color('gray'), | ||
]), | ||
])->collapsible(), | ||
]) | ||
->filters([ | ||
// | ||
]) | ||
->contentGrid([ | ||
'md' => 2, | ||
'xl' => 3, | ||
]) | ||
->paginated([ | ||
18, | ||
36, | ||
72, | ||
'all', | ||
]) | ||
->actions([ | ||
Tables\Actions\Action::make('visit') | ||
->label('Visit link') | ||
->icon('heroicon-m-arrow-top-right-on-square') | ||
->color('gray') | ||
->url(fn (Link $record): string => '#' . urlencode($record->url)), | ||
Tables\Actions\EditAction::make(), | ||
]) | ||
->bulkActions([ | ||
Tables\Actions\BulkActionGroup::make([ | ||
Tables\Actions\DeleteBulkAction::make() | ||
->action(function () { | ||
Notification::make() | ||
->title('Now, now, don\'t be cheeky, leave some records for others to play with!') | ||
->warning() | ||
->send(); | ||
}), | ||
]), | ||
]); | ||
} | ||
|
||
public static function getRelations(): array | ||
{ | ||
return [ | ||
// | ||
]; | ||
} | ||
|
||
public static function getPages(): array | ||
{ | ||
return [ | ||
'index' => Pages\ListLinks::route('/'), | ||
'create' => Pages\CreateLink::route('/create'), | ||
'view' => Pages\ViewLink::route('/{record}'), | ||
'edit' => Pages\EditLink::route('/{record}/edit'), | ||
]; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/Filament/Resources/Blog/LinkResource/Pages/CreateLink.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 | ||
|
||
namespace App\Filament\Resources\Blog\LinkResource\Pages; | ||
|
||
use App\Filament\Resources\Blog\LinkResource; | ||
use Filament\Actions; | ||
use Filament\Resources\Pages\CreateRecord; | ||
|
||
class CreateLink extends CreateRecord | ||
{ | ||
use CreateRecord\Concerns\Translatable; | ||
|
||
protected static string $resource = LinkResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
Actions\LocaleSwitcher::make(), | ||
]; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/Filament/Resources/Blog/LinkResource/Pages/EditLink.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,23 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\Blog\LinkResource\Pages; | ||
|
||
use App\Filament\Resources\Blog\LinkResource; | ||
use Filament\Actions; | ||
use Filament\Resources\Pages\EditRecord; | ||
|
||
class EditLink extends EditRecord | ||
{ | ||
use EditRecord\Concerns\Translatable; | ||
|
||
protected static string $resource = LinkResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
Actions\ViewAction::make(), | ||
Actions\DeleteAction::make(), | ||
Actions\LocaleSwitcher::make(), | ||
]; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/Filament/Resources/Blog/LinkResource/Pages/ListLinks.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,22 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\Blog\LinkResource\Pages; | ||
|
||
use App\Filament\Resources\Blog\LinkResource; | ||
use Filament\Actions; | ||
use Filament\Resources\Pages\ListRecords; | ||
|
||
class ListLinks extends ListRecords | ||
{ | ||
use ListRecords\Concerns\Translatable; | ||
|
||
protected static string $resource = LinkResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
Actions\CreateAction::make(), | ||
Actions\LocaleSwitcher::make(), | ||
]; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/Filament/Resources/Blog/LinkResource/Pages/ViewLink.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,22 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\Blog\LinkResource\Pages; | ||
|
||
use App\Filament\Resources\Blog\LinkResource; | ||
use Filament\Actions; | ||
use Filament\Resources\Pages\ViewRecord; | ||
|
||
class ViewLink extends ViewRecord | ||
{ | ||
use ViewRecord\Concerns\Translatable; | ||
|
||
protected static string $resource = LinkResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
Actions\EditAction::make(), | ||
Actions\LocaleSwitcher::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Models\Blog; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Spatie\Translatable\HasTranslations; | ||
|
||
class Link extends Model | ||
{ | ||
use HasFactory; | ||
use HasTranslations; | ||
|
||
public $translatable = [ | ||
'title', | ||
'description', | ||
]; | ||
|
||
protected $table = 'blog_links'; | ||
} |
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,41 @@ | ||
<?php | ||
|
||
namespace Database\Factories\Blog; | ||
|
||
use Database\Factories\Concerns\CanCreateImages; | ||
use Database\Seeders\DatabaseSeeder; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Illuminate\Support\Facades\Storage; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Blog\Link> | ||
*/ | ||
class LinkFactory extends Factory | ||
{ | ||
use CanCreateImages; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'url' => $this->faker->url(), | ||
'title' => [ | ||
'en' => Str::title($this->faker->words(asText: true)), | ||
'es' => Str::title($this->faker->words(asText: true)), | ||
'nl' => Str::title($this->faker->words(asText: true)), | ||
], | ||
'description' => [ | ||
'en' => $this->faker->sentence(), | ||
'es' => $this->faker->sentence(), | ||
'nl' => $this->faker->sentence(), | ||
], | ||
'color' => $this->faker->hexColor(), | ||
'image' => $this->createImage('https://source.unsplash.com/random/1280x720/?img=1'), | ||
]; | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace Database\Factories\Concerns; | ||
|
||
use Database\Seeders\DatabaseSeeder; | ||
use Illuminate\Support\Facades\Storage; | ||
use Illuminate\Support\Str; | ||
|
||
trait CanCreateImages | ||
{ | ||
public function createImage(string $url): ?string | ||
{ | ||
try { | ||
$image = file_get_contents($url ?? DatabaseSeeder::IMAGE_URL); | ||
} catch (Throwable $exception) { | ||
return null; | ||
} | ||
|
||
$filename = Str::uuid() . '.jpg'; | ||
|
||
Storage::disk('public')->put($filename, $image); | ||
|
||
return $filename; | ||
} | ||
} |
Oops, something went wrong.