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

82 lines
2.5 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
$authInfo = $msz->getAuthInfo();
if(!$authInfo->getPerms('global')->check(Perm::G_NEWS_POSTS_MANAGE))
Template::throwError(403);
2022-09-13 13:14:49 +00:00
2023-09-08 20:40:48 +00:00
$urls = $msz->getURLs();
2023-07-15 17:02:46 +00:00
$news = $msz->getNews();
$postId = (string)filter_input(INPUT_GET, 'p', FILTER_SANITIZE_NUMBER_INT);
$loadPostInfo = fn() => $news->getPost($postId);
2023-07-15 17:02:46 +00:00
if(empty($postId))
$isNew = true;
else
2022-09-13 13:14:49 +00:00
try {
2023-07-15 17:02:46 +00:00
$isNew = false;
$postInfo = $loadPostInfo();
} catch(RuntimeException $ex) {
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'])) {
if(!CSRF::validateRequest())
Template::throwError(403);
$news->deletePost($postInfo);
$msz->createAuditLog('NEWS_POST_DELETE', [$postInfo->getId()]);
2023-09-08 20:40:48 +00:00
Tools::redirect($urls->format('manage-news-posts'));
2023-07-15 17:02:46 +00:00
return;
}
2022-09-13 13:14:49 +00:00
2023-07-15 17:02:46 +00:00
while($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) {
$title = trim((string)filter_input(INPUT_POST, 'np_title'));
$category = (string)filter_input(INPUT_POST, 'np_category', FILTER_SANITIZE_NUMBER_INT);
$featured = !empty($_POST['np_featured']);
$body = trim((string)filter_input(INPUT_POST, 'np_body'));
2022-09-13 13:14:49 +00:00
2023-07-15 17:02:46 +00:00
if($isNew) {
$postInfo = $news->createPost($category, $title, $body, $featured, $authInfo->getUserInfo());
2023-07-15 17:02:46 +00:00
} else {
if($category === $postInfo->getCategoryId())
$category = null;
if($title === $postInfo->getTitle())
$title = null;
if($body === $postInfo->getBody())
$body = null;
if($featured === $postInfo->isFeatured())
$featured = null;
2022-09-13 13:14:49 +00:00
2023-07-15 17:02:46 +00:00
if($category !== null || $title !== null || $body !== null || $featured !== null)
$news->updatePost($postInfo, $category, $title, $body, $featured);
}
2022-09-13 13:14:49 +00:00
$msz->createAuditLog(
$isNew ? 'NEWS_POST_CREATE' : 'NEWS_POST_EDIT',
2022-09-13 13:14:49 +00:00
[$postInfo->getId()]
);
2023-07-15 17:02:46 +00:00
if($isNew) {
2022-09-13 13:14:49 +00:00
if($postInfo->isFeatured()) {
2023-03-09 21:38:03 +00:00
// Twitter integration used to be here, replace with Railgun Pulse integration
2022-09-13 13:14:49 +00:00
}
2023-09-08 20:40:48 +00:00
Tools::redirect($urls->format('manage-news-post', ['post' => $postInfo->getId()]));
2022-09-13 13:14:49 +00:00
return;
2023-07-15 17:02:46 +00:00
} else $postInfo = $loadPostInfo();
break;
2022-09-13 13:14:49 +00:00
}
2023-07-15 17:02:46 +00:00
$categories = [];
foreach($news->getCategories() as $categoryInfo)
2023-07-15 17:02:46 +00:00
$categories[$categoryInfo->getId()] = $categoryInfo->getName();
2022-09-13 13:14:49 +00:00
Template::render('manage.news.post', [
'categories' => $categories,
2023-07-15 17:02:46 +00:00
'post_new' => $isNew,
'post_info' => $postInfo ?? null,
2022-09-13 13:14:49 +00:00
]);