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

69 lines
2.2 KiB
PHP

<?php
namespace Misuzu;
use RuntimeException;
if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext))
die('Script must be called through the Misuzu route dispatcher.');
if(!$msz->authInfo->getPerms('global')->check(Perm::G_NEWS_CATEGORIES_MANAGE))
Template::throwError(403);
$categoryId = !empty($_GET['c']) && is_scalar($_GET['c']) ? (string)$_GET['c'] : '';
$loadCategoryInfo = fn() => $msz->news->getCategory(categoryId: $categoryId);
if(empty($categoryId))
$isNew = true;
else
try {
$isNew = false;
$categoryInfo = $loadCategoryInfo();
} catch(RuntimeException $ex) {
Template::throwError(404);
}
if($_SERVER['REQUEST_METHOD'] === 'GET' && !empty($_GET['delete'])) {
if(!CSRF::validateRequest())
Template::throwError(403);
$msz->news->deleteCategory($categoryInfo);
$msz->createAuditLog('NEWS_CATEGORY_DELETE', [$categoryInfo->id]);
Tools::redirect($msz->urls->format('manage-news-categories'));
return;
}
while($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) {
$name = !empty($_POST['nc_name']) && is_scalar($_POST['nc_name']) ? trim((string)$_POST['nc_name']) : '';
$description = !empty($_POST['nc_desc']) && is_scalar($_POST['nc_desc']) ? trim((string)$_POST['nc_desc']) : '';
$hidden = !empty($_POST['nc_hidden']);
if($isNew) {
$categoryInfo = $msz->news->createCategory($name, $description, $hidden);
} else {
if($name === $categoryInfo->name)
$name = null;
if($description === $categoryInfo->description)
$description = null;
if($hidden === $categoryInfo->hidden)
$hidden = null;
if($name !== null || $description !== null || $hidden !== null)
$msz->news->updateCategory($categoryInfo, $name, $description, $hidden);
}
$msz->createAuditLog(
$isNew ? 'NEWS_CATEGORY_CREATE' : 'NEWS_CATEGORY_EDIT',
[$categoryInfo->id]
);
if($isNew) {
Tools::redirect($msz->urls->format('manage-news-category', ['category' => $categoryInfo->id]));
return;
} else $categoryInfo = $loadCategoryInfo();
break;
}
Template::render('manage.news.category', [
'category_new' => $isNew,
'category_info' => $categoryInfo ?? null,
]);