mirror of
https://github.com/renbaoshuo/S2OJ.git
synced 2024-11-14 03:38:41 +00:00
refactor(contest/manage): managers_form
This commit is contained in:
parent
f9c5f28fe3
commit
d2d195af21
@ -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) {
|
||||||
if (!$user) {
|
$user = $vdata['user'][$username];
|
||||||
dieWithAlert('用户不存在。');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!UOJContest::cur()->userCanManage($user)) {
|
|
||||||
dieWithAlert('用户不是这场比赛的管理员。');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if ($type == '+') {
|
||||||
|
DB::insert([
|
||||||
|
"insert into contests_permissions",
|
||||||
|
"(contest_id, username)",
|
||||||
|
"values", DB::tuple([
|
||||||
|
UOJContest::info('id'), $user['username']
|
||||||
|
])
|
||||||
|
]);
|
||||||
|
} else if ($type == '-') {
|
||||||
DB::delete([
|
DB::delete([
|
||||||
"delete from contests_permissions",
|
"delete from contests_permissions",
|
||||||
"where", [
|
"where", [
|
||||||
"contest_id" => $contest['id'],
|
"contest_id" => UOJContest::info('id'),
|
||||||
"username" => $user['username'],
|
"username" => $user['username']
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
dieWithAlert('移除成功!');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$add_manager_form = new UOJForm('add_manager');
|
|
||||||
$add_manager_form->addInput(
|
|
||||||
'username',
|
|
||||||
[
|
|
||||||
'label' => '用户名',
|
|
||||||
'validator_php' => function ($username, &$vdata) {
|
|
||||||
$user = UOJUser::query($username);
|
|
||||||
|
|
||||||
if (!$user) {
|
|
||||||
return '用户不存在';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (UOJContest::cur()->userCanManage($user)) {
|
|
||||||
return '用户已经是这场比赛的管理员';
|
|
||||||
}
|
|
||||||
|
|
||||||
$vdata['username'] = $username;
|
|
||||||
|
|
||||||
return '';
|
|
||||||
},
|
},
|
||||||
|
null,
|
||||||
|
[
|
||||||
|
'help' => '命令格式:命令一行一个,<code>+mike</code> 表示把 <code>mike</code> 加入管理者,<code>-mike</code> 表示把 <code>mike</code> 从管理者中移除。',
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
$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,18 +485,7 @@ 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">
|
|
||||||
<li class="nav-item">
|
|
||||||
<a class="nav-link active" href="#managers" data-bs-toggle="tab" data-bs-target="#managers">管理者列表</a>
|
|
||||||
</li>
|
|
||||||
<li class="nav-item">
|
|
||||||
<a class="nav-link" href="#add-manager" data-bs-toggle="tab" data-bs-target="#add-manager">添加管理者</a>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
<div class="card-body tab-content">
|
|
||||||
<div class="tab-pane active" id="managers">
|
|
||||||
<?php
|
<?php
|
||||||
echoLongTable(
|
echoLongTable(
|
||||||
['*'],
|
['*'],
|
||||||
@ -552,7 +495,6 @@ EOD);
|
|||||||
<<<EOD
|
<<<EOD
|
||||||
<tr>
|
<tr>
|
||||||
<th>用户名</th>
|
<th>用户名</th>
|
||||||
<th style="width:6em">操作</th>
|
|
||||||
</tr>
|
</tr>
|
||||||
EOD,
|
EOD,
|
||||||
function ($row) {
|
function ($row) {
|
||||||
@ -560,27 +502,6 @@ EOD);
|
|||||||
|
|
||||||
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', [], [
|
|
||||||
HTML::tag('form', [
|
|
||||||
'method' => 'POST',
|
|
||||||
'target' => '_self',
|
|
||||||
'class' => 'd-inline-block',
|
|
||||||
'onsubmit' => "return confirm('你确定要将 {$user['username']} 从比赛管理员列表中移除吗?');",
|
|
||||||
], [
|
|
||||||
HTML::hiddenToken(),
|
|
||||||
HTML::empty_tag('input', [
|
|
||||||
'type' => 'hidden',
|
|
||||||
'name' => 'username',
|
|
||||||
'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 HTML::tag_end('tr');
|
||||||
},
|
},
|
||||||
[
|
[
|
||||||
@ -590,22 +511,8 @@ EOD);
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
?>
|
?>
|
||||||
</div>
|
|
||||||
<div class="tab-pane" id="add-manager">
|
<?php $managers_form->printHTML() ?>
|
||||||
<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') : ?>
|
||||||
|
@ -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();
|
||||||
|
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user