refactor(list/manage): uoj_form_v2

This commit is contained in:
Baoshuo Ren 2023-01-15 21:50:42 +08:00
parent 73155e6dc1
commit 13eeb37c19
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -25,13 +25,11 @@ 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');
$update_profile_form->addVInput( $update_profile_form->addInput('name', [
'name', 'label' => '标题',
'text', 'default_value' => UOJList::info('title'),
'标题', 'validator_php' => function ($title, &$vdata) {
UOJList::info('title'),
function ($title, &$vdata) {
if ($title == '') { if ($title == '') {
return '标题不能为空'; return '标题不能为空';
} }
@ -49,18 +47,23 @@ if ($cur_tab == 'profile') {
return ''; return '';
}, },
null ]);
); $update_profile_form->addCheckboxes('is_hidden', [
$update_profile_form->addVCheckboxes('is_hidden', [ 'div_class' => 'mt-3',
'0' => '公开', 'label' => '可见性',
'1' => '隐藏', 'label_class' => 'me-3',
], '可见性', UOJList::info('is_hidden')); 'select_class' => 'd-inline-block',
$update_profile_form->addVInput( 'option_div_class' => 'form-check d-inline-block ms-2',
'tags', 'default_value' => UOJList::info('is_hidden'),
'text', 'options' => [
'标签(多个标签用逗号隔开)', 0 => '公开',
implode(', ', UOJList::cur()->queryTags()), 1 => '隐藏',
function ($tags_str, &$vdata) { ],
]);
$update_profile_form->addInput('tags', [
'label' => '标签',
'default_value' => implode(', ', UOJList::cur()->queryTags()),
'validator_php' => function ($tags_str, &$vdata) {
$tags_raw = explode(',', str_replace('', ',', $tags_str)); $tags_raw = explode(',', str_replace('', ',', $tags_str));
$tags = []; $tags = [];
@ -90,14 +93,13 @@ if ($cur_tab == 'profile') {
return ''; return '';
}, },
null 'help' => '多个标签请使用逗号隔开。'
); ]);
$update_profile_form->addVTextArea( $update_profile_form->addTextArea('content_md', [
'content_md', 'label' => '描述',
'描述', 'default_value' => UOJList::cur()->queryContent()['content_md'],
UOJList::cur()->queryContent()['content_md'], 'validator_php' => function ($content_md, &$vdata) {
function ($content_md, &$vdata) { if (strlen($content_md) > 5000) {
if (strlen($content_md) > 3000) {
return '描述过长'; return '描述过长';
} }
@ -105,8 +107,7 @@ if ($cur_tab == 'profile') {
return ''; return '';
}, },
null ]);
);
$update_profile_form->handle = function ($vdata) { $update_profile_form->handle = function ($vdata) {
DB::update([ DB::update([
"update lists", "update lists",
@ -135,26 +136,25 @@ if ($cur_tab == 'profile') {
dieWithJsonData(['status' => 'success', 'message' => '修改成功']); dieWithJsonData(['status' => 'success', 'message' => '修改成功']);
}; };
$update_profile_form->setAjaxSubmit(<<<EOD $update_profile_form->setAjaxSubmit(<<<EOD
function(res) { function(res) {
if (res.status === 'success') { if (res.status === 'success') {
$('#result-alert') $('#result-alert')
.html('题单信息修改成功!') .html('题单信息修改成功!')
.addClass('alert-success') .addClass('alert-success')
.removeClass('alert-danger') .removeClass('alert-danger')
.show(); .show();
} else { } else {
$('#result-alert') $('#result-alert')
.html('题单信息修改失败。' + (res.message || '')) .html('题单信息修改失败。' + (res.message || ''))
.removeClass('alert-success') .removeClass('alert-success')
.addClass('alert-danger') .addClass('alert-danger')
.show(); .show();
} }
$(window).scrollTop(0); $(window).scrollTop(0);
} }
EOD); EOD);
$update_profile_form->submit_button_config['class_str'] = 'btn btn-secondary mt-3'; $update_profile_form->config['submit_button']['text'] = '更新';
$update_profile_form->submit_button_config['text'] = '更新';
$update_profile_form->runAtServer(); $update_profile_form->runAtServer();
} elseif ($cur_tab == 'problems') { } elseif ($cur_tab == 'problems') {
if (isset($_POST['submit-remove_problem']) && $_POST['submit-remove_problem'] == 'remove_problem') { if (isset($_POST['submit-remove_problem']) && $_POST['submit-remove_problem'] == 'remove_problem') {
@ -189,13 +189,10 @@ EOD);
], ],
]); ]);
$add_new_problem_form = new UOJBs4Form('add_new_problem'); $add_new_problem_form = new UOJForm('add_new_problem');
$add_new_problem_form->addVInput( $add_new_problem_form->addInput('problem_id', [
'problem_id', 'label' => '题目 ID',
'text', 'validator_php' => function ($problem_id, &$vdata) {
'题目 ID',
'',
function ($problem_id, &$vdata) {
$problem = UOJProblem::query($problem_id); $problem = UOJProblem::query($problem_id);
if (!$problem || !$problem->userCanView(Auth::user())) { if (!$problem || !$problem->userCanView(Auth::user())) {
@ -210,10 +207,8 @@ EOD);
return ''; return '';
}, },
null ]);
); $add_new_problem_form->config['submit_button']['text'] = '添加';
$add_new_problem_form->submit_button_config['class_str'] = 'btn btn-secondary mt-3';
$add_new_problem_form->submit_button_config['text'] = '添加';
$add_new_problem_form->handle = function ($vdata) { $add_new_problem_form->handle = function ($vdata) {
DB::insert([ DB::insert([
"insert into lists_problems", "insert into lists_problems",
@ -224,24 +219,24 @@ EOD);
dieWithJsonData(['status' => 'success', 'message' => '已将题目 #' . $vdata['problem']->info['id'] . ' 添加到题单 #' . UOJList::info('id') . ' 中']); dieWithJsonData(['status' => 'success', 'message' => '已将题目 #' . $vdata['problem']->info['id'] . ' 添加到题单 #' . UOJList::info('id') . ' 中']);
}; };
$add_new_problem_form->setAjaxSubmit(<<<EOD $add_new_problem_form->setAjaxSubmit(<<<EOD
function(res) { function(res) {
if (res.status === 'success') { if (res.status === 'success') {
$('#result-alert') $('#result-alert')
.html('题目添加成功!' + (res.message || '')) .html('题目添加成功!' + (res.message || ''))
.addClass('alert-success') .addClass('alert-success')
.removeClass('alert-danger') .removeClass('alert-danger')
.show(); .show();
} else { } else {
$('#result-alert') $('#result-alert')
.html('题目添加失败。' + (res.message || '')) .html('题目添加失败。' + (res.message || ''))
.removeClass('alert-success') .removeClass('alert-success')
.addClass('alert-danger') .addClass('alert-danger')
.show(); .show();
} }
$(window).scrollTop(0); $(window).scrollTop(0);
} }
EOD); EOD);
$add_new_problem_form->runAtServer(); $add_new_problem_form->runAtServer();
} }
?> ?>