Set different default column for sorting

Started by Julius_2000, September 18, 2022, 06:23:00 PM

Previous topic - Next topic

Julius_2000

Hi,

I couldn't find any answer to my question in existing topics:

Is there a way to set a different column and direction as default for sorting?
For instance, when I enter Member List to view all the registerd members, I'd like to have the Date Registered be sorted descendingly instead of the first column in the table.

Kindred

You'd have to edit the template,  possibly the source file for the member list
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Julius_2000

#2
Yep, that's what I am looking for. I just can't figure out, though, what the code for that default-to-sort column is.

in Source the Memberlist.php code for sorting looks like this:
/**
 * Sets the label, sort and join info for every custom field column.
 *
 * @return array An array of info about the custom fields for the member list
 */
function getCustFieldsMList()
{
    global $smcFunc;

    $cpf = array();

    $request = $smcFunc['db_query']('', '
        SELECT col_name, field_name, field_desc, field_type, field_options, bbc, enclose, default_value
        FROM {db_prefix}custom_fields
        WHERE active = {int:active}
            AND show_mlist = {int:show}
            AND private < {int:private_level}',
        array(
            'active' => 1,
            'show' => 1,
            'private_level' => 2,
        )
    );

    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        // Get all the data we're gonna need.
        $cpf['columns'][$row['col_name']] = array(
            'label' => tokenTxtReplace($row['field_name']),
            'type' => $row['field_type'],
            'options' => tokenTxtReplace($row['field_options']),
            'bbc' => !empty($row['bbc']),
            'enclose' => $row['enclose'],
            'default_value' => tokenTxtReplace($row['default_value']),
        );

        // Get the right sort method depending on the cust field type.
        if ($row['field_type'] != 'check')
            $cpf['columns'][$row['col_name']]['sort'] = array(
                'down' => 'LENGTH(t' . $row['col_name'] . '.value) > 0 ASC, COALESCE(t' . $row['col_name'] . '.value, \'\') DESC',
                'up' => 'LENGTH(t' . $row['col_name'] . '.value) > 0 DESC, COALESCE(t' . $row['col_name'] . '.value, \'\') ASC'
            );

        else
            $cpf['columns'][$row['col_name']]['sort'] = array(
                'down' => 't' . $row['col_name'] . '.value DESC',
                'up' => 't' . $row['col_name'] . '.value ASC'
            );

        $cpf['join'][$row['col_name']] = 'LEFT JOIN {db_prefix}themes AS t' . $row['col_name'] . ' ON (t' . $row['col_name'] . '.variable = {literal:' . $row['col_name'] . '} AND t' . $row['col_name'] . '.id_theme = 1 AND t' . $row['col_name'] . '.id_member = mem.id_member)';
    }
    $smcFunc['db_free_result']($request);

    return $cpf;
}


Wait, here's another line apparently for a default sorting.// Set defaults for sort (real_name) and start. (0)
if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']]))
$_REQUEST['sort'] = 'real_name';

if (!is_numeric($_REQUEST['start']))
{
if (preg_match('~^[^\'\\\\/]~' . ($context['utf8'] ? 'u' : ''), $smcFunc['strtolower']($_REQUEST['start']), $match) === 0)
fatal_error('Hacker?', false);

$_REQUEST['start'] = $match[0];

$request = $smcFunc['db_query']('substring', '
SELECT COUNT(*)
FROM {db_prefix}members
WHERE LOWER(SUBSTRING(real_name, 1, 1)) < {string:first_letter}
AND is_activated = {int:is_activated}',
array(
'is_activated' => 1,
'first_letter' => $_REQUEST['start'],
)
);
list ($_REQUEST['start']) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
}
But when I replace the 'real_name' with 'registered', it has no effect when I refresh the page.

Doug Heffernan

#3
The date_registered field should be used instead of real_name, but not only in the example that you posted. In other places too.

Sesquipedalian

If you know how to create a modification package for SMF, then the best way to do this for the member list would be to use the integrate_memberlist_buttons hook to set the default value of $_REQUEST['sort'] to 'registered' and $_REQUEST['desc'] to true.

If you don't know how to do that, you can accomplish the same thing by making the following change to Sources/Memberlist.php:

Code (Find) Select
// Allow mods to add additional buttons here
call_integration_hook('integrate_memberlist_buttons');

Code (Replace) Select
if (!isset($_REQUEST['sort']))
{
$_REQUEST['sort'] = 'registered';
$_REQUEST['desc'] = true;
}

// Allow mods to add additional buttons here
call_integration_hook('integrate_memberlist_buttons');

Note that this will only change the default sort order for the member list in particular. If you want to change the default sort order of other lists or tables, you will need to make changes elsewhere in the code. The exact changes required will vary (sometimes considerably) for each case.
Slava Ukraini!
Heroiam slava!

I promise you nothing.

Sesqu... Sesqui... what?
Sesquipedalian, the best word in the English language.

Diego Andrés

For the topics list there's a packaged mod: https://custom.simplemachines.org/index.php?mod=4340

It could come useful if you wanted to make one for the member list following Sesq's instructions.
One important consideration is that custom sorting (not defined in the layout) will not have a proper indication of what is currently active as a sorting method.

SMF Tricks - Free & Premium Responsive Themes for SMF.

Julius_2000

#6
Quote from: Sesquipedalian on September 19, 2022, 01:42:33 PMIf you know how to create a modification package for SMF, then the best way to do this for the member list would be to use the integrate_memberlist_buttons hook to set the default value of $_REQUEST['sort'] to 'registered' and $_REQUEST['desc'] to true.

If you don't know how to do that, you can accomplish the same thing by making the following change to Sources/Memberlist.php:


Code (Replace) Select
    if (!isset($_REQUEST['sort']))
    {
        $_REQUEST['sort'] = 'registered';
        $_REQUEST['desc'] = true;
    }

    // Allow mods to add additional buttons here
    call_integration_hook('integrate_memberlist_buttons');

Note that this will only change the default sort order for the member list in particular. If you want to change the default sort order of other lists or tables, you will need to make changes elsewhere in the code. The exact changes required will vary (sometimes considerably) for each case.

Thank you so much! That did it! And thanks to your tip, I can just replace real_name with registered and only need to add $_REQUEST['desc'] = true; to the code!

// Set defaults for sort (real_name) and start. (0)
    if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']]))
    {    $_REQUEST['sort'] = 'real_name';       
    }

// Set defaults for sort (real_name) and start. (0)
    if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']]))
    {   $_REQUEST['sort'] = 'registered';       
        $_REQUEST['desc'] = true;
    }

Thanks!

Edit: Ha, that introduced another issue: When I click the letters to sorty by letter, it now tries to sort the registered date by "letter" (it appears clicking the letters is interpreted as the mere sorting logic dependent on the defaul sorting direction, because A would pull the more recent registered dates and Z the earliest dates). Hm.

Advertisement: