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
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
if(!$msz->authInfo->getPerms('global')->check(Perm::G_NEWS_CATEGORIES_MANAGE))
|
2023-08-31 15:59:53 +00:00
|
|
|
Template::throwError(403);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-07-15 17:02:46 +00:00
|
|
|
$categoryId = (string)filter_input(INPUT_GET, 'c', FILTER_SANITIZE_NUMBER_INT);
|
2024-11-30 04:09:29 +00:00
|
|
|
$loadCategoryInfo = fn() => $msz->news->getCategory(categoryId: $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) {
|
2023-08-31 15:59:53 +00:00
|
|
|
Template::throwError(404);
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 17:02:46 +00:00
|
|
|
if($_SERVER['REQUEST_METHOD'] === 'GET' && !empty($_GET['delete'])) {
|
2023-08-31 15:59:53 +00:00
|
|
|
if(!CSRF::validateRequest())
|
|
|
|
Template::throwError(403);
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
$msz->news->deleteCategory($categoryInfo);
|
|
|
|
$msz->createAuditLog('NEWS_CATEGORY_DELETE', [$categoryInfo->id]);
|
|
|
|
Tools::redirect($msz->urls->format('manage-news-categories'));
|
2023-07-15 17:02:46 +00:00
|
|
|
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) {
|
2024-11-30 04:09:29 +00:00
|
|
|
$categoryInfo = $msz->news->createCategory($name, $description, $hidden);
|
2023-07-15 17:02:46 +00:00
|
|
|
} else {
|
2024-11-30 04:09:29 +00:00
|
|
|
if($name === $categoryInfo->name)
|
2023-07-15 17:02:46 +00:00
|
|
|
$name = null;
|
2024-11-30 04:09:29 +00:00
|
|
|
if($description === $categoryInfo->description)
|
2023-07-15 17:02:46 +00:00
|
|
|
$description = null;
|
2024-11-30 04:09:29 +00:00
|
|
|
if($hidden === $categoryInfo->hidden)
|
2023-07-15 17:02:46 +00:00
|
|
|
$hidden = null;
|
|
|
|
|
|
|
|
if($name !== null || $description !== null || $hidden !== null)
|
2024-11-30 04:09:29 +00:00
|
|
|
$msz->news->updateCategory($categoryInfo, $name, $description, $hidden);
|
2023-07-15 17:02:46 +00:00
|
|
|
}
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-07-17 17:43:17 +00:00
|
|
|
$msz->createAuditLog(
|
|
|
|
$isNew ? 'NEWS_CATEGORY_CREATE' : 'NEWS_CATEGORY_EDIT',
|
2024-11-30 04:09:29 +00:00
|
|
|
[$categoryInfo->id]
|
2022-09-13 13:14:49 +00:00
|
|
|
);
|
|
|
|
|
2023-07-15 17:02:46 +00:00
|
|
|
if($isNew) {
|
2024-11-30 04:09:29 +00:00
|
|
|
Tools::redirect($msz->urls->format('manage-news-category', ['category' => $categoryInfo->id]));
|
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,
|
|
|
|
]);
|