Skip to content

Commit

Permalink
feat: pages (#275)
Browse files Browse the repository at this point in the history
* init pages

* wip

* cleanup
  • Loading branch information
andreiio authored Aug 28, 2023
1 parent 2ddbdbf commit 4667896
Show file tree
Hide file tree
Showing 38 changed files with 2,543 additions and 944 deletions.
135 changes: 135 additions & 0 deletions app/Concerns/HasSlug.php
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,
]);
}
}
20 changes: 20 additions & 0 deletions app/Concerns/HasTranslations.php
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.
92 changes: 92 additions & 0 deletions app/Filament/Resources/PageResource.php
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'),
];
}
}
24 changes: 24 additions & 0 deletions app/Filament/Resources/PageResource/Pages/CreatePage.php
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(),
];
}
}
25 changes: 25 additions & 0 deletions app/Filament/Resources/PageResource/Pages/EditPage.php
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(),
];
}
}
24 changes: 24 additions & 0 deletions app/Filament/Resources/PageResource/Pages/ListPages.php
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(),
];
}
}
25 changes: 25 additions & 0 deletions app/Filament/Resources/PageResource/Pages/ViewPage.php
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(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Filament\Resources\InterventionResource\Actions;
namespace App\Filament\Resources\TicketResource\Actions;

use App\Filament\Resources\TicketResource;
use App\Models\Ticket;
Expand Down
Loading

0 comments on commit 4667896

Please sign in to comment.