mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-14 20:40:49 +08:00
improve filament trans + user profile actions
This commit is contained in:
@@ -3,27 +3,215 @@
|
||||
namespace App\Filament\Resources\User\UserResource\Pages;
|
||||
|
||||
use App\Filament\Resources\User\UserResource;
|
||||
use App\Models\Medal;
|
||||
use App\Models\User;
|
||||
use App\Repositories\ExamRepository;
|
||||
use App\Repositories\UserRepository;
|
||||
use Filament\Resources\Pages\Concerns\HasRelationManagers;
|
||||
use Filament\Resources\Pages\Concerns\InteractsWithRecord;
|
||||
use Filament\Resources\Pages\Page;
|
||||
use Filament\Pages\Actions;
|
||||
use Filament\Forms;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class UserProfile extends Page
|
||||
{
|
||||
use InteractsWithRecord;
|
||||
use HasRelationManagers;
|
||||
|
||||
protected static string $resource = UserResource::class;
|
||||
|
||||
protected static string $view = 'filament.resources.user.user-resource.pages.user-profile';
|
||||
|
||||
protected ?User $user;
|
||||
const EVENT_RECORD_UPDATED = 'recordUpdated';
|
||||
|
||||
protected $listeners = [
|
||||
self::EVENT_RECORD_UPDATED => 'updateRecord'
|
||||
];
|
||||
|
||||
public function updateRecord($id)
|
||||
{
|
||||
$this->record = $this->resolveRecord($id);
|
||||
}
|
||||
|
||||
public function mount($record)
|
||||
{
|
||||
$this->user = User::query()->with(['inviter'])->findOrFail($record);
|
||||
$this->updateRecord($record);
|
||||
}
|
||||
|
||||
protected function getViewData(): array
|
||||
protected function getActions(): array
|
||||
{
|
||||
return [
|
||||
'user' => $this->user,
|
||||
];
|
||||
$actions = [];
|
||||
if ($this->record->two_step_secret) {
|
||||
$actions[] = $this->buildDisableTwoStepAuthenticationAction();
|
||||
}
|
||||
$actions[] = $this->buildResetPasswordAction();
|
||||
$actions[] = $this->buildAssignExamAction();
|
||||
$actions[] = $this->buildGrantMedalAction();
|
||||
$actions[] = $this->buildChangeBonusEtcAction();
|
||||
$actions[] = $this->buildEnableDisableAction();
|
||||
return $actions;
|
||||
}
|
||||
|
||||
private function buildEnableDisableAction(): Actions\Action
|
||||
{
|
||||
return Actions\Action::make($this->record->enabled == 'yes' ? __('admin.resources.user.actions.disable_modal_btn') : __('admin.resources.user.actions.enable_modal_btn'))
|
||||
->modalHeading($this->record->enabled == 'yes' ? __('admin.resources.user.actions.disable_modal_title') : __('admin.resources.user.actions.enable_modal_title'))
|
||||
->form([
|
||||
Forms\Components\TextInput::make('reason')->label(__('admin.resources.user.actions.enable_disable_reason'))->placeholder(__('admin.resources.user.actions.enable_disable_reason_placeholder')),
|
||||
Forms\Components\Hidden::make('action')->default($this->record->enabled == 'yes' ? 'disable' : 'enable'),
|
||||
Forms\Components\Hidden::make('uid')->default($this->record->id),
|
||||
])
|
||||
->action(function ($data) {
|
||||
$userRep = new UserRepository();
|
||||
try {
|
||||
if ($data['action'] == 'enable') {
|
||||
$userRep->enableUser(Auth::user(), $data['uid'], $data['reason']);
|
||||
} elseif ($data['action'] == 'disable') {
|
||||
$userRep->disableUser(Auth::user(), $data['uid'], $data['reason']);
|
||||
}
|
||||
$this->notify('success', 'Success!');
|
||||
$this->emitSelf(self::EVENT_RECORD_UPDATED, $data['uid']);
|
||||
} catch (\Exception $exception) {
|
||||
$this->notify('danger', $exception->getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function buildDisableTwoStepAuthenticationAction(): Actions\Action
|
||||
{
|
||||
return Actions\Action::make(__('admin.resources.user.actions.disable_two_step_authentication'))
|
||||
->modalHeading(__('admin.resources.user.actions.disable_two_step_authentication'))
|
||||
->requiresConfirmation()
|
||||
->action(function ($data) {
|
||||
$userRep = new UserRepository();
|
||||
try {
|
||||
$userRep->removeTwoStepAuthentication(Auth::user(), $this->record->id);
|
||||
$this->notify('success', 'Success!');
|
||||
$this->emitSelf(self::EVENT_RECORD_UPDATED, $this->record->id);
|
||||
} catch (\Exception $exception) {
|
||||
$this->notify('danger', $exception->getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function buildChangeBonusEtcAction(): Actions\Action
|
||||
{
|
||||
return Actions\Action::make(__('admin.resources.user.actions.change_bonus_etc_btn'))
|
||||
->modalHeading(__('admin.resources.user.actions.change_bonus_etc_btn'))
|
||||
->form([
|
||||
Forms\Components\Radio::make('field')->options([
|
||||
'uploaded' => __('label.user.uploaded'),
|
||||
'downloaded' => __('label.user.downloaded'),
|
||||
'invites' => __('label.user.invites'),
|
||||
'seedbonus' => __('label.user.seedbonus'),
|
||||
'attendance_card' => __('label.user.attendance_card'),
|
||||
])->label(__('admin.resources.user.actions.change_bonus_etc_field_label'))->inline()->required(),
|
||||
Forms\Components\Radio::make('action')->options([
|
||||
'Increment' => __("admin.resources.user.actions.change_bonus_etc_action_increment"),
|
||||
'Decrement' => __("admin.resources.user.actions.change_bonus_etc_action_decrement"),
|
||||
])->label(__('admin.resources.user.actions.change_bonus_etc_action_label'))->inline()->required(),
|
||||
Forms\Components\TextInput::make('value')->integer()->required()
|
||||
->label(__('admin.resources.user.actions.change_bonus_etc_value_label'))
|
||||
->helperText(__('admin.resources.user.actions.change_bonus_etc_value_help')),
|
||||
Forms\Components\Textarea::make('reason')->label(__('admin.resources.user.actions.change_bonus_etc_reason_label')),
|
||||
])
|
||||
->action(function ($data) {
|
||||
$userRep = new UserRepository();
|
||||
try {
|
||||
$userRep->incrementDecrement(Auth::user(), $this->record->id, $data['action'], $data['field'], $data['value'], $data['reason']);
|
||||
$this->notify('success', 'Success!');
|
||||
$this->emitSelf(self::EVENT_RECORD_UPDATED, $this->record->id);
|
||||
} catch (\Exception $exception) {
|
||||
$this->notify('danger', $exception->getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function buildResetPasswordAction()
|
||||
{
|
||||
return Actions\Action::make(__('admin.resources.user.actions.reset_password_btn'))
|
||||
->modalHeading(__('admin.resources.user.actions.reset_password_btn'))
|
||||
->form([
|
||||
Forms\Components\TextInput::make('password')->label(__('admin.resources.user.actions.reset_password_label'))->required(),
|
||||
Forms\Components\TextInput::make('password_confirmation')
|
||||
->label(__('admin.resources.user.actions.reset_password_confirmation_label'))
|
||||
->same('password')
|
||||
->required(),
|
||||
])
|
||||
->action(function ($data) {
|
||||
$userRep = new UserRepository();
|
||||
try {
|
||||
$userRep->resetPassword($this->record->id, $data['password'], $data['password_confirmation']);
|
||||
$this->notify('success', 'Success!');
|
||||
$this->emitSelf(self::EVENT_RECORD_UPDATED, $this->record->id);
|
||||
} catch (\Exception $exception) {
|
||||
$this->notify('danger', $exception->getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function buildAssignExamAction()
|
||||
{
|
||||
return Actions\Action::make(__('admin.resources.user.actions.assign_exam_btn'))
|
||||
->modalHeading(__('admin.resources.user.actions.assign_exam_btn'))
|
||||
->form([
|
||||
Forms\Components\Select::make('exam_id')
|
||||
->options((new ExamRepository())->listMatchExam($this->record->id)->pluck('name', 'id'))
|
||||
->label(__('admin.resources.user.actions.assign_exam_exam_label'))->required(),
|
||||
Forms\Components\DateTimePicker::make('begin')->label(__('admin.resources.user.actions.assign_exam_begin_label')),
|
||||
Forms\Components\DateTimePicker::make('end')->label(__('admin.resources.user.actions.assign_exam_end_label'))
|
||||
->helperText(__('admin.resources.user.actions.assign_exam_end_help')),
|
||||
|
||||
])
|
||||
->action(function ($data) {
|
||||
$examRep = new ExamRepository();
|
||||
try {
|
||||
$examRep->assignToUser($this->record->id, $data['exam_id'], $data['begin'], $data['end']);
|
||||
$this->notify('success', 'Success!');
|
||||
$this->emitSelf(self::EVENT_RECORD_UPDATED, $this->record->id);
|
||||
} catch (\Exception $exception) {
|
||||
$this->notify('danger', $exception->getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function buildGrantMedalAction()
|
||||
{
|
||||
return Actions\Action::make(__('admin.resources.user.actions.grant_medal_btn'))
|
||||
->modalHeading(__('admin.resources.user.actions.grant_medal_btn'))
|
||||
->form([
|
||||
Forms\Components\Select::make('medal_id')
|
||||
->options(Medal::query()->pluck('name', 'id'))
|
||||
->label(__('admin.resources.user.actions.grant_medal_medal_label'))
|
||||
->required(),
|
||||
|
||||
Forms\Components\TextInput::make('duration')
|
||||
->label(__('admin.resources.user.actions.grant_medal_duration_label'))
|
||||
->helperText(__('admin.resources.user.actions.grant_medal_duration_help'))
|
||||
->integer(),
|
||||
|
||||
])
|
||||
->action(function ($data) {
|
||||
$examRep = new ExamRepository();
|
||||
try {
|
||||
$examRep->assignToUser($this->record->id, $data['exam_id'], $data['begin'], $data['end']);
|
||||
$this->notify('success', 'Success!');
|
||||
$this->emitSelf(self::EVENT_RECORD_UPDATED, $this->record->id);
|
||||
} catch (\Exception $exception) {
|
||||
$this->notify('danger', $exception->getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// protected function getViewData(): array
|
||||
// {
|
||||
// return [
|
||||
// 'enableDisableAction' => $this->buildEnableDisableAction(),
|
||||
// ];
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user