Gazelle/sections/ajax/subscriptions.php

93 lines
2.8 KiB
PHP
Raw Normal View History

2013-05-28 08:01:02 +00:00
<?php
/*
User topic subscription page
*/
2013-05-04 08:00:48 +00:00
if (!empty($LoggedUser['DisableForums'])) {
2013-05-14 08:00:34 +00:00
json_die('failure');
}
if (isset($LoggedUser['PostsPerPage'])) {
$PerPage = $LoggedUser['PostsPerPage'];
} else {
$PerPage = POSTS_PER_PAGE;
}
2013-05-28 08:01:02 +00:00
list($Page, $Limit) = Format::page_limit($PerPage);
$ShowUnread = (!isset($_GET['showunread']) && !isset($HeavyInfo['SubscriptionsUnread']) || isset($HeavyInfo['SubscriptionsUnread']) && !!$HeavyInfo['SubscriptionsUnread'] || isset($_GET['showunread']) && !!$_GET['showunread']);
$ShowCollapsed = (!isset($_GET['collapse']) && !isset($HeavyInfo['SubscriptionsCollapse']) || isset($HeavyInfo['SubscriptionsCollapse']) && !!$HeavyInfo['SubscriptionsCollapse'] || isset($_GET['collapse']) && !!$_GET['collapse']);
2013-05-04 08:00:48 +00:00
$sql = '
SELECT
SQL_CALC_FOUND_ROWS
MAX(p.ID) AS ID
FROM forums_posts AS p
2013-05-04 08:00:48 +00:00
LEFT JOIN forums_topics AS t ON t.ID = p.TopicID
JOIN users_subscriptions AS s ON s.TopicID = t.ID
LEFT JOIN forums AS f ON f.ID = t.ForumID
LEFT JOIN forums_last_read_topics AS l ON p.TopicID = l.TopicID AND l.UserID = s.UserID
WHERE s.UserID = '.$LoggedUser['ID'].'
2013-10-30 08:01:19 +00:00
AND p.ID <= IFNULL(l.PostID, t.LastPostID)
2013-08-28 23:08:41 +00:00
AND ' . Forums::user_forums_sql();
2013-05-04 08:00:48 +00:00
if ($ShowUnread) {
$sql .= '
2013-05-04 08:00:48 +00:00
AND IF(l.PostID IS NULL OR (t.IsLocked = \'1\' && t.IsSticky = \'0\'), t.LastPostID, l.PostID) < t.LastPostID';
}
2013-10-30 08:01:19 +00:00
$sql .= "
GROUP BY t.ID
ORDER BY t.LastPostID DESC
2013-10-30 08:01:19 +00:00
LIMIT $Limit";
$PostIDs = $DB->query($sql);
$DB->query('SELECT FOUND_ROWS()');
list($NumResults) = $DB->next_record();
2013-05-04 08:00:48 +00:00
if ($NumResults > $PerPage * ($Page - 1)) {
$DB->set_query_id($PostIDs);
$PostIDs = $DB->collect('ID');
2013-05-04 08:00:48 +00:00
$sql = '
SELECT
f.ID AS ForumID,
f.Name AS ForumName,
p.TopicID,
t.Title,
p.Body,
t.LastPostID,
t.IsLocked,
t.IsSticky,
p.ID,
um.ID,
um.Username,
ui.Avatar,
p.EditedUserID,
p.EditedTime,
ed.Username AS EditedUsername
FROM forums_posts AS p
2013-05-04 08:00:48 +00:00
LEFT JOIN forums_topics AS t ON t.ID = p.TopicID
LEFT JOIN forums AS f ON f.ID = t.ForumID
LEFT JOIN users_main AS um ON um.ID = p.AuthorID
LEFT JOIN users_info AS ui ON ui.UserID = um.ID
LEFT JOIN users_main AS ed ON ed.ID = um.ID
2013-10-30 08:01:19 +00:00
WHERE p.ID IN ('.implode(',', $PostIDs).')
ORDER BY f.Name ASC, t.LastPostID DESC';
$DB->query($sql);
}
$JsonPosts = array();
2013-05-04 08:00:48 +00:00
while (list($ForumID, $ForumName, $TopicID, $ThreadTitle, $Body, $LastPostID, $Locked, $Sticky, $PostID, $AuthorID, $AuthorName, $AuthorAvatar, $EditedUserID, $EditedTime, $EditedUsername) = $DB->next_record()) {
$JsonPost = array(
2013-10-30 08:01:19 +00:00
'forumId' => (int)$ForumID,
'forumName' => $ForumName,
2013-10-30 08:01:19 +00:00
'threadId' => (int)$TopicID,
'threadTitle' => $ThreadTitle,
2013-10-30 08:01:19 +00:00
'postId' => (int)$PostID,
'lastPostId' => (int)$LastPostID,
2011-11-26 08:00:20 +00:00
'locked' => $Locked == 1,
2013-10-30 08:01:19 +00:00
'new' => ($PostID < $LastPostID && !$Locked)
);
$JsonPosts[] = $JsonPost;
}
2013-05-04 08:00:48 +00:00
json_die('success', array(
2013-05-14 08:00:34 +00:00
'threads' => $JsonPosts
2013-04-24 08:00:23 +00:00
));
?>