1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Models;
7
8use Illuminate\Database\Eloquent\Builder;
9use Illuminate\Database\Eloquent\Relations\BelongsTo;
10
11/**
12 * @property int $banner_id
13 * @property Country $country
14 * @property string $country_acronym
15 * @property Tournament $tournament
16 * @property int $tournament_id
17 * @property User $user
18 * @property int $user_id
19 */
20class ProfileBanner extends Model
21{
22 protected $table = 'osu_profile_banners';
23 protected $primaryKey = 'banner_id';
24 public $timestamps = false;
25
26 public function user()
27 {
28 return $this->belongsTo(User::class, 'user_id');
29 }
30
31 public function tournament()
32 {
33 return $this->belongsTo(Tournament::class, 'tournament_id');
34 }
35
36 public function tournamentBanner(): BelongsTo
37 {
38 return $this->belongsTo(TournamentBanner::class, 'tournament_id');
39 }
40
41 public function country()
42 {
43 return $this->belongsTo(Country::class, 'country_acronym');
44 }
45
46 public function scopeActive(Builder $query): void
47 {
48 $query->whereHas('tournamentBanner', fn ($q) => $q
49 ->where('is_active', true)
50 ->where(fn ($countryQuery) => $countryQuery
51 ->whereNull('winner_country_acronym')
52 ->orWhereColumn(
53 $countryQuery->qualifyColumn('winner_country_acronym'),
54 $query->qualifyColumn('country_acronym'),
55 )));
56 }
57
58 public function image(): string
59 {
60 return "{$this->tournamentBanner->banner_url_prefix}{$this->country_acronym}.jpg";
61 }
62}