S2OJ/web/app/models/UOJList.php

95 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2022-11-10 00:16:48 +00:00
<?php
class UOJList {
use UOJDataTrait;
use UOJArticleTrait;
public static function query($id) {
if (!isset($id) || !validateUInt($id)) {
return null;
}
$info = DB::selectFirst([
"select * from lists",
"where", ["id" => $id]
]);
if (!$info) {
return null;
}
return new UOJList($info);
}
2022-11-11 23:10:34 +00:00
public static function userCanCreateList(array $user = null) {
if (!$user) {
return false;
}
return isSuperUser($user) || UOJUser::checkPermission($user, 'lists.create');
}
2022-11-10 00:16:48 +00:00
public function __construct($info) {
$this->info = $info;
}
2022-11-11 00:20:33 +00:00
public function getUri($where = '') {
return "/list/{$this->info['id']}{$where}";
}
public function getLink($cfg = []) {
$cfg += [
'where' => '',
'class' => '',
'text' => $this->info['title'],
'with' => 'id',
];
return HTML::tag('a', [
'href' => $this->getUri($cfg['where']),
'class' => $cfg['class'],
], $cfg['text']);
}
2022-11-10 00:16:48 +00:00
public function getProblemIDs() {
return array_map(fn ($x) => $x['problem_id'], DB::selectAll([
DB::lc(), "select problem_id from lists_problems",
"where", ['list_id' => $this->info['id']],
"order by problem_id"
]));
}
public function hasProblem(UOJProblem $problem) {
return DB::selectFirst([
DB::lc(), "select 1 from lists_problems",
"where", [
'list_id' => $this->info['id'],
'problem_id' => $problem->info['id']
]
]) != null;
}
public function userCanManage(array $user = null) {
2022-11-11 23:10:34 +00:00
return isSuperUser($user) || UOJUser::checkPermission($user, 'lists.manage');
2022-11-10 00:16:48 +00:00
}
public function userCanView(array $user = null, array $cfg = []) {
$cfg += ['ensure' => false];
2022-11-11 23:10:34 +00:00
2022-11-10 00:16:48 +00:00
if ($this->info['is_hidden'] && !$this->userCanManage($user)) {
$cfg['ensure'] && UOJResponse::page404();
return false;
}
2022-11-11 23:10:34 +00:00
if (!UOJUser::checkPermission($user, 'lists.view')) {
$cfg['ensure'] && UOJResponse::page403();
return false;
}
2022-11-10 00:16:48 +00:00
return true;
}
}
UOJList::$table_for_content = 'lists_contents';
UOJList::$key_for_content = 'id';
UOJList::$fields_for_content = ['*'];
UOJList::$table_for_tags = 'lists_tags';
UOJList::$key_for_tags = 'list_id';