This repository has been archived on 2021-07-02. You can view files and clone it, but cannot push or open issues or pull requests.
chie/include/_posts.php

101 lines
3.3 KiB
PHP
Raw Normal View History

2020-07-06 17:09:56 +00:00
<?php
define('FMF_POST_TYPE_MESSAGE', 0);
define('FMF_POST_TYPE_RESOLVE', 1);
define('FMF_POST_TYPE_LOCKED', 2);
define('FMF_POST_TYPE_UNLOCKED', 3);
define('FMF_POST_TYPE_UNRESOLVED', 4);
define('FMF_POST_TYPE_CONFIRMED', 5);
define('FMF_POST_TYPE_UNCONFIRMED', 6);
function create_post(int $category, int $topic, int $user, string $text): int {
return create_topic_event($category, $topic, $user, FMF_POST_TYPE_MESSAGE, $text);
}
function create_topic_event(int $category, int $topic, int $user, int $type, ?string $data = null): int {
global $pdo;
$createPost = $pdo->prepare('INSERT INTO `fmf_posts` (`cat_id`, `topic_id`, `user_id`, `post_type`, `post_text`) VALUES (:category, :topic, :user, :type, :text)');
$createPost->bindValue('category', $category);
$createPost->bindValue('topic', $topic);
$createPost->bindValue('user', $user);
$createPost->bindValue('type', $type);
$createPost->bindValue('text', $data);
$createPost->execute();
return (int)$pdo->lastInsertId();
}
function posts_in_topic(int $topic): array {
global $pdo;
if($topic < 1)
return [];
$getTopics = $pdo->prepare('SELECT *, UNIX_TIMESTAMP(`post_created`) AS `post_created`, UNIX_TIMESTAMP(`post_edited`) AS `post_edited`, UNIX_TIMESTAMP(`post_deleted`) AS `post_deleted` FROM `fmf_posts` WHERE `topic_id` = :topic ORDER BY `post_created`');
$getTopics->bindValue('topic', $topic);
$topics = $getTopics->execute() ? $getTopics->fetchAll(PDO::FETCH_ASSOC) : false;
return $topics ? $topics : [];
}
function post_info(?int $post): array {
global $pdo;
static $posts = [];
if($post < 1)
return [];
if(!empty($posts[$post]))
return $posts[$post];
$getPost = $pdo->prepare('SELECT *, UNIX_TIMESTAMP(`post_created`) AS `post_created`, UNIX_TIMESTAMP(`post_edited`) AS `post_edited`, UNIX_TIMESTAMP(`post_deleted`) AS `post_deleted` FROM `fmf_posts` WHERE `post_id` = :post');
$getPost->bindValue('post', $post);
$postInfo = $getPost->execute() ? $getPost->fetch(PDO::FETCH_ASSOC) : false;
return $posts[$post] = ($postInfo ? $postInfo : []);
}
function post_delete(int $post, bool $hard = false): void {
global $pdo;
if($post < 1)
return;
$delete = $pdo->prepare($hard ? 'DELETE FROM `fmf_posts` WHERE `post_id` = :post' : 'UPDATE `fmf_posts` SET `post_deleted` = NOW() WHERE `post_id` = :post');
$delete->bindValue('post', $post);
$delete->execute();
}
function post_restore(int $post): void {
global $pdo;
if($post < 1)
return;
$restore = $pdo->prepare('UPDATE `fmf_posts` SET `post_deleted` = NULL WHERE `post_id` = :post');
$restore->bindValue('post', $post);
$restore->execute();
}
function post_anonymize(int $post): void {
global $pdo;
if($post < 1)
return;
$restore = $pdo->prepare('UPDATE `fmf_posts` SET `user_id` = NULL WHERE `post_id` = :post');
$restore->bindValue('post', $post);
$restore->execute();
}
function post_update(int $post, string $text): void {
global $pdo;
if($post < 1)
return;
$restore = $pdo->prepare('UPDATE `fmf_posts` SET `post_text` = :text, `post_edited` = NOW() WHERE `post_id` = :post');
$restore->bindValue('text', $text);
$restore->bindValue('post', $post);
$restore->execute();
}