Introduce filament

This commit is contained in:
xiaomlove
2022-06-27 01:39:01 +08:00
parent aae45835ee
commit 1aca20070d
92 changed files with 3535 additions and 83 deletions
+48
View File
@@ -2,6 +2,7 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class Exam extends NexusModel
@@ -78,4 +79,51 @@ class Exam extends NexusModel
return '';
}
public function getIndexFormattedAttribute(): string
{
$indexes = $this->indexes;
$arr = [];
foreach ($indexes as $index) {
if (isset($index['checked']) && $index['checked']) {
$arr[] = sprintf(
'%s: %s %s',
self::$indexes[$index['index']]['name'] ?? '',
$index['require_value'],
self::$indexes[$index['index']]['unit'] ?? ''
);
}
}
return implode("<br/>", $arr);
}
public function getFilterFormattedAttribute(): string
{
$currentFilters = $this->filters;
$arr = [];
$filter = self::FILTER_USER_CLASS;
if (!empty($currentFilters->{$filter})) {
$classes = collect(User::$classes)->only($currentFilters->{$filter});
$arr[] = sprintf('%s: %s', self::$filters[$filter]['name'], $classes->pluck('text')->implode(', '));
}
$filter = self::FILTER_USER_REGISTER_TIME_RANGE;
if (!empty($currentFilters->{$filter})) {
$range = $currentFilters->{$filter};
$arr[] = sprintf(
"%s: \n%s ~ %s",
self::$filters[$filter]['name'],
$range[0] ? Carbon::parse($range[0])->toDateTimeString() : '-',
$range[1] ? Carbon::parse($range[1])->toDateTimeString() : '-'
);
}
$filter = self::FILTER_USER_DONATE;
if (!empty($currentFilters->{$filter})) {
$donateStatus = $classes = collect(User::$donateStatus)->only($currentFilters->{$filter});
$arr[] = sprintf('%s: %s', self::$filters[$filter]['name'], $donateStatus->pluck('text')->implode(', '));
}
return implode("<br/>", $arr);
}
}
+15
View File
@@ -41,6 +41,21 @@ class ExamUser extends NexusModel
return self::$isDoneInfo[$this->is_done]['text'] ?? '';
}
public static function listStatus($onlyKeyValue = false): array
{
$result = self::$status;
$keyValues = [];
foreach ($result as $key => &$value) {
$text = nexus_trans('exam-user.status.' . $key);
$value['text'] = $text;
$keyValues[$key] = $text;
}
if ($onlyKeyValue) {
return $keyValues;
}
return $result;
}
public function getBeginAttribute()
{
$begin = $this->getRawOriginal('begin');
+8 -2
View File
@@ -55,11 +55,17 @@ class HitAndRun extends NexusModel
return nexus_trans('hr.status_' . $this->status);
}
public static function listStatus(): array
public static function listStatus($onlyKeyValue = false): array
{
$result = self::$status;
$keyValues = [];
foreach ($result as $key => &$value) {
$value['text'] = nexus_trans('hr.status_' . $key);
$text = nexus_trans('hr.status_' . $key);
$value['text'] = $text;
$keyValues[$key] = $text;
}
if ($onlyKeyValue) {
return $keyValues;
}
return $result;
}
+16 -1
View File
@@ -10,7 +10,7 @@ class Medal extends NexusModel
const GET_TYPE_GRANT = 2;
public static $getTypeText = [
public static array $getTypeText = [
self::GET_TYPE_EXCHANGE => ['text' => 'Exchange'],
self::GET_TYPE_GRANT => ['text' => 'Grant'],
];
@@ -19,6 +19,21 @@ class Medal extends NexusModel
public $timestamps = true;
public static function listGetTypes($onlyKeyValues = false): array
{
$results = self::$getTypeText;
$keyValues = [];
foreach ($results as $type => &$info) {
$text = nexus_trans("medal.get_types.$type");
$keyValues[$type] = $text;
$info['text'] = $text;
}
if ($onlyKeyValues) {
return $keyValues;
}
return $results;
}
public function getGetTypeTextAttribute($value): string
{
return self::$getTypeText[$this->get_type]['text'] ?? '';
+1 -1
View File
@@ -23,7 +23,7 @@ class NexusModel extends Model
*/
protected function serializeDate(\DateTimeInterface $date)
{
return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
return $date->format($this->dateFormat ?: 'Y-m-d H:i');
}
/**
+30 -2
View File
@@ -6,6 +6,7 @@ use App\Http\Middleware\Locale;
use App\Repositories\ExamRepository;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
@@ -13,8 +14,10 @@ use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\HasApiTokens;
use Nexus\Database\NexusDB;
use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasName;
class User extends Authenticatable
class User extends Authenticatable implements FilamentUser, HasName
{
use HasFactory, Notifiable, HasApiTokens;
@@ -99,6 +102,17 @@ class User extends Authenticatable
return $classText;
}
public function canAccessFilament(): bool
{
return true;
// return str_ends_with($this->email, '@yourdomain.com') && $this->hasVerifiedEmail();
}
public function getFilamentName(): string
{
return $this->username;
}
/**
* @see ExamRepository::isExamMatchUser()
*
@@ -120,7 +134,7 @@ class User extends Authenticatable
*/
protected function serializeDate(\DateTimeInterface $date): string
{
return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
return $date->format($this->dateFormat ?: 'Y-m-d H:i');
}
/**
@@ -244,6 +258,20 @@ class User extends Authenticatable
return 'en';
}
protected function uploadedText(): Attribute
{
return new Attribute(
get: fn($value, $attributes) => mksize($attributes['uploaded'])
);
}
protected function downloadedText(): Attribute
{
return new Attribute(
get: fn($value, $attributes) => mksize($attributes['downloaded'])
);
}
public static function getMinSeedPoints($class)
{
$setting = Setting::get("account.{$class}_min_seed_points");