Ein Foto gehoert zu keinem (ganzjaehrig) oder mehreren Erscheinungsbildern und erscheint nur dort, vor den ganzjaehrigen Fotos. Bausteine waehlen "automatisch" oder eine eigene Auswahl; Titelbereich und Abschluss zeigen das erste passende Foto ihrer Liste. Titelbild und "Bilder je Baustein" des Erscheinungsbilds werden in die Bausteine uebernommen und entfallen dort. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
181 lines
6.0 KiB
PHP
181 lines
6.0 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use App\Enums\PhotoCategory;
|
||
use App\Models\Concerns\BelongsToTenant;
|
||
use Database\Factories\PhotoFactory;
|
||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||
use Illuminate\Database\Eloquent\Attributes\Scope;
|
||
use Illuminate\Database\Eloquent\Builder;
|
||
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
use Spatie\Image\Enums\Fit;
|
||
use Spatie\MediaLibrary\HasMedia;
|
||
use Spatie\MediaLibrary\InteractsWithMedia;
|
||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||
|
||
/**
|
||
* A website photo. The uploaded original is kept; web sizes are generated as WebP on upload.
|
||
* A photo may belong to appearances (seasons such as summer, winter, Christmas): then it is only shown while
|
||
* one of them is in effect, and ahead of the all-year photos. Without appearances it is shown all year.
|
||
*/
|
||
#[Fillable(['property_id', 'category', 'appearance_ids', 'alt_text', 'caption', 'is_published', 'sort_order'])]
|
||
class Photo extends Model implements HasMedia
|
||
{
|
||
/** @use HasFactory<PhotoFactory> */
|
||
use BelongsToTenant, HasFactory, InteractsWithMedia;
|
||
|
||
public const COLLECTION = 'image';
|
||
|
||
/** Generated widths in px, used for srcset. */
|
||
public const WIDTHS = ['sm' => 640, 'md' => 1280, 'lg' => 2048];
|
||
|
||
/**
|
||
* Get the attributes that should be cast.
|
||
*
|
||
* @return array<string, string>
|
||
*/
|
||
protected function casts(): array
|
||
{
|
||
return [
|
||
'category' => PhotoCategory::class,
|
||
'appearance_ids' => 'array',
|
||
'is_published' => 'boolean',
|
||
];
|
||
}
|
||
|
||
public function registerMediaCollections(): void
|
||
{
|
||
// Website photos are public files; the app's default disk (FILESYSTEM_DISK=local) is private.
|
||
$this->addMediaCollection(self::COLLECTION)
|
||
->useDisk('public')
|
||
->singleFile()
|
||
->acceptsMimeTypes(['image/jpeg', 'image/png', 'image/webp']);
|
||
}
|
||
|
||
public function registerMediaConversions(?Media $media = null): void
|
||
{
|
||
foreach (self::WIDTHS as $name => $width) {
|
||
$this->addMediaConversion($name)
|
||
->fit(Fit::Max, $width, $width)
|
||
->format('webp')
|
||
->quality(80)
|
||
->performOnCollections(self::COLLECTION);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @return BelongsTo<Property, $this>
|
||
*/
|
||
public function property(): BelongsTo
|
||
{
|
||
return $this->belongsTo(Property::class);
|
||
}
|
||
|
||
/**
|
||
* @param Builder<Photo> $query
|
||
* @return Builder<Photo>
|
||
*/
|
||
#[Scope]
|
||
protected function published(Builder $query): Builder
|
||
{
|
||
return $query->where('is_published', true)->orderBy('sort_order');
|
||
}
|
||
|
||
/**
|
||
* Photos a gallery or an automatic choice may show – floor plans and drawings only appear where chosen.
|
||
*
|
||
* @param Builder<Photo> $query
|
||
* @return Builder<Photo>
|
||
*/
|
||
#[Scope]
|
||
protected function forGalleries(Builder $query): Builder
|
||
{
|
||
return $query->whereIn('category', PhotoCategory::forGalleries());
|
||
}
|
||
|
||
/**
|
||
* Photos of the apartment and those without an apartment (e.g. the area); without an apartment all photos.
|
||
*
|
||
* @param Builder<Photo> $query
|
||
* @return Builder<Photo>
|
||
*/
|
||
#[Scope]
|
||
protected function forProperty(Builder $query, ?Property $property): Builder
|
||
{
|
||
return $property === null
|
||
? $query
|
||
: $query->where(fn (Builder $query) => $query->where('property_id', $property->id)->orWhereNull('property_id'));
|
||
}
|
||
|
||
/**
|
||
* Photos picked automatically: published, of the apartment and those without one, never plans, fitting the
|
||
* season – photos made for the appearance in effect first, then the all-year photos in their order.
|
||
*
|
||
* @return EloquentCollection<int, Photo>
|
||
*/
|
||
public static function automatic(?Property $property, ?Appearance $appearance, ?PhotoCategory $category = null): EloquentCollection
|
||
{
|
||
return self::query()->published()->forGalleries()->forProperty($property)->with('media')
|
||
->when($category, fn (Builder $query) => $query->where('category', $category))
|
||
->get()
|
||
->filter(fn (Photo $photo): bool => $photo->fitsSeason($appearance))
|
||
->sortBy(fn (Photo $photo): int => $photo->isMadeFor($appearance) ? 0 : 1)
|
||
->values();
|
||
}
|
||
|
||
/**
|
||
* Appearance ids as integers (the form may deliver strings).
|
||
*
|
||
* @return list<int>
|
||
*/
|
||
public function appearanceIds(): array
|
||
{
|
||
return array_values(array_unique(array_map('intval', array_filter((array) $this->appearance_ids))));
|
||
}
|
||
|
||
/**
|
||
* Whether the photo may be shown while this appearance is in effect: all-year photos always, seasonal ones in their seasons.
|
||
*/
|
||
public function fitsSeason(?Appearance $appearance): bool
|
||
{
|
||
return $this->appearanceIds() === [] || $this->isMadeFor($appearance);
|
||
}
|
||
|
||
/**
|
||
* Whether the photo was chosen for this appearance (seasonal photos come first while it is in effect).
|
||
*/
|
||
public function isMadeFor(?Appearance $appearance): bool
|
||
{
|
||
return $appearance !== null && in_array($appearance->id, $this->appearanceIds(), true);
|
||
}
|
||
|
||
/**
|
||
* Names of the appearances the photo is made for (empty = all year).
|
||
*
|
||
* @return list<string>
|
||
*/
|
||
public function appearanceNames(): array
|
||
{
|
||
return array_values(array_intersect_key(Appearance::names(), array_flip($this->appearanceIds())));
|
||
}
|
||
|
||
public function url(string $size = 'md'): string
|
||
{
|
||
return $this->getFirstMediaUrl(self::COLLECTION, $size);
|
||
}
|
||
|
||
/**
|
||
* srcset for <img>, e.g. "…-sm.webp 640w, …-md.webp 1280w, …-lg.webp 2048w".
|
||
*/
|
||
public function srcset(): string
|
||
{
|
||
return collect(self::WIDTHS)
|
||
->map(fn (int $width, string $name): string => $this->url($name).' '.$width.'w')
|
||
->implode(', ');
|
||
}
|
||
}
|