Saisonfotos am Foto statt am Erscheinungsbild
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>
This commit is contained in:
1 parent
d02f3e05c7
commit
791786ed3e
21 files changed
+449
-235
No files matched your search
@@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
use App\Tenancy\TenantContext;
|
||||
use Carbon\CarbonInterface;
|
||||
use Database\Factories\AppearanceFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
@@ -11,12 +12,12 @@
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* A seasonal look of a site (e.g. summer, winter, Christmas, New Year's Eve): colour tokens, hero photo,
|
||||
* photos per section, texts and banner change – the page structure does not (sections may be limited to
|
||||
* A seasonal look of a site (e.g. summer, winter, Christmas, New Year's Eve): colour tokens, texts and banner
|
||||
* change, photos made for it (Photo::appearance_ids) are shown – the page structure does not (sections may be limited to
|
||||
* appearances, see SectionRegistry). Offers are advertised by their own rules (OfferShowcase). The period recurs every year ("MM-DD" to "MM-DD",
|
||||
* inclusive; a period may run over the turn of the year). The default appearance applies when none matches.
|
||||
*/
|
||||
#[Fillable(['site_id', 'name', 'starts_md', 'ends_md', 'priority', 'is_default', 'is_active', 'tokens', 'overrides', 'hero_photo_id', 'section_photos'])]
|
||||
#[Fillable(['site_id', 'name', 'starts_md', 'ends_md', 'priority', 'is_default', 'is_active', 'tokens', 'overrides'])]
|
||||
class Appearance extends Model
|
||||
{
|
||||
/** @use HasFactory<AppearanceFactory> */
|
||||
@@ -48,7 +49,6 @@ protected function casts(): array
|
||||
'is_active' => 'boolean',
|
||||
'tokens' => 'array',
|
||||
'overrides' => 'array',
|
||||
'section_photos' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -61,11 +61,47 @@ public function site(): BelongsTo
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Photo, $this>
|
||||
* Names by id of the landlord's appearances, loaded once per request and landlord.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function heroPhoto(): BelongsTo
|
||||
public static function names(): array
|
||||
{
|
||||
return $this->belongsTo(Photo::class, 'hero_photo_id');
|
||||
// once() keys its cache by the closure's bound variables, so each landlord gets its own entry.
|
||||
$tenantId = app(TenantContext::class)->id();
|
||||
|
||||
return once(function (): array {
|
||||
return self::query()->pluck('name', 'id')->all();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Choices for "Zeigen in" at the photos, e.g. "Winter · 01.11.–31.03."; with several websites prefixed by the website.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public static function photoOptions(): array
|
||||
{
|
||||
$appearances = self::query()->with('site')->orderByDesc('is_default')->orderBy('starts_md')->orderBy('name')->get();
|
||||
$severalSites = $appearances->pluck('site_id')->unique()->count() > 1;
|
||||
|
||||
return $appearances->mapWithKeys(fn (Appearance $appearance): array => [
|
||||
$appearance->id => ($severalSites ? $appearance->site?->name.': ' : '').$appearance->periodLabel(),
|
||||
])->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* "Winter · 01.11.–31.03.", "Standard · wenn kein Zeitraum passt".
|
||||
*/
|
||||
public function periodLabel(): string
|
||||
{
|
||||
if ($this->starts_md === null || $this->ends_md === null) {
|
||||
return $this->name.($this->is_default ? ' · wenn kein Zeitraum passt' : '');
|
||||
}
|
||||
|
||||
$day = fn (string $monthDay): string => substr($monthDay, 3, 2).'.'.substr($monthDay, 0, 2).'.';
|
||||
|
||||
return $this->name.' · '.$day($this->starts_md).'–'.$day($this->ends_md);
|
||||
}
|
||||
|
||||
public function covers(CarbonInterface $date): bool
|
||||
|
||||
+57
-1
@@ -8,6 +8,7 @@
|
||||
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;
|
||||
@@ -18,8 +19,10 @@
|
||||
|
||||
/**
|
||||
* 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', 'alt_text', 'caption', 'is_published', 'sort_order'])]
|
||||
#[Fillable(['property_id', 'category', 'appearance_ids', 'alt_text', 'caption', 'is_published', 'sort_order'])]
|
||||
class Photo extends Model implements HasMedia
|
||||
{
|
||||
/** @use HasFactory<PhotoFactory> */
|
||||
@@ -39,6 +42,7 @@ protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'category' => PhotoCategory::class,
|
||||
'appearance_ids' => 'array',
|
||||
'is_published' => 'boolean',
|
||||
];
|
||||
}
|
||||
@@ -107,6 +111,58 @@ protected function forProperty(Builder $query, ?Property $property): Builder
|
||||
: $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);
|
||||
|
||||
Reference in new issue
Block a user