Topic: Help in modifying this topic sort order hack

I know this is a bit lame of me, but I am all thumbs in SQL or PHP.

Could I ask for some advice and coding help from folks with more experience of both?

There is a code hack on the Punres wiki to allow another sort option within the Admin interface -  to allow admins to specify that specific topics should display topic posts in reverse order:

viz:
http://wiki.punres.org/Sort_topics_in_reverse_order

Could someone advise me how to modify it to allow alphabetical sorting of topic posts?

I think I am on the right path by working from the hack posted in the punres wiki.

Could this work:
(a) creating a new option value '3' for 'Alphabetical' in admin_forums.php:
viz:

<option value="3"<?php if ($cur_forum['sort_by'] == '3') echo ' selected="selected"' ?>>Alphabetical</option>

and then
(b) modifying viewforum.php to add a new 'else if' declaration like so:

} else if ($cur_forum['sort_by'] == '3') {
    $newsort = 'natsort';
}

and then
(c) doing the rest of the replacements suggested for viewforum.php so that the SQL queries pick up the newsort variable?

But would 'natsort' be the right thing to work here? I thought another 'ORDER BY DESC' would be right.

I am muddling towards implementation of the feature request I raised recently:
http://punbb.org/forums/viewtopic.php?id=15585
which has had no response from anyone so far, alas.

Re: Help in modifying this topic sort order hack

You wouldn't use a PHP function (natsort) to sort it wink
What you would want to do is create a series of if statements for the sort_by values and then replace the places the code uses

(($cur_forum['sort_by'] == '1') ? 'posted' : 'last_post')

with your new variable

3 (edited by soonotes 2007-04-21 19:28)

Re: Help in modifying this topic sort order hack

Something like this

Open admin_forums.php, around line 283 find

                                            <option value="0"<?php if ($cur_forum['sort_by'] == '0') echo ' selected="selected"' ?>>Last post</option>
                                            <option value="1"<?php if ($cur_forum['sort_by'] == '1') echo ' selected="selected"' ?>>Topic start</option>

Replace with

                                            <option value="0"<?php if ($cur_forum['sort_by'] == '0') echo ' selected="selected"' ?>>Last post</option>
                                            <option value="1"<?php if ($cur_forum['sort_by'] == '1') echo ' selected="selected"' ?>>Topic start</option>
                                            <option value="2"<?php if ($cur_forum['sort_by'] == '2') echo ' selected="selected"' ?>>Subject</option>

Now open viewforum.php and find around line 109

// Fetch list of topics to display on this page
if ($pun_user['is_guest'] || $pun_config['o_show_dot'] == '0')
{
    // Without "the dot"
    $sql = 'SELECT id, poster, subject, posted, last_post, last_post_id, last_poster, num_views, num_replies, closed, sticky, moved_to FROM '.$db->prefix.'topics WHERE forum_id='.$id.' ORDER BY sticky DESC, '.(($cur_forum['sort_by'] == '1') ? 'posted' : 'last_post').' DESC LIMIT '.$start_from.', '.$pun_user['disp_topics'];
}
else
{
    // With "the dot"
    switch ($db_type)
    {
        case 'mysql':
        case 'mysqli':
            $sql = 'SELECT p.poster_id AS has_posted, t.id, t.subject, t.poster, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, t.closed, t.sticky, t.moved_to FROM '.$db->prefix.'topics AS t LEFT JOIN '.$db->prefix.'posts AS p ON t.id=p.topic_id AND p.poster_id='.$pun_user['id'].' WHERE t.forum_id='.$id.' GROUP BY t.id ORDER BY sticky DESC, '.(($cur_forum['sort_by'] == '1') ? 'posted' : 'last_post').' DESC LIMIT '.$start_from.', '.$pun_user['disp_topics'];
            break;

        case 'sqlite':
            $sql = 'SELECT p.poster_id AS has_posted, t.id, t.subject, t.poster, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, t.closed, t.sticky, t.moved_to FROM '.$db->prefix.'topics AS t LEFT JOIN '.$db->prefix.'posts AS p ON t.id=p.topic_id AND p.poster_id='.$pun_user['id'].' WHERE t.id IN(SELECT id FROM '.$db->prefix.'topics WHERE forum_id='.$id.' ORDER BY sticky DESC, '.(($cur_forum['sort_by'] == '1') ? 'posted' : 'last_post').' DESC LIMIT '.$start_from.', '.$pun_user['disp_topics'].') GROUP BY t.id ORDER BY t.sticky DESC, t.last_post DESC';
            break;

        default:
            $sql = 'SELECT p.poster_id AS has_posted, t.id, t.subject, t.poster, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, t.closed, t.sticky, t.moved_to FROM '.$db->prefix.'topics AS t LEFT JOIN '.$db->prefix.'posts AS p ON t.id=p.topic_id AND p.poster_id='.$pun_user['id'].' WHERE t.forum_id='.$id.' GROUP BY t.id, t.subject, t.poster, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, t.closed, t.sticky, t.moved_to, p.poster_id ORDER BY sticky DESC, '.(($cur_forum['sort_by'] == '1') ? 'posted' : 'last_post').' DESC LIMIT '.$start_from.', '.$pun_user['disp_topics'];
            break;

    }
}

and replace with 

if ($cur_forum['sort_by'] == 2)
{
    $order = 'ASC';
    $by = 'subject';
}
else if ($cur_forum['sort_by'] == 1)
{
    $order = 'DESC';
    $by = 'posted';
}
else
{
    $order = 'DESC';
    $by = 'last_post';
}
// Fetch list of topics to display on this page
if ($pun_user['is_guest'] || $pun_config['o_show_dot'] == '0')
{
    // Without "the dot"
    $sql = 'SELECT id, poster, subject, posted, last_post, last_post_id, last_poster, num_views, num_replies, closed, sticky, moved_to FROM '.$db->prefix.'topics WHERE forum_id='.$id.' ORDER BY sticky DESC, '.$by.' '.$order.' LIMIT '.$start_from.', '.$pun_user['disp_topics'];
}
else
{
    // With "the dot"
    switch ($db_type)
    {
        case 'mysql':
        case 'mysqli':
            $sql = 'SELECT p.poster_id AS has_posted, t.id, t.subject, t.poster, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, t.closed, t.sticky, t.moved_to FROM '.$db->prefix.'topics AS t LEFT JOIN '.$db->prefix.'posts AS p ON t.id=p.topic_id AND p.poster_id='.$pun_user['id'].' WHERE t.forum_id='.$id.' GROUP BY t.id ORDER BY sticky DESC, '.$by.' '.$order.' LIMIT '.$start_from.', '.$pun_user['disp_topics'];
            break;

        case 'sqlite':
            $sql = 'SELECT p.poster_id AS has_posted, t.id, t.subject, t.poster, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, t.closed, t.sticky, t.moved_to FROM '.$db->prefix.'topics AS t LEFT JOIN '.$db->prefix.'posts AS p ON t.id=p.topic_id AND p.poster_id='.$pun_user['id'].' WHERE t.id IN(SELECT id FROM '.$db->prefix.'topics WHERE forum_id='.$id.' ORDER BY sticky DESC, '.$by.' '.$order.' LIMIT '.$start_from.', '.$pun_user['disp_topics'].') GROUP BY t.id ORDER BY t.sticky DESC, '.$by.' '.$order.'';
            break;

        default:
            $sql = 'SELECT p.poster_id AS has_posted, t.id, t.subject, t.poster, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, t.closed, t.sticky, t.moved_to FROM '.$db->prefix.'topics AS t LEFT JOIN '.$db->prefix.'posts AS p ON t.id=p.topic_id AND p.poster_id='.$pun_user['id'].' WHERE t.forum_id='.$id.' GROUP BY t.id, t.subject, t.poster, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, t.closed, t.sticky, t.moved_to, p.poster_id ORDER BY sticky DESC, '.$by.' '.$order.' LIMIT '.$start_from.', '.$pun_user['disp_topics'];
            break;

    }
}

edited to correct as per smartys comment

Not sure about the sqlite query though

Re: Help in modifying this topic sort order hack

Except

ORDER BY sticky '.$order.', '.$by.'

should be

ORDER BY sticky DESC, '.$by.' '.$order.'

Re: Help in modifying this topic sort order hack

Thanks guys - I really appreciate the tips.

I'll now study your code and the punBB code and see if I can make it all work together. Wish me luck smile

Re: Help in modifying this topic sort order hack

Works great!!

Thanks all - this will be really useful to a punBB project of mine where alpha sorting of topics just made so much sense.

Soonotes, can I suggest that you submit your code to the punres wiki - I'm sure others will find it useful. I wouldn't want to take credit for your code.

pogenwurst may also like to post this solution to the 'How do I' FAQ sticky in the punbb 'Modifications' forum:

http://punbb.org/forums/viewtopic.php?id=10134

And while I am dishing out suggestions left and right, could this be rolled into the code for punBB proper by one of the developers?

It doesn't add too much 'weight' to the codebase but is very useful.

Re: Help in modifying this topic sort order hack

sirena wrote:

Works great!!

Thanks all - this will be really useful to a punBB project of mine where alpha sorting of topics just made so much sense.

Soonotes, can I suggest that you submit your code to the punres wiki - I'm sure others will find it useful. I wouldn't want to take credit for your code.

pogenwurst may also like to post this solution to the 'How do I' FAQ sticky in the punbb 'Modifications' forum:

http://punbb.org/forums/viewtopic.php?id=10134

And while I am dishing out suggestions left and right, could this be rolled into the code for punBB proper by one of the developers?

It doesn't add too much 'weight' to the codebase but is very useful.

At the same time, we could add any number of possible sorts for just that reason wink
I'm sure it can be written as an extension for 1.3 smile

Re: Help in modifying this topic sort order hack

Fair enough Smartys. You da man smile

Will the extensions system be really that flexible? Sounds pretty impressive if it will be able to sweep away these sort of direct code hacks. Make maintenance/upgrades a lot easier, that's for sure.

Re: Help in modifying this topic sort order hack

Hopefully we'll put hooks in enough places that it will be that flexible wink

Re: Help in modifying this topic sort order hack

sirena wrote:

Soonotes, can I suggest that you submit your code to the punres wiki - I'm sure others will find it useful. I wouldn't want to take credit for your code.

Not familiar with the wiki but if you think others will find it useful go ahead and submit it. Doesn't really matter who does it, just that it's there for those who want it.