You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.4 KiB
79 lines
2.4 KiB
|
1 month ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use App\Support\PortalLocale;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
use Illuminate\Support\Facades\Schema;
|
||
|
|
|
||
|
|
class CompetitionPortalLocale extends Model
|
||
|
|
{
|
||
|
|
protected $fillable = [
|
||
|
|
'user_id',
|
||
|
|
'competition_id',
|
||
|
|
'portal',
|
||
|
|
'locale',
|
||
|
|
];
|
||
|
|
|
||
|
|
public function user(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(User::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function competition(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Competition::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function lockOrGet(int $userId, int $competitionId, string $portal, string $requested): string
|
||
|
|
{
|
||
|
|
if (! Schema::hasTable((new static)->getTable())) {
|
||
|
|
return PortalLocale::isValid($requested) ? $requested : PortalLocale::ZH;
|
||
|
|
}
|
||
|
|
|
||
|
|
$locale = PortalLocale::isValid($requested) ? $requested : PortalLocale::ZH;
|
||
|
|
|
||
|
|
$row = static::query()->firstOrCreate(
|
||
|
|
[
|
||
|
|
'user_id' => $userId,
|
||
|
|
'competition_id' => $competitionId,
|
||
|
|
'portal' => $portal,
|
||
|
|
],
|
||
|
|
['locale' => $locale]
|
||
|
|
);
|
||
|
|
|
||
|
|
return PortalLocale::isValid((string) $row->locale) ? (string) $row->locale : PortalLocale::ZH;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 未提交时跟随本次请求语言,可覆盖草稿语言。 */
|
||
|
|
public static function remember(int $userId, int $competitionId, string $portal, string $requested): string
|
||
|
|
{
|
||
|
|
if (! Schema::hasTable((new static)->getTable())) {
|
||
|
|
return PortalLocale::isValid($requested) ? $requested : PortalLocale::ZH;
|
||
|
|
}
|
||
|
|
|
||
|
|
$locale = PortalLocale::isValid($requested) ? $requested : PortalLocale::ZH;
|
||
|
|
|
||
|
|
$row = static::query()->updateOrCreate(
|
||
|
|
[
|
||
|
|
'user_id' => $userId,
|
||
|
|
'competition_id' => $competitionId,
|
||
|
|
'portal' => $portal,
|
||
|
|
],
|
||
|
|
['locale' => $locale]
|
||
|
|
);
|
||
|
|
|
||
|
|
return PortalLocale::isValid((string) $row->locale) ? (string) $row->locale : PortalLocale::ZH;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function resolveForLogin(int $userId, int $competitionId, string $portal, string $requested, bool $locked): string
|
||
|
|
{
|
||
|
|
if ($locked) {
|
||
|
|
return static::lockOrGet($userId, $competitionId, $portal, $requested);
|
||
|
|
}
|
||
|
|
|
||
|
|
return static::remember($userId, $competitionId, $portal, $requested);
|
||
|
|
}
|
||
|
|
}
|