Skip to main content
Topic: Function to get avatar from id (Read 1690 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Function to get avatar from id

Do we have such a function? I want to add avatars to the recent topics box on my board index, but cannot find a suitable function to do so.

determineAvatar from Load.php requires far too much information.

Re: Function to get avatar from id

Reply #1

I think the closest thing is determineAvatar...
But write one should be too difficult, you just need to grab the things needed in determineAvatar, something like:
Code: [Select]
function getAvatarsByMemberId($ids)
{
$db = database();
$request = $db->query('', '
SELECT m.id_member, m.avatar, m.email_address,
IFNULL(a.id_attach, 0) AS id_attach, a.filename, a.attachment_type
FROM {db_prefix}members AS m
LEFT JOIN {db_prefix}attachments AS a ON (m.id_member = a.id_member)
WHERE m.id_member IN ({array_int:members})',
array(
'members' = (array) $ids,
)
);
$avatars = array();
while ($row = $db->fetch_assoc($request))
$avatars[$row['id_member']] = determineAvatar($row);
$db->free_result($request);

return $avatars;
}
Bugs creator.
Features destroyer.
Template killer.

Re: Function to get avatar from id

Reply #2

Thanks, that works perfectly :)

For reference for others:
Code: [Select]
array(
'members' = (array) $ids,
)

should be =>
Code: [Select]
array(
'members' => (array) $ids,
)

My finished BoardIndex code:
Code: [Select]
/**
 * The infocenter ... stats, recent topics, other important information that never gets seen :P
 */
function template_info_center()
{
global $context, $txt, $modSettings;

// Here's where the "Info Center" starts...
echo '
<div class="row">
<div class="large-12 columns">';

call_template_callbacks('ic', $context['info_center_callbacks']);

// Staff Online TODO: SHOULD BE IN A MOD SOURCE
require_once(SUBSDIR . '/Members.subs.php');
$global_mods = membersAllowedTo('moderate_board', 0);
$admins = membersAllowedTo('admin_forum');

$all_staff = array_merge($global_mods, $admins);
$all_staff = array_unique($all_staff);
$all_staff_online = array_intersect($all_staff, array_map(function($user) {
    return $user['id'];
}, $context['users_online']));

$db = database();
$request = $db->query('', '
SELECT
m.id_member, m.real_name, m.avatar, m.email_address,
mg.group_name,
a.id_attach, a.attachment_type, a.filename
FROM {db_prefix}members AS m
LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = m.id_member)
LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN m.id_group = {int:reg_group_id} THEN m.id_post_group ELSE m.id_group END)
WHERE m.id_member IN ({array_int:staff_list})',
array(
'staff_list' => $all_staff_online,
'reg_group_id' => 0,
)
);
$staff_list = array();
while ($row = $db->fetch_assoc($request))
{
if (in_array($row['id_member'], $admins))
$row['type'] = 1;
elseif (in_array($row['id_member'], $global_mods))
$row['type'] = 2;
else
$row['type'] = 3;

$staff_list[$row['type'] . '-' . $row['id_member']] = array(
'id' => $row['id_member'],
'name' => $row['real_name'],
'group' => $row['group_name'],
'type' => $row['type'],
'avatar' => determineAvatar(array(
'avatar' => $row['avatar'],
'filename' => $row['filename'],
'id_attach' => $row['id_attach'],
'email_address' => $row['email_address'],
'attachment_type' => $row['attachment_type'],
)),
);
}
$db->free_result($request);

// Who's online?
echo '
<div class="category">
<div class="category_header">
<h2>Users Online</h2>
</div>

  <div id="num_online_users">
<dl>
<dt>Staff Online</dt>
<dd class="clearfix">';

foreach ($staff_list as $staff) {
echo '
<a data-tooltip aria-haspopup="true" class="has-tip th left" title="', $staff['name'], '" href="', $scripturl, '?action=profile;u=', $staff['id'], '"><img class="avatar" src="', $staff['avatar']['href'], '" alt="" /></a>';
}

echo '
</dd>
<dt>Members Online</dt>
<dd>', implode(', ', $context['list_users_online']), '</dd>
<dt>Total</dt>
<dd>', comma_format($context['num_guests'] + $context['num_users_online'] + $context['num_spiders']), ' ('
, comma_format($context['num_guests']), ' ', $context['num_guests'] == 1 ? $txt['guest'] : $txt['guests'], ', '
, comma_format($context['num_users_online']), ' ', $context['num_users_online'] == 1 ? $txt['user'] : $txt['users'], ', '
, comma_format($context['num_spiders']), ' ', $context['num_spiders'] == 1 ? $txt['spider'] : $txt['spiders'], ', '
, comma_format($context['num_users_hidden']), ' ', $context['num_spiders'] == 1 ? $txt['hidden'] : $txt['hidden_s']
, ')</dd>
</dl>
  </div>
</div>
</div>
</div>';
}

/**
 * This is the "Recent Posts" bar.
 */
function template_ic_recent_posts()
{
global $context, $txt, $scripturl, $settings;

// Show the Recent Posts title, and attach webslices feed to this section
// The format requires: hslice, entry-title and entry-content classes.
echo '
<div class="category">
<div class="category_header">
<a href="', $scripturl, '?action=recent"><h2>', $txt['recent_posts'], '</h2></a>
</div>
<div class="entry-title" style="display: none;">', $context['forum_name_html_safe'], ' - ', $txt['recent_posts'], '</div>
<div class="entry-content" style="display: none;">
<a rel="feedurl" href="', $scripturl, '?action=.xml;type=webslice">', $txt['subscribe_webslice'], '</a>
</div>';

// Only show one post.
if ($settings['number_recent_posts'] == 1)
{
// latest_post has link, href, time, subject, short_subject (shortened with...), and topic. (its id.)
echo '
<p id="infocenter_onepost" class="inline">
<a href="', $scripturl, '?action=recent">', $txt['recent_view'], '</a> ', sprintf($txt['is_recent_updated'], '"' . $context['latest_post']['link'] . '"'), ' (', $context['latest_post']['html_time'], ')
</p>';
}
elseif (!empty($context['latest_posts']))
{
// Prepare avatars TODO: SHOULD BE IN A MOD SOURCE
$ids = array_map(function($post) {
    return $post['poster']['id'];
}, $context['latest_posts']);
$avatars = getAvatarsByMemberId($ids);

echo '<div class="category_boards">';

// Each post in latest_posts has:
// board (with an id, name, and link.), topic (the topic's id.), poster (with id, name, and link.),
// subject, short_subject (shortened with...), time, link, and href.
foreach ($context['latest_posts'] as $post) {
echo '
<div class="board_row">';

if (!empty($settings['avatars_on_indexes']))
echo '
<a class="th left" href="', $post['poster']['href'], '"><img class="avatar" src="', $avatars[$post['poster']['id']]['href'], '" alt="" /></a>';

echo'
<p>', $post['link'], '<br>
', $post['poster']['link'], ' in ', $post['board']['link'], '<br>
', $post['html_time'], '</p>
</div>';
}

echo '</div>';
}
echo '
</div>';
}

Re: Function to get avatar from id

Reply #3

Can I blame the keyboard? O:-) :P
Bugs creator.
Features destroyer.
Template killer.