89 lines
2.1 KiB
PHP
89 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class DisplayPlaylist extends Model
|
|
{
|
|
/** @use HasFactory<\Database\Factories\DisplayPlaylistFactory> */
|
|
use HasFactory;
|
|
|
|
public const STATUS_PUBLISHED = 'published';
|
|
|
|
public const STATUS_DRAFT = 'draft';
|
|
|
|
protected $fillable = [
|
|
'display_id',
|
|
'status',
|
|
'published_at',
|
|
'published_by',
|
|
'notes',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'published_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function display(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Display::class);
|
|
}
|
|
|
|
public function publisher(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'published_by');
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<DisplayPlaylistItem, $this>
|
|
*/
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(DisplayPlaylistItem::class)->orderBy('sort_order');
|
|
}
|
|
|
|
/**
|
|
* Die der Bespielung zugeordneten Module in korrekter Reihenfolge.
|
|
*/
|
|
public function modules(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(
|
|
DisplayVersion::class,
|
|
'display_playlist_items',
|
|
'display_playlist_id',
|
|
'display_version_id'
|
|
)
|
|
->withPivot(['sort_order', 'id'])
|
|
->withTimestamps()
|
|
->orderByPivot('sort_order');
|
|
}
|
|
|
|
public function isPublished(): bool
|
|
{
|
|
return $this->status === self::STATUS_PUBLISHED;
|
|
}
|
|
|
|
public function isDraft(): bool
|
|
{
|
|
return $this->status === self::STATUS_DRAFT;
|
|
}
|
|
|
|
public function scopePublished(Builder $query): Builder
|
|
{
|
|
return $query->where('status', self::STATUS_PUBLISHED);
|
|
}
|
|
|
|
public function scopeDraft(Builder $query): Builder
|
|
{
|
|
return $query->where('status', self::STATUS_DRAFT);
|
|
}
|
|
}
|