refactor(group/manage): users_form

This commit is contained in:
Baoshuo Ren 2023-02-17 21:46:28 +08:00
parent fe7b22959b
commit 18c501354c
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
2 changed files with 55 additions and 128 deletions

View File

@ -8,7 +8,7 @@ UOJGroup::init(UOJRequest::get('id')) || UOJResponse::page404();
UOJGroup::cur()->userCanView(Auth::user(), ['ensure' => true]); UOJGroup::cur()->userCanView(Auth::user(), ['ensure' => true]);
?> ?>
<?php echoUOJPageHeader('小组:' . UOJGroup::info('title')) ?> <?php echoUOJPageHeader(UOJGroup::info('title') . ' - 小组') ?>
<div class="row"> <div class="row">
<!-- left col --> <!-- left col -->

View File

@ -219,82 +219,44 @@ if ($cur_tab == 'profile') {
$hidden_time = new DateTime(); $hidden_time = new DateTime();
$hidden_time->sub(new DateInterval('P3D')); $hidden_time->sub(new DateInterval('P3D'));
} elseif ($cur_tab == 'users') { } elseif ($cur_tab == 'users') {
if (isset($_POST['submit-remove_user']) && $_POST['submit-remove_user'] == 'remove_user') { $users_form = newAddDelCmdForm(
$user = UOJUser::query(UOJRequest::post('remove_username')); 'users',
function ($username) {
if (!$user) {
dieWithAlert('用户不存在。');
}
if (!UOJGroup::cur()->hasUser($user)) {
dieWithAlert('该用户不在小组中。');
}
DB::delete([
"delete from groups_users",
"where", [
"username" => $user['username'],
"group_id" => UOJGroup::info('id'),
],
]);
dieWithAlert('移除成功!');
}
$add_new_user_form = new UOJForm('add_new_user');
$add_new_user_form->addInput('new_username', [
'label' => '用户名',
'validator_php' => function ($username, &$vdata) {
$user = UOJUser::query($username); $user = UOJUser::query($username);
if (!$user) { if (!$user) {
return '用户不存在。'; return '用户不存在。';
} }
if (UOJGroup::cur()->hasUser($user)) {
return '该用户已经在小组中';
}
$vdata['username'] = $user['username'];
return ''; return '';
}, },
]); function ($type, $username) {
$add_new_user_form->config['submit_button']['class'] = 'btn btn-secondary'; if ($type === '+') {
$add_new_user_form->config['submit_button']['text'] = '添加'; DB::insert([
$add_new_user_form->handle = function (&$vdata) { "insert into groups_users",
DB::insert([ DB::bracketed_fields(["group_id", "username"]),
"insert into groups_users", "values",
DB::bracketed_fields(["group_id", "username"]), DB::tuple([
"values", UOJGroup::info('id'),
DB::tuple([ $username,
UOJGroup::info('id'), ]),
$vdata['username'] ]);
]), } else if ($type === '-') {
]); DB::delete([
"delete from groups_users",
dieWithJsonData(['status' => 'success', 'message' => '已将用户名为 ' . $vdata['username'] . ' 的用户添加到本小组。']); "where", [
}; "group_id" => UOJGroup::info('id'),
$add_new_user_form->setAjaxSubmit(<<<EOD "username" => $username,
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); null,
} [
EOD); 'help' => '命令格式:命令一行一个,<code>+baoshuo</code> 表示把用户名为 <code>baoshuo</code> 的用户加入小组,<code>-baoshuo</code> 表示把用户名为 <code>baoshuo</code> 的用户从小组中移除。',
$add_new_user_form->runAtServer(); ]
);
$users_form->runAtServer();
} }
?> ?>
<?php echoUOJPageHeader('管理 - ' . UOJGroup::info('title')); ?> <?php echoUOJPageHeader('管理 - ' . UOJGroup::info('title')); ?>
@ -429,67 +391,32 @@ if ($cur_tab == 'profile') {
</div> </div>
<?php elseif ($cur_tab == 'users') : ?> <?php elseif ($cur_tab == 'users') : ?>
<div class="card mt-3 mt-md-0"> <div class="card mt-3 mt-md-0">
<div class="card-header">
<ul class="nav nav-tabs card-header-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" href="#users" data-bs-toggle="tab" data-bs-target="#users">用户列表</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#add-user" data-bs-toggle="tab" data-bs-target="#add-user">添加用户</a>
</li>
</ul>
</div>
<div class="card-body"> <div class="card-body">
<div class="tab-content"> <?php
<div class="tab-pane active" id="users"> echoLongTable(
<?php ['*'],
echoLongTable( 'groups_users',
['*'], ["group_id" => UOJGroup::info('id')],
'groups_users', 'order by username asc',
["group_id" => UOJGroup::info('id')], <<<EOD
'order by username asc', <tr>
<<<EOD <th>用户名</th>
<tr> </tr>
<th>用户名</th> EOD,
<th>操作</th> function ($row) {
</tr> echo HTML::tag_begin('tr');
EOD, echo HTML::tag('td', [], UOJUser::getLink($row['username']));
function ($row) { echo HTML::tag_end('tr');
echo HTML::tag_begin('tr'); },
echo HTML::tag('td', [], UOJUser::getLink($row['username'])); [
echo '<td>'; 'page_len' => 10,
echo '<form class="d-inline-block" method="POST" onsubmit=\'return confirm("你真的要从小组中移除这个用户吗?")\'>' 'div_classes' => ['table-responsive'],
. HTML::hiddenToken() 'table_classes' => ['table', 'align-middle'],
. '<input type="hidden" name="remove_username" value="' . $row['username'] . '">' ]
. '<button class="btn btn-link text-danger text-decoration-none p-0" type="submit" name="submit-remove_user" value="remove_user">移除</button>' );
. '</form>'; ?>
echo '</td>';
echo HTML::tag_end('tr'); <?php $users_form->printHTML() ?>
},
[
'page_len' => 10,
'div_classes' => ['table-responsive'],
'table_classes' => ['table', 'align-middle'],
]
);
?>
</div>
<div class="tab-pane" id="add-user">
<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_new_user_form->printHTML() ?>
</div>
<div class="col">
<h5>注意事项</h5>
<ul class="mb-0">
<li>添加用户前请确认用户名是否正确以免带来不必要的麻烦。</li>
<li>用户被添加到小组后将自动被加入组内的所有作业排行中。</li>
</ul>
</div>
</div>
</div>
</div>
</div> </div>
</div> </div>
<?php endif ?> <?php endif ?>