Files
nexusphp/app/Filament/Resources/User/UserResource/Pages/UserProfile.php

408 lines
18 KiB
PHP
Raw Normal View History

2022-06-28 13:33:18 +08:00
<?php
namespace App\Filament\Resources\User\UserResource\Pages;
use App\Filament\OptionsTrait;
2022-06-28 13:33:18 +08:00
use App\Filament\Resources\User\UserResource;
2024-05-18 14:53:30 +08:00
use App\Models\Exam;
2022-12-13 13:51:39 +08:00
use App\Models\Invite;
use App\Models\Medal;
2022-06-29 17:00:15 +08:00
use App\Models\User;
2022-08-11 17:12:36 +08:00
use App\Models\UserMeta;
use App\Repositories\ExamRepository;
use App\Repositories\MedalRepository;
use App\Repositories\UserRepository;
2022-12-13 13:51:39 +08:00
use Carbon\Carbon;
use Filament\Resources\Pages\Concerns\HasRelationManagers;
use Filament\Resources\Pages\Concerns\InteractsWithRecord;
2022-06-28 13:33:18 +08:00
use Filament\Resources\Pages\Page;
use Filament\Pages\Actions;
use Filament\Forms;
use Filament\Resources\Pages\ViewRecord;
use Illuminate\Support\Facades\Auth;
2022-08-11 17:12:36 +08:00
use Nexus\Database\NexusDB;
2022-06-28 13:33:18 +08:00
class UserProfile extends ViewRecord
2022-06-28 13:33:18 +08:00
{
use InteractsWithRecord;
use HasRelationManagers;
use OptionsTrait;
2022-08-11 17:12:36 +08:00
private static $rep;
2022-06-28 13:33:18 +08:00
protected static string $resource = UserResource::class;
protected static string $view = 'filament.resources.user.user-resource.pages.user-profile';
2022-06-29 17:00:15 +08:00
const EVENT_RECORD_UPDATED = 'recordUpdated';
protected $listeners = [
self::EVENT_RECORD_UPDATED => 'updateRecord'
];
2022-06-29 17:00:15 +08:00
2022-08-11 17:12:36 +08:00
private function getRep(): UserRepository
{
if (!self::$rep) {
self::$rep = new UserRepository();
}
return self::$rep;
}
public function updateRecord($id)
{
$this->record = $this->resolveRecord($id);
}
2022-06-29 17:00:15 +08:00
protected function getActions(): array
{
$actions = [];
2022-07-18 01:37:50 +08:00
if (Auth::user()->class > $this->record->class) {
2022-08-11 17:12:36 +08:00
$actions[] = $this->buildGrantPropsAction();
2022-07-18 01:37:50 +08:00
$actions[] = $this->buildGrantMedalAction();
2022-08-11 17:12:36 +08:00
$actions[] = $this->buildAssignExamAction();
2022-07-18 01:37:50 +08:00
$actions[] = $this->buildChangeBonusEtcAction();
if ($this->record->two_step_secret) {
$actions[] = $this->buildDisableTwoStepAuthenticationAction();
}
if ($this->record->status == User::STATUS_PENDING) {
$actions[] = $this->buildConfirmAction();
}
$actions[] = $this->buildResetPasswordAction();
$actions[] = $this->buildEnableDisableAction();
2022-07-23 23:35:43 +08:00
$actions[] = $this->buildEnableDisableDownloadPrivilegesAction();
2023-01-07 19:34:59 +08:00
if (user_can('user-change-class')) {
$actions[] = $this->buildChangeClassAction();
}
if (user_can('user-delete')) {
$actions[] = $this->buildDeleteAction();
}
2022-08-20 21:19:11 +08:00
$actions = apply_filter('user_profile_actions', $actions);
}
2022-08-20 21:19:11 +08:00
return $actions;
}
private function buildEnableDisableAction(): Actions\Action
{
2022-08-31 23:08:26 +08:00
return Actions\Action::make('enable_disable')
->label($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),
])
2022-08-31 23:08:26 +08:00
// ->visible(false)
// ->hidden(true)
->action(function ($data) {
2022-08-11 17:12:36 +08:00
$userRep = $this->getRep();
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) {
2022-08-11 17:12:36 +08:00
$userRep = $this->getRep();
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'),
2022-12-13 13:51:39 +08:00
'tmp_invites' => __('label.user.tmp_invites'),
])
->label(__('admin.resources.user.actions.change_bonus_etc_field_label'))
->inline()
->required()
->reactive()
,
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"),
2022-12-13 13:51:39 +08:00
])
->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'))
2022-12-13 13:51:39 +08:00
->helperText(__('admin.resources.user.actions.change_bonus_etc_value_help'))
,
Forms\Components\TextInput::make('duration')->integer()
->label(__('admin.resources.user.actions.change_bonus_etc_duration_label'))
->helperText(__('admin.resources.user.actions.change_bonus_etc_duration_help'))
->hidden(fn (\Closure $get) => $get('field') != 'tmp_invites')
,
2023-01-10 17:25:53 +08:00
Forms\Components\TextInput::make('reason')
2022-12-13 13:51:39 +08:00
->label(__('admin.resources.user.actions.change_bonus_etc_reason_label'))
,
])
->action(function ($data) {
2022-08-11 17:12:36 +08:00
$userRep = $this->getRep();
try {
2022-12-13 13:51:39 +08:00
if ($data['field'] == 'tmp_invites') {
$userRep->addTemporaryInvite(Auth::user(), $this->record->id, $data['action'], $data['value'], $data['duration'], $data['reason']);
} else {
$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) {
2022-08-11 17:12:36 +08:00
$userRep = $this->getRep();
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());
}
});
2022-06-29 17:00:15 +08:00
}
private function buildAssignExamAction()
2022-06-29 17:00:15 +08:00
{
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());
}
});
2022-06-29 17:00:15 +08:00
}
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) {
$medalRep = new MedalRepository();
try {
$medalRep->grantToUser($this->record->id, $data['medal_id'], $data['duration']);
$this->notify('success', 'Success!');
$this->emitSelf(self::EVENT_RECORD_UPDATED, $this->record->id);
} catch (\Exception $exception) {
$this->notify('danger', $exception->getMessage());
}
});
}
private function buildConfirmAction()
{
return Actions\Action::make(__('admin.resources.user.actions.confirm_btn'))
->modalHeading(__('admin.resources.user.actions.confirm_btn'))
->requiresConfirmation()
->action(function () {
2022-07-18 01:37:50 +08:00
if (Auth::user()->class <= $this->record->class) {
$this->notify('danger', 'No permission!');
return;
}
$this->record->status = User::STATUS_CONFIRMED;
$this->record->info= null;
$this->record->save();
$this->notify('success', 'Success!');
$this->emitSelf(self::EVENT_RECORD_UPDATED, $this->record->id);
});
}
2022-07-23 23:35:43 +08:00
private function buildEnableDisableDownloadPrivilegesAction(): Actions\Action
{
return Actions\Action::make($this->record->downloadpos == 'yes' ? __('admin.resources.user.actions.disable_download_privileges_btn') : __('admin.resources.user.actions.enable_download_privileges_btn'))
// ->modalHeading($this->record->enabled == 'yes' ? __('admin.resources.user.actions.disable_modal_title') : __('admin.resources.user.actions.enable_modal_title'))
->requiresConfirmation()
->action(function () {
2022-08-11 17:12:36 +08:00
$userRep = $this->getRep();
2022-07-23 23:35:43 +08:00
try {
2022-07-30 15:06:51 +08:00
$userRep->updateDownloadPrivileges(Auth::user(), $this->record->id, $this->record->downloadpos == 'yes' ? 'no' : 'yes');
2022-07-23 23:35:43 +08:00
$this->notify('success', 'Success!');
$this->emitSelf(self::EVENT_RECORD_UPDATED, $this->record->id);
} catch (\Exception $exception) {
$this->notify('danger', $exception->getMessage());
}
});
}
2022-08-11 17:12:36 +08:00
private function buildGrantPropsAction()
{
return Actions\Action::make(__('admin.resources.user.actions.grant_prop_btn'))
->modalHeading(__('admin.resources.user.actions.grant_prop_btn'))
->form([
Forms\Components\Select::make('meta_key')
->options(UserMeta::listProps())
->label(__('admin.resources.user.actions.grant_prop_form_prop'))->required(),
Forms\Components\TextInput::make('duration')->label(__('admin.resources.user.actions.grant_prop_form_duration'))
->helperText(__('admin.resources.user.actions.grant_prop_form_duration_help')),
])
->action(function ($data) {
$rep = $this->getRep();
try {
2022-09-08 01:42:59 +08:00
$rep->addMeta($this->record, $data, $data);
2022-08-11 17:12:36 +08:00
$this->notify('success', 'Success!');
$this->emitSelf(self::EVENT_RECORD_UPDATED, $this->record->id);
} catch (\Exception $exception) {
$this->notify('danger', $exception->getMessage());
}
});
}
private function buildDeleteAction(): Actions\Action
{
return Actions\DeleteAction::make()->using(function () {
$this->getRep()->destroy($this->record->id);
2024-11-25 12:08:16 +08:00
return redirect(self::$resource::getUrl('index'));
});
}
2022-08-11 17:12:36 +08:00
public function getViewData(): array
{
return [
'props' => $this->listUserProps(),
2022-12-13 13:51:39 +08:00
'temporary_invite_count' => $this->countTemporaryInvite()
2022-08-11 17:12:36 +08:00
];
}
private function listUserProps(): array
{
$metaKeys = [
UserMeta::META_KEY_PERSONALIZED_USERNAME,
UserMeta::META_KEY_CHANGE_USERNAME,
];
$metaList = $this->getRep()->listMetas($this->record->id, $metaKeys);
$props = [];
foreach ($metaList as $metaKey => $metas) {
$meta = $metas->first();
$text = sprintf('[%s]', $meta->metaKeyText, );
if ($meta->meta_key == UserMeta::META_KEY_PERSONALIZED_USERNAME) {
$text .= sprintf('(%s)', $meta->getDeadlineText());
}
$props[] = "<div>{$text}</div>";
}
return $props;
}
2022-12-13 13:51:39 +08:00
private function countTemporaryInvite()
{
return Invite::query()->where('inviter', $this->record->id)
->where('invitee', '')
->whereNotNull('expired_at')
->where('expired_at', '>', Carbon::now())
->count();
}
2023-01-07 19:34:59 +08:00
private function buildChangeClassAction(): Actions\Action
{
return Actions\Action::make('change_class')
->label(__('admin.resources.user.actions.change_class_btn'))
->form([
Forms\Components\Select::make('class')
2023-01-10 17:25:53 +08:00
->options(User::listClass(User::CLASS_PEASANT, Auth::user()->class - 1))
2023-01-07 19:34:59 +08:00
->default($this->record->class)
->label(__('user.labels.class'))
->required()
->reactive()
,
Forms\Components\Radio::make('vip_added')
->options(self::getYesNoOptions('yes', 'no'))
->default($this->record->vip_added)
->label(__('user.labels.vip_added'))
->helperText(__('user.labels.vip_added_help'))
->hidden(fn (\Closure $get) => $get('class') != User::CLASS_VIP)
,
Forms\Components\DateTimePicker::make('vip_until')
->default($this->record->vip_until)
->label(__('user.labels.vip_until'))
->helperText(__('user.labels.vip_until_help'))
->hidden(fn (\Closure $get) => $get('class') != User::CLASS_VIP)
2023-01-07 19:34:59 +08:00
,
Forms\Components\TextInput::make('reason')
->label(__('admin.resources.user.actions.enable_disable_reason'))
->placeholder(__('admin.resources.user.actions.enable_disable_reason_placeholder'))
,
])
->action(function ($data) {
$userRep = $this->getRep();
try {
$userRep->changeClass(Auth::user(), $this->record, $data['class'], $data['reason'], $data);
2023-01-07 19:34:59 +08:00
$this->notify('success', 'Success!');
$this->emitSelf(self::EVENT_RECORD_UPDATED, $this->record->id);
} catch (\Exception $exception) {
$this->notify('danger', $exception->getMessage());
}
});
}
2022-06-28 13:33:18 +08:00
}