[Tip/Trick] Adding poster's profile info to ssi_recentTopics()

Started by Kays, August 04, 2009, 01:25:36 PM

Previous topic - Next topic

Kays

I think I've just come up with a simple method of grabbing a poster's profile info, such as avatar, so it can be displayed in the SSI output.

I've only tested it on ssi_recentTopics() for SMF 1.1.X. With a few small changes, it should work on ssi_boardNews() also. I don't have a running SMF 2.0 to test on, but it looks like this should work on ssi_queryPosts() also. Something I would like confirmation on.

in SSI.php at the begining of the ssi_recentTopics() functon add $memberContext to the globals.

Quoteglobal $user_info, $modSettings, $func, $memberContext;

Then above:


// Just return it.


Add:


$poster_ids = array();
foreach($posts as  $post)
$poster_ids[] = $post['poster']['id'];

$poster_ids = array_unique($poster_ids);
loadMemberData($poster_ids);

foreach($posts as $key => $post)
{
if($posts[$key]['poster']['id'] > 0)
{
loadMemberContext($posts[$key]['poster']['id']);
$posts[$key]['poster']['avatar'] = $memberContext[$posts[$key]['poster']['id']]['avatar']['image'];
$posts[$key]['poster']['title'] = $memberContext[$posts[$key]['poster']['id']]['title'];
}
else // Must be a guest.
{
$posts[$key]['poster']['avatar'] = '';
$posts[$key]['poster']['title'] = '';
}
}
unset($memberContext);


The above example will display the poster's avatar as well as thier title.

To display either as in the echo below that, use either $post['poster']['avatar'] or $post['poster']['title']

To add additional profile information, just add another line and/or change the following. Add/change the line for guests also. For what profile info is available look in Sources/load.php at around line 1025

Quote$posts[$key]['poster']['title'] = $memberContext[$posts[$key]['poster']['id']]['title'];

I hope someone finds this usefull.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Marcus Forsberg


Arantor

Interesting suggestion, and really appreciate you posting it.

Two things I can see with it, though.

1. Performance. The extra call to loadMemberData and subsequent call to loadMemberContext grabs a number of tables (multiple queries), it may have been better for performance to have expanded the query to pull 1 extra field from the member table, plus bounce to the attachments table for that.

2. There are possibly two places, not one, where an attachment can be returned to - as well as $memberContext[$posts[$key]['poster']['id']]['avatar']['image'], I have seen it return into $membercontext[...]['filename'] for non-uploaded avatars - at least last I checked when using loadMemberData.

Kays

Thanks. :)

On the first point. There's only one call to loadMemberData. Display.php processes the poster's info in a similar manner and there's usually more posts and posters on a page than is shown by SSI.php. So I consider it to be minor.

The second point I couldn't check since I don't allow direct linking to an avatar. But I don't think that will be a problem since ['avatar']['image'] is suppose to display the image. In Display.template.php $message['member']['avatar']['image'] is used the display the avatar. But if someone could verify it either way, I'm curious.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Arantor

There is only one call, I noticed, plus the call to loadMemberContext per id, which between them are several calls. I haven't dug into the mechanics of Display.php but figured it was done more cleanly than that.

Hmm, I never used loadMemberContext when I did it, I just pulled it directly in an SSI page using loadMemberData only. filename doesn't just cover external hosted, but also the pre-available ones in the profile page.

I should dig out my code and examine this a bit more to be honest. You did good, and I have the uncomfortable feeling I'm talking rubbish again :(

Kays

QuoteI should dig out my code and examine this a bit more to be honest. You did good, and I have the uncomfortable feeling I'm talking rubbish again 

:D

It took me a bit to figure it out, but, loadMemberContext() pulls the data from the $memberContext array. So there's no additional database queries. loadMemberData() creates the $memberContext array which uses the poster's id as the index.

Quote
filename doesn't just cover external hosted, but also the pre-available ones in the profile page.

This is from Load.php and I think it covers all of the bases.



'image' => $profile['avatar'] == '' ? ($profile['ID_ATTACH'] > 0 ? '<img src="' . (empty($profile['attachmentType']) ? $scripturl . '?action=dlattach;attach=' . $profile['ID_ATTACH'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $profile['filename']) . '" alt="" class="avatar" border="0" />' : '') : (stristr($profile['avatar'], 'http://') ? '<img src="' . $profile['avatar'] . '"' . $avatar_width . $avatar_height . ' alt="" class="avatar" border="0" />' : '<img src="' . $modSettings['avatar_url'] . '/' . htmlspecialchars($profile['avatar']) . '" alt="" class="avatar" border="0" />'),

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Arantor

Yup, that does cover everything, so please feel free to ignore both of my concerns in this, because I truly don't know what I'm talking about.

Kays


If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Advertisement: