Gazelle/sections/ajax/inbox/inbox.php

116 lines
3.1 KiB
PHP
Raw Normal View History

2013-05-28 08:01:02 +00:00
<?php
$UserID = $LoggedUser['ID'];
2013-05-05 08:00:31 +00:00
if (empty($_GET['type'])) {
$Section = 'inbox';
} else {
$Section = $_GET['type']; // either 'inbox' or 'sentbox'
}
2013-05-05 08:00:31 +00:00
if (!in_array($Section, array('inbox', 'sentbox'))) {
print
json_encode(
array(
'status' => 'failure'
)
);
die();
}
2013-05-28 08:01:02 +00:00
list($Page, $Limit) = Format::page_limit(MESSAGES_PER_PAGE);
$Sort = empty($_GET['sort']) || $_GET['sort'] != "unread" ? "Date DESC" : "cu.Unread = '1' DESC, DATE DESC";
2013-05-05 08:00:31 +00:00
$sql = "
SELECT
SQL_CALC_FOUND_ROWS
c.ID,
c.Subject,
cu.Unread,
cu.Sticky,
cu.ForwardedTo,
um2.Username AS ForwardedName,
cu2.UserID,
um.Username,
ui.Donor,
ui.Warned,
2013-06-13 08:01:05 +00:00
um.Enabled,
ui.Avatar,";
$sql .= ($Section == 'sentbox')? ' cu.SentDate ' : ' cu.ReceivedDate ';
$sql .= "AS Date
FROM pm_conversations AS c
2013-05-05 08:00:31 +00:00
LEFT JOIN pm_conversations_users AS cu ON cu.ConvID=c.ID AND cu.UserID='$UserID'
LEFT JOIN pm_conversations_users AS cu2 ON cu2.ConvID=c.ID AND cu2.UserID!='$UserID' AND cu2.ForwardedTo=0
LEFT JOIN users_main AS um ON um.ID=cu2.UserID
LEFT JOIN users_info AS ui ON ui.UserID=um.ID
LEFT JOIN users_main AS um2 ON um2.ID=cu.ForwardedTo";
2013-05-05 08:00:31 +00:00
if (!empty($_GET['search']) && $_GET['searchtype'] == "message") {
$sql .= " JOIN pm_messages AS m ON c.ID=m.ConvID";
}
$sql .= " WHERE ";
2013-05-05 08:00:31 +00:00
if (!empty($_GET['search'])) {
$Search = db_string($_GET['search']);
2013-05-05 08:00:31 +00:00
if ($_GET['searchtype'] == "user") {
$sql .= "um.Username LIKE '".$Search."' AND ";
2013-05-05 08:00:31 +00:00
} elseif ($_GET['searchtype'] == "subject") {
$Words = explode(' ', $Search);
$sql .= "c.Subject LIKE '%".implode("%' AND c.Subject LIKE '%", $Words)."%' AND ";
2013-05-05 08:00:31 +00:00
} elseif ($_GET['searchtype'] == "message") {
$Words = explode(' ', $Search);
$sql .= "m.Body LIKE '%".implode("%' AND m.Body LIKE '%", $Words)."%' AND ";
}
}
2013-05-05 08:00:31 +00:00
$sql .= ($Section == 'sentbox') ? ' cu.InSentbox' : ' cu.InInbox';
$sql .="='1'";
$sql .=" GROUP BY c.ID
ORDER BY cu.Sticky, ".$Sort." LIMIT $Limit";
$Results = $DB->query($sql);
$DB->query('SELECT FOUND_ROWS()');
list($NumResults) = $DB->next_record();
$DB->set_query_id($Results);
2012-10-11 08:00:15 +00:00
$CurURL = Format::get_url(array('sort'));
2013-05-05 08:00:31 +00:00
if (empty($CurURL)) {
$CurURL = "inbox.php?";
} else {
$CurURL = "inbox.php?".$CurURL."&";
}
2013-05-15 08:00:54 +00:00
$Pages = Format::get_pages($Page, $NumResults, MESSAGES_PER_PAGE, 9);
$JsonMessages = array();
2013-06-13 08:01:05 +00:00
while (list($ConvID, $Subject, $Unread, $Sticky, $ForwardedID, $ForwardedName, $SenderID, $Username, $Donor, $Warned, $Enabled, $Avatar, $Date) = $DB->next_record()) {
$JsonMessage = array(
2011-11-26 08:00:20 +00:00
'convId' => (int) $ConvID,
'subject' => $Subject,
2011-11-26 08:00:20 +00:00
'unread' => $Unread == 1,
'sticky' => $Sticky == 1,
'forwardedId' => (int) $ForwardedID,
'forwardedName' => $ForwardedName,
2011-11-26 08:00:20 +00:00
'senderId' => (int) $SenderID,
'username' => $Username,
2013-06-13 08:01:05 +00:00
'avatar' => $Avatar,
2011-11-26 08:00:20 +00:00
'donor' => $Donor == 1,
'warned' => $Warned == 1,
'enabled' => ($Enabled == 2 ? false : true),
'date' => $Date
);
$JsonMessages[] = $JsonMessage;
}
print
json_encode(
array(
'status' => 'success',
'response' => array(
2011-11-26 08:00:20 +00:00
'currentPage' => (int) $Page,
2013-05-05 08:00:31 +00:00
'pages' => ceil($NumResults / MESSAGES_PER_PAGE),
'messages' => $JsonMessages
)
)
);
2013-05-05 08:00:31 +00:00
?>