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
2024-12-02 02:28:08 +00:00
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_POSTS_MANAGE))
Template::throwError(403);
2022-09-13 13:14:49 +00:00
2023-07-15 17:02:46 +00:00
$postId = (string)filter_input(INPUT_GET, 'p', FILTER_SANITIZE_NUMBER_INT);
$loadPostInfo = fn() => $msz->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);
$msz->news->deletePost($postInfo);
$msz->createAuditLog('NEWS_POST_DELETE', [$postInfo->id]);
Tools::redirect($msz->urls->format('manage-news-posts'));
2023-07-15 17:02:46 +00:00
return;
}
2022-09-13 13:14:49 +00:00
2024-12-02 21:33:15 +00:00
while($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) {
2023-07-15 17:02:46 +00:00
$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 = $msz->news->createPost($category, $title, $body, $featured, $msz->authInfo->userInfo);
2023-07-15 17:02:46 +00:00
} else {
if($category === $postInfo->categoryId)
2023-07-15 17:02:46 +00:00
$category = null;
if($title === $postInfo->title)
2023-07-15 17:02:46 +00:00
$title = null;
if($body === $postInfo->body)
2023-07-15 17:02:46 +00:00
$body = null;
if($featured === $postInfo->featured)
2023-07-15 17:02:46 +00:00
$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)
$msz->news->updatePost($postInfo, $category, $title, $body, $featured);
2023-07-15 17:02:46 +00:00
}
2022-09-13 13:14:49 +00:00
$msz->createAuditLog(
$isNew ? 'NEWS_POST_CREATE' : 'NEWS_POST_EDIT',
[$postInfo->id]
2022-09-13 13:14:49 +00:00
);
2023-07-15 17:02:46 +00:00
if($isNew) {
if($postInfo->featured) {
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
}
Tools::redirect($msz->urls->format('manage-news-post', ['post' => $postInfo->id]));
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($msz->news->getCategories() as $categoryInfo)
$categories[$categoryInfo->id] = $categoryInfo->name;
2023-07-15 17:02:46 +00:00
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
]);