Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#222 - activities crud #230

Merged
merged 15 commits into from
Apr 18, 2024
10 changes: 10 additions & 0 deletions app/Enums/DateFormats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Blumilk\Website\Enums;

class DateFormats
{
public const string DATE_DISPLAY = "j.m.Y";
}
129 changes: 129 additions & 0 deletions app/Filament/Resources/ActivityResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

declare(strict_types=1);

namespace Blumilk\Website\Filament\Resources;

use Blumilk\Website\Enums\DateFormats;
use Blumilk\Website\Filament\Resources\ActivityResource\Pages;
use Blumilk\Website\Models\Activity;
use Exception;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Concerns\Translatable;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Mvenghaus\FilamentPluginTranslatableInline\Forms\Components\TranslatableContainer;

class ActivityResource extends Resource
{
use Translatable;

protected static ?string $model = Activity::class;
protected static ?string $navigationIcon = "heroicon-o-rectangle-stack";

public static function form(Form $form): Form
{
return $form
->schema([
TranslatableContainer::make(
Forms\Components\TextInput::make("title")
->label("Tytuł")
->required()
->maxLength(255),
)->requiredLocales(config("app.translatable_locales")),
TranslatableContainer::make(
Forms\Components\TextInput::make("subtitle")
->label("Podtytuł")
->maxLength(255),
)->requiredLocales(config("app.translatable_locales")),
Forms\Components\Checkbox::make("published")
->label("Opublikowane"),
Forms\Components\DateTimePicker::make("published_at")
->format(DateFormats::DATE_DISPLAY)
->time(false)
->requiredUnless("published", true)
->label("Data publikacji"),
Forms\Components\FileUpload::make("photo")
->label("Zdjęcie")
->required()
->directory(Activity::PHOTOS_DIRECTORY)
->multiple(false)
->maxSize(1000),
TranslatableContainer::make(
Forms\Components\Textarea::make("description")
->label("Opis")
->required()
->maxLength(65000),
)->requiredLocales(config("app.translatable_locales")),
]);
}

/**
* @throws Exception
*/
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make("title")
->label("Tytuł")
->searchable(),
Tables\Columns\CheckboxColumn::make("published")
->label("Opublikowane"),
Tables\Columns\TextColumn::make("published_at")
->date(DateFormats::DATE_DISPLAY)
->label("Data publikacji")
->sortable(),
])->filters([
TernaryFilter::make("published")
->label("Status publikacji")
->placeholder("Wszystkie")
->trueLabel("Opublikowane")
->falseLabel("Nieopublikowane"),
Filter::make("published_at")
->form([
Forms\Components\DatePicker::make("published_from"),
Forms\Components\DatePicker::make("published_to"),
])->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data["published_from"],
fn(Builder $query, $date): Builder => $query->whereDate("published_at", ">=", $date),
)
->when(
$data["published_to"],
fn(Builder $query, $date): Builder => $query->whereDate("published_at", "<=", $date),
);
}),
])
->actions([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

public static function getPages(): array
{
return [
"index" => Pages\ListActivities::route("/"),
"create" => Pages\CreateActivity::route("/create"),
"edit" => Pages\EditActivity::route("/{record}/edit"),
];
}

public static function getTranslatableLocales(): array
{
return config("app.translatable_locales");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Blumilk\Website\Filament\Resources\ActivityResource\Pages;

use Blumilk\Website\Filament\Resources\ActivityResource;
use Filament\Resources\Pages\CreateRecord;

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

declare(strict_types=1);

namespace Blumilk\Website\Filament\Resources\ActivityResource\Pages;

use Blumilk\Website\Filament\Resources\ActivityResource;
use Filament\Resources\Pages\EditRecord;

class EditActivity extends EditRecord
{
protected static string $resource = ActivityResource::class;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Blumilk\Website\Filament\Resources\ActivityResource\Pages;

use Blumilk\Website\Filament\Resources\ActivityResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListActivities extends ListRecords
{
protected static string $resource = ActivityResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
47 changes: 47 additions & 0 deletions app/Models/Activity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Blumilk\Website\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;

/**
* @property array $title
* @property array $subtitle
* @property array $description
* @property string $photo
* @property bool $published
* @property Carbon $published_at
*/
class Activity extends Model
{
use HasTranslations;
use HasFactory;

public const string PHOTOS_DIRECTORY = "activities";

public $translatable = [
"title",
"subtitle",
"description",
];
protected $fillable = [
"photo",
"title",
"subtitle",
"description",
"published",
"published_at",
];
protected $casts = [
"title" => "array",
"subtitle" => "array",
"description" => "array",
"published" => "boolean",
"published_at" => "datetime",
];
}
5 changes: 5 additions & 0 deletions app/Providers/Filament/AdminPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Filament\Pages;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\SpatieLaravelTranslatablePlugin;
use Filament\Support\Colors\Color;
use Filament\Widgets;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
Expand Down Expand Up @@ -42,6 +43,10 @@ public function panel(Panel $panel): Panel
Widgets\AccountWidget::class,
Widgets\FilamentInfoWidget::class,
])
->plugin(
SpatieLaravelTranslatablePlugin::make()
->defaultLocales(config("app.translatable_locales")),
)
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
"ext-pdo": "*",
"codezero/laravel-localized-routes": "^4.0",
"filament/filament": "^3.2",
"filament/spatie-laravel-translatable-plugin": "^3.2",
"guzzlehttp/guzzle": "^7.8",
"laravel/framework": "^11.0",
"laravel/sanctum": "^4.0",
"laravel/tinker": "^2.9"
"laravel/tinker": "^2.9",
"mvenghaus/filament-plugin-translatable-inline": "^3.0"
},
"require-dev": {
"blumilksoftware/codestyle": "^v2.8",
Expand Down
Loading
Loading