Skip to content

Commit

Permalink
Merge pull request #106 from JosJuice/patch-1
Browse files Browse the repository at this point in the history
Avoid double redirect when no new posts are found
  • Loading branch information
Quy committed Apr 24, 2014
2 parents d8feea4 + e108655 commit 7b6d390
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions viewtopic.php
Expand Up @@ -39,41 +39,42 @@

$_GET['p'] = ceil($num_posts / $pun_user['disp_posts']);
}

// If action=new, we redirect to the first new post (if any)
else if ($action == 'new')
else
{
if (!$pun_user['is_guest'])
// If action=new, we redirect to the first new post (if any)
if ($action == 'new')
{
// We need to check if this topic has been viewed recently by the user
$tracked_topics = get_tracked_topics();
$last_viewed = isset($tracked_topics['topics'][$id]) ? $tracked_topics['topics'][$id] : $pun_user['last_visit'];
if (!$pun_user['is_guest'])
{
// We need to check if this topic has been viewed recently by the user
$tracked_topics = get_tracked_topics();
$last_viewed = isset($tracked_topics['topics'][$id]) ? $tracked_topics['topics'][$id] : $pun_user['last_visit'];

$result = $db->query('SELECT MIN(id) FROM '.$db->prefix.'posts WHERE topic_id='.$id.' AND posted>'.$last_viewed) or error('Unable to fetch first new post info', __FILE__, __LINE__, $db->error());
$first_new_post_id = $db->result($result);
$result = $db->query('SELECT MIN(id) FROM '.$db->prefix.'posts WHERE topic_id='.$id.' AND posted>'.$last_viewed) or error('Unable to fetch first new post info', __FILE__, __LINE__, $db->error());
$first_new_post_id = $db->result($result);

if ($first_new_post_id)
{
header('Location: viewtopic.php?pid='.$first_new_post_id.'#p'.$first_new_post_id);
exit;
if ($first_new_post_id)
{
header('Location: viewtopic.php?pid='.$first_new_post_id.'#p'.$first_new_post_id);
exit;
}
}
}

// If there is no new post, we go to the last post
header('Location: viewtopic.php?id='.$id.'&action=last');
exit;
}

// If action=last, we redirect to the last post
else if ($action == 'last')
{
$result = $db->query('SELECT MAX(id) FROM '.$db->prefix.'posts WHERE topic_id='.$id) or error('Unable to fetch last post info', __FILE__, __LINE__, $db->error());
$last_post_id = $db->result($result);
// If there is no new post, we go to the last post
$action = 'last';
}

if ($last_post_id)
// If action=last, we redirect to the last post
if ($action == 'last')
{
header('Location: viewtopic.php?pid='.$last_post_id.'#p'.$last_post_id);
exit;
$result = $db->query('SELECT MAX(id) FROM '.$db->prefix.'posts WHERE topic_id='.$id) or error('Unable to fetch last post info', __FILE__, __LINE__, $db->error());
$last_post_id = $db->result($result);

if ($last_post_id)
{
header('Location: viewtopic.php?pid='.$last_post_id.'#p'.$last_post_id);
exit;
}
}
}

Expand Down

0 comments on commit 7b6d390

Please sign in to comment.