refactor(web/user/edit): update_profile_form

This commit is contained in:
Baoshuo Ren 2022-12-24 12:07:06 +08:00
parent 1ce6ddd8cb
commit 1ac27d40fe

View File

@ -37,7 +37,7 @@ if (!isset($tabs_info[$cur_tab])) {
} }
if ($cur_tab == 'profile') { if ($cur_tab == 'profile') {
$update_profile_form = new UOJBs4Form('update_profile'); $update_profile_form = new UOJForm('update_profile');
$username = UOJLocale::get('username'); $username = UOJLocale::get('username');
$avatar = UOJLocale::get('avatar'); $avatar = UOJLocale::get('avatar');
$update_profile_form->appendHTML(<<<EOD $update_profile_form->appendHTML(<<<EOD
@ -48,17 +48,18 @@ if ($cur_tab == 'profile') {
</div> </div>
EOD); EOD);
if (isSuperUser(Auth::user())) { if (isSuperUser(Auth::user())) {
$update_profile_form->addVInput( $update_profile_form->addInput(
'realname', 'realname',
'text', [
UOJLocale::get('user::real name'), 'div_class' => 'mb-3',
$user['realname'], 'label' => UOJLocale::get('user::real name'),
function ($realname, &$vdata) { 'default_value' => $user['realname'],
'validator_php' => function ($realname, &$vdata) {
$vdata['realname'] = $realname; $vdata['realname'] = $realname;
return ''; return '';
}, },
null ]
); );
} else { } else {
$real_name = UOJLocale::get('user::real name'); $real_name = UOJLocale::get('user::real name');
@ -72,12 +73,13 @@ EOD);
} }
if (isTmpUser($user)) { if (isTmpUser($user)) {
if (isSuperUser(Auth::user())) { if (isSuperUser(Auth::user())) {
$update_profile_form->addVInput( $update_profile_form->addInput(
'expiration_time', 'expiration_time',
'text', [
UOJLocale::get('user::expiration time'), 'div_class' => 'mb-3',
$user['expiration_time'], 'label' => UOJLocale::get('user::expiration time'),
function ($str, &$vdata) { 'default_value' => $user['expiration_time'],
'validator_php' => function ($str, &$vdata) {
try { try {
$vdata['expiration_time'] = new DateTime($str); $vdata['expiration_time'] = new DateTime($str);
} catch (Exception $e) { } catch (Exception $e) {
@ -86,7 +88,7 @@ EOD);
return ''; return '';
}, },
null ]
); );
} else { } else {
$expiration_time = UOJLocale::get('user::expiration time'); $expiration_time = UOJLocale::get('user::expiration time');
@ -98,23 +100,40 @@ EOD);
</div> </div>
EOD); EOD);
} }
} } else {
$update_profile_form->addVCheckboxes('avatar_source', [ $expiration_time = UOJLocale::get('user::expiration time');
'gravatar' => 'Gravatar', $expiration_help_text = isSuperUser(Auth::user())
'qq' => 'QQ', ? '只有用户组别为「临时用户」的用户才能被修改过期时间。'
], UOJLocale::get('user::avatar source'), $extra['avatar_source'] ?: 'gravatar'); : '只有管理员才能修改用户的账号过期时间。';
$change_avatar_help = UOJLocale::get('change avatar help');
$update_profile_form->appendHTML(<<<EOD $update_profile_form->appendHTML(<<<EOD
<div style="margin-top: -1.25rem;" class="mb-3 small text-muted"> <div class="mb-3">
$change_avatar_help <label for="input-expiration_time" class="form-label">$expiration_time</label>
<input type="text" class="form-control" id="input-expiration_time" aria-describedby="help-expiration_time" value="永不过期" disabled>
<div id="help-expiration_time" class="form-text">$expiration_help_text</div>
</div> </div>
EOD); EOD);
$update_profile_form->addVInput( }
$update_profile_form->addCheckboxes('avatar_source', [
'div_class' => 'mb-3',
'label' => UOJLocale::get('user::avatar source'),
'label_class' => 'me-3',
'default_value' => $extra['avatar_source'] ?: 'gravatar',
'select_class' => 'd-inline-block',
'option_div_class' => 'form-check d-inline-block ms-2',
'options' => [
'gravatar' => 'Gravatar',
'qq' => 'QQ',
],
'help' => UOJLocale::get('change avatar help'),
]);
$update_profile_form->addInput(
'email', 'email',
'email', [
UOJLocale::get('email'), 'div_class' => 'mb-3',
$user['email'] ?: '', 'type' => 'email',
function ($email, &$vdata) { 'label' => UOJLocale::get('email'),
'default_value' => $user['email'] ?: '',
'validator_php' => function ($email, &$vdata) {
if (!validateEmail($email)) { if (!validateEmail($email)) {
return 'Email 格式不合法。'; return 'Email 格式不合法。';
} }
@ -123,14 +142,15 @@ EOD);
return ''; return '';
}, },
null ]
); );
$update_profile_form->addVInput( $update_profile_form->addInput(
'qq', 'qq',
'text', [
UOJLocale::get('QQ'), 'div_class' => 'mb-3',
$user['qq'] == 0 ? '' : $user['qq'], 'label' => UOJLocale::get('QQ'),
function ($qq, &$vdata) { 'default_value' => $user['qq'] == 0 ? '' : $user['qq'],
'validator_php' => function ($qq, &$vdata) {
if ($qq && !validateQQ($qq)) { if ($qq && !validateQQ($qq)) {
return 'QQ 格式不合法。'; return 'QQ 格式不合法。';
} }
@ -139,14 +159,15 @@ EOD);
return ''; return '';
}, },
null ]
); );
$update_profile_form->addVInput( $update_profile_form->addInput(
'github', 'github',
'text', [
'GitHub', 'div_class' => 'mb-3',
$extra['social']['github'] ?: '', 'label' => 'GitHub',
function ($github, &$vdata) { 'default_value' => $extra['social']['github'] ?: '',
'validator_php' => function ($github, &$vdata) {
if ($github && !validateGitHubUsername($github)) { if ($github && !validateGitHubUsername($github)) {
return 'GitHub 用户名不合法。'; return 'GitHub 用户名不合法。';
} }
@ -155,20 +176,21 @@ EOD);
return ''; return '';
}, },
null ]
); );
if (isSuperUser(Auth::user())) { if (isSuperUser(Auth::user())) {
$update_profile_form->addVInput( $update_profile_form->addInput(
'school', 'school',
'text', [
UOJLocale::get('school'), 'div_class' => 'mb-3',
$user['school'] ?: '', 'label' => UOJLocale::get('school'),
function ($school, &$vdata) { 'default_value' => $user['school'] ?: '',
'validator_php' => function ($school, &$vdata) {
$vdata['school'] = $school; $vdata['school'] = $school;
return ''; return '';
}, },
null ]
); );
} else { } else {
$school = UOJLocale::get('school'); $school = UOJLocale::get('school');
@ -180,17 +202,26 @@ EOD);
</div> </div>
EOD); EOD);
} }
$update_profile_form->addVCheckboxes('sex', [ $update_profile_form->addCheckboxes('sex', [
'div_class' => 'mb-3',
'label' => UOJLocale::get('sex'),
'label_class' => 'me-3',
'default_value' => $user['sex'],
'select_class' => 'd-inline-block',
'option_div_class' => 'form-check d-inline-block ms-2',
'options' => [
'U' => UOJLocale::get('refuse to answer'), 'U' => UOJLocale::get('refuse to answer'),
'M' => UOJLocale::get('male'), 'M' => UOJLocale::get('male'),
'F' => UOJLocale::get('female'), 'F' => UOJLocale::get('female'),
], UOJLocale::get('sex'), $user['sex']); ],
$update_profile_form->addVInput( ]);
$update_profile_form->addInput(
'motto', 'motto',
'text', [
UOJLocale::get('motto'), 'div_class' => 'mb-3',
$user['motto'] ?: '', 'label' => UOJLocale::get('motto'),
function ($motto, &$vdata) { 'default_value' => $user['motto'] ?: '',
'validator_php' => function ($motto, &$vdata) {
if (!validateMotto($motto)) { if (!validateMotto($motto)) {
return '格言格式不合法'; return '格言格式不合法';
} }
@ -199,14 +230,15 @@ EOD);
return ''; return '';
}, },
null ]
); );
$update_profile_form->addVInput( $update_profile_form->addInput(
'codeforces', 'codeforces',
'text', [
UOJLocale::get('codeforces handle'), 'div_class' => 'mb-3',
$extra['social']['codeforces'] ?: '', 'label' => UOJLocale::get('codeforces handle'),
function ($codeforces, &$vdata) { 'default_value' => $extra['social']['codeforces'] ?: '',
'validator_php' => function ($codeforces, &$vdata) {
if ($codeforces && !validateUsername($codeforces)) { if ($codeforces && !validateUsername($codeforces)) {
return 'Codeforces 用户名格式不合法。'; return 'Codeforces 用户名格式不合法。';
} }
@ -215,14 +247,15 @@ EOD);
return ''; return '';
}, },
null ]
); );
$update_profile_form->addVInput( $update_profile_form->addInput(
'website', 'website',
'text', [
UOJLocale::get('user::website'), 'div_class' => 'mb-3',
$extra['social']['website'] ?: '', 'label' => UOJLocale::get('user::website'),
function ($url, &$vdata) { 'default_value' => $extra['social']['website'] ?: '',
'validator_php' => function ($url, &$vdata) {
if ($url && !validateURL($url)) { if ($url && !validateURL($url)) {
return '链接格式不合法。'; return '链接格式不合法。';
} }
@ -231,7 +264,7 @@ EOD);
return ''; return '';
}, },
null ]
); );
$update_profile_form->handle = function (&$vdata) use ($user) { $update_profile_form->handle = function (&$vdata) use ($user) {
$data = [ $data = [
@ -276,8 +309,9 @@ EOD);
dieWithJsonData(['status' => 'success']); dieWithJsonData(['status' => 'success']);
}; };
$update_profile_form->submit_button_config['class_str'] = 'btn btn-secondary mt-3'; $update_profile_form->config['submit_container']['class'] = 'text-center mt-3';
$update_profile_form->submit_button_config['text'] = '更新'; $update_profile_form->config['submit_button']['class'] = 'btn btn-secondary';
$update_profile_form->config['submit_button']['text'] = '更新';
$update_profile_form->setAjaxSubmit(<<<EOD $update_profile_form->setAjaxSubmit(<<<EOD
function(res) { function(res) {
if (res.status === 'success') { if (res.status === 'success') {