Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Dec 17, 2023
1 parent 838a7fd commit a2b118a
Show file tree
Hide file tree
Showing 13 changed files with 459 additions and 102 deletions.
152 changes: 152 additions & 0 deletions app/Filament/Resources/Blog/LinkResource.php
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 app/Filament/Resources/Blog/LinkResource/Pages/CreateLink.php
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 app/Filament/Resources/Blog/LinkResource/Pages/EditLink.php
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 app/Filament/Resources/Blog/LinkResource/Pages/ListLinks.php
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 app/Filament/Resources/Blog/LinkResource/Pages/ViewLink.php
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(),
];
}
}
1 change: 0 additions & 1 deletion app/Filament/Resources/Blog/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public static function form(Form $form): Form
Forms\Components\Section::make('Image')
->schema([
Forms\Components\FileUpload::make('image')
->label('Image')
->image()
->hiddenLabel(),
])
Expand Down
20 changes: 20 additions & 0 deletions app/Models/Blog/Link.php
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';
}
7 changes: 6 additions & 1 deletion app/Providers/Filament/AdminPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Filament\Pages;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\SpatieLaravelTranslatablePlugin;
use Filament\Widgets;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
Expand Down Expand Up @@ -55,6 +56,10 @@ public function panel(Panel $panel): Panel
])
->authMiddleware([
Authenticate::class,
]);
])
->plugin(
SpatieLaravelTranslatablePlugin::make()
->defaultLocales(['en', 'es', 'nl']),
);
}
}
41 changes: 41 additions & 0 deletions database/factories/Blog/LinkFactory.php
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'),
];
}
}
18 changes: 3 additions & 15 deletions database/factories/Blog/PostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Database\Factories\Blog;

use App\Models\Blog\Post;
use Database\Factories\Concerns\CanCreateImages;
use Database\Seeders\DatabaseSeeder;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Storage;
Expand All @@ -11,6 +12,8 @@

class PostFactory extends Factory
{
use CanCreateImages;

/**
* @var string
*/
Expand All @@ -28,19 +31,4 @@ public function definition(): array
'updated_at' => $this->faker->dateTimeBetween('-5 month', 'now'),
];
}

public function createImage(): ?string
{
try {
$image = file_get_contents(DatabaseSeeder::IMAGE_URL);
} catch (Throwable $exception) {
return null;
}

$filename = Str::uuid() . '.jpg';

Storage::disk('public')->put($filename, $image);

return $filename;
}
}
25 changes: 25 additions & 0 deletions database/factories/Concerns/CanCreateImages.php
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;
}
}
Loading

0 comments on commit a2b118a

Please sign in to comment.