misuzu/public/manage/news/post.php

72 lines
1.9 KiB
PHP
Raw Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
use Misuzu\AuditLog;
use Misuzu\News\NewsCategory;
use Misuzu\News\NewsPost;
use Misuzu\News\NewsPostNotFoundException;
use Misuzu\Users\User;
require_once '../../../misuzu.php';
if(!User::hasCurrent() || !perms_check_user(MSZ_PERMS_NEWS, User::getCurrent()->getId(), MSZ_PERM_NEWS_MANAGE_POSTS)) {
echo render_error(403);
return;
}
$postId = (int)filter_input(INPUT_GET, 'p', FILTER_SANITIZE_NUMBER_INT);
if($postId > 0)
try {
$postInfo = NewsPost::byId($postId);
Template::set('post_info', $postInfo);
} catch(NewsPostNotFoundException $ex) {
echo render_error(404);
return;
}
$categories = NewsCategory::all(null, true);
if(!empty($_POST['post']) && CSRF::validateRequest()) {
if(!isset($postInfo)) {
$postInfo = new NewsPost;
$isNew = true;
}
$currentUserId = User::getCurrent()->getId();
$postInfo->setTitle( $_POST['post']['title'])
->setText($_POST['post']['text'])
->setCategoryId($_POST['post']['category'])
->setFeatured(!empty($_POST['post']['featured']));
if(!empty($isNew))
$postInfo->setUserId($currentUserId);
$postInfo->save();
AuditLog::create(
$_SERVER['REMOTE_ADDR'],
2022-09-13 13:14:49 +00:00
empty($isNew)
? AuditLog::NEWS_POST_EDIT
: AuditLog::NEWS_POST_CREATE,
[$postInfo->getId()]
);
if(!empty($isNew)) {
if($postInfo->isFeatured()) {
$twitter = $msz->createTwitterClient();
2022-09-13 13:14:49 +00:00
if($twitter->hasAccessToken()) {
2022-09-13 13:14:49 +00:00
$url = url('news-post', ['post' => $postInfo->getId()]);
$twitter->sendTweet("News :: {$postInfo->getTitle()}\nhttps://{$_SERVER['HTTP_HOST']}{$url}");
2022-09-13 13:14:49 +00:00
}
}
header('Location: ' . url('manage-news-post', ['post' => $postInfo->getId()]));
return;
}
}
Template::render('manage.news.post', [
'categories' => $categories,
]);