From 1a8866f3d013f1bb48dcf3b15c16289ec08f27fb Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Wed, 29 Jun 2022 17:00:15 +0800 Subject: [PATCH] filament setting apge --- app/Console/Commands/Test.php | 6 +- ...NexusOptionsTrait.php => OptionsTrait.php} | 2 +- .../Resources/System/AgentAllowResource.php | 14 ++- .../Resources/System/AgentDenyResource.php | 5 + .../Resources/System/ExamResource.php | 5 + .../Resources/System/MedalResource.php | 5 + .../Resources/System/SettingResource.php | 17 ++-- .../SettingResource/Pages/EditHitAndRun.php | 15 --- .../SettingResource/Pages/EditSetting.php | 97 ++++++++++++++++--- .../Resources/Torrent/TagResource.php | 5 + .../Resources/User/ExamUserResource.php | 5 + .../Resources/User/HitAndRunResource.php | 5 + app/Filament/Resources/User/UserResource.php | 4 + .../User/UserResource/Pages/UserProfile.php | 16 +++ app/Models/HitAndRun.php | 10 +- app/Models/Setting.php | 4 +- config/filament.php | 2 +- resources/lang/zh_CN/label.php | 41 ++++++++ .../pages/edit-hit-and-run.blade.php | 9 +- .../pages/list-settings.blade.php | 3 + .../pages/user-profile.blade.php | 91 +++++++++++++++++ 21 files changed, 314 insertions(+), 47 deletions(-) rename app/Filament/{NexusOptionsTrait.php => OptionsTrait.php} (88%) delete mode 100644 app/Filament/Resources/System/SettingResource/Pages/EditHitAndRun.php create mode 100644 resources/lang/zh_CN/label.php create mode 100644 resources/views/filament/resources/system/setting-resource/pages/list-settings.blade.php diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index 68862e20..6b40bd93 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -78,10 +78,8 @@ class Test extends Command */ public function handle() { - $r = 'MTAwNDI%3D'; - $r = 'MTAwNDI%3D'; - $r = 'MTAwMDM%3D'; - dd(base64_decode($r)); + $r = range(0, 23); + dd($r); } diff --git a/app/Filament/NexusOptionsTrait.php b/app/Filament/OptionsTrait.php similarity index 88% rename from app/Filament/NexusOptionsTrait.php rename to app/Filament/OptionsTrait.php index 90fb71a0..fb3e25c3 100644 --- a/app/Filament/NexusOptionsTrait.php +++ b/app/Filament/OptionsTrait.php @@ -2,7 +2,7 @@ namespace App\Filament; -trait NexusOptionsTrait +trait OptionsTrait { private static array $matchTypes = ['dec' => 'dec', 'hex' => 'hex']; diff --git a/app/Filament/Resources/System/AgentAllowResource.php b/app/Filament/Resources/System/AgentAllowResource.php index 0dce8d55..85298d63 100644 --- a/app/Filament/Resources/System/AgentAllowResource.php +++ b/app/Filament/Resources/System/AgentAllowResource.php @@ -2,7 +2,7 @@ namespace App\Filament\Resources\System; -use App\Filament\NexusOptionsTrait; +use App\Filament\OptionsTrait; use App\Filament\Resources\System\AgentAllowResource\Pages; use App\Filament\Resources\System\AgentAllowResource\RelationManagers; use App\Models\AgentAllow; @@ -16,7 +16,7 @@ use Illuminate\Database\Eloquent\SoftDeletingScope; class AgentAllowResource extends Resource { - use NexusOptionsTrait; + use OptionsTrait; protected static ?string $model = AgentAllow::class; @@ -29,6 +29,16 @@ class AgentAllowResource extends Resource return __('admin.sidebar.agent_allows'); } + public static function getBreadcrumb(): string + { + return self::getNavigationLabel(); + } + +// public static function getModelLabel(): string +// { +// +// } + public static function form(Form $form): Form { diff --git a/app/Filament/Resources/System/AgentDenyResource.php b/app/Filament/Resources/System/AgentDenyResource.php index 9b0518f1..94b191c5 100644 --- a/app/Filament/Resources/System/AgentDenyResource.php +++ b/app/Filament/Resources/System/AgentDenyResource.php @@ -26,6 +26,11 @@ class AgentDenyResource extends Resource return __('admin.sidebar.agent_denies'); } + public static function getBreadcrumb(): string + { + return self::getNavigationLabel(); + } + public static function form(Form $form): Form { return $form diff --git a/app/Filament/Resources/System/ExamResource.php b/app/Filament/Resources/System/ExamResource.php index 1156f8e6..a63ac219 100644 --- a/app/Filament/Resources/System/ExamResource.php +++ b/app/Filament/Resources/System/ExamResource.php @@ -27,6 +27,11 @@ class ExamResource extends Resource return __('admin.sidebar.exams_list'); } + public static function getBreadcrumb(): string + { + return self::getNavigationLabel(); + } + public static function form(Form $form): Form { $userRep = new UserRepository(); diff --git a/app/Filament/Resources/System/MedalResource.php b/app/Filament/Resources/System/MedalResource.php index 12fde5b0..7efc2224 100644 --- a/app/Filament/Resources/System/MedalResource.php +++ b/app/Filament/Resources/System/MedalResource.php @@ -26,6 +26,11 @@ class MedalResource extends Resource return __('admin.sidebar.medals_list'); } + public static function getBreadcrumb(): string + { + return self::getNavigationLabel(); + } + public static function form(Form $form): Form { return $form diff --git a/app/Filament/Resources/System/SettingResource.php b/app/Filament/Resources/System/SettingResource.php index 12d2d9c1..f870208b 100644 --- a/app/Filament/Resources/System/SettingResource.php +++ b/app/Filament/Resources/System/SettingResource.php @@ -2,7 +2,7 @@ namespace App\Filament\Resources\System; -use App\Filament\NexusOptionsTrait; +use App\Filament\OptionsTrait; use App\Filament\Resources\System\SettingResource\Pages; use App\Filament\Resources\System\SettingResource\RelationManagers; use App\Models\Setting; @@ -16,7 +16,7 @@ use Illuminate\Database\Eloquent\SoftDeletingScope; class SettingResource extends Resource { - use NexusOptionsTrait; + use OptionsTrait; protected static ?string $model = Setting::class; @@ -24,13 +24,19 @@ class SettingResource extends Resource protected static ?string $navigationGroup = 'System'; - protected static bool $shouldRegisterNavigation = false; + protected static bool $shouldRegisterNavigation = true; protected static function getNavigationLabel(): string { return __('admin.sidebar.settings'); } + public static function getBreadcrumb(): string + { + return self::getNavigationLabel(); + } + + public static function form(Form $form): Form { return $form @@ -77,10 +83,9 @@ class SettingResource extends Resource public static function getPages(): array { return [ - 'index' => Pages\ListSettings::route('/'), +// 'index' => Pages\ListSettings::route('/'), // 'create' => Pages\CreateSetting::route('/create'), - 'edit' => Pages\EditSetting::route('/{record}/edit'), - 'hit-and-run' => Pages\EditHitAndRun::route('/hit-and-run'), + 'index' => Pages\EditSetting::route('/'), ]; } } diff --git a/app/Filament/Resources/System/SettingResource/Pages/EditHitAndRun.php b/app/Filament/Resources/System/SettingResource/Pages/EditHitAndRun.php deleted file mode 100644 index 2376937c..00000000 --- a/app/Filament/Resources/System/SettingResource/Pages/EditHitAndRun.php +++ /dev/null @@ -1,15 +0,0 @@ -form->fill($settings); + + } + + protected function getFormSchema(): array { return [ - Actions\DeleteAction::make(), + Forms\Components\Tabs::make('Heading') + ->tabs([ + Forms\Components\Tabs\Tab::make(__('label.setting.hr.tab_header')) + ->schema([ + Forms\Components\Radio::make('hr.mode')->options(HitAndRun::listModes(true))->inline(true)->label(__('label.setting.hr.mode')), + Forms\Components\TextInput::make('hr.inspect_time')->helperText(__('label.setting.hr.inspect_time_help'))->label(__('label.setting.hr.inspect_time'))->integer(), + Forms\Components\TextInput::make('hr.seed_time_minimum')->helperText(__('label.setting.hr.seed_time_minimum_help'))->label(__('label.setting.hr.seed_time_minimum'))->integer(), + Forms\Components\TextInput::make('hr.ignore_when_ratio_reach')->helperText(__('label.setting.hr.ignore_when_ratio_reach_help'))->label(__('label.setting.hr.ignore_when_ratio_reach'))->integer(), + Forms\Components\TextInput::make('hr.ban_user_when_counts_reach')->helperText(__('label.setting.hr.ban_user_when_counts_reach_help'))->label(__('label.setting.hr.ban_user_when_counts_reach'))->integer(), + ])->columns(2), + Forms\Components\Tabs\Tab::make(__('label.setting.backup.tab_header')) + ->schema([ + Forms\Components\Radio::make('backup.enabled')->options(self::$yesOrNo)->inline(true)->label(__('label.setting.backup.enabled'))->helperText(__('label.setting.backup.enabled_help')), + Forms\Components\Radio::make('backup.frequency')->options(['daily' => 'daily', 'hourly' => 'hourly'])->inline(true)->label(__('label.setting.backup.frequency'))->helperText(__('label.setting.backup.frequency_help')), + Forms\Components\Select::make('backup.hour')->options(range(0, 23))->label(__('label.setting.backup.hour'))->helperText(__('label.setting.backup.hour_help')), + Forms\Components\Select::make('backup.minute')->options(range(0, 59))->label(__('label.setting.backup.minute'))->helperText(__('label.setting.backup.minute_help')), + Forms\Components\TextInput::make('backup.google_drive_client_id')->label(__('label.setting.backup.google_drive_client_id')), + Forms\Components\TextInput::make('backup.google_drive_client_secret')->label(__('label.setting.backup.google_drive_client_secret')), + Forms\Components\TextInput::make('backup.google_drive_refresh_token')->label(__('label.setting.backup.google_drive_refresh_token')), + Forms\Components\TextInput::make('backup.google_drive_folder_id')->label(__('label.setting.backup.google_drive_folder_id')), + Forms\Components\Radio::make('backup.via_ftp')->options(self::$yesOrNo)->inline(true)->label(__('label.setting.backup.via_ftp'))->helperText(__('label.setting.backup.via_ftp_help')), + Forms\Components\Radio::make('backup.via_sftp')->options(self::$yesOrNo)->inline(true)->label(__('label.setting.backup.via_sftp'))->helperText(__('label.setting.backup.via_sftp_help')), + ])->columns(2), + ]) ]; } - protected function getRedirectUrl(): ?string + public function submit() { - return $this->getResource()::getUrl('index'); + $formData = $this->form->getState(); + $notAutoloadNames = ['donation_custom']; + $data = []; + foreach ($formData as $prefix => $parts) { + foreach ($parts as $name => $value) { + if (is_null($value)) { + continue; + } + if (in_array($name, $notAutoloadNames)) { + $autoload = 'no'; + } else { + $autoload = 'yes'; + } + if (is_array($value)) { + $value = json_encode($value); + } + $data[] = [ + 'name' => "$prefix.$name", + 'value' => $value, + 'autoload' => $autoload, + ]; + + } + } + + Setting::query()->upsert($data, ['name'], ['value']); + NexusDB::cache_del('nexus_settings_in_laravel'); + NexusDB::cache_del('nexus_settings_in_nexus'); + + $this->notify('success', __('filament::resources/pages/edit-record.messages.saved')); } - protected function mutateFormDataBeforeSave(array $data): array - { - $arr = json_decode($data['value'], true); - if (is_array($arr)) { - throw new \LogicException("Not support edit this !"); - } - return $data; - } } diff --git a/app/Filament/Resources/Torrent/TagResource.php b/app/Filament/Resources/Torrent/TagResource.php index cd9becc3..22148419 100644 --- a/app/Filament/Resources/Torrent/TagResource.php +++ b/app/Filament/Resources/Torrent/TagResource.php @@ -26,6 +26,11 @@ class TagResource extends Resource return __('admin.sidebar.tags_list'); } + public static function getBreadcrumb(): string + { + return self::getNavigationLabel(); + } + public static function form(Form $form): Form { return $form diff --git a/app/Filament/Resources/User/ExamUserResource.php b/app/Filament/Resources/User/ExamUserResource.php index 38efa7c3..80071189 100644 --- a/app/Filament/Resources/User/ExamUserResource.php +++ b/app/Filament/Resources/User/ExamUserResource.php @@ -30,6 +30,11 @@ class ExamUserResource extends Resource return __('admin.sidebar.exam_users'); } + public static function getBreadcrumb(): string + { + return self::getNavigationLabel(); + } + public static function form(Form $form): Form { return $form diff --git a/app/Filament/Resources/User/HitAndRunResource.php b/app/Filament/Resources/User/HitAndRunResource.php index 50520cd2..aa2190b5 100644 --- a/app/Filament/Resources/User/HitAndRunResource.php +++ b/app/Filament/Resources/User/HitAndRunResource.php @@ -29,6 +29,11 @@ class HitAndRunResource extends Resource return __('admin.sidebar.hit_and_runs'); } + public static function getBreadcrumb(): string + { + return self::getNavigationLabel(); + } + public static function form(Form $form): Form { return $form diff --git a/app/Filament/Resources/User/UserResource.php b/app/Filament/Resources/User/UserResource.php index 137a08fa..10761c1f 100644 --- a/app/Filament/Resources/User/UserResource.php +++ b/app/Filament/Resources/User/UserResource.php @@ -28,6 +28,10 @@ class UserResource extends Resource return __('admin.sidebar.users_list'); } + public static function getBreadcrumb(): string + { + return self::getNavigationLabel(); + } public static function form(Form $form): Form { diff --git a/app/Filament/Resources/User/UserResource/Pages/UserProfile.php b/app/Filament/Resources/User/UserResource/Pages/UserProfile.php index ca304169..c163e130 100644 --- a/app/Filament/Resources/User/UserResource/Pages/UserProfile.php +++ b/app/Filament/Resources/User/UserResource/Pages/UserProfile.php @@ -3,6 +3,7 @@ namespace App\Filament\Resources\User\UserResource\Pages; use App\Filament\Resources\User\UserResource; +use App\Models\User; use Filament\Resources\Pages\Page; class UserProfile extends Page @@ -10,4 +11,19 @@ class UserProfile extends Page protected static string $resource = UserResource::class; protected static string $view = 'filament.resources.user.user-resource.pages.user-profile'; + + protected ?User $user; + + + public function mount($record) + { + $this->user = User::query()->with(['inviter'])->findOrFail($record); + } + + protected function getViewData(): array + { + return [ + 'user' => $this->user, + ]; + } } diff --git a/app/Models/HitAndRun.php b/app/Models/HitAndRun.php index 80901f84..96c948f5 100644 --- a/app/Models/HitAndRun.php +++ b/app/Models/HitAndRun.php @@ -70,11 +70,17 @@ class HitAndRun extends NexusModel return $result; } - public static function listModes(): array + public static function listModes($onlyKeyValue = false): array { $result = self::$modes; + $keyValues = []; foreach ($result as $key => &$value) { - $value['text'] = nexus_trans('hr.mode_' . $key); + $text = nexus_trans('hr.mode_' . $key); + $value['text'] = $text; + $keyValues[$key] = $text; + } + if ($onlyKeyValue) { + return $keyValues; } return $result; } diff --git a/app/Models/Setting.php b/app/Models/Setting.php index 4c53a3aa..95547f94 100644 --- a/app/Models/Setting.php +++ b/app/Models/Setting.php @@ -7,7 +7,9 @@ use Nexus\Database\NexusDB; class Setting extends NexusModel { - protected $fillable = ['name', 'value']; + protected $fillable = ['name', 'value', 'autoload']; + + public $timestamps = true; /** * get setting autoload = yes with cache diff --git a/config/filament.php b/config/filament.php index 7be0ee18..c2340380 100644 --- a/config/filament.php +++ b/config/filament.php @@ -199,7 +199,7 @@ return [ ], 'max_content_width' => null, 'notifications' => [ - 'vertical_alignment' => 'center', + 'vertical_alignment' => 'top', 'alignment' => 'center', ], 'sidebar' => [ diff --git a/resources/lang/zh_CN/label.php b/resources/lang/zh_CN/label.php new file mode 100644 index 00000000..bd5f3abc --- /dev/null +++ b/resources/lang/zh_CN/label.php @@ -0,0 +1,41 @@ + [ + 'nav_text' => '设置', + 'backup' => [ + 'tab_header' => '备份', + 'enabled' => '是否启用', + 'enabled_help' => '是否启用备份功能', + 'frequency' => '频率', + 'frequency_help' => '备份频率', + 'hour' => '小时', + 'hour_help' => '在这个点钟数进行备份', + 'minute' => '分钟', + 'minute_help' => "在前面点钟数的这一分钟进行备份。如果频率是按 'hourly',此值会被忽略", + 'google_drive_client_id' => 'Google Drive client ID', + 'google_drive_client_secret' => 'Google Drive client secret', + 'google_drive_refresh_token' => 'Google Drive refresh token', + 'google_drive_folder_id' => 'Google Drive folder ID', + 'via_ftp' => '通过 FTP 保存', + 'via_ftp_help' => '是否通过 FTP 保存。如果通过,把配置信息添加到 .env 文件,参考 Laravel 文档', + 'via_sftp' => '通过 SFTP 保存', + 'via_sftp_help' => '是否通过 SFTP 保存。如果通过,把配置信息添加到 .env 文件,参考 Laravel 文档', + ], + 'hr' => [ + 'tab_header' => 'H&R', + 'mode' => '模式', + 'inspect_time' => '考察时长', + 'inspect_time_help' => '考察时长自下载完成后开始计算,单位:小时', + 'seed_time_minimum' => '达标做种时长', + 'seed_time_minimum_help' => '达标的最短做种时长,单位:小时,必须小于考察时长', + 'ignore_when_ratio_reach' => '达标分享率', + 'ignore_when_ratio_reach_help' => '达标的最小分享率', + 'ban_user_when_counts_reach' => 'H&R 数量上限', + 'ban_user_when_counts_reach_help' => 'H&R 数量达到此值,账号会被禁用', + ] + ], + 'user' => [ + + ] +]; diff --git a/resources/views/filament/resources/system/setting-resource/pages/edit-hit-and-run.blade.php b/resources/views/filament/resources/system/setting-resource/pages/edit-hit-and-run.blade.php index cd00dafb..1ece3ffd 100644 --- a/resources/views/filament/resources/system/setting-resource/pages/edit-hit-and-run.blade.php +++ b/resources/views/filament/resources/system/setting-resource/pages/edit-hit-and-run.blade.php @@ -1,3 +1,10 @@ - +
+ {{ $this->form }} +
+ +
+
diff --git a/resources/views/filament/resources/system/setting-resource/pages/list-settings.blade.php b/resources/views/filament/resources/system/setting-resource/pages/list-settings.blade.php new file mode 100644 index 00000000..394b4b4e --- /dev/null +++ b/resources/views/filament/resources/system/setting-resource/pages/list-settings.blade.php @@ -0,0 +1,3 @@ + + {{ $this->form }} + diff --git a/resources/views/filament/resources/user/user-resource/pages/user-profile.blade.php b/resources/views/filament/resources/user/user-resource/pages/user-profile.blade.php index cd00dafb..03744c83 100644 --- a/resources/views/filament/resources/user/user-resource/pages/user-profile.blade.php +++ b/resources/views/filament/resources/user/user-resource/pages/user-profile.blade.php @@ -1,3 +1,94 @@ +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
UID{{$user->id}}
Username{{$user->username}}
Email{{$user->email}}
Status{{$user->status}}
Enabled{{$user->enabled}}
Added{{$user->added}}
Last access{{$user->last_access}}
Class{{$user->classText}}
Invite by{{$user->inviter->username ?? ''}}
Tow-step authentication{{$user->two_step_secret ? 'Enabled' : 'Disabled'}}
Seed points{{$user->seed_points}}
Attendance card{{$user->attendance_card}}
Invites{{$user->invites}}
Uploaded{{$user->uploadedText}}
Downloaded{{$user->downloadedText}}
Bonus{{$user->seedbonus}}
+
+
+
+ +