S2OJ/web/app/controllers/add_contest.php

108 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2016-07-18 16:39:37 +00:00
<?php
2022-11-06 02:26:21 +00:00
requirePHPLib('form');
Auth::check() || redirectToLogin();
2022-11-11 23:10:34 +00:00
UOJContest::userCanCreateContest(Auth::user()) || UOJResponse::page403();
2022-11-06 02:26:21 +00:00
2022-12-24 03:11:06 +00:00
$time_form = new UOJForm('time');
$time_form->addInput(
2022-11-06 02:26:21 +00:00
'name',
2022-12-24 03:11:06 +00:00
[
'label' => '比赛标题',
'default_value' => 'New Contest',
'validator_php' => function ($name, &$vdata) {
if ($name == '') {
return '标题不能为空';
}
2022-11-06 02:26:21 +00:00
2022-12-24 03:11:06 +00:00
if (strlen($name) > 100) {
return '标题过长';
}
2022-11-06 02:26:21 +00:00
2022-12-24 03:11:06 +00:00
$name = HTML::escape($name);
2022-11-06 02:26:21 +00:00
2022-12-24 03:11:06 +00:00
if ($name === '') {
return '无效编码';
}
2022-11-06 02:26:21 +00:00
2022-12-24 03:11:06 +00:00
$vdata['name'] = $name;
2022-11-06 02:26:21 +00:00
2022-12-24 03:11:06 +00:00
return '';
},
]
2022-11-06 02:26:21 +00:00
);
2022-12-24 03:11:06 +00:00
$time_form->addInput(
2022-11-06 02:26:21 +00:00
'start_time',
2022-12-24 03:11:06 +00:00
[
'div_class' => 'mt-2',
'label' => '开始时间',
'default_value' => date("Y-m-d H:i:s"),
'validator_php' => function ($str, &$vdata) {
try {
$vdata['start_time'] = new DateTime($str);
} catch (Exception $e) {
return '无效时间格式';
}
return '';
},
]
2022-11-06 02:26:21 +00:00
);
2022-12-24 03:11:06 +00:00
$time_form->addInput(
2022-11-06 02:26:21 +00:00
'last_min',
2022-12-24 03:11:06 +00:00
[
'div_class' => 'mt-2',
'label' => '时长(单位:分钟)',
'default_value' => '180',
'validator_php' => function ($str, &$vdata) {
if (!validateUInt($str)) {
return '必须为一个整数';
}
$vdata['last_min'] = $str;
return '';
},
]
2022-11-06 02:26:21 +00:00
);
$time_form->handle = function (&$vdata) {
$start_time_str = $vdata['start_time']->format('Y-m-d H:i:s');
DB::insert([
"insert into contests",
"(name, start_time, last_min, status)", "values",
DB::tuple([$vdata['name'], $start_time_str, $vdata['last_min'], 'unfinished'])
]);
};
$time_form->succ_href = "/contests";
$time_form->runAtServer();
?>
2022-12-24 03:11:06 +00:00
2022-12-23 13:41:46 +00:00
<?php echoUOJPageHeader(UOJLocale::get('contests::add new contest')) ?>
2022-10-02 06:41:26 +00:00
<div class="row">
2022-11-06 02:26:21 +00:00
<!-- left col -->
<div class="col-lg-9">
<div class="card card-default mb-2">
<div class="card-body">
2022-12-23 13:41:46 +00:00
<h1 class="card-title">
<?= UOJLocale::get('contests::add new contest') ?>
</h1>
2022-11-06 02:26:21 +00:00
<div class="w-full" style="max-width: 400px">
2023-01-15 13:27:12 +00:00
<?php $time_form->printHTML() ?>
2022-11-06 02:26:21 +00:00
</div>
</div>
</div>
</div>
<!-- end left col -->
<!-- right col -->
<aside class="col-lg-3 mt-3 mt-lg-0">
<?php uojIncludeView('sidebar') ?>
</aside>
2023-01-15 13:27:12 +00:00
<!-- end right col -->
2022-10-02 06:41:26 +00:00
</div>
2022-03-17 04:00:03 +00:00
<?php echoUOJPageFooter() ?>