refactor(contest/manage): managers_form

This commit is contained in:
Baoshuo Ren 2023-02-03 20:49:03 +08:00
parent f9c5f28fe3
commit d2d195af21
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
3 changed files with 62 additions and 151 deletions

View File

@ -256,82 +256,36 @@ function(res) {
EOD); EOD);
$add_problem_form->runAtServer(); $add_problem_form->runAtServer();
} elseif ($cur_tab == 'managers') { } elseif ($cur_tab == 'managers') {
if (isset($_POST['submit-remove_manager']) && $_POST['submit-remove_manager'] == 'remove_manager') { $managers_form = newAddDelCmdForm(
$username = $_POST['username']; 'managers',
$user = UOJUser::query($username); 'validateUserAndStoreByUsername',
function ($type, $username, &$vdata) {
$user = $vdata['user'][$username];
if (!$user) { if ($type == '+') {
dieWithAlert('用户不存在。'); DB::insert([
} "insert into contests_permissions",
"(contest_id, username)",
if (!UOJContest::cur()->userCanManage($user)) { "values", DB::tuple([
dieWithAlert('用户不是这场比赛的管理员。'); UOJContest::info('id'), $user['username']
} ])
]);
DB::delete([ } else if ($type == '-') {
"delete from contests_permissions", DB::delete([
"where", [ "delete from contests_permissions",
"contest_id" => $contest['id'], "where", [
"username" => $user['username'], "contest_id" => UOJContest::info('id'),
] "username" => $user['username']
]); ]
dieWithAlert('移除成功!'); ]);
} }
},
$add_manager_form = new UOJForm('add_manager'); null,
$add_manager_form->addInput(
'username',
[ [
'label' => '用户名', 'help' => '命令格式:命令一行一个,<code>+mike</code> 表示把 <code>mike</code> 加入管理者,<code>-mike</code> 表示把 <code>mike</code> 从管理者中移除。',
'validator_php' => function ($username, &$vdata) {
$user = UOJUser::query($username);
if (!$user) {
return '用户不存在';
}
if (UOJContest::cur()->userCanManage($user)) {
return '用户已经是这场比赛的管理员';
}
$vdata['username'] = $username;
return '';
},
] ]
); );
$add_manager_form->handle = function (&$vdata) { $managers_form->runAtServer();
DB::insert([
"insert into contests_permissions",
DB::bracketed_fields(["contest_id", "username"]),
"values",
DB::tuple([UOJContest::info('id'), $vdata['username']])
]);
dieWithJsonData(['status' => 'success', 'message' => '已将用户名为 ' . $vdata['username'] . ' 的用户设置为本场比赛的管理者。']);
};
$add_manager_form->config['submit_button']['text'] = '添加';
$add_manager_form->config['submit_button']['class'] = 'btn btn-secondary mt-3';
$add_manager_form->setAjaxSubmit(<<<EOD
function(res) {
if (res.status === 'success') {
$('#result-alert')
.html('添加成功!' + (res.message || ''))
.addClass('alert-success')
.removeClass('alert-danger')
.show();
} else {
$('#result-alert')
.html('添加失败。' + (res.message || ''))
.removeClass('alert-success')
.addClass('alert-danger')
.show();
}
$(window).scrollTop(0);
}
EOD);
$add_manager_form->runAtServer();
} elseif ($cur_tab == 'others') { } elseif ($cur_tab == 'others') {
$rule_form = new UOJForm('basic_rule'); $rule_form = new UOJForm('basic_rule');
$rule_form->addSelect('basic_rule', [ $rule_form->addSelect('basic_rule', [
@ -531,81 +485,34 @@ EOD);
</div> </div>
<?php elseif ($cur_tab == 'managers') : ?> <?php elseif ($cur_tab == 'managers') : ?>
<div class="card mt-3 mt-md-0"> <div class="card mt-3 mt-md-0">
<div class="card-header"> <div class="card-body">
<ul class="nav nav-tabs card-header-tabs" role="tablist"> <?php
<li class="nav-item"> echoLongTable(
<a class="nav-link active" href="#managers" data-bs-toggle="tab" data-bs-target="#managers">管理者列表</a> ['*'],
</li> 'contests_permissions',
<li class="nav-item"> "contest_id = {$contest['id']}",
<a class="nav-link" href="#add-manager" data-bs-toggle="tab" data-bs-target="#add-manager">添加管理者</a> 'ORDER BY username',
</li> <<<EOD
</ul> <tr>
</div> <th>用户名</th>
<div class="card-body tab-content"> </tr>
<div class="tab-pane active" id="managers"> EOD,
<?php function ($row) {
echoLongTable( $user = UOJUser::query($row['username']);
['*'],
'contests_permissions',
"contest_id = {$contest['id']}",
'ORDER BY username',
<<<EOD
<tr>
<th>用户名</th>
<th style="width:6em">操作</th>
</tr>
EOD,
function ($row) {
$user = UOJUser::query($row['username']);
echo HTML::tag_begin('tr'); echo HTML::tag_begin('tr');
echo HTML::tag('td', [], UOJUser::getLink($user)); echo HTML::tag('td', [], UOJUser::getLink($user));
echo HTML::tag('td', [], [ echo HTML::tag_end('tr');
HTML::tag('form', [ },
'method' => 'POST', [
'target' => '_self', 'echo_full' => true,
'class' => 'd-inline-block', 'div_classes' => ['table-responsive'],
'onsubmit' => "return confirm('你确定要将 {$user['username']} 从比赛管理员列表中移除吗?');", 'table_classes' => ['table'],
], [ ]
HTML::hiddenToken(), );
HTML::empty_tag('input', [ ?>
'type' => 'hidden',
'name' => 'username', <?php $managers_form->printHTML() ?>
'value' => $user['username'],
]),
HTML::tag('button', [
'type' => 'submit',
'class' => 'btn btn-link text-danger text-decoration-none p-0',
'name' => 'submit-remove_manager',
'value' => 'remove_manager',
], '移除'),
]),
]);
echo HTML::tag_end('tr');
},
[
'echo_full' => true,
'div_classes' => ['table-responsive'],
'table_classes' => ['table'],
]
);
?>
</div>
<div class="tab-pane" id="add-manager">
<div id="result-alert" class="alert" role="alert" style="display: none"></div>
<div class="row row-cols-1 row-cols-md-2">
<div class="col">
<?php $add_manager_form->printHTML(); ?>
</div>
<div class="col mt-3 mt-md-0">
<h5>注意事项</h5>
<ul class="mb-0">
<li>添加管理者前请确认用户名是否正确以免带来不必要的麻烦。</li>
<li>比赛管理者如果报名了比赛仍可以正常参赛,但不报名比赛也可以查看并管理本场比赛。</li>
</ul>
</div>
</div>
</div>
</div> </div>
</div> </div>
<?php elseif ($cur_tab == 'others') : ?> <?php elseif ($cur_tab == 'others') : ?>

View File

@ -46,7 +46,11 @@ $managers_form = newAddDelCmdForm(
] ]
]); ]);
} }
} },
null,
[
'help' => '命令格式:命令一行一个,<code>+mike</code> 表示把 <code>mike</code> 加入管理者,<code>-mike</code> 表示把 <code>mike</code> 从管理者中移除。',
]
); );
$managers_form->runAtServer(); $managers_form->runAtServer();

View File

@ -1,13 +1,13 @@
<?php <?php
define('SCRIPT_REFRESH_AS_GET', '<script>;window.location = window.location.origin + window.location.pathname + (window.location.search.length ? window.location.search + "&" : "?") + "_refresh_" + (+new Date()) + "=" + (+new Date()) + window.location.hash;</script>'); define('SCRIPT_REFRESH_AS_GET', '<script>;window.location = window.location.origin + window.location.pathname + (window.location.search.length ? window.location.search + "&" : "?") + "_refresh_" + (+new Date()) + "=" + (+new Date()) + window.location.hash;</script>');
function newAddDelCmdForm($form_name, $validate, $handle, $final = null) { function newAddDelCmdForm($form_name, $validate, $handle, $final = null, $cfg = []) {
$form = new UOJForm($form_name); $form = new UOJForm($form_name);
$form->addTextArea("{$form_name}_cmds", [ $form->addTextArea("{$form_name}_cmds", [
'label' => '命令', 'label' => '命令',
'input_class' => 'form-control font-monospace', 'input_class' => 'form-control font-monospace',
'validator_php' => function ($str, &$vdata) use ($validate) { 'validator_php' => function ($str, &$vdata) use ($validate) {
$cmds = array(); $cmds = [];
foreach (explode("\n", $str) as $line_id => $raw_line) { foreach (explode("\n", $str) as $line_id => $raw_line) {
$line = trim($raw_line); $line = trim($raw_line);
if ($line == '') { if ($line == '') {
@ -26,8 +26,8 @@ function newAddDelCmdForm($form_name, $validate, $handle, $final = null) {
$vdata['cmds'] = $cmds; $vdata['cmds'] = $cmds;
return ''; return '';
}, },
]); ] + $cfg);
if (!isset($final)) { if (!isset($final) || !$final) {
$form->handle = function (&$vdata) use ($handle) { $form->handle = function (&$vdata) use ($handle) {
foreach ($vdata['cmds'] as $cmd) { foreach ($vdata['cmds'] as $cmd) {
$handle($cmd['type'], $cmd['obj'], $vdata); $handle($cmd['type'], $cmd['obj'], $vdata);