misuzu/src/Forum/post.php

164 lines
5.6 KiB
PHP
Raw Normal View History

2018-05-23 03:41:57 +02:00
<?php
function forum_post_create(
int $topicId,
int $forumId,
int $userId,
string $ipAddress,
2018-05-24 21:31:48 +02:00
string $text,
int $parser = MSZ_PARSER_PLAIN
2018-05-23 03:41:57 +02:00
): int {
$createPost = db_prepare('
2018-05-23 03:41:57 +02:00
INSERT INTO `msz_forum_posts`
2018-05-24 21:31:48 +02:00
(`topic_id`, `forum_id`, `user_id`, `post_ip`, `post_text`, `post_parse`)
2018-05-23 03:41:57 +02:00
VALUES
2018-05-24 21:31:48 +02:00
(:topic_id, :forum_id, :user_id, INET6_ATON(:post_ip), :post_text, :post_parse)
2018-05-23 03:41:57 +02:00
');
$createPost->bindValue('topic_id', $topicId);
$createPost->bindValue('forum_id', $forumId);
$createPost->bindValue('user_id', $userId);
$createPost->bindValue('post_ip', $ipAddress);
$createPost->bindValue('post_text', $text);
2018-05-24 21:31:48 +02:00
$createPost->bindValue('post_parse', $parser);
2018-05-23 03:41:57 +02:00
return $createPost->execute() ? db_last_insert_id() : 0;
2018-05-23 03:41:57 +02:00
}
function forum_post_update(
2018-12-30 04:02:35 +01:00
int $postId,
string $ipAddress,
string $text,
2018-12-30 18:23:55 +01:00
int $parser = MSZ_PARSER_PLAIN,
bool $bumpUpdate = true
2018-12-30 04:02:35 +01:00
): bool {
if ($postId < 1) {
return false;
}
$updatePost = db_prepare('
UPDATE `msz_forum_posts`
SET `post_ip` = INET6_ATON(:post_ip),
`post_text` = :post_text,
2018-12-30 18:23:55 +01:00
`post_parse` = :post_parse,
`post_edited` = IF(:bump, NOW(), NULL)
2018-12-30 04:02:35 +01:00
WHERE `post_id` = :post_id
');
$updatePost->bindValue('post_id', $postId);
$updatePost->bindValue('post_ip', $ipAddress);
$updatePost->bindValue('post_text', $text);
$updatePost->bindValue('post_parse', $parser);
2018-12-30 18:23:55 +01:00
$updatePost->bindValue('bump', $bumpUpdate ? 1 : 0);
2018-12-30 04:02:35 +01:00
return $updatePost->execute();
}
2018-05-23 03:41:57 +02:00
function forum_post_find(int $postId): array
{
$getPostInfo = db_prepare('
2018-05-23 03:41:57 +02:00
SELECT
:post_id as `target_post_id`,
(
SELECT `topic_id`
FROM `msz_forum_posts`
WHERE `post_id` = `target_post_id`
) as `target_topic_id`,
(
SELECT COUNT(`post_id`)
FROM `msz_forum_posts`
WHERE `topic_id` = `target_topic_id`
AND `post_id` < `target_post_id`
ORDER BY `post_id`
) as `preceeding_post_count`
');
$getPostInfo->bindValue('post_id', $postId);
2018-12-30 04:02:35 +01:00
return $getPostInfo->execute() ? $getPostInfo->fetch(PDO::FETCH_ASSOC) : [];
}
function forum_post_get(int $postId, bool $allowDeleted = false): array
{
$getPost = db_prepare(sprintf(
'
SELECT
p.`post_id`, p.`post_text`, p.`post_created`, p.`post_parse`,
p.`topic_id`, p.`post_deleted`, p.`post_edited`,
INET6_NTOA(p.`post_ip`) AS `post_ip`,
u.`user_id` AS `poster_id`,
u.`username` AS `poster_name`,
u.`user_created` AS `poster_joined`,
u.`user_country` AS `poster_country`,
COALESCE(u.`user_colour`, r.`role_colour`) AS `poster_colour`,
2018-12-30 04:02:35 +01:00
(
SELECT COUNT(`post_id`)
FROM `msz_forum_posts`
WHERE `user_id` = p.`user_id`
AND `post_deleted` IS NULL
) AS `poster_post_count`,
(
SELECT MIN(`post_id`) = p.`post_id`
FROM `msz_forum_posts`
WHERE `topic_id` = p.`topic_id`
) AS `is_opening_post`
FROM `msz_forum_posts` AS p
LEFT JOIN `msz_users` AS u
2018-12-30 04:02:35 +01:00
ON u.`user_id` = p.`user_id`
LEFT JOIN `msz_roles` AS r
2018-12-30 04:02:35 +01:00
ON r.`role_id` = u.`display_role`
WHERE `post_id` = :post_id
%1$s
ORDER BY `post_id`
',
$allowDeleted ? '' : 'AND `post_deleted` IS NULL'
));
$getPost->bindValue('post_id', $postId);
$post = $getPost->execute() ? $getPost->fetch(PDO::FETCH_ASSOC) : false;
return $post ? $post : [];
2018-05-23 03:41:57 +02:00
}
function forum_post_listing(int $topicId, int $offset = 0, int $take = 0, bool $showDeleted = false): array
{
$hasPagination = $offset >= 0 && $take > 0;
$getPosts = db_prepare(sprintf(
'
SELECT
p.`post_id`, p.`post_text`, p.`post_created`, p.`post_parse`,
p.`topic_id`, p.`post_deleted`, p.`post_edited`,
INET6_NTOA(p.`post_ip`) as `post_ip`,
u.`user_id` as `poster_id`,
u.`username` as `poster_name`,
u.`user_created` as `poster_joined`,
u.`user_country` as `poster_country`,
COALESCE(u.`user_colour`, r.`role_colour`) as `poster_colour`,
(
SELECT COUNT(`post_id`)
FROM `msz_forum_posts`
WHERE `user_id` = p.`user_id`
AND `post_deleted` IS NULL
) as `poster_post_count`,
(
SELECT MIN(`post_id`) = p.`post_id`
FROM `msz_forum_posts`
WHERE `topic_id` = p.`topic_id`
) AS `is_opening_post`
FROM `msz_forum_posts` as p
LEFT JOIN `msz_users` as u
ON u.`user_id` = p.`user_id`
LEFT JOIN `msz_roles` as r
ON r.`role_id` = u.`display_role`
WHERE `topic_id` = :topic_id
%1$s
ORDER BY `post_id`
%2$s
',
$showDeleted ? '' : 'AND `post_deleted` IS NULL',
$hasPagination ? 'LIMIT :offset, :take' : ''
));
$getPosts->bindValue('topic_id', $topicId);
if ($hasPagination) {
$getPosts->bindValue('offset', $offset);
$getPosts->bindValue('take', $take);
}
2018-12-30 04:02:35 +01:00
return $getPosts->execute() ? $getPosts->fetchAll(PDO::FETCH_ASSOC) : [];
}