-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* init pages * wip * cleanup
- Loading branch information
Showing
38 changed files
with
2,543 additions
and
944 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,135 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Concerns; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Str; | ||
use Illuminate\Support\Traits\Localizable; | ||
|
||
trait HasSlug | ||
{ | ||
use Localizable; | ||
|
||
public function initializeHasSlug(): void | ||
{ | ||
$this->fillable[] = 'slug'; | ||
} | ||
|
||
public function getSlugFieldSource(): string | ||
{ | ||
return $this->slugFieldSource ?? 'title'; | ||
} | ||
|
||
public static function bootHasSlug(): void | ||
{ | ||
static::creating(fn (Model $model) => $model->fillSlugs()); | ||
static::updating(fn (Model $model) => $model->fillSlugs()); | ||
} | ||
|
||
public function scopeForSlug(Builder $query, string $slug, ?string $locale = null): Builder | ||
{ | ||
return $query->where($this->getSlugColumn($locale), $slug); | ||
} | ||
|
||
public function resolveRouteBindingQuery($query, $value, $field = null) | ||
{ | ||
if ($field === 'slug') { | ||
$field = $this->getSlugColumn(); | ||
} | ||
|
||
return parent::resolveRouteBindingQuery($query, $value, $field); | ||
} | ||
|
||
protected function getSlugColumn(?string $locale = null): string | ||
{ | ||
$column = 'slug'; | ||
|
||
if ($this->slugIsTranslatable()) { | ||
$column .= '->' . ($locale ?? app()->getLocale()); | ||
} | ||
|
||
return $column; | ||
} | ||
|
||
protected function fillSlugs(): void | ||
{ | ||
if ( | ||
! \array_key_exists('slug', $this->attributes) || | ||
! \array_key_exists($this->getSlugFieldSource(), $this->attributes) | ||
) { | ||
return; | ||
} | ||
|
||
if ($this->slugIsTranslatable()) { | ||
$slugs = $this->getTranslationsWithFallback('slug'); | ||
|
||
locales()->each( | ||
fn (string $locale) => $this->withLocale($locale, function () use ($slugs) { | ||
$slug = Str::slug($slugs[app()->getLocale()]); | ||
|
||
if (! $slug || $this->slugAlreadyUsed($slug)) { | ||
$this->slug = $this->generateSlug(); | ||
} | ||
}) | ||
); | ||
} else { | ||
locales()->each(function () { | ||
$this->slug = Str::slug($this->slug); | ||
|
||
if (! $this->slug || ! $this->slugAlreadyUsed($this->slug)) { | ||
$this->slug = $this->generateSlug(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public function generateSlug(): string | ||
{ | ||
$base = $slug = Str::slug($this->{$this->getSlugFieldSource()}); | ||
$suffix = 1; | ||
|
||
while ($this->slugAlreadyUsed($slug)) { | ||
$slug = Str::slug($base . '_' . $suffix++); | ||
} | ||
|
||
return $slug; | ||
} | ||
|
||
protected function slugAlreadyUsed(string $slug): bool | ||
{ | ||
$query = static::forSlug($slug) | ||
->withoutGlobalScopes(); | ||
|
||
if ($this->exists) { | ||
$query->where($this->getKeyName(), '!=', $this->getKey()); | ||
} | ||
|
||
return $query->exists(); | ||
} | ||
|
||
protected function slugIsTranslatable(): bool | ||
{ | ||
return property_exists($this, 'translatable') && \in_array('slug', $this->translatable); | ||
} | ||
|
||
public function getUrlAttribute(): ?string | ||
{ | ||
$key = $this->getMorphClass(); | ||
|
||
$slug = $this->slugIsTranslatable() | ||
? $this->getTranslationWithoutFallback('slug', app()->getLocale()) | ||
: $this->slug; | ||
|
||
if (! $slug) { | ||
return null; | ||
} | ||
|
||
return route('front.' . Str::plural($key) . '.show', [ | ||
'locale' => app()->getLocale(), | ||
$key => $slug, | ||
]); | ||
} | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Concerns; | ||
|
||
use Spatie\Translatable\HasTranslations as BaseHasTranslations; | ||
|
||
trait HasTranslations | ||
{ | ||
use BaseHasTranslations; | ||
|
||
public function getTranslationsWithFallback(?string $key = null): array | ||
{ | ||
return locales() | ||
->mapWithKeys(fn (string $locale) => [$locale => null]) | ||
->merge($this->getTranslations($key)) | ||
->toArray(); | ||
} | ||
} |
File renamed without changes.
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,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources; | ||
|
||
use App\Filament\Resources\PageResource\Pages; | ||
use App\Models\Page; | ||
use Camya\Filament\Forms\Components\TitleWithSlugInput; | ||
use Filament\Resources\Concerns\Translatable; | ||
use Filament\Resources\Form; | ||
use Filament\Resources\Resource; | ||
use Filament\Resources\Table; | ||
use Filament\Tables; | ||
use Filament\Tables\Columns\TextColumn; | ||
use FilamentTiptapEditor\TiptapEditor; | ||
|
||
class PageResource extends Resource | ||
{ | ||
use Translatable; | ||
|
||
protected static ?string $model = Page::class; | ||
|
||
protected static ?string $navigationGroup = 'Administrează'; | ||
|
||
protected static ?int $navigationSort = 8; | ||
|
||
protected static ?string $navigationIcon = 'heroicon-o-collection'; | ||
|
||
protected static ?string $modelLabel = 'Pagină'; | ||
|
||
protected static ?string $pluralModelLabel = 'Pagini'; | ||
|
||
public static function form(Form $form): Form | ||
{ | ||
return $form | ||
->schema([ | ||
TitleWithSlugInput::make( | ||
fieldTitle: 'title', | ||
fieldSlug: 'slug', | ||
urlVisitLinkRoute: fn (?Page $record) => $record?->slug | ||
? route('page', $record) | ||
: null, | ||
)->columnSpanFull(), | ||
|
||
TiptapEditor::make('content') | ||
->extraInputAttributes([ | ||
'style' => 'min-height: 12rem;', | ||
]) | ||
->columnSpanFull(), | ||
]); | ||
} | ||
|
||
public static function table(Table $table): Table | ||
{ | ||
return $table | ||
->columns([ | ||
TextColumn::make('title') | ||
->searchable() | ||
->sortable(), | ||
|
||
TextColumn::make('created_at') | ||
->sortable(), | ||
]) | ||
->filters([ | ||
// | ||
]) | ||
->actions([ | ||
Tables\Actions\EditAction::make(), | ||
]) | ||
->bulkActions([ | ||
Tables\Actions\DeleteBulkAction::make(), | ||
]); | ||
} | ||
|
||
public static function getRelations(): array | ||
{ | ||
return [ | ||
// | ||
]; | ||
} | ||
|
||
public static function getPages(): array | ||
{ | ||
return [ | ||
'index' => Pages\ListPages::route('/'), | ||
'create' => Pages\CreatePage::route('/create'), | ||
'view' => Pages\ViewPage::route('/{record}'), | ||
'edit' => Pages\EditPage::route('/{record}/edit'), | ||
]; | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources\PageResource\Pages; | ||
|
||
use App\Filament\Resources\PageResource; | ||
use Filament\Pages\Actions; | ||
use Filament\Resources\Pages\CreateRecord; | ||
use Filament\Resources\Pages\CreateRecord\Concerns\Translatable; | ||
|
||
class CreatePage extends CreateRecord | ||
{ | ||
use Translatable; | ||
|
||
protected static string $resource = PageResource::class; | ||
|
||
protected function getActions(): array | ||
{ | ||
return [ | ||
// 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources\PageResource\Pages; | ||
|
||
use App\Filament\Resources\PageResource; | ||
use Filament\Pages\Actions; | ||
use Filament\Resources\Pages\EditRecord; | ||
use Filament\Resources\Pages\EditRecord\Concerns\Translatable; | ||
|
||
class EditPage extends EditRecord | ||
{ | ||
use Translatable; | ||
|
||
protected static string $resource = PageResource::class; | ||
|
||
protected function getActions(): array | ||
{ | ||
return [ | ||
Actions\DeleteAction::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources\PageResource\Pages; | ||
|
||
use App\Filament\Resources\PageResource; | ||
use Filament\Pages\Actions; | ||
use Filament\Resources\Pages\ListRecords; | ||
use Filament\Resources\Pages\ListRecords\Concerns\Translatable; | ||
|
||
class ListPages extends ListRecords | ||
{ | ||
use Translatable; | ||
|
||
protected static string $resource = PageResource::class; | ||
|
||
protected function getActions(): array | ||
{ | ||
return [ | ||
Actions\CreateAction::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources\PageResource\Pages; | ||
|
||
use App\Filament\Resources\PageResource; | ||
use Filament\Pages\Actions; | ||
use Filament\Resources\Pages\ViewRecord; | ||
use Filament\Resources\Pages\ViewRecord\Concerns\Translatable; | ||
|
||
class ViewPage extends ViewRecord | ||
{ | ||
use Translatable; | ||
|
||
protected static string $resource = PageResource::class; | ||
|
||
protected function getActions(): 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
Oops, something went wrong.