Skip to content

Instantly share code, notes, and snippets.

@joshuaadickerson
Last active December 17, 2015 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuaadickerson/5541980 to your computer and use it in GitHub Desktop.
Save joshuaadickerson/5541980 to your computer and use it in GitHub Desktop.
Load all of the membergroups instead of loading them individually
<?php
/**
* Load all of the membergroups
*
* @global array $smcFunc
* @global array $context
* @global array $modSettings
*/
function loadMemberGroups($force = false)
{
global $smcFunc, $context, $modSettings;
$context['membergroups'] = array();
if ($force || (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2 && ($context['membergroups'] = cache_get_data('all-membergroups')) !== null))
{
$request = $smcFunc['db_query']('', '
SELECT *
FROM {db_prefix}membergroups
ORDER BY group_name',
array()
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$id = (int) $row['id_group'];
$context['membergroups'][$id] = $row;
$context['membergroups'][$id]['assignable'] = $context['membergroups'][$id]['min_posts'] == -1;
$context['membergroups'][$id]['is_post_group'] = $context['membergroups'][$id]['min_posts'] != -1;
}
$smcFunc['db_free_result']($request);
cache_put_data('all-membegroups', $context['membergroups'], 180);
}
}
@joshuaadickerson
Copy link
Author

In loadMemberContext()

// Setup the group info
if (!empty($context['membergroups'][$profile['id_group']]))
{
    $member_group = $context['membergroups'][$profile['id_group']];
    $profile['member_group'] = $member_group['group_name'];
    $profile['member_group_color'] = $member_group['online_color'];
}
if (!empty($context['membergroups'][$profile['id_post_group']]))
{
    $post_group = $context['membergroups'][$profile['id_post_group']];
    $profile['post_group'] = $post_group['group_name'];
    $profile['post_group_color'] = $post_group['online_color'];
}
// The badge icons
$profile['icons'] = empty($profile['id_group']) || empty($member_group['icons']) ? $post_group['icons'] : $member_group_icons;

@joshuaadickerson
Copy link
Author

Also reduce the number of joins and columns in that function:
// Used by default
$select_columns = '
IFNULL(lo.log_time, 0) AS is_online, IFNULL(a.id_attach, 0) AS id_attach, a.filename, a.attachment_type,
mem.signature, mem.personal_text, mem.location, mem.gender, mem.avatar, mem.id_member, mem.member_name,
mem.real_name, mem.email_address, mem.hide_email, mem.date_registered, mem.website_title, mem.website_url,
mem.birthdate, mem.member_ip, mem.member_ip2, mem.posts, mem.last_login,
mem.karma_good, mem.id_post_group, mem.karma_bad, mem.lngfile, mem.id_group, mem.time_offset, mem.show_online,
mem.is_activated, mem.warning, mem.usertitle';
$select_tables = '
LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member)
LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = mem.id_member)';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment