Gazelle/sections/ajax/userhistory/post_history.php

191 lines
4.6 KiB
PHP
Raw Normal View History

2013-05-28 08:01:02 +00:00
<?php
2012-07-28 08:00:17 +00:00
/*
User post history page
*/
2013-04-13 08:00:19 +00:00
function error_out($reason = '') {
2013-04-17 08:00:58 +00:00
$error = array('status' => 'failure');
2013-07-13 08:00:46 +00:00
if ($reason !== '')
2013-04-17 08:00:58 +00:00
$error['reason'] = $reason;
print $error;
die();
2012-07-28 08:00:17 +00:00
}
2013-04-13 08:00:19 +00:00
if (!empty($LoggedUser['DisableForums'])) {
2013-04-17 08:00:58 +00:00
error_out('You do not have access to the forums!');
2012-07-28 08:00:17 +00:00
}
2013-05-27 08:00:58 +00:00
include(SERVER_ROOT.'/classes/text.class.php'); // Text formatting class
2012-07-28 08:00:17 +00:00
$Text = new TEXT;
$UserID = empty($_GET['userid']) ? $LoggedUser['ID'] : $_GET['userid'];
2013-04-13 08:00:19 +00:00
if (!is_number($UserID)) {
2013-04-17 08:00:58 +00:00
error_out('User does not exist!');
2012-07-28 08:00:17 +00:00
}
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);
2012-07-28 08:00:17 +00:00
2013-05-11 08:00:29 +00:00
$UserInfo = Users::user_info($UserID);
extract(array_intersect_key($UserInfo, array_flip(array('Username', 'Enabled', 'Title', 'Avatar', 'Donor', 'Warned'))));
2012-07-28 08:00:17 +00:00
2013-07-13 08:00:46 +00:00
$ViewingOwn = ($UserID === $LoggedUser['ID']);
2012-07-28 08:00:17 +00:00
$ShowUnread = ($ViewingOwn && (!isset($_GET['showunread']) || !!$_GET['showunread']));
$ShowGrouped = ($ViewingOwn && (!isset($_GET['group']) || !!$_GET['group']));
2013-04-13 08:00:19 +00:00
if ($ShowGrouped) {
2013-07-24 08:00:46 +00:00
$SQL = '
2013-07-13 08:00:46 +00:00
SELECT
2013-07-24 08:00:46 +00:00
SQL_CALC_FOUND_ROWS
MAX(p.ID) AS ID
2012-07-28 08:00:17 +00:00
FROM forums_posts AS p
2013-04-17 08:00:58 +00:00
LEFT JOIN forums_topics AS t ON t.ID = p.TopicID';
2013-04-13 08:00:19 +00:00
if ($ShowUnread) {
2013-07-24 08:00:46 +00:00
$SQL .= '
2013-04-17 08:00:58 +00:00
LEFT JOIN forums_last_read_topics AS l ON l.TopicID = t.ID AND l.UserID = '.$LoggedUser['ID'];
2012-07-28 08:00:17 +00:00
}
2013-07-24 08:00:46 +00:00
$SQL .= '
2013-04-17 08:00:58 +00:00
LEFT JOIN forums AS f ON f.ID = t.ForumID
2012-07-28 08:00:17 +00:00
WHERE p.AuthorID = '.$UserID.'
2013-08-28 23:08:41 +00:00
AND ' . Forums::user_forums_sql();
2013-04-13 08:00:19 +00:00
if ($ShowUnread) {
2013-07-24 08:00:46 +00:00
$SQL .= '
AND ((t.IsLocked = \'0\' OR t.IsSticky = \'1\')
AND (l.PostID < t.LastPostID OR l.PostID IS NULL))';
2012-07-28 08:00:17 +00:00
}
2013-07-24 08:00:46 +00:00
$SQL .= "
2012-07-28 08:00:17 +00:00
GROUP BY t.ID
2013-07-13 08:00:46 +00:00
ORDER BY p.ID DESC
LIMIT $Limit";
2013-07-24 08:00:46 +00:00
$PostIDs = $DB->query($SQL);
2013-07-13 08:00:46 +00:00
$DB->query('SELECT FOUND_ROWS()');
2012-07-28 08:00:17 +00:00
list($Results) = $DB->next_record();
2013-04-13 08:00:19 +00:00
if ($Results > $PerPage * ($Page - 1)) {
2012-07-28 08:00:17 +00:00
$DB->set_query_id($PostIDs);
$PostIDs = $DB->collect('ID');
2013-07-24 08:00:46 +00:00
$SQL = "
2013-07-13 08:00:46 +00:00
SELECT
p.ID,
p.AddedTime,
p.Body,
p.EditedUserID,
p.EditedTime,
ed.Username,
p.TopicID,
t.Title,
t.LastPostID,
l.PostID AS LastRead,
t.IsLocked,
t.IsSticky
2012-07-28 08:00:17 +00:00
FROM forums_posts as p
2013-04-17 08:00:58 +00:00
LEFT JOIN users_main AS um ON um.ID = p.AuthorID
LEFT JOIN users_info AS ui ON ui.UserID = p.AuthorID
LEFT JOIN users_main AS ed ON ed.ID = p.EditedUserID
JOIN forums_topics AS t ON t.ID = p.TopicID
JOIN forums AS f ON f.ID = t.ForumID
2013-07-13 08:00:46 +00:00
LEFT JOIN forums_last_read_topics AS l ON l.UserID = $UserID AND l.TopicID = t.ID
WHERE p.ID IN (".implode(',', $PostIDs).')
2012-07-28 08:00:17 +00:00
ORDER BY p.ID DESC';
2013-07-24 08:00:46 +00:00
$Posts = $DB->query($SQL);
2012-07-28 08:00:17 +00:00
}
} else {
2013-07-24 08:00:46 +00:00
$SQL = '
2013-07-13 08:00:46 +00:00
SELECT
SQL_CALC_FOUND_ROWS';
2013-04-13 08:00:19 +00:00
if ($ShowGrouped) {
2013-07-24 08:00:46 +00:00
$SQL .= '
2013-07-13 08:00:46 +00:00
*
FROM (
SELECT';
2012-07-28 08:00:17 +00:00
}
2013-07-24 08:00:46 +00:00
$SQL .= '
2013-07-13 08:00:46 +00:00
p.ID,
p.AddedTime,
p.Body,
p.EditedUserID,
p.EditedTime,
ed.Username,
p.TopicID,
t.Title,
t.LastPostID,';
if ($UserID === $LoggedUser['ID']) {
2013-07-24 08:00:46 +00:00
$SQL .= '
2013-07-13 08:00:46 +00:00
l.PostID AS LastRead,';
2012-07-28 08:00:17 +00:00
}
2013-07-24 08:00:46 +00:00
$SQL .= "
2013-07-13 08:00:46 +00:00
t.IsLocked,
t.IsSticky
FROM forums_posts as p
LEFT JOIN users_main AS um ON um.ID = p.AuthorID
LEFT JOIN users_info AS ui ON ui.UserID = p.AuthorID
LEFT JOIN users_main AS ed ON ed.ID = p.EditedUserID
JOIN forums_topics AS t ON t.ID = p.TopicID
JOIN forums AS f ON f.ID = t.ForumID
LEFT JOIN forums_last_read_topics AS l ON l.UserID = $UserID AND l.TopicID = t.ID
WHERE p.AuthorID = $UserID
2013-08-28 23:08:41 +00:00
AND " . Forums::user_forums_sql();
2012-07-28 08:00:17 +00:00
2013-04-13 08:00:19 +00:00
if ($ShowUnread) {
2013-07-24 08:00:46 +00:00
$SQL .= '
2013-07-13 08:00:46 +00:00
AND ((t.IsLocked = \'0\' OR t.IsSticky = \'1\')
AND (l.PostID < t.LastPostID OR l.PostID IS NULL)
) ';
2012-07-28 08:00:17 +00:00
}
2013-02-22 08:00:24 +00:00
2013-07-24 08:00:46 +00:00
$SQL .= '
2013-07-13 08:00:46 +00:00
ORDER BY p.ID DESC';
2013-02-22 08:00:24 +00:00
2013-04-13 08:00:19 +00:00
if ($ShowGrouped) {
2013-07-24 08:00:46 +00:00
$SQL .= '
2012-07-28 08:00:17 +00:00
) AS sub
2013-07-13 08:00:46 +00:00
GROUP BY TopicID
ORDER BY ID DESC';
2012-07-28 08:00:17 +00:00
}
2013-02-22 08:00:24 +00:00
2013-07-24 08:00:46 +00:00
$SQL .= "
2013-07-13 08:00:46 +00:00
LIMIT $Limit";
2013-07-24 08:00:46 +00:00
$Posts = $DB->query($SQL);
2013-02-22 08:00:24 +00:00
2013-07-13 08:00:46 +00:00
$DB->query('SELECT FOUND_ROWS()');
2012-07-28 08:00:17 +00:00
list($Results) = $DB->next_record();
2013-02-22 08:00:24 +00:00
2012-07-28 08:00:17 +00:00
$DB->set_query_id($Posts);
}
$JsonResults = array();
2013-04-13 08:00:19 +00:00
while (list($PostID, $AddedTime, $Body, $EditedUserID, $EditedTime, $EditedUsername, $TopicID, $ThreadTitle, $LastPostID, $LastRead, $Locked, $Sticky) = $DB->next_record()) {
2013-04-17 08:00:58 +00:00
$JsonResults[] = array(
'postId' => (int) $PostID,
'topicId' => (int) $TopicID,
'threadTitle' => $ThreadTitle,
'lastPostId' => (int) $LastPostID,
'lastRead' => (int) $LastRead,
2013-07-13 08:00:46 +00:00
'locked' => $Locked === '1',
'sticky' => $Sticky === '1',
2013-04-17 08:00:58 +00:00
'addedTime' => $AddedTime,
'body' => $Text->full_format($Body),
'bbbody' => $Body,
'editedUserId' => (int) $EditedUserID,
'editedTime' => $EditedTime,
'editedUsername' => $EditedUsername
);
2012-07-28 08:00:17 +00:00
}
print json_encode(
2013-04-17 08:00:58 +00:00
array(
'status' => 'success',
'response' => array(
'currentPage' => (int) $Page,
2013-07-13 08:00:46 +00:00
'pages' => ceil($Results / $PerPage),
2013-04-17 08:00:58 +00:00
'threads' => $JsonResults
)
)
);