Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Jun 4, 2024
1 parent 745c07d commit 0858cef
Show file tree
Hide file tree
Showing 33 changed files with 1,344 additions and 270 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ RUN apk update && \
#
# production dependencies
apk add --no-cache \
ffmpeg \
nginx && \
#
# install extensions
Expand Down
80 changes: 80 additions & 0 deletions app/Concerns/Publishable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace App\Concerns;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;

trait Publishable
{
public function initializePublishable(): void
{
$this->fillable[] = 'published_at';

$this->casts['published_at'] = 'datetime';
}

public function scopeWithDrafted(Builder $query): Builder
{
return $query->withoutGlobalScope('published');
}

public function scopeOnlyDrafted(Builder $query): Builder
{
return $query
->withDrafted()
->whereNull('published_at');
}

public function scopeOnlyScheduled(Builder $query): Builder
{
return $query
->withDrafted()
->whereNotNull('published_at')
->where('published_at', '>', Carbon::now());
}

public function scopeOnlyPublished(Builder $query): Builder
{
return $query
->whereNotNull('published_at')
->where('published_at', '<=', Carbon::now());
}

public function isDraft(): bool
{
return \is_null($this->published_at);
}

public function isPublished(): bool
{
return ! $this->isDraft() && $this->published_at->isPast();
}

public function isScheduled(): bool
{
return ! $this->isDraft() && $this->published_at->isFuture();
}

/**
* Determine the publish status of the model instance.
*
* @return string
*/
public function status(): string
{
if ($this->isDraft()) {
return 'draft';
}

if ($this->isPublished()) {
return 'published';
}

if ($this->isScheduled()) {
return 'scheduled';
}
}
}
50 changes: 50 additions & 0 deletions app/Enums/Country.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace App\Enums;

use App\Concerns\Enums\Arrayable;
use App\Concerns\Enums\Comparable;
use App\Concerns\Enums\HasLabel;

enum Country: string
{
use Arrayable;
use Comparable;
use HasLabel;

case EU = 'eu';
case AT = 'at';
case BE = 'be';
case BG = 'bg';
case HR = 'hr';
case CY = 'cy';
case CZ = 'cz';
case DK = 'dk';
case EE = 'ee';
case FI = 'fi';
case FR = 'fr';
case DE = 'de';
case GR = 'gr';
case HU = 'hu';
case IE = 'ie';
case IT = 'it';
case LV = 'lv';
case LT = 'lt';
case LU = 'lu';
case MT = 'mt';
case NL = 'nl';
case PL = 'pl';
case PT = 'pt';
case RO = 'ro';
case SK = 'sk';
case SI = 'si';
case ES = 'es';
case SE = 'se';

protected function labelKeyPrefix(): ?string
{
return 'countries';
}
}
79 changes: 79 additions & 0 deletions app/Filament/Resources/ElectionDayResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources;

use App\Filament\Resources\ElectionDayResource\Pages;
use App\Models\ElectionDay;
use Carbon\Carbon;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class ElectionDayResource extends Resource
{
protected static ?string $model = ElectionDay::class;

protected static ?string $navigationIcon = 'heroicon-o-calendar-days';

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

public static function form(Form $form): Form
{
return $form
->schema([
DatePicker::make('date')
->time(false)
->unique(ignoreRecord:true)
->columnSpanFull(),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('id')
->prefix('#')
->sortable()
->shrink(),

TextColumn::make('date')
->sortable()
->formatStateUsing(fn (Carbon $state) => $state->toDateString()),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
])
->defaultSort('date', 'desc');
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ManageElectionDays::route('/'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\ElectionDayResource\Pages;

use App\Filament\Resources\ElectionDayResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ManageElectionDays extends ListRecords
{
protected static string $resource = ElectionDayResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
96 changes: 92 additions & 4 deletions app/Filament/Resources/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,31 @@

namespace App\Filament\Resources;

use App\Enums\Country;
use App\Filament\Resources\PostResource\Pages;
use App\Models\ElectionDay;
use App\Models\Post;
use Carbon\Carbon;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class PostResource extends Resource
{
protected static ?string $model = Post::class;

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

public static function getNavigationGroup(): ?string
{
Expand All @@ -27,20 +39,95 @@ public static function form(Form $form): Form
{
return $form
->schema([
//
Section::make()
->columns(2)
->schema([
TextInput::make('title')
->required()
->maxLength(255),

Select::make('country')
->options(Country::options())
->enum(Country::class),

Select::make('author_id')
->relationship('author', 'name')
->required()
->preload(),

DateTimePicker::make('published_at')
->nullable(),

RichEditor::make('content')
->required()
->columnSpanFull(),
]),

Section::make()
->schema([
SpatieMediaLibraryFileUpload::make('media')
->multiple()
->reorderable()
->previewable(false),
]),

Section::make()
->schema([
Repeater::make('embeds')
->schema([
Textarea::make('html'),
]),
]),

]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('id')
->prefix('#')
->sortable()
->shrink(),

TextColumn::make('electionDay.date')
->formatStateUsing(fn (?Carbon $state) => $state?->toDateString())
->sortable()
->toggleable(),

TextColumn::make('title')
->searchable()
->sortable(),

TextColumn::make('country')
->badge()
->formatStateUsing(fn (?Country $state) => $state?->label()),

TextColumn::make('author.name')
->sortable()
->toggleable(),

TextColumn::make('published_at')
->formatStateUsing(fn (?Carbon $state) => $state?->toDateTimeString())
->sortable()
->toggleable(),
])
->filters([
//
SelectFilter::make('country')
->options(Country::options())
->multiple(),

SelectFilter::make('author')
->relationship('author', 'name')
->multiple()
->preload(),

SelectFilter::make('electionDay')
->relationship('electionDay', 'date')
->getOptionLabelFromRecordUsing(fn (ElectionDay $record) => $record->date->toDateString())
->multiple()
->preload(),
])
->actions([
Tables\Actions\EditAction::make(),
Expand All @@ -49,7 +136,8 @@ public static function table(Table $table): Table
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
])
->defaultSort('id', 'desc');
}

public static function getRelations(): array
Expand Down
Loading

0 comments on commit 0858cef

Please sign in to comment.