Skip to content

Commit

Permalink
Fixed drafts pagination in user profile (topic=558)
Browse files Browse the repository at this point in the history
Signed-off-by: emanuele <emanuele45@gmail.com>
  • Loading branch information
emanuele45 committed Sep 23, 2013
1 parent d1e27e9 commit 59043e6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
10 changes: 4 additions & 6 deletions sources/controllers/Draft.controller.php
Expand Up @@ -79,7 +79,7 @@ public function action_showProfileDrafts()

// Get things started
$user_drafts = array();
$msgCount = draftsCount($memID, 0);
$msgCount = draftsCount($memID, 0, false);
$maxIndex = (int) $modSettings['defaultMaxMessages'];

// Make sure the starting place makes sense and construct our friend the page index.
Expand All @@ -98,8 +98,7 @@ public function action_showProfileDrafts()
// Find this user's drafts
$limit = $start . ', ' . $maxIndex;
$order = 'ud.id_draft ' . ($reverse ? 'ASC' : 'DESC');
$drafts_keep_days = !empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0;
$user_drafts = load_user_drafts($memID, 0, false, $drafts_keep_days, $order, $limit);
$user_drafts = load_user_drafts($memID, 0, false, $order, $limit);

// Start counting at the number of the first message displayed.
$counter = $reverse ? $context['start'] + $maxIndex + 1 : $context['start'];
Expand Down Expand Up @@ -206,7 +205,7 @@ public function action_showPMDrafts()
$_REQUEST['viewscount'] = 10;

// Get the count of applicable drafts
$msgCount = draftsCount($memID, 1);
$msgCount = draftsCount($memID, 1, false);

// Make sure the starting place makes sense and construct our friend the page index.
$context['page_index'] = constructPageIndex($scripturl . '?action=pm;sa=showpmdrafts', $context['start'], $msgCount, $maxIndex);
Expand All @@ -224,8 +223,7 @@ public function action_showPMDrafts()
// Go get em'
$order = 'ud.id_draft ' . ($reverse ? 'ASC' : 'DESC');
$limit = $start . ', ' . $maxIndex;
$drafts_keep_days = !empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0;
$user_drafts = load_user_drafts($memID, 1, false, $drafts_keep_days, $order, $limit);
$user_drafts = load_user_drafts($memID, 1, false, $order, $limit);

// Start counting at the number of the first message displayed.
$counter = $reverse ? $context['start'] + $maxIndex + 1 : $context['start'];
Expand Down
3 changes: 1 addition & 2 deletions sources/controllers/PersonalMessage.controller.php
Expand Up @@ -2903,9 +2903,8 @@ function prepareDraftsContext($member_id, $id_pm = false)
loadDraft((int) $_REQUEST['id_draft'], 1, true, true);

// load all the drafts for this user that meet the criteria
$drafts_keep_days = !empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0;
$order = 'poster_time DESC';
$user_drafts = load_user_drafts($member_id, 1, $id_pm, $drafts_keep_days, $order);
$user_drafts = load_user_drafts($member_id, 1, $id_pm, $order);

// add them to the context draft array for template display
foreach ($user_drafts as $draft)
Expand Down
3 changes: 1 addition & 2 deletions sources/controllers/Post.controller.php
Expand Up @@ -2291,9 +2291,8 @@ private function _prepareDraftsContext($member_id, $id_topic = false)
loadDraft((int) $_REQUEST['id_draft'], 0, true, true);

// load all the drafts for this user that meet the criteria
$drafts_keep_days = !empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0;
$order = 'poster_time DESC';
$user_drafts = load_user_drafts($member_id, 0, $id_topic, $drafts_keep_days, $order);
$user_drafts = load_user_drafts($member_id, 0, $id_topic, $order);

// add them to the context draft array for template display
foreach ($user_drafts as $draft)
Expand Down
21 changes: 13 additions & 8 deletions sources/subs/Drafts.subs.php
Expand Up @@ -230,13 +230,14 @@ function load_draft($id_draft, $uid, $type = 0, $drafts_keep_days = 0, $check =
* @param int $member_id - user id to get drafts for
* @param int $draft_type - 0 for post, 1 for pm
* @param int $topic - if set, load drafts for that specific topic / pm
* @param int $drafts_keep_days - number of days to consider a draft is still valid
* @param string $order - optional parameter to order the results
* @param string $limit - optional parameter to limit the number returned 0,15
* @return array
*/
function load_user_drafts($member_id, $draft_type = 0, $topic = false, $drafts_keep_days = 0, $order = '', $limit = '')
function load_user_drafts($member_id, $draft_type = 0, $topic = false, $order = '', $limit = '')
{
global $modSettings;

$db = database();

// Load the drafts that the user has available for the given type & action
Expand All @@ -245,18 +246,18 @@ function load_user_drafts($member_id, $draft_type = 0, $topic = false, $drafts_k
SELECT ud.*' . ($draft_type === 0 ? ',b.id_board, b.name AS bname' : '') . '
FROM {db_prefix}user_drafts as ud' . ($draft_type === 0 ? '
INNER JOIN {db_prefix}boards AS b ON (b.id_board = ud.id_board)' : '') . '
WHERE ud.id_member = {int:id_member}' . (($draft_type === 0) ? '
WHERE ud.id_member = {int:id_member}' . ($draft_type === 0 ? '
AND id_topic = {int:id_topic}' : (!empty($topic) ? '
AND id_reply = {int:id_topic}' : '')) . '
AND type = {int:draft_type}' . (!empty($drafts_keep_days) ? '
AND type = {int:draft_type}' . (!empty($modSettings['drafts_keep_days']) ? '
AND poster_time > {int:time}' : '') . (!empty($order) ? '
ORDER BY {raw:order}' : '') . (!empty($limit) ? '
LIMIT {raw:limit}' : ''),
array(
'id_member' => $member_id,
'id_topic' => (int) $topic,
'draft_type' => $draft_type,
'time' => $drafts_keep_days,
'time' => !empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0,
'order' => $order,
'limit' => $limit,
)
Expand Down Expand Up @@ -310,8 +311,9 @@ function deleteDrafts($id_draft, $member_id = -1, $check = true)
*
* @param int $member_id
* @param int $draft_type
* @param int $topic
*/
function draftsCount($member_id, $draft_type)
function draftsCount($member_id, $draft_type = 0, $topic = false)
{
global $modSettings;

Expand All @@ -320,13 +322,16 @@ function draftsCount($member_id, $draft_type)
$request = $db->query('', '
SELECT COUNT(id_draft)
FROM {db_prefix}user_drafts
WHERE id_member = {int:id_member}
WHERE id_member = {int:id_member}' . ($draft_type === 0 ? '
AND id_topic = {int:id_topic}' : (!empty($topic) ? '
AND id_reply = {int:id_topic}' : '')) . '
AND type={int:draft_type}' . (!empty($modSettings['drafts_keep_days']) ? '
AND poster_time > {int:time}' : ''),
array(
'id_member' => $member_id,
'id_topic' => (int) $topic,
'draft_type' => $draft_type,
'time' => (!empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0),
'time' => !empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0,
)
);
list ($msgCount) = $db->fetch_row($request);
Expand Down

0 comments on commit 59043e6

Please sign in to comment.