- Webseite > Videos (Datei, Standbild, Kapitel mit Raum im Grundriss); Upload bis 100 MB - Titelbereich: Video statt Foto (stumme Schleife, respektiert Bewegung reduzieren/Save-Data) und naechste freie Termine mit Gesamtpreis (NextFreeStays) - Neuer Baustein Rundgang (Video) mit waehlbarer Anordnung; Grundriss springt ins Video - Buchungsseite: Preis unter dem Kalender, Formular rechts, Ablauf als Grafik - Seebad: Fahrkarten, Fahrplan-Kapitel, Sonnenaufgang als einmaliges Ereignis Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
147 lines
4.4 KiB
PHP
147 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\BelongsToTenant;
|
|
use Database\Factories\VideoFactory;
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
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 video: a short silent loop for the hero section or a tour with chapters (TourSection).
|
|
* Chapters link a moment to a floor plan room by its name, so the floor plan can jump into the video.
|
|
*
|
|
* @property list<array{at?: string, label?: string, room?: string|null}>|null $chapters
|
|
*/
|
|
#[Fillable(['property_id', 'title', 'chapters'])]
|
|
class Video extends Model implements HasMedia
|
|
{
|
|
/** @use HasFactory<VideoFactory> */
|
|
use BelongsToTenant, HasFactory, InteractsWithMedia;
|
|
|
|
public const COLLECTION = 'video';
|
|
|
|
public const POSTER = 'poster';
|
|
|
|
/** Largest accepted video file in KB (Strato allows uploads up to 128 MB). */
|
|
public const MAX_KB = 100 * 1024;
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'chapters' => 'array',
|
|
];
|
|
}
|
|
|
|
public function registerMediaCollections(): void
|
|
{
|
|
$this->addMediaCollection(self::COLLECTION)
|
|
->useDisk('public')
|
|
->singleFile()
|
|
->acceptsMimeTypes(['video/mp4', 'application/mp4', 'video/webm']);
|
|
|
|
$this->addMediaCollection(self::POSTER)
|
|
->useDisk('public')
|
|
->singleFile()
|
|
->acceptsMimeTypes(['image/jpeg', 'image/png', 'image/webp']);
|
|
}
|
|
|
|
public function registerMediaConversions(?Media $media = null): void
|
|
{
|
|
$this->addMediaConversion('poster')
|
|
->fit(Fit::Max, 1920, 1920)
|
|
->format('webp')
|
|
->quality(80)
|
|
->performOnCollections(self::POSTER);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<Property, $this>
|
|
*/
|
|
public function property(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Property::class);
|
|
}
|
|
|
|
public function url(): ?string
|
|
{
|
|
return $this->getFirstMediaUrl(self::COLLECTION) ?: null;
|
|
}
|
|
|
|
public function mimeType(): string
|
|
{
|
|
return $this->getFirstMedia(self::COLLECTION)?->mime_type ?? 'video/mp4';
|
|
}
|
|
|
|
public function posterUrl(): ?string
|
|
{
|
|
return $this->getFirstMediaUrl(self::POSTER, 'poster') ?: null;
|
|
}
|
|
|
|
/**
|
|
* Chapters in time order with the start in seconds; entries without a valid time or label are left out.
|
|
*
|
|
* @return list<array{seconds: int, time: string, label: string, room: string|null}>
|
|
*/
|
|
public function chapterList(): array
|
|
{
|
|
$chapters = [];
|
|
foreach ($this->chapters ?? [] as $chapter) {
|
|
$seconds = self::seconds($chapter['at'] ?? null);
|
|
if ($seconds === null || blank($chapter['label'] ?? null)) {
|
|
continue;
|
|
}
|
|
$chapters[] = [
|
|
'seconds' => $seconds,
|
|
'time' => sprintf('%d:%02d', intdiv($seconds, 60), $seconds % 60),
|
|
'label' => (string) $chapter['label'],
|
|
'room' => filled($chapter['room'] ?? null) ? (string) $chapter['room'] : null,
|
|
];
|
|
}
|
|
|
|
usort($chapters, fn (array $a, array $b): int => $a['seconds'] <=> $b['seconds']);
|
|
|
|
return $chapters;
|
|
}
|
|
|
|
/**
|
|
* The chapter showing a floor plan room (matched by name), if any.
|
|
*
|
|
* @return array{seconds: int, time: string, label: string, room: string|null}|null
|
|
*/
|
|
public function chapterForRoom(string $room): ?array
|
|
{
|
|
foreach ($this->chapterList() as $chapter) {
|
|
if ($chapter['room'] !== null && mb_strtolower($chapter['room']) === mb_strtolower($room)) {
|
|
return $chapter;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* "1:05" or "65" → 65; anything else → null.
|
|
*/
|
|
public static function seconds(mixed $value): ?int
|
|
{
|
|
$value = trim((string) $value);
|
|
if (preg_match('/^(\d{1,3}):([0-5]\d)$/', $value, $match)) {
|
|
return (int) $match[1] * 60 + (int) $match[2];
|
|
}
|
|
|
|
return preg_match('/^\d{1,4}$/', $value) ? (int) $value : null;
|
|
}
|
|
}
|