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

80
app/Enums/ParkStatus.php Normal file
View File

@@ -0,0 +1,80 @@
<?php
namespace App\Enums;
enum ParkStatus: string
{
case OPERATING = 'OPERATING';
case CLOSED_TEMP = 'CLOSED_TEMP';
case CLOSED_PERM = 'CLOSED_PERM';
case UNDER_CONSTRUCTION = 'UNDER_CONSTRUCTION';
case DEMOLISHED = 'DEMOLISHED';
case RELOCATED = 'RELOCATED';
/**
* Get the display label for the status
*/
public function label(): string
{
return match($this) {
self::OPERATING => 'Operating',
self::CLOSED_TEMP => 'Temporarily Closed',
self::CLOSED_PERM => 'Permanently Closed',
self::UNDER_CONSTRUCTION => 'Under Construction',
self::DEMOLISHED => 'Demolished',
self::RELOCATED => 'Relocated',
};
}
/**
* Get Tailwind CSS classes for status badge
*/
public function getStatusClasses(): string
{
return match($this) {
self::OPERATING => 'bg-green-100 text-green-800',
self::CLOSED_TEMP => 'bg-yellow-100 text-yellow-800',
self::CLOSED_PERM => 'bg-red-100 text-red-800',
self::UNDER_CONSTRUCTION => 'bg-blue-100 text-blue-800',
self::DEMOLISHED => 'bg-gray-100 text-gray-800',
self::RELOCATED => 'bg-purple-100 text-purple-800',
};
}
/**
* Check if the park is currently operational
*/
public function isOperational(): bool
{
return $this === self::OPERATING;
}
/**
* Check if the park is permanently closed
*/
public function isPermanentlyClosed(): bool
{
return in_array($this, [self::CLOSED_PERM, self::DEMOLISHED]);
}
/**
* Check if the park is temporarily closed
*/
public function isTemporarilyClosed(): bool
{
return $this === self::CLOSED_TEMP;
}
/**
* Get all status options as an array for forms
*
* @return array<string, string>
*/
public static function options(): array
{
return array_reduce(self::cases(), function ($carry, $status) {
$carry[$status->value] = $status->label();
return $carry;
}, []);
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Enums;
enum ThemePreference: string
{
case LIGHT = 'light';
case DARK = 'dark';
public function label(): string
{
return match($this) {
self::LIGHT => 'Light',
self::DARK => 'Dark',
};
}
public function cssClass(): string
{
return match($this) {
self::LIGHT => 'theme-light',
self::DARK => 'theme-dark',
};
}
}

37
app/Enums/UserRole.php Normal file
View File

@@ -0,0 +1,37 @@
<?php
namespace App\Enums;
enum UserRole: string
{
case USER = 'USER';
case MODERATOR = 'MODERATOR';
case ADMIN = 'ADMIN';
case SUPERUSER = 'SUPERUSER';
public function label(): string
{
return match($this) {
self::USER => 'User',
self::MODERATOR => 'Moderator',
self::ADMIN => 'Admin',
self::SUPERUSER => 'Superuser',
};
}
public function canModerate(): bool
{
return match($this) {
self::MODERATOR, self::ADMIN, self::SUPERUSER => true,
default => false,
};
}
public function canAdmin(): bool
{
return match($this) {
self::ADMIN, self::SUPERUSER => true,
default => false,
};
}
}

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

221
app/Models/Location.php Normal file
View File

@@ -0,0 +1,221 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
class Location extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'address',
'city',
'state',
'country',
'postal_code',
'latitude',
'longitude',
'elevation',
'timezone',
'metadata',
'is_approximate',
'source',
'geocoding_data',
'geocoded_at',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'latitude' => 'decimal:8',
'longitude' => 'decimal:8',
'elevation' => 'decimal:2',
'metadata' => 'array',
'geocoding_data' => 'array',
'geocoded_at' => 'datetime',
'is_approximate' => 'boolean',
];
/**
* Get the parent locatable model.
*/
public function locatable(): MorphTo
{
return $this->morphTo();
}
/**
* Get the location's coordinates as an array.
*
* @return array<string, float|null>
*/
public function getCoordinatesAttribute(): array
{
return [
'lat' => $this->latitude,
'lng' => $this->longitude,
];
}
/**
* Get the formatted address.
*
* @return string
*/
public function getFormattedAddressAttribute(): string
{
$parts = [];
if ($this->address) {
$parts[] = $this->address;
}
if ($this->city) {
$parts[] = $this->city;
}
if ($this->state) {
$parts[] = $this->state;
}
if ($this->postal_code) {
$parts[] = $this->postal_code;
}
if ($this->country) {
$parts[] = $this->country;
}
return implode(', ', $parts);
}
/**
* Get Google Maps URL for the location.
*
* @return string|null
*/
public function getMapUrlAttribute(): ?string
{
if (!$this->latitude || !$this->longitude) {
return null;
}
return sprintf(
'https://www.google.com/maps?q=%f,%f',
$this->latitude,
$this->longitude
);
}
/**
* Update the location's coordinates.
*
* @param float $latitude
* @param float $longitude
* @param float|null $elevation
* @return bool
*/
public function updateCoordinates(float $latitude, float $longitude, ?float $elevation = null): bool
{
return $this->update([
'latitude' => $latitude,
'longitude' => $longitude,
'elevation' => $elevation,
]);
}
/**
* Set the location's address components.
*
* @param array<string, string> $components
* @return bool
*/
public function setAddress(array $components): bool
{
return $this->update([
'address' => $components['address'] ?? $this->address,
'city' => $components['city'] ?? $this->city,
'state' => $components['state'] ?? $this->state,
'country' => $components['country'] ?? $this->country,
'postal_code' => $components['postal_code'] ?? $this->postal_code,
]);
}
/**
* Scope a query to find locations within a radius.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param float $latitude
* @param float $longitude
* @param float $radius
* @param string $unit
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNearby($query, float $latitude, float $longitude, float $radius, string $unit = 'km')
{
$earthRadius = $unit === 'mi' ? 3959 : 6371;
return $query->whereRaw(
"($earthRadius * acos(
cos(radians(?)) *
cos(radians(latitude)) *
cos(radians(longitude) - radians(?)) +
sin(radians(?)) *
sin(radians(latitude))
)) <= ?",
[$latitude, $longitude, $latitude, $radius]
);
}
/**
* Scope a query to find locations within bounds.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array<string, float> $ne Northeast corner [lat, lng]
* @param array<string, float> $sw Southwest corner [lat, lng]
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeInBounds($query, array $ne, array $sw)
{
return $query->whereBetween('latitude', [$sw['lat'], $ne['lat']])
->whereBetween('longitude', [$sw['lng'], $ne['lng']]);
}
/**
* Calculate the distance to a point.
*
* @param float $latitude
* @param float $longitude
* @param string $unit
* @return float|null
*/
public function distanceTo(float $latitude, float $longitude, string $unit = 'km'): ?float
{
if (!$this->latitude || !$this->longitude) {
return null;
}
$earthRadius = $unit === 'mi' ? 3959 : 6371;
$latFrom = deg2rad($this->latitude);
$lonFrom = deg2rad($this->longitude);
$latTo = deg2rad($latitude);
$lonTo = deg2rad($longitude);
$latDelta = $latTo - $latFrom;
$lonDelta = $lonTo - $lonFrom;
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
return $angle * $earthRadius;
}
}

View File

@@ -0,0 +1,98 @@
<?php
namespace App\Models;
use App\Traits\HasSlugHistory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Manufacturer extends Model
{
use HasFactory, HasSlugHistory;
/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'name',
'slug',
'website',
'headquarters',
'description',
'total_rides',
'total_roller_coasters',
];
/**
* Get the rides manufactured by this company.
* Note: This relationship will be properly set up when we implement the Rides system.
*/
public function rides(): HasMany
{
return $this->hasMany(Ride::class);
}
/**
* Update ride statistics.
*/
public function updateStatistics(): void
{
$this->total_rides = $this->rides()->count();
$this->total_roller_coasters = $this->rides()
->where('type', 'roller_coaster')
->count();
$this->save();
}
/**
* Get the manufacturer's name with total rides.
*/
public function getDisplayNameAttribute(): string
{
return "{$this->name} ({$this->total_rides} rides)";
}
/**
* Get formatted website URL (ensures proper URL format).
*/
public function getWebsiteUrlAttribute(): string
{
if (!$this->website) {
return '';
}
$website = $this->website;
if (!str_starts_with($website, 'http://') && !str_starts_with($website, 'https://')) {
$website = 'https://' . $website;
}
return $website;
}
/**
* Scope a query to only include major manufacturers (with multiple rides).
*/
public function scopeMajorManufacturers($query, int $minRides = 5)
{
return $query->where('total_rides', '>=', $minRides);
}
/**
* Scope a query to only include coaster manufacturers.
*/
public function scopeCoasterManufacturers($query)
{
return $query->where('total_roller_coasters', '>', 0);
}
/**
* Get the route key for the model.
*/
public function getRouteKeyName(): string
{
return 'slug';
}
}

87
app/Models/Operator.php Normal file
View File

@@ -0,0 +1,87 @@
<?php
namespace App\Models;
use App\Traits\HasSlugHistory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Operator extends Model
{
use HasFactory, HasSlugHistory;
/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'name',
'slug',
'website',
'headquarters',
'description',
'total_parks',
'total_rides',
];
/**
* Get the parks operated by this company.
*/
public function parks(): HasMany
{
return $this->hasMany(Park::class);
}
/**
* Update park statistics.
*/
public function updateStatistics(): void
{
$this->total_parks = $this->parks()->count();
$this->total_rides = $this->parks()->sum('ride_count');
$this->save();
}
/**
* Get the operator's name with total parks.
*/
public function getDisplayNameAttribute(): string
{
return "{$this->name} ({$this->total_parks} parks)";
}
/**
* Get formatted website URL (ensures proper URL format).
*/
public function getWebsiteUrlAttribute(): string
{
if (!$this->website) {
return '';
}
$website = $this->website;
if (!str_starts_with($website, 'http://') && !str_starts_with($website, 'https://')) {
$website = 'https://' . $website;
}
return $website;
}
/**
* Scope a query to only include major operators (with multiple parks).
*/
public function scopeMajorOperators($query, int $minParks = 3)
{
return $query->where('total_parks', '>=', $minParks);
}
/**
* Get the route key for the model.
*/
public function getRouteKeyName(): string
{
return 'slug';
}
}

182
app/Models/Park.php Normal file
View File

@@ -0,0 +1,182 @@
<?php
namespace App\Models;
use App\Enums\ParkStatus;
use App\Traits\HasSlugHistory;
use App\Traits\HasParkStatistics;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Park extends Model
{
use HasFactory, HasSlugHistory, HasParkStatistics;
/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'name',
'slug',
'description',
'status',
'opening_date',
'closing_date',
'operating_season',
'size_acres',
'website',
'operator_id',
'total_areas',
'operating_areas',
'closed_areas',
'total_rides',
'total_coasters',
'total_flat_rides',
'total_water_rides',
'total_daily_capacity',
'average_wait_time',
'average_rating',
'total_rides_operated',
'total_rides_retired',
'last_expansion_date',
'last_major_update',
'utilization_rate',
'peak_daily_attendance',
'guest_satisfaction',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'status' => ParkStatus::class,
'opening_date' => 'date',
'closing_date' => 'date',
'size_acres' => 'decimal:2',
'total_areas' => 'integer',
'operating_areas' => 'integer',
'closed_areas' => 'integer',
'total_rides' => 'integer',
'total_coasters' => 'integer',
'total_flat_rides' => 'integer',
'total_water_rides' => 'integer',
'total_daily_capacity' => 'integer',
'average_wait_time' => 'integer',
'average_rating' => 'decimal:2',
'total_rides_operated' => 'integer',
'total_rides_retired' => 'integer',
'last_expansion_date' => 'date',
'last_major_update' => 'date',
'utilization_rate' => 'decimal:2',
'peak_daily_attendance' => 'integer',
'guest_satisfaction' => 'decimal:2',
];
/**
* Get the operator that owns the park.
*/
public function operator(): BelongsTo
{
return $this->belongsTo(Operator::class);
}
/**
* Get the areas in the park.
*/
public function areas(): HasMany
{
return $this->hasMany(ParkArea::class);
}
/**
* Get formatted website URL (ensures proper URL format).
*/
public function getWebsiteUrlAttribute(): string
{
if (!$this->website) {
return '';
}
$website = $this->website;
if (!str_starts_with($website, 'http://') && !str_starts_with($website, 'https://')) {
$website = 'https://' . $website;
}
return $website;
}
/**
* Get the status display classes for Tailwind CSS.
*/
public function getStatusClassesAttribute(): string
{
return $this->status->getStatusClasses();
}
/**
* Get a formatted display of the park's size.
*/
public function getSizeDisplayAttribute(): string
{
return $this->size_acres ? number_format($this->size_acres, 1) . ' acres' : 'Unknown size';
}
/**
* Get the formatted opening year.
*/
public function getOpeningYearAttribute(): ?string
{
return $this->opening_date?->format('Y');
}
/**
* Get a brief description suitable for cards and previews.
*/
public function getBriefDescriptionAttribute(): string
{
$description = $this->description ?? '';
return strlen($description) > 200 ? substr($description, 0, 200) . '...' : $description;
}
/**
* Scope a query to only include operating parks.
*/
public function scopeOperating($query)
{
return $query->where('status', ParkStatus::OPERATING);
}
/**
* Scope a query to only include closed parks.
*/
public function scopeClosed($query)
{
return $query->whereIn('status', [
ParkStatus::CLOSED_TEMP,
ParkStatus::CLOSED_PERM,
ParkStatus::DEMOLISHED,
]);
}
/**
* Boot the model.
*/
protected static function boot()
{
parent::boot();
static::created(function (Park $park) {
$park->operator?->updateStatistics();
});
static::deleted(function (Park $park) {
$park->operator?->updateStatistics();
});
}
}

261
app/Models/ParkArea.php Normal file
View File

@@ -0,0 +1,261 @@
<?php
namespace App\Models;
use App\Traits\HasSlugHistory;
use App\Traits\HasAreaStatistics;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class ParkArea extends Model
{
use HasFactory, HasSlugHistory, HasAreaStatistics;
/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'name',
'slug',
'description',
'opening_date',
'closing_date',
'position',
'parent_id',
'ride_count',
'coaster_count',
'flat_ride_count',
'water_ride_count',
'daily_capacity',
'peak_wait_time',
'average_rating',
'total_rides_operated',
'retired_rides_count',
'last_new_ride_added',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'opening_date' => 'date',
'closing_date' => 'date',
'position' => 'integer',
'ride_count' => 'integer',
'coaster_count' => 'integer',
'flat_ride_count' => 'integer',
'water_ride_count' => 'integer',
'daily_capacity' => 'integer',
'peak_wait_time' => 'integer',
'average_rating' => 'decimal:2',
'total_rides_operated' => 'integer',
'retired_rides_count' => 'integer',
'last_new_ride_added' => 'date',
];
/**
* Get the park that owns the area.
*/
public function park(): BelongsTo
{
return $this->belongsTo(Park::class);
}
/**
* Get the parent area if this is a sub-area.
*/
public function parent(): BelongsTo
{
return $this->belongsTo(ParkArea::class, 'parent_id');
}
/**
* Get the sub-areas of this area.
*/
public function children(): HasMany
{
return $this->hasMany(ParkArea::class, 'parent_id')
->orderBy('position');
}
/**
* Get a brief description suitable for cards and previews.
*/
public function getBriefDescriptionAttribute(): string
{
$description = $this->description ?? '';
return strlen($description) > 150 ? substr($description, 0, 150) . '...' : $description;
}
/**
* Get the opening year of the area.
*/
public function getOpeningYearAttribute(): ?string
{
return $this->opening_date?->format('Y');
}
/**
* Check if the area is currently operating.
*/
public function isOperating(): bool
{
if ($this->closing_date) {
return false;
}
return true;
}
/**
* Check if this area has sub-areas.
*/
public function hasChildren(): bool
{
return $this->children()->exists();
}
/**
* Check if this is a top-level area.
*/
public function isTopLevel(): bool
{
return is_null($this->parent_id);
}
/**
* Get the next available position for a new area.
*/
public function getNextPosition(): int
{
$maxPosition = static::where('park_id', $this->park_id)
->where('parent_id', $this->parent_id)
->max('position');
return ($maxPosition ?? -1) + 1;
}
/**
* Move this area to a new position.
*/
public function moveToPosition(int $newPosition): void
{
if ($newPosition === $this->position) {
return;
}
$oldPosition = $this->position;
if ($newPosition > $oldPosition) {
// Moving down: decrement positions of items in between
static::where('park_id', $this->park_id)
->where('parent_id', $this->parent_id)
->whereBetween('position', [$oldPosition + 1, $newPosition])
->decrement('position');
} else {
// Moving up: increment positions of items in between
static::where('park_id', $this->park_id)
->where('parent_id', $this->parent_id)
->whereBetween('position', [$newPosition, $oldPosition - 1])
->increment('position');
}
$this->update(['position' => $newPosition]);
}
/**
* Scope query to only include operating areas.
*/
public function scopeOperating($query)
{
return $query->whereNull('closing_date');
}
/**
* Scope query to only include top-level areas.
*/
public function scopeTopLevel($query)
{
return $query->whereNull('parent_id');
}
/**
* Override parent method to ensure unique slugs within a park.
*/
protected function generateSlug(): string
{
$slug = \Str::slug($this->name);
$count = 2;
while (
static::where('park_id', $this->park_id)
->where('slug', $slug)
->where('id', '!=', $this->id)
->exists() ||
static::whereHas('slugHistories', function ($query) use ($slug) {
$query->where('slug', $slug);
})
->where('park_id', $this->park_id)
->where('id', '!=', $this->id)
->exists()
) {
$slug = \Str::slug($this->name) . '-' . $count++;
}
return $slug;
}
/**
* Get the route key for the model.
*/
public function getRouteKeyName(): string
{
return 'slug';
}
/**
* Find an area by its slug within a specific park.
*/
public static function findByParkAndSlug(Park $park, string $slug): ?self
{
// Try current slug
$area = static::where('park_id', $park->id)
->where('slug', $slug)
->first();
if ($area) {
return $area;
}
// Try historical slug
$slugHistory = SlugHistory::where('slug', $slug)
->where('sluggable_type', static::class)
->whereHas('sluggable', function ($query) use ($park) {
$query->where('park_id', $park->id);
})
->latest()
->first();
return $slugHistory ? static::find($slugHistory->sluggable_id) : null;
}
/**
* Boot the model.
*/
protected static function boot()
{
parent::boot();
static::creating(function (ParkArea $area) {
if (is_null($area->position)) {
$area->position = $area->getNextPosition();
}
});
}
}

131
app/Models/Profile.php Normal file
View File

@@ -0,0 +1,131 @@
<?php
namespace App\Models;
use App\Services\IdGenerator;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
class Profile extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'display_name',
'pronouns',
'bio',
'twitter',
'instagram',
'youtube',
'discord',
'coaster_credits',
'dark_ride_credits',
'flat_ride_credits',
'water_ride_credits',
];
/**
* Get the user that owns the profile
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* Get the avatar URL or generate a default one
*/
public function getAvatarUrl(): string
{
if ($this->avatar) {
return Storage::disk('public')->url($this->avatar);
}
// Get first letter of username for default avatar
$firstLetter = strtoupper(substr($this->user->name, 0, 1));
$avatarPath = "avatars/letters/{$firstLetter}_avatar.png";
// Check if letter avatar exists, if not use default
if (Storage::disk('public')->exists($avatarPath)) {
return Storage::disk('public')->url($avatarPath);
}
return asset('images/default-avatar.png');
}
/**
* Set the avatar image
*/
public function setAvatar($file): void
{
if ($this->avatar) {
Storage::disk('public')->delete($this->avatar);
}
$filename = 'avatars/' . uniqid() . '.' . $file->getClientOriginalExtension();
// Process and save the image
$image = Image::make($file)
->fit(200, 200)
->encode();
Storage::disk('public')->put($filename, $image);
$this->update(['avatar' => $filename]);
}
/**
* Generate a letter avatar
*/
protected function generateLetterAvatar(string $letter): void
{
$letter = strtoupper($letter);
$image = Image::canvas(200, 200, '#007bff');
$image->text($letter, 100, 100, function ($font) {
$font->file(public_path('fonts/Roboto-Bold.ttf'));
$font->size(120);
$font->color('#ffffff');
$font->align('center');
$font->valign('center');
});
$filename = "avatars/letters/{$letter}_avatar.png";
Storage::disk('public')->put($filename, $image->encode('png'));
}
/**
* Boot the model
*/
protected static function boot()
{
parent::boot();
static::creating(function (Profile $profile) {
if (!$profile->profile_id) {
$profile->profile_id = IdGenerator::generate(Profile::class, 'profile_id');
}
if (!$profile->display_name) {
$profile->display_name = $profile->user->name;
}
});
static::created(function (Profile $profile) {
// Generate letter avatar if it doesn't exist
$letter = strtoupper(substr($profile->user->name, 0, 1));
$avatarPath = "avatars/letters/{$letter}_avatar.png";
if (!Storage::disk('public')->exists($avatarPath)) {
$profile->generateLetterAvatar($letter);
}
});
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
class SlugHistory extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'slug',
];
/**
* Get the parent sluggable model.
*/
public function sluggable(): MorphTo
{
return $this->morphTo();
}
}

View File

@@ -2,31 +2,36 @@
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Enums\ThemePreference;
use App\Enums\UserRole;
use App\Services\IdGenerator;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var list<string>
* @var array<string>
*/
protected $fillable = [
'name',
'email',
'password',
'role',
'theme_preference',
'pending_email',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
* @var array<string>
*/
protected $hidden = [
'password',
@@ -43,6 +48,107 @@ class User extends Authenticatable
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'role' => UserRole::class,
'theme_preference' => ThemePreference::class,
'is_banned' => 'boolean',
'ban_date' => 'datetime',
];
}
/**
* Get the user's profile
*/
public function profile(): HasOne
{
return $this->hasOne(Profile::class);
}
/**
* Get the user's display name, falling back to username if not set
*/
public function getDisplayName(): string
{
return $this->profile?->display_name ?? $this->name;
}
/**
* Check if the user has moderation privileges
*/
public function canModerate(): bool
{
return $this->role->canModerate();
}
/**
* Check if the user has admin privileges
*/
public function canAdmin(): bool
{
return $this->role->canAdmin();
}
/**
* Ban the user with a reason
*/
public function ban(string $reason): void
{
$this->update([
'is_banned' => true,
'ban_reason' => $reason,
'ban_date' => now(),
]);
}
/**
* Unban the user
*/
public function unban(): void
{
$this->update([
'is_banned' => false,
'ban_reason' => null,
'ban_date' => null,
]);
}
/**
* Handle pending email changes
*/
public function setPendingEmail(string $email): void
{
$this->update(['pending_email' => $email]);
}
/**
* Confirm pending email change
*/
public function confirmEmailChange(): void
{
if ($this->pending_email) {
$this->update([
'email' => $this->pending_email,
'pending_email' => null,
]);
}
}
/**
* Boot the model
*/
protected static function boot()
{
parent::boot();
static::creating(function (User $user) {
if (!$user->user_id) {
$user->user_id = IdGenerator::generate(User::class, 'user_id');
}
if (!$user->role) {
$user->role = UserRole::USER;
}
if (!$user->theme_preference) {
$user->theme_preference = ThemePreference::LIGHT;
}
});
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Services;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class IdGenerator
{
/**
* Generate a random ID starting at 4 digits, expanding to 5 if needed
*
* @param string $model Model class name
* @param string $field Field to check for uniqueness
* @return string
*/
public static function generate(string $model, string $field): string
{
$attempts = 0;
$maxAttempts = 10;
while ($attempts < $maxAttempts) {
// Try 4 digits first
if ($attempts < 5) {
$id = (string) random_int(1000, 9999);
} else {
// Try 5 digits if all 4-digit numbers are taken
$id = (string) random_int(10000, 99999);
}
if (!$model::where($field, $id)->exists()) {
return $id;
}
$attempts++;
}
// If we get here, try a completely random string as fallback
return Str::random(10);
}
}

View File

@@ -0,0 +1,217 @@
<?php
namespace App\Services;
use App\Models\Park;
use App\Models\ParkArea;
use App\Models\Operator;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
class StatisticsCacheService
{
/**
* Cache TTL in seconds (24 hours).
*/
protected const CACHE_TTL = 86400;
/**
* Cache key prefixes.
*/
protected const AREA_PREFIX = 'stats:area:';
protected const PARK_PREFIX = 'stats:park:';
protected const OPERATOR_PREFIX = 'stats:operator:';
/**
* Cache area statistics.
*/
public function cacheAreaStatistics(ParkArea $area): void
{
try {
Cache::put(
$this->getAreaKey($area),
[
'ride_distribution' => $area->ride_distribution,
'daily_capacity' => $area->formatted_daily_capacity,
'rating' => $area->rating_display,
'wait_time' => $area->formatted_peak_wait_time,
'historical' => $area->historical_stats,
'updated_at' => now(),
],
static::CACHE_TTL
);
Log::info("Cached statistics for area {$area->id}");
} catch (\Exception $e) {
Log::error("Failed to cache area statistics: {$e->getMessage()}");
}
}
/**
* Cache park statistics.
*/
public function cacheParkStatistics(Park $park): void
{
try {
Cache::put(
$this->getParkKey($park),
[
'area_distribution' => $park->area_distribution,
'ride_distribution' => $park->ride_distribution,
'daily_capacity' => $park->formatted_daily_capacity,
'rating' => $park->rating_display,
'wait_time' => $park->formatted_wait_time,
'historical' => $park->historical_stats,
'performance' => $park->performance_metrics,
'updated_at' => now(),
],
static::CACHE_TTL
);
Log::info("Cached statistics for park {$park->id}");
} catch (\Exception $e) {
Log::error("Failed to cache park statistics: {$e->getMessage()}");
}
}
/**
* Cache operator statistics.
*/
public function cacheOperatorStatistics(Operator $operator): void
{
try {
Cache::put(
$this->getOperatorKey($operator),
[
'park_count' => $operator->total_parks,
'operating_parks' => $operator->operating_parks,
'closed_parks' => $operator->closed_parks,
'total_rides' => $operator->total_rides,
'total_coasters' => $operator->total_coasters,
'average_rating' => $operator->average_rating,
'total_capacity' => $operator->total_daily_capacity,
'updated_at' => now(),
],
static::CACHE_TTL
);
Log::info("Cached statistics for operator {$operator->id}");
} catch (\Exception $e) {
Log::error("Failed to cache operator statistics: {$e->getMessage()}");
}
}
/**
* Get cached area statistics.
*
* @return array<string, mixed>|null
*/
public function getAreaStatistics(ParkArea $area): ?array
{
return Cache::get($this->getAreaKey($area));
}
/**
* Get cached park statistics.
*
* @return array<string, mixed>|null
*/
public function getParkStatistics(Park $park): ?array
{
return Cache::get($this->getParkKey($park));
}
/**
* Get cached operator statistics.
*
* @return array<string, mixed>|null
*/
public function getOperatorStatistics(Operator $operator): ?array
{
return Cache::get($this->getOperatorKey($operator));
}
/**
* Invalidate area statistics cache.
*/
public function invalidateAreaCache(ParkArea $area): void
{
Cache::forget($this->getAreaKey($area));
Log::info("Invalidated cache for area {$area->id}");
}
/**
* Invalidate park statistics cache.
*/
public function invalidateParkCache(Park $park): void
{
Cache::forget($this->getParkKey($park));
Log::info("Invalidated cache for park {$park->id}");
}
/**
* Invalidate operator statistics cache.
*/
public function invalidateOperatorCache(Operator $operator): void
{
Cache::forget($this->getOperatorKey($operator));
Log::info("Invalidated cache for operator {$operator->id}");
}
/**
* Warm up caches for all entities.
*/
public function warmCaches(): void
{
try {
// Cache area statistics
ParkArea::chunk(100, function ($areas) {
foreach ($areas as $area) {
$this->cacheAreaStatistics($area);
}
});
// Cache park statistics
Park::chunk(100, function ($parks) {
foreach ($parks as $park) {
$this->cacheParkStatistics($park);
}
});
// Cache operator statistics
Operator::chunk(100, function ($operators) {
foreach ($operators as $operator) {
$this->cacheOperatorStatistics($operator);
}
});
Log::info('Successfully warmed up all statistics caches');
} catch (\Exception $e) {
Log::error("Failed to warm up caches: {$e->getMessage()}");
}
}
/**
* Get cache key for area.
*/
protected function getAreaKey(ParkArea $area): string
{
return static::AREA_PREFIX . $area->id;
}
/**
* Get cache key for park.
*/
protected function getParkKey(Park $park): string
{
return static::PARK_PREFIX . $park->id;
}
/**
* Get cache key for operator.
*/
protected function getOperatorKey(Operator $operator): string
{
return static::OPERATOR_PREFIX . $operator->id;
}
}

View File

@@ -0,0 +1,162 @@
<?php
namespace App\Services;
use App\Models\Park;
use App\Models\ParkArea;
use App\Models\Operator;
use Illuminate\Support\Facades\DB;
class StatisticsRollupService
{
/**
* Update statistics for a specific area.
*/
public function updateAreaStatistics(ParkArea $area): void
{
DB::transaction(function () use ($area) {
// Update area statistics (will be implemented with Rides system)
// For now, we'll just ensure the area triggers park updates
$this->updateParkStatistics($area->park);
});
}
/**
* Update statistics for a specific park.
*/
public function updateParkStatistics(Park $park): void
{
DB::transaction(function () use ($park) {
// Update area counts
$park->updateAreaCounts();
// Update ride statistics
$park->updateRideStatistics();
// Update visitor statistics
$park->updateVisitorStats();
// Update operator statistics
if ($park->operator) {
$this->updateOperatorStatistics($park->operator);
}
});
}
/**
* Update statistics for a specific operator.
*/
public function updateOperatorStatistics(Operator $operator): void
{
DB::transaction(function () use ($operator) {
$parks = $operator->parks;
// Update park counts
$operator->update([
'total_parks' => $parks->count(),
'operating_parks' => $parks->operating()->count(),
'closed_parks' => $parks->closed()->count(),
]);
// Update ride totals
$operator->update([
'total_rides' => $parks->sum('total_rides'),
'total_coasters' => $parks->sum('total_coasters'),
'total_flat_rides' => $parks->sum('total_flat_rides'),
'total_water_rides' => $parks->sum('total_water_rides'),
]);
// Update performance metrics
$ratedParks = $parks->whereNotNull('average_rating');
if ($ratedParks->count() > 0) {
$operator->update([
'average_rating' => $ratedParks->avg('average_rating'),
'total_daily_capacity' => $parks->sum('total_daily_capacity'),
'average_utilization' => $parks->avg('utilization_rate'),
]);
}
});
}
/**
* Update all statistics in the system.
*/
public function refreshAllStatistics(): void
{
DB::transaction(function () {
// Update all areas first
ParkArea::chunk(100, function ($areas) {
foreach ($areas as $area) {
// Area statistics will be implemented with Rides system
}
});
// Update all parks
Park::chunk(100, function ($parks) {
foreach ($parks as $park) {
$this->updateParkStatistics($park);
}
});
// Update all operators
Operator::chunk(100, function ($operators) {
foreach ($operators as $operator) {
$this->updateOperatorStatistics($operator);
}
});
});
}
/**
* Schedule regular statistics updates.
*/
public function scheduleUpdates(): void
{
// This method will be called by the scheduler
$this->refreshAllStatistics();
}
/**
* Handle ride addition event.
*/
public function handleRideAdded(ParkArea $area): void
{
DB::transaction(function () use ($area) {
$area->recordNewRide();
$this->updateAreaStatistics($area);
});
}
/**
* Handle ride retirement event.
*/
public function handleRideRetired(ParkArea $area): void
{
DB::transaction(function () use ($area) {
$area->recordRetirement();
$this->updateAreaStatistics($area);
});
}
/**
* Handle park expansion event.
*/
public function handleParkExpansion(Park $park): void
{
DB::transaction(function () use ($park) {
$park->recordExpansion();
$this->updateParkStatistics($park);
});
}
/**
* Handle major update event.
*/
public function handleMajorUpdate(Park $park): void
{
DB::transaction(function () use ($park) {
$park->recordMajorUpdate();
$this->updateParkStatistics($park);
});
}
}

View File

@@ -0,0 +1,169 @@
<?php
namespace App\Traits;
trait HasAreaStatistics
{
/**
* Get the total ride count including all types.
*/
public function getTotalRideCountAttribute(): int
{
return $this->ride_count ?? 0;
}
/**
* Get the percentage of coasters among all rides.
*/
public function getCoasterPercentageAttribute(): float
{
if ($this->ride_count === 0) {
return 0;
}
return round(($this->coaster_count / $this->ride_count) * 100, 1);
}
/**
* Get a summary of ride types distribution.
*
* @return array<string, int>
*/
public function getRideDistributionAttribute(): array
{
return [
'coasters' => $this->coaster_count ?? 0,
'flat_rides' => $this->flat_ride_count ?? 0,
'water_rides' => $this->water_ride_count ?? 0,
];
}
/**
* Get the formatted daily capacity.
*/
public function getFormattedDailyCapacityAttribute(): string
{
if (!$this->daily_capacity) {
return 'Unknown capacity';
}
return number_format($this->daily_capacity) . ' riders/day';
}
/**
* Get the formatted peak wait time.
*/
public function getFormattedPeakWaitTimeAttribute(): string
{
if (!$this->peak_wait_time) {
return 'Unknown wait time';
}
return $this->peak_wait_time . ' minutes';
}
/**
* Get the rating display with stars.
*/
public function getRatingDisplayAttribute(): string
{
if (!$this->average_rating) {
return 'Not rated';
}
$stars = str_repeat('★', floor($this->average_rating));
$stars .= str_repeat('☆', 5 - floor($this->average_rating));
return $stars . ' (' . number_format($this->average_rating, 1) . ')';
}
/**
* Get historical statistics summary.
*
* @return array<string, mixed>
*/
public function getHistoricalStatsAttribute(): array
{
return [
'total_operated' => $this->total_rides_operated,
'retired_count' => $this->retired_rides_count,
'last_addition' => $this->last_new_ride_added?->format('M Y') ?? 'Never',
'retirement_rate' => $this->getRetirementRate(),
];
}
/**
* Calculate the retirement rate (retired rides as percentage of total operated).
*/
protected function getRetirementRate(): float
{
if ($this->total_rides_operated === 0) {
return 0;
}
return round(($this->retired_rides_count / $this->total_rides_operated) * 100, 1);
}
/**
* Update ride counts.
*
* @param array<string, int> $counts
*/
public function updateRideCounts(array $counts): void
{
$this->update([
'ride_count' => $counts['total'] ?? 0,
'coaster_count' => $counts['coasters'] ?? 0,
'flat_ride_count' => $counts['flat_rides'] ?? 0,
'water_ride_count' => $counts['water_rides'] ?? 0,
]);
}
/**
* Update visitor statistics.
*/
public function updateVisitorStats(int $dailyCapacity, int $peakWaitTime, float $rating): void
{
$this->update([
'daily_capacity' => $dailyCapacity,
'peak_wait_time' => $peakWaitTime,
'average_rating' => round($rating, 2),
]);
}
/**
* Record a new ride addition.
*/
public function recordNewRide(): void
{
$this->increment('total_rides_operated');
$this->update(['last_new_ride_added' => now()]);
}
/**
* Record a ride retirement.
*/
public function recordRetirement(): void
{
$this->increment('retired_rides_count');
}
/**
* Reset all statistics to zero.
*/
public function resetStatistics(): void
{
$this->update([
'ride_count' => 0,
'coaster_count' => 0,
'flat_ride_count' => 0,
'water_ride_count' => 0,
'daily_capacity' => null,
'peak_wait_time' => null,
'average_rating' => null,
'total_rides_operated' => 0,
'retired_rides_count' => 0,
'last_new_ride_added' => null,
]);
}
}

View File

@@ -0,0 +1,202 @@
<?php
namespace App\Traits;
trait HasParkStatistics
{
/**
* Get the total ride count including all types.
*/
public function getTotalRideCountAttribute(): int
{
return $this->total_rides ?? 0;
}
/**
* Get the percentage of coasters among all rides.
*/
public function getCoasterPercentageAttribute(): float
{
if ($this->total_rides === 0) {
return 0;
}
return round(($this->total_coasters / $this->total_rides) * 100, 1);
}
/**
* Get a summary of ride types distribution.
*
* @return array<string, int>
*/
public function getRideDistributionAttribute(): array
{
return [
'coasters' => $this->total_coasters ?? 0,
'flat_rides' => $this->total_flat_rides ?? 0,
'water_rides' => $this->total_water_rides ?? 0,
];
}
/**
* Get a summary of area statistics.
*
* @return array<string, int>
*/
public function getAreaDistributionAttribute(): array
{
return [
'total' => $this->total_areas ?? 0,
'operating' => $this->operating_areas ?? 0,
'closed' => $this->closed_areas ?? 0,
];
}
/**
* Get the formatted daily capacity.
*/
public function getFormattedDailyCapacityAttribute(): string
{
if (!$this->total_daily_capacity) {
return 'Unknown capacity';
}
return number_format($this->total_daily_capacity) . ' riders/day';
}
/**
* Get the formatted average wait time.
*/
public function getFormattedWaitTimeAttribute(): string
{
if (!$this->average_wait_time) {
return 'Unknown wait time';
}
return $this->average_wait_time . ' minutes';
}
/**
* Get the rating display with stars.
*/
public function getRatingDisplayAttribute(): string
{
if (!$this->average_rating) {
return 'Not rated';
}
$stars = str_repeat('★', floor($this->average_rating));
$stars .= str_repeat('☆', 5 - floor($this->average_rating));
return $stars . ' (' . number_format($this->average_rating, 1) . ')';
}
/**
* Get historical statistics summary.
*
* @return array<string, mixed>
*/
public function getHistoricalStatsAttribute(): array
{
return [
'total_operated' => $this->total_rides_operated,
'total_retired' => $this->total_rides_retired,
'last_expansion' => $this->last_expansion_date?->format('M Y') ?? 'Never',
'last_update' => $this->last_major_update?->format('M Y') ?? 'Never',
'retirement_rate' => $this->getRetirementRate(),
];
}
/**
* Get performance metrics summary.
*
* @return array<string, mixed>
*/
public function getPerformanceMetricsAttribute(): array
{
return [
'utilization' => $this->utilization_rate ? $this->utilization_rate . '%' : 'Unknown',
'peak_attendance' => $this->peak_daily_attendance ? number_format($this->peak_daily_attendance) : 'Unknown',
'satisfaction' => $this->guest_satisfaction ? number_format($this->guest_satisfaction, 1) . '/5.0' : 'Unknown',
];
}
/**
* Calculate the retirement rate (retired rides as percentage of total operated).
*/
protected function getRetirementRate(): float
{
if ($this->total_rides_operated === 0) {
return 0;
}
return round(($this->total_rides_retired / $this->total_rides_operated) * 100, 1);
}
/**
* Update area counts.
*/
public function updateAreaCounts(): void
{
$this->update([
'total_areas' => $this->areas()->count(),
'operating_areas' => $this->areas()->operating()->count(),
'closed_areas' => $this->areas()->whereNotNull('closing_date')->count(),
]);
}
/**
* Update ride statistics.
*/
public function updateRideStatistics(): void
{
$areas = $this->areas;
$this->update([
'total_rides' => $areas->sum('ride_count'),
'total_coasters' => $areas->sum('coaster_count'),
'total_flat_rides' => $areas->sum('flat_ride_count'),
'total_water_rides' => $areas->sum('water_ride_count'),
'total_daily_capacity' => $areas->sum('daily_capacity'),
]);
}
/**
* Update visitor statistics.
*/
public function updateVisitorStats(): void
{
$areas = $this->areas()->whereNotNull('average_rating');
$this->update([
'average_wait_time' => $areas->avg('peak_wait_time'),
'average_rating' => $areas->avg('average_rating'),
]);
}
/**
* Record an expansion.
*/
public function recordExpansion(): void
{
$this->update(['last_expansion_date' => now()]);
}
/**
* Record a major update.
*/
public function recordMajorUpdate(): void
{
$this->update(['last_major_update' => now()]);
}
/**
* Update all statistics.
*/
public function refreshStatistics(): void
{
$this->updateAreaCounts();
$this->updateRideStatistics();
$this->updateVisitorStats();
}
}

View File

@@ -0,0 +1,106 @@
<?php
namespace App\Traits;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Str;
use App\Models\SlugHistory;
trait HasSlugHistory
{
/**
* Boot the trait.
*/
protected static function bootHasSlugHistory(): void
{
static::saving(function ($model) {
if (!$model->slug) {
$model->slug = $model->generateSlug();
}
});
static::updating(function ($model) {
if ($model->isDirty('slug') && $model->getOriginal('slug')) {
static::addToSlugHistory($model->getOriginal('slug'));
}
});
}
/**
* Get all slug histories for this model.
*/
public function slugHistories(): MorphMany
{
return $this->morphMany(SlugHistory::class, 'sluggable');
}
/**
* Add a slug to the history.
*/
protected function addToSlugHistory(string $slug): void
{
$this->slugHistories()->create(['slug' => $slug]);
}
/**
* Generate a unique slug.
*/
protected function generateSlug(): string
{
$slug = Str::slug($this->name);
$count = 2;
while (
static::where('slug', $slug)
->where('id', '!=', $this->id)
->exists() ||
SlugHistory::where('slug', $slug)
->where('sluggable_type', get_class($this))
->where('sluggable_id', '!=', $this->id)
->exists()
) {
$slug = Str::slug($this->name) . '-' . $count++;
}
return $slug;
}
/**
* Find a model by its current or historical slug.
*
* @param string $slug
* @return static|null
*/
public static function findBySlug(string $slug)
{
// Try current slug
$model = static::where('slug', $slug)->first();
if ($model) {
return $model;
}
// Try historical slug
$slugHistory = SlugHistory::where('slug', $slug)
->where('sluggable_type', static::class)
->latest()
->first();
if ($slugHistory) {
return static::find($slugHistory->sluggable_id);
}
return null;
}
/**
* Find a model by its current or historical slug or fail.
*
* @param string $slug
* @return static
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public static function findBySlugOrFail(string $slug)
{
return static::findBySlug($slug) ?? throw new \Illuminate\Database\Eloquent\ModelNotFoundException;
}
}