Add models, enums, and services for user roles, theme preferences, slug history, and ID generation

This commit is contained in:
pacnpal
2025-02-23 19:50:40 -05:00
parent 32aea21e48
commit 7e5d15eb46
55 changed files with 6462 additions and 4 deletions

View File

@@ -0,0 +1,59 @@
<?php
namespace App\Livewire;
use App\Models\ParkArea;
use Livewire\Component;
class AreaStatisticsComponent extends Component
{
public ParkArea $area;
public bool $showDetails = false;
public bool $showHistorical = false;
public function mount(ParkArea $area): void
{
$this->area = $area;
}
public function toggleDetails(): void
{
$this->showDetails = !$this->showDetails;
}
public function toggleHistorical(): void
{
$this->showHistorical = !$this->showHistorical;
}
/**
* Get the ride type distribution as percentages.
*
* @return array<string, float>
*/
protected function getRidePercentages(): array
{
if ($this->area->ride_count === 0) {
return [
'coasters' => 0,
'flat_rides' => 0,
'water_rides' => 0,
];
}
return [
'coasters' => round(($this->area->coaster_count / $this->area->ride_count) * 100, 1),
'flat_rides' => round(($this->area->flat_ride_count / $this->area->ride_count) * 100, 1),
'water_rides' => round(($this->area->water_ride_count / $this->area->ride_count) * 100, 1),
];
}
public function render()
{
return view('livewire.area-statistics-component', [
'rideDistribution' => $this->area->ride_distribution,
'ridePercentages' => $this->getRidePercentages(),
'historicalStats' => $this->area->historical_stats,
]);
}
}

View File

@@ -0,0 +1,78 @@
<?php
namespace App\Livewire;
use App\Models\Park;
use App\Models\ParkArea;
use Livewire\Component;
use Illuminate\Validation\Rule;
class ParkAreaFormComponent extends Component
{
public Park $park;
public ?ParkArea $area = null;
// Form fields
public string $name = '';
public ?string $description = '';
public ?string $opening_date = '';
public ?string $closing_date = '';
public function mount(Park $park, ?ParkArea $area = null): void
{
$this->park = $park;
$this->area = $area;
if ($area) {
$this->name = $area->name;
$this->description = $area->description ?? '';
$this->opening_date = $area->opening_date?->format('Y-m-d') ?? '';
$this->closing_date = $area->closing_date?->format('Y-m-d') ?? '';
}
}
public function rules(): array
{
$unique = $this->area
? Rule::unique('park_areas', 'name')
->where('park_id', $this->park->id)
->ignore($this->area->id)
: Rule::unique('park_areas', 'name')
->where('park_id', $this->park->id);
return [
'name' => ['required', 'string', 'min:2', 'max:255', $unique],
'description' => ['nullable', 'string'],
'opening_date' => ['nullable', 'date'],
'closing_date' => ['nullable', 'date', 'after:opening_date'],
];
}
public function save(): void
{
$data = $this->validate();
$data['park_id'] = $this->park->id;
if ($this->area) {
$this->area->update($data);
$message = 'Area updated successfully!';
} else {
$this->area = ParkArea::create($data);
$message = 'Area created successfully!';
}
session()->flash('message', $message);
$this->redirectRoute('parks.areas.show', [
'park' => $this->park,
'area' => $this->area,
]);
}
public function render()
{
return view('livewire.park-area-form-component', [
'isEditing' => (bool)$this->area,
]);
}
}

View File

@@ -0,0 +1,88 @@
<?php
namespace App\Livewire;
use App\Models\Park;
use App\Models\ParkArea;
use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Contracts\Database\Eloquent\Builder;
class ParkAreaListComponent extends Component
{
use WithPagination;
public Park $park;
public string $search = '';
public string $sort = 'name';
public string $direction = 'asc';
public bool $showClosed = false;
/** @var array<string, string> */
public array $sortOptions = [
'name' => 'Name',
'opening_date' => 'Opening Date',
];
protected $queryString = [
'search' => ['except' => ''],
'sort' => ['except' => 'name'],
'direction' => ['except' => 'asc'],
'showClosed' => ['except' => false],
];
public function mount(Park $park): void
{
$this->park = $park;
$this->resetPage('areas-page');
}
public function updatedSearch(): void
{
$this->resetPage('areas-page');
}
public function updatedShowClosed(): void
{
$this->resetPage('areas-page');
}
public function sortBy(string $field): void
{
if ($this->sort === $field) {
$this->direction = $this->direction === 'asc' ? 'desc' : 'asc';
} else {
$this->sort = $field;
$this->direction = 'asc';
}
}
public function deleteArea(ParkArea $area): void
{
$area->delete();
session()->flash('message', 'Area deleted successfully.');
}
public function render()
{
$query = $this->park->areas()
->when($this->search, function (Builder $query) {
$query->where('name', 'like', '%' . $this->search . '%')
->orWhere('description', 'like', '%' . $this->search . '%');
})
->when(!$this->showClosed, function (Builder $query) {
$query->whereNull('closing_date');
})
->when($this->sort === 'name', function (Builder $query) {
$query->orderBy('name', $this->direction);
})
->when($this->sort === 'opening_date', function (Builder $query) {
$query->orderBy('opening_date', $this->direction)
->orderBy('name', 'asc');
});
return view('livewire.park-area-list-component', [
'areas' => $query->paginate(10, pageName: 'areas-page'),
]);
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace App\Livewire;
use App\Models\Park;
use App\Models\ParkArea;
use Livewire\Component;
use Illuminate\Support\Collection;
class ParkAreaReorderComponent extends Component
{
public Park $park;
public ?int $parentId = null;
public array $areas = [];
public function mount(Park $park, ?int $parentId = null): void
{
$this->park = $park;
$this->parentId = $parentId;
$this->loadAreas();
}
/**
* Load areas for the current context (either top-level or within a parent).
*/
protected function loadAreas(): void
{
$query = $this->park->areas()
->where('parent_id', $this->parentId)
->orderBy('position')
->select(['id', 'name', 'position', 'closing_date']);
$this->areas = $query->get()
->map(fn ($area) => [
'id' => $area->id,
'name' => $area->name,
'position' => $area->position,
'is_closed' => !is_null($area->closing_date),
'has_children' => $area->hasChildren(),
])
->toArray();
}
/**
* Handle reordering of areas.
*/
public function reorder(array $orderedIds): void
{
// Validate that all IDs belong to this park and parent context
$validIds = $this->park->areas()
->where('parent_id', $this->parentId)
->pluck('id')
->toArray();
$orderedIds = array_values(array_intersect($orderedIds, $validIds));
// Update positions
foreach ($orderedIds as $position => $id) {
ParkArea::where('id', $id)->update(['position' => $position]);
}
$this->loadAreas();
$this->dispatch('areas-reordered');
}
/**
* Move an area to a new parent.
*/
public function moveToParent(int $areaId, ?int $newParentId): void
{
$area = $this->park->areas()->findOrFail($areaId);
// Prevent circular references
if ($newParentId === $area->id) {
return;
}
// If moving to a new parent, validate it exists and belongs to this park
if ($newParentId) {
$newParent = $this->park->areas()->findOrFail($newParentId);
// Prevent moving to own descendant
$ancestorIds = Collection::make();
$current = $newParent;
while ($current) {
if ($ancestorIds->contains($current->id)) {
return; // Circular reference detected
}
$ancestorIds->push($current->id);
if ($current->id === $area->id) {
return; // Would create circular reference
}
$current = $current->parent;
}
}
// Get the next position in the new parent context
$maxPosition = $this->park->areas()
->where('parent_id', $newParentId)
->max('position');
$area->update([
'parent_id' => $newParentId,
'position' => ($maxPosition ?? -1) + 1,
]);
// Reorder the old parent's remaining areas to close gaps
$this->park->areas()
->where('parent_id', $this->parentId)
->where('position', '>', $area->position)
->decrement('position');
$this->loadAreas();
$this->dispatch('area-moved');
}
public function render()
{
return view('livewire.park-area-reorder-component', [
'parentArea' => $this->parentId
? $this->park->areas()->find($this->parentId)
: null,
]);
}
}

View File

@@ -0,0 +1,105 @@
<?php
namespace App\Livewire;
use App\Models\Park;
use App\Models\Operator;
use App\Enums\ParkStatus;
use Livewire\Component;
use Livewire\WithFileUploads;
use Illuminate\Validation\Rules\Enum;
class ParkFormComponent extends Component
{
use WithFileUploads;
public ?Park $park = null;
// Form fields
public string $name = '';
public ?string $description = '';
public string $status = '';
public ?string $opening_date = '';
public ?string $closing_date = '';
public ?string $operating_season = '';
public ?string $size_acres = '';
public ?string $website = '';
public ?int $operator_id = null;
/** @var array<string> */
public array $statusOptions = [];
/** @var array<array-key, array<string, string>> */
public array $operators = [];
public function mount(?Park $park = null): void
{
$this->park = $park;
if ($park) {
$this->name = $park->name;
$this->description = $park->description ?? '';
$this->status = $park->status->value;
$this->opening_date = $park->opening_date?->format('Y-m-d') ?? '';
$this->closing_date = $park->closing_date?->format('Y-m-d') ?? '';
$this->operating_season = $park->operating_season ?? '';
$this->size_acres = $park->size_acres ? (string)$park->size_acres : '';
$this->website = $park->website ?? '';
$this->operator_id = $park->operator_id;
} else {
$this->status = ParkStatus::OPERATING->value;
}
// Load status options
$this->statusOptions = collect(ParkStatus::cases())
->mapWithKeys(fn (ParkStatus $status) => [$status->value => $status->label()])
->toArray();
// Load operators for select
$this->operators = Operator::orderBy('name')
->get(['id', 'name'])
->map(fn ($operator) => ['id' => $operator->id, 'name' => $operator->name])
->toArray();
}
public function rules(): array
{
$unique = $this->park
? "unique:parks,name,{$this->park->id}"
: 'unique:parks,name';
return [
'name' => ['required', 'string', 'min:2', 'max:255', $unique],
'description' => ['nullable', 'string'],
'status' => ['required', new Enum(ParkStatus::class)],
'opening_date' => ['nullable', 'date'],
'closing_date' => ['nullable', 'date', 'after:opening_date'],
'operating_season' => ['nullable', 'string', 'max:255'],
'size_acres' => ['nullable', 'numeric', 'min:0', 'max:999999.99'],
'website' => ['nullable', 'url', 'max:255'],
'operator_id' => ['nullable', 'exists:operators,id'],
];
}
public function save(): void
{
$data = $this->validate();
if ($this->park) {
$this->park->update($data);
$message = 'Park updated successfully!';
} else {
$this->park = Park::create($data);
$message = 'Park created successfully!';
}
session()->flash('message', $message);
$this->redirectRoute('parks.show', $this->park);
}
public function render()
{
return view('livewire.park-form-component');
}
}

View File

@@ -0,0 +1,124 @@
<?php
namespace App\Livewire;
use App\Models\Park;
use App\Enums\ParkStatus;
use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Contracts\Database\Eloquent\Builder;
class ParkListComponent extends Component
{
use WithPagination;
public string $search = '';
public string $status = '';
public string $sort = 'name';
public string $direction = 'asc';
public ?string $operator = null;
/** @var array<string, string> */
public array $sortOptions = [
'name' => 'Name',
'opening_date' => 'Opening Date',
'ride_count' => 'Ride Count',
'coaster_count' => 'Coaster Count',
'size_acres' => 'Size',
];
protected $queryString = [
'search' => ['except' => ''],
'status' => ['except' => ''],
'sort' => ['except' => 'name'],
'direction' => ['except' => 'asc'],
'operator' => ['except' => ''],
];
public function mount(): void
{
$this->resetPage('parks-page');
}
public function updatedSearch(): void
{
$this->resetPage('parks-page');
}
public function updatedStatus(): void
{
$this->resetPage('parks-page');
}
public function updatedOperator(): void
{
$this->resetPage('parks-page');
}
public function sortBy(string $field): void
{
if ($this->sort === $field) {
$this->direction = $this->direction === 'asc' ? 'desc' : 'asc';
} else {
$this->sort = $field;
$this->direction = 'asc';
}
}
public function getStatusOptions(): array
{
return collect(ParkStatus::cases())
->mapWithKeys(fn (ParkStatus $status) => [$status->value => $status->label()])
->prepend('All Statuses', '')
->toArray();
}
public function getOperatorOptions(): array
{
return \App\Models\Operator::orderBy('name')
->pluck('name', 'id')
->prepend('All Operators', '')
->toArray();
}
public function render()
{
$query = Park::query()
->with(['operator'])
->when($this->search, function (Builder $query) {
$query->where('name', 'like', '%' . $this->search . '%')
->orWhere('description', 'like', '%' . $this->search . '%');
})
->when($this->status, function (Builder $query) {
$query->where('status', $this->status);
})
->when($this->operator, function (Builder $query) {
$query->where('operator_id', $this->operator);
})
->when($this->sort === 'name', function (Builder $query) {
$query->orderBy('name', $this->direction);
})
->when($this->sort === 'opening_date', function (Builder $query) {
$query->orderBy('opening_date', $this->direction)
->orderBy('name', 'asc');
})
->when($this->sort === 'ride_count', function (Builder $query) {
$query->orderBy('ride_count', $this->direction)
->orderBy('name', 'asc');
})
->when($this->sort === 'coaster_count', function (Builder $query) {
$query->orderBy('coaster_count', $this->direction)
->orderBy('name', 'asc');
})
->when($this->sort === 'size_acres', function (Builder $query) {
$query->orderBy('size_acres', $this->direction)
->orderBy('name', 'asc');
});
return view('livewire.park-list-component', [
'parks' => $query->paginate(12, pageName: 'parks-page'),
'statusOptions' => $this->getStatusOptions(),
'operatorOptions' => $this->getOperatorOptions(),
]);
}
}

View File

@@ -0,0 +1,86 @@
<?php
namespace App\Livewire;
use App\Models\Profile;
use Livewire\Component;
use Livewire\WithFileUploads;
use Livewire\Attributes\Rule;
use Illuminate\Support\Facades\Auth;
class ProfileComponent extends Component
{
use WithFileUploads;
public Profile $profile;
#[Rule('required|min:2|max:50|unique:profiles,display_name')]
public string $display_name = '';
#[Rule('nullable|max:50')]
public ?string $pronouns = '';
#[Rule('nullable|max:500')]
public ?string $bio = '';
#[Rule('nullable|url|max:255')]
public ?string $twitter = '';
#[Rule('nullable|url|max:255')]
public ?string $instagram = '';
#[Rule('nullable|url|max:255')]
public ?string $youtube = '';
#[Rule('nullable|max:100')]
public ?string $discord = '';
#[Rule('nullable|image|max:1024')] // 1MB Max
public $avatar;
public function mount()
{
$this->profile = Auth::user()->profile;
$this->fill($this->profile->only([
'display_name',
'pronouns',
'bio',
'twitter',
'instagram',
'youtube',
'discord',
]));
}
public function save()
{
$this->validate();
if ($this->avatar) {
$this->profile->setAvatar($this->avatar);
}
$this->profile->update([
'display_name' => $this->display_name,
'pronouns' => $this->pronouns,
'bio' => $this->bio,
'twitter' => $this->twitter,
'instagram' => $this->instagram,
'youtube' => $this->youtube,
'discord' => $this->discord,
]);
session()->flash('message', 'Profile updated successfully!');
}
public function removeAvatar()
{
$this->profile->setAvatar(null);
session()->flash('message', 'Avatar removed successfully!');
}
public function render()
{
return view('livewire.profile-component');
}
}