1
3lr0n
How to put avatars in forum blocks
  • 2010/9/10 13:36

  • 3lr0n

  • Not too shy to talk

  • Posts: 167

  • Since: 2004/3/13


Hello, I need some help to display the author avatar in last posts and last topics newbb blocks.

The things I did.

1. create a new function on /include/functions.php to get avatar from a given Id.

function getAvatarFromId($userid$usereal 0)
    {
        
$userid intval($userid);
        
$usereal intval($usereal);
        if (
$userid 0) {
            
$member_handler = & xoops_gethandler('member');
            
$user =& $member_handler->getUser($userid);
            if (
is_object($user)) {
                
$ts =& MyTextSanitizer::getInstance();
                if (
$usereal) {
                    
$avatar $user->getVar('user_avatar');
                    if (
$avatar != '') {
                        return 
$ts->htmlSpecialChars($avatar);
                    } else {
                        return 
$ts->htmlSpecialChars($user->getVar('user_avatar'));
                    }
                } else {
                    return 
$ts->htmlSpecialChars($user->getVar('user_avatar'));
                }
            }
        }
       
    }


Cloned from getunamefromid with some modifications.

2. in newbb/blocks/newbb_block.php in the function b_newbb_topic_show in this section

$query 'SELECT'.
            
'    t.topic_id, t.topic_replies, t.forum_id, t.topic_title, t.topic_views, t.topic_subject, t.topic_time, t.topic_poster, t.poster_name,'.
            
'    f.forum_name, f.allow_subject_prefix'.
            
'    FROM ' $db->prefix('bb_topics') . ' AS t '.
            
'    LEFT JOIN ' $db->prefix('bb_forums') . ' AS f ON f.forum_id=t.forum_id'.
            
'    WHERE 1=1 ' .
                
$forum_criteria .
                
$approve_criteria .
                
$extra_criteria .
                
' ORDER BY ' $order ' DESC';

    
$result $db->query($query$options[1], 0);
    if (!
$result) {
        
newbb_message("newbb block query error: ".$query);
        return 
false;
    }
    
$block['disp_mode'] = $options[3]; // 0 - full view; 1 - compact view; 2 - lite view;
    
$rows = array();
    
$author = array();

    while (
$row $db->fetchArray($result)) {
        
$rows[] = $row;
        
$author[$row["topic_poster"]] = 1;
    }
    if (
count($rows) < 1) return false;
    
$author_name newbb_getUnameFromIds(array_keys($author), $newbbConfig['show_realname'], true);


include this line just after the getunamefromids array

$author_avatar getAvatarFromId(array_keys($author));


so i have an array of the avatars for each id

But i cant pass it to smarty wit this

$topic['avatar'] = $author_avatar[$arr['topic_subject']];


I suppose it is because the $arr is an array created from $rows and this inherit the number of colums created in the $query and $result variables.

Bus as I am not a programmer dont know how to pass it to smarty so I can have a variable like this: <{$topic.avatar}> or what ever..

Any help

Thks

Spanish Overclocking Community xoops based site : www.overclocking.es

2
ghia
Re: How to put avatars in forum blocks
  • 2010/9/10 15:11

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


1 You must use XoopsUserUtility::getUnameFromIds as skeleton for your getAvatarFromIds
2 You should have a similar block as this one
if (!empty($author_name[$arr['topic_poster']])) {
            
$topic_poster $author_name[$arr['topic_poster']];
        } else {
            
$topic_poster $myts->htmlSpecialChars( ($arr['poster_name'])?$arr['poster_name']:$GLOBALS["xoopsConfig"]["anonymous"] );
        }
        
$topic['topic_poster'] = $topic_poster;
for the avatar
if (!empty($author_avatar[$arr['topic_poster']])) {
            
$topic_avatar $author_avatar[$arr['topic_poster']];
        } else {
            
$topic_avatar '';
        }
        
$topic['topic_avatar'] = $topic_avatar;

3
3lr0n
Re: How to put avatars in forum blocks
  • 2010/9/10 22:54

  • 3lr0n

  • Not too shy to talk

  • Posts: 167

  • Since: 2004/3/13


Done!

For all of you that want to have this feature I explain the steps.

1- Include in /include/functions.php the getAvatarFromIds, cloned from getUnameFromIds.
function getAvatarFromIds($uid$usereal false)
    {
        if (!
is_array($uid)) {
            
$uid = array($uid);
        }
        
$userid array_map('intval'array_filter($uid));

        
$myts =& MyTextSanitizer::getInstance();
        
$avatars = array();
        if (
count($userid) > 0) {
            
$xoopsDB =& XoopsDatabaseFactory::getDatabaseConnection();
            
$sql 'SELECT uid, user_avatar FROM ' $xoopsDB->prefix('users') . ' WHERE level > 0 AND uid IN(' implode(','array_unique($userid)) . ')';
            if (!
$result $xoopsDB->query($sql)) {
                return 
$avatars;
            }
            while (
$row $xoopsDB->fetchArray($result)) {
                
$uid $row['uid'];
                
$avatars[$uid] = $myts->htmlSpecialChars($row['user_avatar']);

                }

           }

        return 
$avatars;
    }


2-Open /modules/newbb/blocks/newbb_block.php. There are 3 functions, one called b_newbb_show for recent answered topic, b_newbb_topic_show for last topics and b_newbb_post_show for last posts. All works the same in this modification, but you have to modify each function in order to have the hack running in all the three blocks.

Find in each
$author_name newbb_getUnameFromIds(array_keys($author), $newbbConfig['show_realname'], true);


After add

$author_avatar getAvatarFromIds(array_keys($author));


And now for b_newbb_show and b_newbb_post_show find

$topic['topic_poster'] = $topic_poster;


and after add
if (!empty($author_avatar[$arr['uid']])) {
            
$topic_avatar $author_avatar[$arr['uid']];
        } else {
            
$topic_avatar '';
        }
        
$topic['topic_avatar'] = $topic_avatar;


for b_newbb_topic_show find

$topic['topic_poster'] = $topic_poster;


and after add

if (!empty($author_avatar[$arr['topic_poster']])) {
            
$topic_avatar $author_avatar[$arr['topic_poster']];
        } else {
            
$topic_avatar '';
        }
        
$topic['topic_avatar'] = $topic_avatar;


3-Finally add to your templates located in /modules/newbb/templates/blocks/ this code to show the new smarty variable.

<img src="<{$xoops_url}>/uploads/<{$topic.topic_avatar}>" alt="avatar" />


To have all the avatars same size and shape I use smart resizer.

Hope you find this useful.

Demo here, the red block in the bottom of the page.

PS: Thks guia for the help




Spanish Overclocking Community xoops based site : www.overclocking.es

4
narkomic
Re: How to put avatars in forum blocks
  • 2010/10/11 17:23

  • narkomic

  • Just popping in

  • Posts: 10

  • Since: 2010/9/23


Were do i put the code in number tree?

3-Finally add to your templates located in /modules/newbb/templates/blocks/ this code to show the new smarty variable.


<img src="<{$xoops_url}>/uploads/<{$topic.topic_avatar}>" alt="avatar" />

I have tryet several times now but cant get it to work...:(

5
ghia
Re: How to put avatars in forum blocks
  • 2010/10/11 22:50

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


You must put it in the template of the section where you want to show the avatar of a user.
Where do you want it (block, URL)?

Login

Who's Online

175 user(s) are online (108 user(s) are browsing Support Forums)


Members: 0


Guests: 175


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Apr 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits