Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Nov 16, 2024
1 parent a84fc3b commit a318c90
Show file tree
Hide file tree
Showing 56 changed files with 1,800 additions and 48 deletions.
84 changes: 84 additions & 0 deletions app/Concerns/HasSlug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace App\Concerns;

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());
}

protected function fillSlugs(): void
{
if (
! \array_key_exists('slug', $this->attributes) ||
! \array_key_exists($this->getSlugFieldSource(), $this->attributes)
) {
return;
}

$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::query()
->where('slug', $slug)
->withoutGlobalScopes();

if ($this->exists) {
$query->where($this->getKeyName(), '!=', $this->getKey());
}

return $query->exists();
}

public function getUrlAttribute(): ?string
{
$key = $this->getMorphClass();

if (! $this->slug) {
return null;
}

return route('front.' . Str::plural($key) . '.show', [
$key => $this->slug,
]);
}
}
62 changes: 62 additions & 0 deletions app/Enums/VoteMonitorStatKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace App\Enums;

use App\Concerns\Enums\Arrayable;
use App\Concerns\Enums\Comparable;
use Filament\Support\Colors\Color;
use Filament\Support\Contracts\HasColor;
use Filament\Support\Contracts\HasIcon;
use Filament\Support\Contracts\HasLabel;

enum VoteMonitorStatKey: string implements HasLabel, HasIcon, HasColor
{
use Arrayable;
use Comparable;

case OBSERVERS = 'observers';
case COUNTIES = 'counties';
case POLLING_STATIONS = 'polling_stations';
case MESSAGES = 'messages';
case PROBLEMS = 'problems';

protected function labelKeyPrefix(): ?string
{
return 'app.stats';
}

public function getLabel(): ?string
{
return match ($this) {
self::OBSERVERS => __('app.vote_monitor_stats.observers'),
self::COUNTIES => __('app.vote_monitor_stats.counties'),
self::POLLING_STATIONS => __('app.vote_monitor_stats.polling_stations'),
self::MESSAGES => __('app.vote_monitor_stats.messages'),
self::PROBLEMS => __('app.vote_monitor_stats.problems'),
};
}

public function getIcon(): ?string
{
return match ($this) {
self::OBSERVERS => 'gmdi-remove-red-eye-tt',
self::COUNTIES => 'gmdi-map-tt',
self::POLLING_STATIONS => 'gmdi-how-to-vote-tt',
self::MESSAGES => 'gmdi-send-to-mobile-tt',
self::PROBLEMS => 'gmdi-report-problem-tt',
};
}

public function getColor(): string|array|null
{
return match ($this) {
self::OBSERVERS => Color::Purple,
self::COUNTIES => Color::Purple,
self::POLLING_STATIONS => Color::Purple,
self::MESSAGES => Color::Purple,
self::PROBLEMS => Color::Amber,
};
}
}
105 changes: 105 additions & 0 deletions app/Filament/Admin/Resources/PageResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace App\Filament\Admin\Resources;

use App\Filament\Admin\Resources\PageResource\Pages;
use App\Models\Page;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use FilamentTiptapEditor\TiptapEditor;
use Illuminate\Support\Str;

class PageResource extends Resource
{
protected static ?string $model = Page::class;

protected static ?string $navigationIcon = 'heroicon-o-document-text';

protected static bool $isScopedToTenant = false;

protected static ?int $navigationSort = 32;

public static function getNavigationGroup(): ?string
{
return __('app.navigation.admin');
}

public static function getModelLabel(): string
{
return __('app.page.label.singular');
}

public static function getPluralModelLabel(): string
{
return __('app.page.label.plural');
}

public static function form(Form $form): Form
{
return $form
->schema([
Section::make()
->columns(2)
->schema([
TextInput::make('title')
->required()
->maxLength(255)
->lazy()
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}

$set('slug', Str::slug($state));
}),

TextInput::make('slug')
->required()
->unique(ignoreRecord: true)
->maxLength(255),

TiptapEditor::make('content')
->required()
->columnSpanFull(),
]),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('title')
->searchable()
->sortable(),
])
->filters([
//
])
->actions([
Tables\Actions\ViewAction::make()
->url(fn (Page $record) => $record->url)
->openUrlInNewTab(),

Tables\Actions\EditAction::make(),
]);
}

public static function getPages(): array
{
return [
'index' => Pages\ListPages::route('/'),
'create' => Pages\CreatePage::route('/create'),
'edit' => Pages\EditPage::route('/{record}/edit'),
];
}
}
13 changes: 13 additions & 0 deletions app/Filament/Admin/Resources/PageResource/Pages/CreatePage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Filament\Admin\Resources\PageResource\Pages;

use App\Filament\Admin\Resources\PageResource;
use Filament\Resources\Pages\CreateRecord;

class CreatePage extends CreateRecord
{
protected static string $resource = PageResource::class;
}
21 changes: 21 additions & 0 deletions app/Filament/Admin/Resources/PageResource/Pages/EditPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Filament\Admin\Resources\PageResource\Pages;

use App\Filament\Admin\Resources\PageResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

class EditPage extends EditRecord
{
protected static string $resource = PageResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}
21 changes: 21 additions & 0 deletions app/Filament/Admin/Resources/PageResource/Pages/ListPages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Filament\Admin\Resources\PageResource\Pages;

use App\Filament\Admin\Resources\PageResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListPages extends ListRecords
{
protected static string $resource = PageResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
Loading

0 comments on commit a318c90

Please sign in to comment.