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'],
$vdata['realname'] = $realname; 'validator_php' => function ($realname, &$vdata) {
$vdata['realname'] = $realname;
return ''; return '';
}, },
null ]
); );
} else { } else {
$real_name = UOJLocale::get('user::real name'); $real_name = UOJLocale::get('user::real name');
@ -72,21 +73,22 @@ 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'],
try { 'validator_php' => function ($str, &$vdata) {
$vdata['expiration_time'] = new DateTime($str); try {
} catch (Exception $e) { $vdata['expiration_time'] = new DateTime($str);
return '无效时间格式'; } catch (Exception $e) {
} return '无效时间格式';
}
return ''; return '';
}, },
null ]
); );
} else { } else {
$expiration_time = UOJLocale::get('user::expiration time'); $expiration_time = UOJLocale::get('user::expiration time');
@ -98,77 +100,97 @@ 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 class="mb-3">
<div style="margin-top: -1.25rem;" class="mb-3 small text-muted"> <label for="input-expiration_time" class="form-label">$expiration_time</label>
$change_avatar_help <input type="text" class="form-control" id="input-expiration_time" aria-describedby="help-expiration_time" value="永不过期" disabled>
</div> <div id="help-expiration_time" class="form-text">$expiration_help_text</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'),
if (!validateEmail($email)) { 'default_value' => $user['email'] ?: '',
return 'Email 格式不合法。'; 'validator_php' => function ($email, &$vdata) {
} if (!validateEmail($email)) {
return 'Email 格式不合法。';
}
$vdata['email'] = $email; $vdata['email'] = $email;
return '';
},
null
);
$update_profile_form->addVInput(
'qq',
'text',
UOJLocale::get('QQ'),
$user['qq'] == 0 ? '' : $user['qq'],
function ($qq, &$vdata) {
if ($qq && !validateQQ($qq)) {
return 'QQ 格式不合法。';
}
$vdata['qq'] = $qq;
return '';
},
null
);
$update_profile_form->addVInput(
'github',
'text',
'GitHub',
$extra['social']['github'] ?: '',
function ($github, &$vdata) {
if ($github && !validateGitHubUsername($github)) {
return 'GitHub 用户名不合法。';
}
$vdata['github'] = $github;
return '';
},
null
);
if (isSuperUser(Auth::user())) {
$update_profile_form->addVInput(
'school',
'text',
UOJLocale::get('school'),
$user['school'] ?: '',
function ($school, &$vdata) {
$vdata['school'] = $school;
return ''; return '';
}, },
null ]
);
$update_profile_form->addInput(
'qq',
[
'div_class' => 'mb-3',
'label' => UOJLocale::get('QQ'),
'default_value' => $user['qq'] == 0 ? '' : $user['qq'],
'validator_php' => function ($qq, &$vdata) {
if ($qq && !validateQQ($qq)) {
return 'QQ 格式不合法。';
}
$vdata['qq'] = $qq;
return '';
},
]
);
$update_profile_form->addInput(
'github',
[
'div_class' => 'mb-3',
'label' => 'GitHub',
'default_value' => $extra['social']['github'] ?: '',
'validator_php' => function ($github, &$vdata) {
if ($github && !validateGitHubUsername($github)) {
return 'GitHub 用户名不合法。';
}
$vdata['github'] = $github;
return '';
},
]
);
if (isSuperUser(Auth::user())) {
$update_profile_form->addInput(
'school',
[
'div_class' => 'mb-3',
'label' => UOJLocale::get('school'),
'default_value' => $user['school'] ?: '',
'validator_php' => function ($school, &$vdata) {
$vdata['school'] = $school;
return '';
},
]
); );
} else { } else {
$school = UOJLocale::get('school'); $school = UOJLocale::get('school');
@ -180,58 +202,69 @@ EOD);
</div> </div>
EOD); EOD);
} }
$update_profile_form->addVCheckboxes('sex', [ $update_profile_form->addCheckboxes('sex', [
'U' => UOJLocale::get('refuse to answer'), 'div_class' => 'mb-3',
'M' => UOJLocale::get('male'), 'label' => UOJLocale::get('sex'),
'F' => UOJLocale::get('female'), 'label_class' => 'me-3',
], UOJLocale::get('sex'), $user['sex']); 'default_value' => $user['sex'],
$update_profile_form->addVInput( 'select_class' => 'd-inline-block',
'option_div_class' => 'form-check d-inline-block ms-2',
'options' => [
'U' => UOJLocale::get('refuse to answer'),
'M' => UOJLocale::get('male'),
'F' => UOJLocale::get('female'),
],
]);
$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'] ?: '',
if (!validateMotto($motto)) { 'validator_php' => function ($motto, &$vdata) {
return '格言格式不合法'; if (!validateMotto($motto)) {
} return '格言格式不合法';
}
$vdata['motto'] = $motto; $vdata['motto'] = $motto;
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'] ?: '',
if ($codeforces && !validateUsername($codeforces)) { 'validator_php' => function ($codeforces, &$vdata) {
return 'Codeforces 用户名格式不合法。'; if ($codeforces && !validateUsername($codeforces)) {
} return 'Codeforces 用户名格式不合法。';
}
$vdata['codeforces'] = $codeforces; $vdata['codeforces'] = $codeforces;
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'] ?: '',
if ($url && !validateURL($url)) { 'validator_php' => function ($url, &$vdata) {
return '链接格式不合法。'; if ($url && !validateURL($url)) {
} return '链接格式不合法。';
}
$vdata['website'] = $url; $vdata['website'] = $url;
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') {