refactor(group/manage): uoj_form_v2

This commit is contained in:
Baoshuo Ren 2023-01-15 08:48:08 +08:00
parent 87013054c4
commit d992a07e96
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
2 changed files with 144 additions and 106 deletions

View File

@ -31,13 +31,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' => UOJGroup::info('title'),
'名称', 'validator_php' => function ($title, &$vdata) {
UOJGroup::info('title'),
function ($title, &$vdata) {
if ($title == '') { if ($title == '') {
return '名称不能为空'; return '名称不能为空';
} }
@ -55,17 +53,26 @@ 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',
], '可见性', UOJGroup::info('is_hidden')); 'options' => [
$update_profile_form->addVTextArea( 0 => '公开',
'announcement', 1 => '隐藏',
'公告', ],
UOJGroup::info('announcement'), 'select_class' => 'd-inline-block',
function ($announcement, &$vdata) { 'option_div_class' => 'form-check d-inline-block ms-2',
'default_value' => UOJGroup::info('is_hidden'),
]);
$update_profile_form->addTextArea('announcement', [
'div_class' => 'mt-3',
'label' => '公告',
'input_class' => 'form-control font-monospace',
'default_value' => UOJGroup::info('announcement'),
'help' => '公告支持 Markdown 语法。',
'validator_php' => function ($announcement, &$vdata) {
if (strlen($announcement) > 3000) { if (strlen($announcement) > 3000) {
return '公告过长'; return '公告过长';
} }
@ -74,8 +81,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 `groups`", "update `groups`",
@ -92,26 +98,26 @@ 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']['class'] = 'btn btn-secondary';
$update_profile_form->submit_button_config['text'] = '更新'; $update_profile_form->config['submit_button']['text'] = '更新';
$update_profile_form->runAtServer(); $update_profile_form->runAtServer();
} elseif ($cur_tab == 'assignments') { } elseif ($cur_tab == 'assignments') {
if (isset($_POST['submit-remove_assignment']) && $_POST['submit-remove_assignment'] == 'remove_assignment') { if (isset($_POST['submit-remove_assignment']) && $_POST['submit-remove_assignment'] == 'remove_assignment') {
@ -133,13 +139,10 @@ EOD);
dieWithAlert('移除成功!'); dieWithAlert('移除成功!');
} }
$add_new_assignment_form = new UOJBs4Form('add_new_assignment'); $add_new_assignment_form = new UOJForm('add_new_assignment');
$add_new_assignment_form->addVInput( $add_new_assignment_form->addInput('new_assignment_list_id', [
'new_assignment_list_id', 'label' => '题单 ID',
'text', 'validator_php' => function ($list_id, &$vdata) {
'题单 ID',
'',
function ($list_id, &$vdata) {
if (!validateUInt($list_id)) { if (!validateUInt($list_id)) {
return '题单 ID 不合法'; return '题单 ID 不合法';
} }
@ -162,17 +165,11 @@ EOD);
return ''; return '';
}, },
null ]);
); $add_new_assignment_form->addInput('new_assignment_end_time', [
$default_end_time = new DateTime(); 'label' => '截止时间',
$default_end_time->setTime(22, 30, 0); 'default_value' => UOJTime::time2str((new DateTime())->add(new DateInterval("P7D"))->setTime(22, 30, 0)),
$default_end_time->add(new DateInterval("P7D")); 'validator_php' => function ($end_time, &$vdata) {
$add_new_assignment_form->addVInput(
'new_assignment_end_time',
'text',
'截止时间',
$default_end_time->format('Y-m-d H:i'),
function ($end_time, &$vdata) {
try { try {
$vdata['end_time'] = new DateTime($end_time); $vdata['end_time'] = new DateTime($end_time);
} catch (Exception $e) { } catch (Exception $e) {
@ -181,8 +178,7 @@ EOD);
return ''; return '';
}, },
null ]);
);
$add_new_assignment_form->handle = function (&$vdata) { $add_new_assignment_form->handle = function (&$vdata) {
DB::insert([ DB::insert([
"insert into groups_assignments", "insert into groups_assignments",
@ -199,27 +195,27 @@ EOD);
'message' => '题单 #' . $vdata['list_id'] . ' 已经被添加到作业列表中,结束时间为 ' . $vdata['end_time']->format('Y-m-d H:i:s') . '。' 'message' => '题单 #' . $vdata['list_id'] . ' 已经被添加到作业列表中,结束时间为 ' . $vdata['end_time']->format('Y-m-d H:i:s') . '。'
]); ]);
}; };
$add_new_assignment_form->submit_button_config['class_str'] = 'btn btn-secondary mt-3'; $add_new_assignment_form->config['submit_button']['class'] = 'btn btn-secondary';
$add_new_assignment_form->submit_button_config['text'] = '添加'; $add_new_assignment_form->config['submit_button']['text'] = '添加';
$add_new_assignment_form->setAjaxSubmit(<<<EOD $add_new_assignment_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_assignment_form->runAtServer(); $add_new_assignment_form->runAtServer();
$hidden_time = new DateTime(); $hidden_time = new DateTime();
@ -247,13 +243,10 @@ EOD);
dieWithAlert('移除成功!'); dieWithAlert('移除成功!');
} }
$add_new_user_form = new UOJBs4Form('add_new_user'); $add_new_user_form = new UOJForm('add_new_user');
$add_new_user_form->addVInput( $add_new_user_form->addInput('new_username', [
'new_username', 'label' => '用户名',
'text', 'validator_php' => function ($username, &$vdata) {
'用户名',
'',
function ($username, &$vdata) {
$user = UOJUser::query($username); $user = UOJUser::query($username);
if (!$user) { if (!$user) {
@ -268,10 +261,9 @@ EOD);
return ''; return '';
}, },
null ]);
); $add_new_user_form->config['submit_button']['class'] = 'btn btn-secondary';
$add_new_user_form->submit_button_config['class_str'] = 'btn btn-secondary mt-3'; $add_new_user_form->config['submit_button']['text'] = '添加';
$add_new_user_form->submit_button_config['text'] = '添加';
$add_new_user_form->handle = function (&$vdata) { $add_new_user_form->handle = function (&$vdata) {
DB::insert([ DB::insert([
"insert into groups_users", "insert into groups_users",
@ -286,24 +278,24 @@ EOD);
dieWithJsonData(['status' => 'success', 'message' => '已将用户名为 ' . $vdata['username'] . ' 的用户添加到本小组。']); dieWithJsonData(['status' => 'success', 'message' => '已将用户名为 ' . $vdata['username'] . ' 的用户添加到本小组。']);
}; };
$add_new_user_form->setAjaxSubmit(<<<EOD $add_new_user_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_user_form->runAtServer(); $add_new_user_form->runAtServer();
} }
?> ?>

View File

@ -167,6 +167,52 @@ class UOJForm {
$this->add($name, $html, $config['validator_php'], $config['validator_js']); $this->add($name, $html, $config['validator_php'], $config['validator_js']);
} }
public function addTextarea($name, $config) {
$config += [
'type' => 'text',
'div_class' => '',
'input_class' => 'form-control',
'default_value' => '',
'label' => '',
'label_class' => 'form-label',
'placeholder' => '',
'help' => '',
'help_class' => 'form-text',
'validator_php' => function ($x) {
return '';
},
'validator_js' => null,
];
$html = '';
$html .= HTML::tag_begin('div', ['class' => $config['div_class'], 'id' => "div-$name"]);
if ($config['label']) {
$html .= HTML::tag('label', [
'class' => $config['label_class'],
'for' => "input-$name",
'id' => "label-$name"
], $config['label']);
}
$html .= HTML::tag('textarea', [
'class' => $config['input_class'],
'type' => $config['type'],
'name' => $name,
'id' => "input-$name",
'placeholder' => $config['placeholder'],
], HTML::escape($config['default_value']));
$html .= HTML::tag('div', ['class' => 'invalid-feedback', 'id' => "help-$name"], '');
if ($config['help']) {
$html .= HTML::tag('div', ['class' => $config['help_class']], $config['help']);
}
$html .= HTML::tag_end('div');
$this->add($name, $html, $config['validator_php'], $config['validator_js']);
}
public function addCheckbox($name, $config) { public function addCheckbox($name, $config) {
$config += [ $config += [
'checked' => false, 'checked' => false,