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/_topics.php
2020-07-06 17:09:56 +00:00

78 lines
3.2 KiB
PHP

<?php
function topics_in_category(int $category): array {
global $pdo;
if($category < 1)
return [];
$getTopics = $pdo->prepare('SELECT *, UNIX_TIMESTAMP(`topic_created`) AS `topic_created`, UNIX_TIMESTAMP(`topic_bumped`) AS `topic_bumped`, UNIX_TIMESTAMP(`topic_locked`) AS `topic_locked`, UNIX_TIMESTAMP(`topic_resolved`) AS `topic_resolved`, UNIX_TIMESTAMP(`topic_confirmed`) AS `topic_confirmed` FROM `fmf_topics` WHERE `cat_id` = :category AND `topic_bumped` IS NOT NULL ORDER BY `topic_bumped` DESC');
$getTopics->bindValue('category', $category);
$topics = $getTopics->execute() ? $getTopics->fetchAll(PDO::FETCH_ASSOC) : false;
return $topics ? $topics : [];
}
function create_topic(int $category, int $user, string $title): int {
global $pdo;
$createTopic = $pdo->prepare('INSERT INTO `fmf_topics` (`cat_id`, `user_id`, `topic_title`) VALUES (:cat, :user, :title)');
$createTopic->bindValue('cat', $category);
$createTopic->bindValue('user', $user);
$createTopic->bindValue('title', $title);
$createTopic->execute();
return (int)$pdo->lastInsertId();
}
function topic_info(int $topic): array {
global $pdo;
if($topic < 1)
return [];
$getTopic = $pdo->prepare('SELECT *, UNIX_TIMESTAMP(`topic_created`) AS `topic_created`, UNIX_TIMESTAMP(`topic_locked`) AS `topic_locked`, UNIX_TIMESTAMP(`topic_resolved`) AS `topic_resolved`, UNIX_TIMESTAMP(`topic_confirmed`) AS `topic_confirmed` FROM `fmf_topics` WHERE `topic_id` = :id');
$getTopic->bindValue('id', $topic);
$topic = $getTopic->execute() ? $getTopic->fetch(PDO::FETCH_ASSOC) : false;
return $topic ? $topic : [];
}
function topic_bump(int $topic, ?int $post = null, bool $onlyPostId = false): void {
global $pdo;
if($topic < 1)
return;
$bump = $pdo->prepare('UPDATE `fmf_topics` SET `topic_bumped` = IF(:no_bump, `topic_bumped`, NOW()), `topic_count_replies` = IF(`topic_bumped` IS NULL, `topic_count_replies`, `topic_count_replies` + 1), `topic_last_post_id` = COALESCE(:post, `topic_last_post_id`) WHERE `topic_id` = :topic');
$bump->bindValue('topic', $topic);
$bump->bindValue('post', $post);
$bump->bindValue('no_bump', $onlyPostId ? 1 : 0);
$bump->execute();
}
function lock_topic(int $topic, bool $state): void {
global $pdo;
$lock = $pdo->prepare('UPDATE `fmf_topics` SET `topic_locked` = IF(:state, NOW(), NULL) WHERE `topic_id` = :topic');
$lock->bindValue('state', $state ? 1 : 0);
$lock->bindValue('topic', $topic);
$lock->execute();
}
function mark_topic_resolved(int $topic, bool $state): void {
global $pdo;
$resolve = $pdo->prepare('UPDATE `fmf_topics` SET `topic_resolved` = IF(:state, NOW(), NULL) WHERE `topic_id` = :topic');
$resolve->bindValue('state', $state ? 1 : 0);
$resolve->bindValue('topic', $topic);
$resolve->execute();
}
function mark_topic_confirmed(int $topic, bool $state): void {
global $pdo;
$confirm = $pdo->prepare('UPDATE `fmf_topics` SET `topic_confirmed` = IF(:state, NOW(), NULL) WHERE `topic_id` = :topic');
$confirm->bindValue('state', $state ? 1 : 0);
$confirm->bindValue('topic', $topic);
$confirm->execute();
}