2018-05-23 03:41:57 +02:00
|
|
|
<?php
|
|
|
|
define('MSZ_TOPIC_TYPE_DISCUSSION', 0);
|
|
|
|
define('MSZ_TOPIC_TYPE_STICKY', 1);
|
|
|
|
define('MSZ_TOPIC_TYPE_ANNOUNCEMENT', 2);
|
2018-12-30 19:58:30 +01:00
|
|
|
define('MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT', 3);
|
2018-05-23 03:41:57 +02:00
|
|
|
define('MSZ_TOPIC_TYPES', [
|
|
|
|
MSZ_TOPIC_TYPE_DISCUSSION,
|
|
|
|
MSZ_TOPIC_TYPE_STICKY,
|
|
|
|
MSZ_TOPIC_TYPE_ANNOUNCEMENT,
|
2018-12-30 19:58:30 +01:00
|
|
|
MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT,
|
2018-05-23 03:41:57 +02:00
|
|
|
]);
|
|
|
|
|
2018-12-30 19:58:30 +01:00
|
|
|
define('MSZ_TOPIC_TYPE_ORDER', [ // in which order to display topics, only add types here that should appear above others
|
|
|
|
MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT,
|
|
|
|
MSZ_TOPIC_TYPE_ANNOUNCEMENT,
|
|
|
|
MSZ_TOPIC_TYPE_STICKY,
|
|
|
|
]);
|
|
|
|
|
|
|
|
function forum_topic_is_valid_type(int $type): bool
|
2018-05-23 03:41:57 +02:00
|
|
|
{
|
2018-12-30 19:58:30 +01:00
|
|
|
return in_array($type, MSZ_TOPIC_TYPES, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function forum_topic_create(int $forumId, int $userId, string $title, int $type = MSZ_TOPIC_TYPE_DISCUSSION): int
|
|
|
|
{
|
|
|
|
if (empty($title) || !forum_topic_is_valid_type($type)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-07 01:30:48 +02:00
|
|
|
$createTopic = db_prepare('
|
2018-05-23 03:41:57 +02:00
|
|
|
INSERT INTO `msz_forum_topics`
|
2018-12-30 19:58:30 +01:00
|
|
|
(`forum_id`, `user_id`, `topic_title`, `topic_type`)
|
2018-05-23 03:41:57 +02:00
|
|
|
VALUES
|
2018-12-30 19:58:30 +01:00
|
|
|
(:forum_id, :user_id, :topic_title, :topic_type)
|
2018-05-23 03:41:57 +02:00
|
|
|
');
|
|
|
|
$createTopic->bindValue('forum_id', $forumId);
|
|
|
|
$createTopic->bindValue('user_id', $userId);
|
|
|
|
$createTopic->bindValue('topic_title', $title);
|
2018-12-30 19:58:30 +01:00
|
|
|
$createTopic->bindValue('topic_type', $type);
|
2018-05-23 03:41:57 +02:00
|
|
|
|
2018-10-07 01:30:48 +02:00
|
|
|
return $createTopic->execute() ? (int)db_last_insert_id() : 0;
|
2018-05-23 03:41:57 +02:00
|
|
|
}
|
|
|
|
|
2018-12-30 19:58:30 +01:00
|
|
|
function forum_topic_update(int $topicId, ?string $title, ?int $type = null): bool
|
2018-12-30 04:23:04 +01:00
|
|
|
{
|
2018-12-30 19:58:30 +01:00
|
|
|
if ($topicId < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure it's null and not some other kinda empty
|
|
|
|
if (empty($title)) {
|
|
|
|
$title = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type !== null && !forum_topic_is_valid_type($type)) {
|
2018-12-30 04:23:04 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$updateTopic = db_prepare('
|
|
|
|
UPDATE `msz_forum_topics`
|
2018-12-30 20:51:32 +01:00
|
|
|
SET `topic_title` = COALESCE(:topic_title, `topic_title`),
|
2018-12-30 19:58:30 +01:00
|
|
|
`topic_type` = COALESCE(:topic_type, `topic_type`)
|
2018-12-30 04:23:04 +01:00
|
|
|
WHERE `topic_id` = :topic_id
|
|
|
|
');
|
|
|
|
$updateTopic->bindValue('topic_id', $topicId);
|
|
|
|
$updateTopic->bindValue('topic_title', $title);
|
2018-12-30 19:58:30 +01:00
|
|
|
$updateTopic->bindValue('topic_type', $type);
|
|
|
|
|
2018-12-30 04:23:04 +01:00
|
|
|
return $updateTopic->execute();
|
|
|
|
}
|
|
|
|
|
2019-01-05 18:21:48 +01:00
|
|
|
function forum_topic_fetch(int $topicId, int $userId = 0): array
|
2018-05-24 02:38:42 +02:00
|
|
|
{
|
2019-01-03 16:57:17 +01:00
|
|
|
$getTopic = db_prepare(sprintf(
|
|
|
|
'
|
|
|
|
SELECT
|
|
|
|
t.`topic_id`, t.`forum_id`, t.`topic_title`, t.`topic_type`, t.`topic_locked`, t.`topic_created`,
|
|
|
|
f.`forum_archived` as `topic_archived`, t.`topic_deleted`, t.`topic_bumped`,
|
2019-01-05 18:21:48 +01:00
|
|
|
((%s) & %d) as `can_view_deleted`,
|
2019-01-03 16:57:17 +01:00
|
|
|
(
|
|
|
|
SELECT MIN(`post_id`)
|
|
|
|
FROM `msz_forum_posts`
|
|
|
|
WHERE `topic_id` = t.`topic_id`
|
2019-01-05 18:21:48 +01:00
|
|
|
AND (`can_view_deleted` OR `post_deleted` IS NULL)
|
2019-01-03 16:57:17 +01:00
|
|
|
) as `topic_first_post_id`,
|
|
|
|
(
|
|
|
|
SELECT COUNT(`post_id`)
|
|
|
|
FROM `msz_forum_posts`
|
|
|
|
WHERE `topic_id` = t.`topic_id`
|
2019-01-05 18:21:48 +01:00
|
|
|
AND (`can_view_deleted` OR `post_deleted` IS NULL)
|
2019-01-03 16:57:17 +01:00
|
|
|
) as `topic_post_count`
|
|
|
|
FROM `msz_forum_topics` as t
|
|
|
|
LEFT JOIN `msz_forum_categories` as f
|
|
|
|
ON f.`forum_id` = t.`forum_id`
|
|
|
|
WHERE t.`topic_id` = :topic_id
|
|
|
|
',
|
2019-01-05 18:21:48 +01:00
|
|
|
forum_perms_get_user_sql(MSZ_FORUM_PERMS_GENERAL, 't.`forum_id`'),
|
|
|
|
MSZ_FORUM_PERM_DELETE_TOPIC | MSZ_FORUM_PERM_DELETE_ANY_POST
|
2019-01-03 16:57:17 +01:00
|
|
|
));
|
2018-05-24 02:38:42 +02:00
|
|
|
$getTopic->bindValue('topic_id', $topicId);
|
2019-01-05 18:21:48 +01:00
|
|
|
$getTopic->bindValue('perm_user_id_user', $userId);
|
|
|
|
$getTopic->bindValue('perm_user_id_role', $userId);
|
2019-01-09 20:06:02 +01:00
|
|
|
return db_fetch($getTopic);
|
2018-05-24 02:38:42 +02:00
|
|
|
}
|
|
|
|
|
2018-05-23 03:41:57 +02:00
|
|
|
function forum_topic_bump(int $topicId): bool
|
|
|
|
{
|
2018-10-07 01:30:48 +02:00
|
|
|
$bumpTopic = db_prepare('
|
2018-05-23 03:41:57 +02:00
|
|
|
UPDATE `msz_forum_topics`
|
|
|
|
SET `topic_bumped` = NOW()
|
|
|
|
WHERE `topic_id` = :topic_id
|
2019-01-03 16:57:17 +01:00
|
|
|
AND `topic_deleted` IS NULL
|
2018-05-23 03:41:57 +02:00
|
|
|
');
|
|
|
|
$bumpTopic->bindValue('topic_id', $topicId);
|
|
|
|
return $bumpTopic->execute();
|
|
|
|
}
|
2018-05-24 02:38:42 +02:00
|
|
|
|
|
|
|
function forum_topic_mark_read(int $userId, int $topicId, int $forumId): void
|
|
|
|
{
|
|
|
|
if ($userId < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-07 01:30:48 +02:00
|
|
|
$markAsRead = db_prepare('
|
2018-05-24 02:38:42 +02:00
|
|
|
REPLACE INTO `msz_forum_topics_track`
|
|
|
|
(`user_id`, `topic_id`, `forum_id`, `track_last_read`)
|
|
|
|
VALUES
|
|
|
|
(:user_id, :topic_id, :forum_id, NOW())
|
|
|
|
');
|
|
|
|
$markAsRead->bindValue('user_id', $userId);
|
|
|
|
$markAsRead->bindValue('topic_id', $topicId);
|
|
|
|
$markAsRead->bindValue('forum_id', $forumId);
|
|
|
|
$markAsRead->execute();
|
|
|
|
}
|
|
|
|
|
2018-12-22 12:02:26 +01:00
|
|
|
function forum_topic_listing(int $forumId, int $userId, int $offset = 0, int $take = 0, bool $showDeleted = false): array
|
2018-05-24 02:38:42 +02:00
|
|
|
{
|
|
|
|
$hasPagination = $offset >= 0 && $take > 0;
|
2018-12-22 12:02:26 +01:00
|
|
|
$getTopics = db_prepare(sprintf(
|
|
|
|
'
|
|
|
|
SELECT
|
2019-01-04 02:40:18 +01:00
|
|
|
:user_id AS `target_user_id`,
|
2018-12-22 12:02:26 +01:00
|
|
|
t.`topic_id`, t.`topic_title`, t.`topic_locked`, t.`topic_type`, t.`topic_created`,
|
|
|
|
t.`topic_bumped`, t.`topic_deleted`,
|
2019-01-04 02:40:18 +01:00
|
|
|
au.`user_id` AS `author_id`, au.`username` AS `author_name`,
|
|
|
|
COALESCE(au.`user_colour`, ar.`role_colour`) AS `author_colour`,
|
|
|
|
lp.`post_id` AS `response_id`,
|
|
|
|
lp.`post_created` AS `response_created`,
|
|
|
|
lu.`user_id` AS `respondent_id`,
|
|
|
|
lu.`username` AS `respondent_name`,
|
|
|
|
COALESCE(lu.`user_colour`, lr.`role_colour`) AS `respondent_colour`,
|
2018-12-22 12:02:26 +01:00
|
|
|
(
|
|
|
|
SELECT COUNT(`post_id`)
|
|
|
|
FROM `msz_forum_posts`
|
|
|
|
WHERE `topic_id` = t.`topic_id`
|
2019-01-03 16:57:17 +01:00
|
|
|
%5$s
|
2019-01-04 02:40:18 +01:00
|
|
|
) AS `topic_post_count`,
|
|
|
|
(
|
|
|
|
SELECT CEIL(COUNT(`post_id`) / %6$d)
|
|
|
|
FROM `msz_forum_posts`
|
|
|
|
WHERE `topic_id` = t.`topic_id`
|
|
|
|
%5$s
|
|
|
|
) AS `topic_pages`,
|
2018-12-22 12:02:26 +01:00
|
|
|
(
|
|
|
|
SELECT COUNT(`user_id`)
|
|
|
|
FROM `msz_forum_topics_track`
|
|
|
|
WHERE `topic_id` = t.`topic_id`
|
2019-01-04 02:40:18 +01:00
|
|
|
) AS `topic_view_count`,
|
2018-12-22 12:02:26 +01:00
|
|
|
(
|
|
|
|
SELECT
|
|
|
|
`target_user_id` > 0
|
|
|
|
AND
|
|
|
|
t.`topic_bumped` > NOW() - INTERVAL 1 MONTH
|
|
|
|
AND (
|
|
|
|
SELECT COUNT(ti.`topic_id`) < 1
|
2019-01-04 02:40:18 +01:00
|
|
|
FROM `msz_forum_topics_track` AS tt
|
|
|
|
RIGHT JOIN `msz_forum_topics` AS ti
|
2018-12-22 12:02:26 +01:00
|
|
|
ON ti.`topic_id` = tt.`topic_id`
|
|
|
|
WHERE ti.`topic_id` = t.`topic_id`
|
|
|
|
AND tt.`user_id` = `target_user_id`
|
2019-01-03 16:57:17 +01:00
|
|
|
AND `track_last_read` >= `topic_bumped`
|
2018-12-22 12:02:26 +01:00
|
|
|
)
|
2019-01-04 02:40:18 +01:00
|
|
|
) AS `topic_unread`
|
|
|
|
FROM `msz_forum_topics` AS t
|
|
|
|
LEFT JOIN `msz_users` AS au
|
2018-12-22 12:02:26 +01:00
|
|
|
ON t.`user_id` = au.`user_id`
|
2019-01-04 02:40:18 +01:00
|
|
|
LEFT JOIN `msz_roles` AS ar
|
2018-12-22 12:02:26 +01:00
|
|
|
ON ar.`role_id` = au.`display_role`
|
2019-01-04 02:40:18 +01:00
|
|
|
LEFT JOIN `msz_forum_posts` AS lp
|
2018-12-22 12:02:26 +01:00
|
|
|
ON lp.`post_id` = (
|
|
|
|
SELECT `post_id`
|
|
|
|
FROM `msz_forum_posts`
|
|
|
|
WHERE `topic_id` = t.`topic_id`
|
2019-01-03 16:57:17 +01:00
|
|
|
%5$s
|
2018-12-22 12:02:26 +01:00
|
|
|
ORDER BY `post_id` DESC
|
|
|
|
LIMIT 1
|
|
|
|
)
|
2019-01-04 02:40:18 +01:00
|
|
|
LEFT JOIN `msz_users` AS lu
|
2018-12-22 12:02:26 +01:00
|
|
|
ON lu.`user_id` = lp.`user_id`
|
2019-01-04 02:40:18 +01:00
|
|
|
LEFT JOIN `msz_roles` AS lr
|
2018-12-22 12:02:26 +01:00
|
|
|
ON lr.`role_id` = lu.`display_role`
|
2018-12-30 19:58:30 +01:00
|
|
|
WHERE (
|
|
|
|
t.`forum_id` = :forum_id
|
|
|
|
OR t.`topic_type` = %3$d
|
|
|
|
)
|
2018-12-22 12:02:26 +01:00
|
|
|
%1$s
|
2018-12-30 19:58:30 +01:00
|
|
|
ORDER BY FIELD(t.`topic_type`, %4$s) DESC, t.`topic_bumped` DESC
|
2018-12-22 12:02:26 +01:00
|
|
|
%2$s
|
|
|
|
',
|
|
|
|
$showDeleted ? '' : 'AND t.`topic_deleted` IS NULL',
|
2018-12-30 19:58:30 +01:00
|
|
|
$hasPagination ? 'LIMIT :offset, :take' : '',
|
|
|
|
MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT,
|
2019-01-03 16:57:17 +01:00
|
|
|
implode(',', array_reverse(MSZ_TOPIC_TYPE_ORDER)),
|
2019-01-04 02:40:18 +01:00
|
|
|
$showDeleted ? '' : 'AND `post_deleted` IS NULL',
|
|
|
|
MSZ_FORUM_POSTS_PER_PAGE
|
2018-12-22 12:02:26 +01:00
|
|
|
));
|
2018-05-24 02:38:42 +02:00
|
|
|
$getTopics->bindValue('forum_id', $forumId);
|
|
|
|
$getTopics->bindValue('user_id', $userId);
|
|
|
|
|
|
|
|
if ($hasPagination) {
|
|
|
|
$getTopics->bindValue('offset', $offset);
|
|
|
|
$getTopics->bindValue('take', $take);
|
|
|
|
}
|
|
|
|
|
2019-01-09 20:06:02 +01:00
|
|
|
return db_fetch_all($getTopics);
|
2018-05-24 02:38:42 +02:00
|
|
|
}
|