147 lines
3.7 KiB
PHP
147 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\AuditionFlags;
|
|
use Carbon\Carbon;
|
|
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;
|
|
|
|
use function in_array;
|
|
|
|
class Audition extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $guarded = [];
|
|
|
|
public function event(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Event::class);
|
|
}
|
|
|
|
public function entries(): HasMany
|
|
{
|
|
return $this->hasMany(Entry::class);
|
|
}
|
|
|
|
public function unscoredEntries()
|
|
{
|
|
return $this->hasMany(Entry::class)
|
|
->whereDoesntHave('scoreSheets');
|
|
}
|
|
|
|
public function room(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Room::class);
|
|
}
|
|
|
|
public function scoringGuide(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ScoringGuide::class);
|
|
}
|
|
|
|
public function display_fee(): string
|
|
{
|
|
return '$'.number_format($this->entry_fee / 100, 2);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsToMany|User[]
|
|
*/
|
|
public function judges(): array|BelongsToMany
|
|
{
|
|
return $this->belongsToMany(
|
|
User::class, // The related model
|
|
'room_user', // The intermediate table
|
|
'room_id', // The foreign key on the intermediate table
|
|
'user_id', // The related key on the intermediate table
|
|
'room_id', // The local key
|
|
'id' // The local ke
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Ensures judges_count property is always available
|
|
*/
|
|
public function getJudgesCountAttribute()
|
|
{
|
|
if (! isset($this->attributes['judges_count'])) {
|
|
$this->attributes['judges_count'] = $this->judges()->count();
|
|
}
|
|
|
|
return $this->attributes['judges_count'];
|
|
}
|
|
|
|
public function flags(): HasMany
|
|
{
|
|
return $this->hasMany(AuditionFlag::class);
|
|
}
|
|
|
|
public function hasFlag($flag): bool
|
|
{
|
|
$flags = [];
|
|
foreach ($this->flags as $checkFlag) {
|
|
$flags[] = $checkFlag->flag_name->value;
|
|
}
|
|
|
|
return in_array($flag, $flags);
|
|
}
|
|
|
|
public function addFlag($flag): void
|
|
{
|
|
if ($this->hasFlag($flag)) {
|
|
return;
|
|
}
|
|
$enum = match ($flag) {
|
|
'drawn' => AuditionFlags::DRAWN,
|
|
'seats_published' => AuditionFlags::SEATS_PUBLISHED,
|
|
'advancement_published' => AuditionFlags::ADVANCEMENT_PUBLISHED,
|
|
};
|
|
$this->flags()->create(['flag_name' => $enum]);
|
|
$this->load('flags');
|
|
}
|
|
|
|
public function removeFlag($flag): void
|
|
{
|
|
// remove related auditionFlag where flag_name = $flag
|
|
$this->flags()->where('flag_name', $flag)->delete();
|
|
$this->load('flags');
|
|
}
|
|
|
|
public function scopeOpen(Builder $query): void
|
|
{
|
|
$query->where('entry_deadline', '>=', Carbon::now());
|
|
}
|
|
|
|
public function scopeForSeating(Builder $query): void
|
|
{
|
|
$query->where('for_seating', 1)->orderBy('score_order');
|
|
}
|
|
|
|
public function scopeForAdvancement(Builder $query): void
|
|
{
|
|
$query->where('for_advancement', 1)->orderBy('score_order');
|
|
}
|
|
|
|
public function scopeSeatsPublished(Builder $query): Builder
|
|
{
|
|
return $query->whereHas('flags', function (Builder $query) {
|
|
$query->where('flag_name', 'seats_published');
|
|
});
|
|
|
|
}
|
|
|
|
public function scopeAdvancementPublished(Builder $query): Builder
|
|
{
|
|
return $query->whereHas('flags', function (Builder $query) {
|
|
$query->where('flag_name', 'advancement_published');
|
|
});
|
|
|
|
}
|
|
}
|