2024-05-15 00:31:24 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2024-05-31 17:44:52 -04:00
|
|
|
use App\Models\Scopes\DisabledScope;
|
2024-05-15 00:31:24 -04:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2024-07-04 22:10:48 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2024-05-15 00:31:24 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2024-07-15 23:13:51 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
2024-05-15 00:31:24 -04:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2024-06-23 08:34:22 -04:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2024-05-15 00:31:24 -04:00
|
|
|
use Illuminate\Support\Str;
|
2024-06-02 22:03:59 -04:00
|
|
|
use Laravel\Scout\Searchable;
|
2024-05-15 00:31:24 -04:00
|
|
|
|
2024-05-31 17:44:52 -04:00
|
|
|
/**
|
|
|
|
* @property string $slug
|
|
|
|
*/
|
2024-05-15 00:31:24 -04:00
|
|
|
class Mod extends Model
|
|
|
|
{
|
2024-06-03 02:04:49 +00:00
|
|
|
use HasFactory, Searchable, SoftDeletes;
|
2024-05-15 00:31:24 -04:00
|
|
|
|
2024-05-31 17:44:52 -04:00
|
|
|
protected static function booted(): void
|
|
|
|
{
|
2024-06-23 08:34:22 -04:00
|
|
|
// Apply the global scope to exclude disabled mods.
|
2024-05-31 17:44:52 -04:00
|
|
|
static::addGlobalScope(new DisabledScope);
|
|
|
|
}
|
|
|
|
|
2024-07-04 22:10:48 -04:00
|
|
|
/**
|
|
|
|
* The users that belong to the mod.
|
|
|
|
*/
|
|
|
|
public function users(): BelongsToMany
|
2024-05-15 00:31:24 -04:00
|
|
|
{
|
2024-07-04 22:10:48 -04:00
|
|
|
return $this->belongsToMany(User::class);
|
2024-05-15 00:31:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function license(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(License::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function versions(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(ModVersion::class);
|
|
|
|
}
|
|
|
|
|
2024-06-23 08:34:22 -04:00
|
|
|
/**
|
|
|
|
* Scope a query to include the total number of downloads for a mod.
|
|
|
|
*/
|
2024-05-24 17:06:02 -04:00
|
|
|
public function scopeWithTotalDownloads($query)
|
2024-05-17 17:11:54 -04:00
|
|
|
{
|
2024-07-15 23:13:51 -04:00
|
|
|
return $query->addSelect([
|
|
|
|
'total_downloads' => ModVersion::selectRaw('SUM(downloads) AS total_downloads')
|
|
|
|
->whereColumn('mod_id', 'mods.id'),
|
2024-05-24 17:06:02 -04:00
|
|
|
]);
|
2024-05-17 23:54:03 -04:00
|
|
|
}
|
|
|
|
|
2024-07-15 23:13:51 -04:00
|
|
|
public function lastUpdatedVersion(): HasOne
|
2024-05-17 23:54:03 -04:00
|
|
|
{
|
2024-07-15 23:13:51 -04:00
|
|
|
return $this->hasOne(ModVersion::class)->orderByDesc('updated_at')->with('sptVersion');
|
2024-05-15 00:31:24 -04:00
|
|
|
}
|
2024-06-02 22:03:59 -04:00
|
|
|
|
2024-06-23 08:34:22 -04:00
|
|
|
/**
|
|
|
|
* Get the indexable data array for the model.
|
|
|
|
*/
|
2024-06-02 22:03:59 -04:00
|
|
|
public function toSearchableArray(): array
|
|
|
|
{
|
2024-07-15 23:13:51 -04:00
|
|
|
$latestSptVersion = $this->latestSptVersion()->first();
|
|
|
|
|
2024-06-02 22:03:59 -04:00
|
|
|
return [
|
|
|
|
'id' => (int) $this->id,
|
|
|
|
'name' => $this->name,
|
|
|
|
'slug' => $this->slug,
|
|
|
|
'description' => $this->description,
|
|
|
|
'thumbnail' => $this->thumbnail,
|
|
|
|
'featured' => $this->featured,
|
|
|
|
'created_at' => strtotime($this->created_at),
|
|
|
|
'updated_at' => strtotime($this->updated_at),
|
2024-07-15 23:13:51 -04:00
|
|
|
'latestSptVersion' => $latestSptVersion?->sptVersion->version,
|
|
|
|
'latestSptVersionColorClass' => $latestSptVersion?->sptVersion->color_class,
|
2024-06-02 22:03:59 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-07-15 23:13:51 -04:00
|
|
|
public function latestSptVersion(): HasOne
|
|
|
|
{
|
|
|
|
return $this->hasOne(ModVersion::class)
|
|
|
|
->orderByDesc(
|
|
|
|
SptVersion::select('version')
|
|
|
|
->whereColumn('mod_versions.spt_version_id', 'spt_versions.id')
|
|
|
|
->orderByDesc('version')
|
|
|
|
->take(1),
|
|
|
|
)
|
|
|
|
->with('sptVersion')
|
|
|
|
->orderByDesc('version')
|
|
|
|
->take(1);
|
|
|
|
}
|
|
|
|
|
2024-06-23 08:34:22 -04:00
|
|
|
/**
|
|
|
|
* Determine if the model should be searchable.
|
|
|
|
*/
|
2024-06-02 22:03:59 -04:00
|
|
|
public function shouldBeSearchable(): bool
|
|
|
|
{
|
|
|
|
return ! $this->disabled;
|
|
|
|
}
|
2024-06-23 08:34:22 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the URL to the thumbnail.
|
|
|
|
*/
|
|
|
|
public function thumbnailUrl(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::get(function (): string {
|
|
|
|
return $this->thumbnail
|
|
|
|
? Storage::disk($this->thumbnailDisk())->url($this->thumbnail)
|
|
|
|
: '';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the disk where the thumbnail is stored.
|
|
|
|
*/
|
|
|
|
protected function thumbnailDisk(): string
|
|
|
|
{
|
|
|
|
return match (config('app.env')) {
|
|
|
|
'production' => 'r2', // Cloudflare R2 Storage
|
|
|
|
default => 'public', // Local
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-07-04 22:10:48 -04:00
|
|
|
protected function casts(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'featured' => 'boolean',
|
|
|
|
'contains_ai_content' => 'boolean',
|
|
|
|
'contains_ads' => 'boolean',
|
|
|
|
'disabled' => 'boolean',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-06-23 08:34:22 -04:00
|
|
|
/**
|
|
|
|
* Ensure the slug is always lower case when retrieved and slugified when saved.
|
|
|
|
*/
|
|
|
|
protected function slug(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
get: fn (string $value) => Str::lower($value),
|
|
|
|
set: fn (string $value) => Str::slug($value),
|
|
|
|
);
|
|
|
|
}
|
2024-05-15 00:31:24 -04:00
|
|
|
}
|