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

83 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
use Misuzu\Users\User;
if(!User::hasCurrent() || !perms_check_user(MSZ_PERMS_NEWS, User::getCurrent()->getId(), MSZ_PERM_NEWS_MANAGE_POSTS)) {
echo render_error(403);
return;
}
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->getPostById($postId);
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) {
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->deletePost($postInfo);
$msz->createAuditLog('NEWS_POST_DELETE', [$postInfo->getId()]);
2023-07-15 17:02:46 +00:00
url_redirect('manage-news-posts');
} else render_error(403);
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, User::getCurrent());
} 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-07-15 17:02:46 +00:00
url_redirect('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->getAllCategories(true) as $categoryInfo)
$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
]);