Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
markvaneijk committed Oct 30, 2024
1 parent d7844ff commit 984f737
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -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
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('redirects', function (Blueprint $table) {
$table->after('ulid', function ($table) {
$table->foreignUlid('site_ulid')->nullable()->constrained(table: 'sites', column: 'ulid')->cascadeOnUpdate()->cascadeOnDelete();
$table->char('language_code', 2)->nullable();
$table->char('country_code', 2)->nullable();
$table->foreignUlid('content_ulid')->nullable()->constrained(table: 'content', column: 'ulid')->cascadeOnUpdate()->cascadeOnDelete();
});

$table->foreign(['language_code', 'country_code'])->references(['code', 'country_code'])->on('languages')->cascadeOnUpdate()->nullOnDelete();
});
}
};
23 changes: 0 additions & 23 deletions database/migrations/create_redirects_table_filament.php.stub

This file was deleted.

5 changes: 0 additions & 5 deletions src/FilamentRedirects.php

This file was deleted.

11 changes: 3 additions & 8 deletions src/FilamentRedirectsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ class FilamentRedirectsServiceProvider extends PackageServiceProvider

public function configurePackage(Package $package): void
{
/*
* This class is a Package Service Provider
*
* More info: https://github.com/spatie/laravel-package-tools
*/
$package->name(static::$name)
->hasCommands($this->getCommands())
->hasInstallCommand(function (InstallCommand $command) {
Expand Down Expand Up @@ -101,8 +96,8 @@ protected function getAssets(): array
{
return [
// AlpineComponent::make('filament-redirects', __DIR__ . '/../resources/dist/components/filament-redirects.js'),
Css::make('filament-redirects-styles', __DIR__ . '/../resources/dist/filament-redirects.css'),
Js::make('filament-redirects-scripts', __DIR__ . '/../resources/dist/filament-redirects.js'),
// Css::make('filament-redirects-styles', __DIR__ . '/../resources/dist/filament-redirects.css'),
// Js::make('filament-redirects-scripts', __DIR__ . '/../resources/dist/filament-redirects.js'),
];
}

Expand Down Expand Up @@ -146,7 +141,7 @@ protected function getScriptData(): array
protected function getMigrations(): array
{
return [
'create_filament-redirects_table',
'add_columns_to_redirects_table_for_backstage',
];
}
}
75 changes: 75 additions & 0 deletions src/Imports/RedirectImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Vormkracht10\FilamentRedirects\Imports;

use Vormkracht10\Redirects\Models\Redirect;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;

class RedirectImporter extends Importer
{
protected static ?string $model = Redirect::class;

public static function getColumns(): array
{
return [
ImportColumn::make('site_ulid')
->label(__('Site'))
->requiredMapping()
->guess(['site', 'site_ulid']),
ImportColumn::make('language_code')
->label(__('Language Code'))
->requiredMapping()
->guess(['language', 'language_code']),
ImportColumn::make('country_code')
->label(__('Country Code'))
->guess(['KvKnr']),
ImportColumn::make('content_ulid')
->label(__('Content'))
->requiredMapping()
->guess(['content', 'content_ulid']),
ImportColumn::make('source')
->requiredMapping()
->label(__('Source'))
->guess(['source']),
ImportColumn::make('destination')
->requiredMapping()
->label(__('Destination'))
->guess(['destination']),
ImportColumn::make('code')
->requiredMapping()
->rules(['max:3'])
->guess(['code', 'status', 'status_code']),
ImportColumn::make('hits')
->requiredMapping()
->guess(['hits']),
];
}

public function resolveRecord(): ?Redirect
{
if (isset($this->data['source'])) {
return Redirect::firstOrNew([
'source' => $this->data['source'],
]);
}
}

public static function getCompletedNotificationBody(Import $import): string
{
$body = __('Your redirects import has completed and :numbers :row imported.', [
'numbers' => number_format($import->successful_rows),
'row' => str('row')->plural($import->successful_rows),
]);

if ($failedRowsCount = $import->getFailedRowsCount()) {
$body .= __(' :numbers :row failed to import.', [
'numbers' => number_format($failedRowsCount),
'row' => str('row')->plural($failedRowsCount),
]);
}

return $body;
}
}
5 changes: 5 additions & 0 deletions src/Middleware/MatchRedirects.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Vormkracht10\FilamentRedirects\Middleware;

class MatchRedirects {}
9 changes: 6 additions & 3 deletions src/FilamentRedirectsPlugin.php → src/RedirectsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@

use Filament\Contracts\Plugin;
use Filament\Panel;
use Vormkracht10\FilamentRedirects\Resources\RedirectResource;

class FilamentRedirectsPlugin implements Plugin
class RedirectsPlugin implements Plugin
{
public function getId(): string
{
return 'filament-redirects';
return 'redirects';
}

public function register(Panel $panel): void
{
//
$panel->resources([
RedirectResource::class,
]);
}

public function boot(Panel $panel): void
Expand Down
94 changes: 89 additions & 5 deletions src/Resources/RedirectResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

namespace Vormkracht10\FilamentRedirects\Resources;

use Locale;
use Filament\Tables;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Section;
use Illuminate\Support\Facades\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Vormkracht10\Backstage\Models\Site;
use Filament\Forms\Components\TextInput;
use Vormkracht10\Backstage\Models\Language;
use Vormkracht10\Redirects\Models\Redirect;
use Vormkracht10\Backstage\Resources\RedirectResource\Pages;
use Vormkracht10\FilamentRedirects\Resources\RedirectResource\Pages;

class RedirectResource extends Resource
{
Expand All @@ -18,6 +25,8 @@ class RedirectResource extends Resource

public static ?string $recordTitleAttribute = 'name';

protected static ?int $navigationSort = 10;

public static function getModelLabel(): string
{
return __('Redirect');
Expand All @@ -31,14 +40,89 @@ public static function getPluralModelLabel(): string
public static function form(Form $form): Form
{
return $form
->schema([]);
->schema([
Section::make('From')
->columns([
'sm' => 1,
'md' => 12,
])
->schema([
Select::make('site_ulid')
->label(__('Site'))
->columnSpan(4)
->placeholder(__('Select Site'))
->prefixIcon('heroicon-o-link')
->options(Site::orderBy('default', 'desc')->orderBy('name', 'asc')->pluck('name', 'ulid'))
->default(Site::where('default', true)->first()?->ulid)
->visible(fn() => Schema::hasColumn('redirects', 'content_ulid') && Site::count() > 0)
->hidden(fn() => Schema::hasColumn('redirects', 'content_ulid') && Site::count() === 1),
Select::make('country_code')
->label(__('Country'))
->columnSpan(4)
->placeholder(__('Select Country'))
->prefixIcon('heroicon-o-flag')
->options(Language::whereNotNull('country_code')->distinct('country_code')->get()->mapWithKeys(fn($language) => [$language->country_code => Locale::getDisplayRegion('-' . $language->country_code, app()->getLocale())])->sort())
->default(Language::whereNotNull('country_code')->distinct('country_code')->count() === 1 ? Language::whereNotNull('country_code')->first()->country_code : null)
->visible(fn() => Schema::hasColumn('redirects', 'content_ulid') && Language::whereNotNull('country_code')->distinct('country_code')->count() > 0)
->hidden(fn() => Schema::hasColumn('redirects', 'content_ulid') && Language::whereNotNull('country_code')->distinct('country_code')->count() === 1),
Select::make('language_code')
->label(__('Language'))
->columnSpan(4)
->placeholder(__('Select Language'))
->prefixIcon('heroicon-o-language')
->options(Language::get()->mapWithKeys(fn($language) => [$language->code => Locale::getDisplayLanguage($language->code, app()->getLocale())])->sort())
->default(Language::count() === 1 ? Language::first()->code : Language::where('default', true)->first()->code)
->visible(fn() => Schema::hasColumn('redirects', 'content_ulid') && Language::count() > 0)
->hidden(fn() => Schema::hasColumn('redirects', 'content_ulid') && Language::count() === 1),
TextInput::make('source')
->label(__('Source'))
->columnSpan(12)
->required()
->prefixIcon('heroicon-o-arrow-uturn-right')
->placeholder(__('Type path, URL or pattern...'))
->helperText(__('The path to match, you can use regular expressions.')),
]),
Section::make('To')
->columns([
'sm' => 1,
'md' => 12,
])
->schema([
Select::make('code')
->label(__('Type'))
->columnSpan(4)
->native(false)
->options(collect(config('redirects.status_codes'))->map(fn(string $type, int $code) => $type . ' (' . $code . ')'))
->searchable()
->required()
->default(config('redirects.default_status_code'))
->prefixIcon('heroicon-o-hashtag')
->placeholder('HTTP status message'),
Select::make('content')
->label(__('Content'))
->columnSpan(8)
->placeholder('Content')
->prefixIcon('heroicon-o-link')
->helperText(__('Select a content page to redirect to the current URL.'))
->visible(fn() => Schema::hasColumn('redirects', 'content_ulid')),
TextInput::make('destination')
->label(__('Destination'))
->columnSpan(12)
->required()
->prefixIcon('heroicon-o-arrow-uturn-left')
->placeholder('Type path or URL...'),
]),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('path')
TextColumn::make('source')
->searchable()
->sortable(),
TextColumn::make('destination')
->searchable()
->sortable(),
])
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/RedirectResource/Pages/CreateRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Vormkracht10\FilamentRedirects\Resources\RedirectResource\Pages;

use Filament\Resources\Pages\CreateRecord;
use Vormkracht10\Backstage\Resources\RedirectResource;
use Vormkracht10\FilamentRedirects\Resources\RedirectResource;

class CreateRedirect extends CreateRecord
{
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/RedirectResource/Pages/EditRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use Vormkracht10\Backstage\Resources\RedirectResource;
use Vormkracht10\FilamentRedirects\Resources\RedirectResource;

class EditRedirect extends EditRecord
{
Expand Down
6 changes: 5 additions & 1 deletion src/Resources/RedirectResource/Pages/ListRedirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
use Vormkracht10\Backstage\Resources\RedirectResource;
use Vormkracht10\FilamentRedirects\Imports\RedirectImporter;
use Vormkracht10\FilamentRedirects\Resources\RedirectResource;

class ListRedirects extends ListRecords
{
Expand All @@ -13,6 +14,9 @@ class ListRedirects extends ListRecords
protected function getHeaderActions(): array
{
return [
Actions\ImportAction::make()
->label(__('Import'))
->importer(RedirectImporter::class),
Actions\CreateAction::make(),
];
}
Expand Down

0 comments on commit 984f737

Please sign in to comment.