misuzu/public-legacy/manage/news/category.php

71 lines
2.1 KiB
PHP
Raw Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
2023-07-15 17:02:46 +00:00
use RuntimeException;
2022-09-13 13:14:49 +00:00
use Misuzu\Users\User;
if(!User::hasCurrent() || !perms_check_user(MSZ_PERMS_NEWS, User::getCurrent()->getId(), MSZ_PERM_NEWS_MANAGE_CATEGORIES)) {
echo render_error(403);
return;
}
2023-07-15 17:02:46 +00:00
$news = $msz->getNews();
$categoryId = (string)filter_input(INPUT_GET, 'c', FILTER_SANITIZE_NUMBER_INT);
$loadCategoryInfo = fn() => $news->getCategoryById($categoryId);
2022-09-13 13:14:49 +00:00
2023-07-15 17:02:46 +00:00
if(empty($categoryId))
$isNew = true;
else
2022-09-13 13:14:49 +00:00
try {
2023-07-15 17:02:46 +00:00
$isNew = false;
$categoryInfo = $loadCategoryInfo();
} catch(RuntimeException $ex) {
2022-09-13 13:14:49 +00:00
echo render_error(404);
return;
}
2023-07-15 17:02:46 +00:00
if($_SERVER['REQUEST_METHOD'] === 'GET' && !empty($_GET['delete'])) {
if(CSRF::validateRequest()) {
$news->deleteCategory($categoryInfo);
$msz->createAuditLog('NEWS_CATEGORY_DELETE', [$categoryInfo->getId()]);
2023-07-15 17:02:46 +00:00
url_redirect('manage-news-categories');
} else render_error(403);
return;
}
while($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) {
$name = trim((string)filter_input(INPUT_POST, 'nc_name'));
$description = trim((string)filter_input(INPUT_POST, 'nc_desc'));
$hidden = !empty($_POST['nc_hidden']);
2022-09-13 13:14:49 +00:00
2023-07-15 17:02:46 +00:00
if($isNew) {
$categoryInfo = $news->createCategory($name, $description, $hidden);
} else {
if($name === $categoryInfo->getName())
$name = null;
if($description === $categoryInfo->getDescription())
$description = null;
if($hidden === $categoryInfo->isHidden())
$hidden = null;
if($name !== null || $description !== null || $hidden !== null)
$news->updateCategory($categoryInfo, $name, $description, $hidden);
}
2022-09-13 13:14:49 +00:00
$msz->createAuditLog(
$isNew ? 'NEWS_CATEGORY_CREATE' : 'NEWS_CATEGORY_EDIT',
2022-09-13 13:14:49 +00:00
[$categoryInfo->getId()]
);
2023-07-15 17:02:46 +00:00
if($isNew) {
url_redirect('manage-news-category', ['category' => $categoryInfo->getId()]);
2022-09-13 13:14:49 +00:00
return;
2023-07-15 17:02:46 +00:00
} else $categoryInfo = $loadCategoryInfo();
break;
2022-09-13 13:14:49 +00:00
}
2023-07-15 17:02:46 +00:00
Template::render('manage.news.category', [
'category_new' => $isNew,
'category_info' => $categoryInfo ?? null,
]);