Here are a couple of snippets I was using on my custom userpage template with buddylist. they seem to be either giving wrong info or no info with BL2.

First is:

<?php
$uid = arg(1);
$buddies = db_fetch_object(db_query("SELECT COUNT(buddy) AS count FROM {buddylist} WHERE uid= %d AND received=0", $uid));
echo "$buddies->count";
?>

which should give a number of buddies (e.g. 6) that a user has.

The next one should list out the actions (add/delete from BL) but seems to be actually breaking pages:

<?php
if ($GLOBALS['user']->uid != $account->uid) {
    if (@in_array($account->uid, array_keys(buddylist_get_buddies($viewing_user->uid))) && user_access('maintain buddy list')) {
        print l(t('Remove @username from your contact list',array('@username'=>$account->name)), 'buddy/delete/'. $account->uid);
    }
    else {
        if ($account->uid != $viewing_user->uid && user_access('maintain buddy list')) {
            print l(t('Add @username to your contact list',array('@username'=>$account->name)), 'buddy/add/'. $account->uid);
        }
    }
}
?>

this last one should list last few buddies added but I'm getting a list of only their first letters and links going to user/T for example.

<ul>
<?php
$contacts = $account->buddies;
if($contacts){
    $n=1;
    foreach($contacts as $contact){
    if($n<=3) {
    echo "<li>";
        print l($contact[name], 'user/'.$contact[uid]);
    echo "</li>";
    }
    $n++;
    }
}
else {
    echo "<li>$account->name has no contacts</li>";
}
?>
</ul>

has anything fundamental changed with BL2 that would render these snippets useless?

Comments

nodestroy’s picture

to your first snipped. it should work, the database structur is the same (with some additonal fields)

anyway, try this:

<?php
$uid = arg(1);
$buddies = db_result(db_query("SELECT COUNT(buddy) AS count FROM {buddylist} WHERE uid= %d AND received=0", $uid));
echo $buddies;
?>
esllou’s picture

OK, yes that first one is working actually. I saw a figure that looked odd but it was actually correct.

The snippet I really need for BL2, which I had working for BL is the one which shows either

Add to BL | Delete from BL

depending on whether the user whose userpage you're on is on your BL or not. Any pointers for that one?

criz’s picture

With some little adjustments this should work too. So the function that helps you to get user's buddies is now called "buddy_api_get_buddies()". But you could use my snippet, I have something simular in use:

  <?php
  //load current user object
  global $user;
  //load profile-user object
  $puser = user_load(array('uid' => $node->uid));
  //load all buddies from active user
  $buddies = buddy_api_get_buddies($user->uid);

  //buddylist links
  if ($user->uid != $puser->uid && user_access('maintain buddy list')) {
    in_array($puser->uid, array_keys($buddies))  ? print l(t('Remove @username from your contact list',array('@username'=>$puser->name)), 'buddy/delete/'.$puser->uid, array(), 'destination=node/'.$node->nid, NULL, FALSE, TRUE) : print l(t('Add @username to your contact list',array('@username'=>$puser->name)), 'buddy/add/'.$puser->uid, array(), 'destination=node/'.$node->nid, NULL, FALSE, TRUE);
  }
  ?>     
esllou’s picture

I got the following error when I load up my user pages:

Parse error: syntax error, unexpected '{' in /home/mysite/public_html/sites/all/themes/mytheme/user_profile.tpl.php on line 329

line 329 is:

  if ($user->uid != $puser->uid && user_access('maintain buddy list') {

is that exact code working on your site?

criz’s picture

no, sorry...

change the line in
if ($user->uid != $puser->uid && user_access('maintain buddy list')) {

esllou’s picture

OK, no errors this time but nothing is printed at all in the part of the page where the code sits, whether I go onto a member's page who's on my buddylist or not. Nothing is being printed at all. :-(

criz’s picture

I tested exactly this script and it was working...

Are you sure you tested it not on your own profile and you have maintaining buddylist rights?

esllou’s picture

Version: » 5.x-1.x-dev

OK, we have progress. I am user 2 and have view/maintain buddylist permissions.

when I go to my page, I still see "Add to your Contact list", which I shouldn't be seeing.

I already have members 3, 33 and 49 on my contact list. When I go to each of their pages, I still see the "add to..." link. I don't see the "delete from..." link, whatever I do. The other odd thing is that the destination URL for these "add to" links is "http://www.mysite.com/buddy/add/0?destination=node/", which, when I click just takes me to the front page without doing anything. This happens even if I click on the "add to...." link on a userpage whose members ISN'T on my buddylist, so basically the code isn't working.

I don't know if this is relevant but I use both the $user variable (for the viewing member) and the $account variable (for the member whose page is being viewed)

criz’s picture

Ok, one more try. Your problem seems to be that you don't load the right user-objects. This former snippet only works if $node is available and the author of the node is the userpage owner.

But with some small adjustements you should get it working anyway. I have no idea how your variables are configured, but try it with the following version (assuming that you have already loaded the $user variable for the viewing member and the $account variable for the member whose page is being viewed):

<?php
//load all buddies from active user
$userbuddies = buddy_api_get_buddies($user->uid);
//buddylist links
if ($account->uid != $user->uid && user_access('maintain buddy list')) {
  in_array($account->uid, array_keys($userbuddies)) ? print l(t('Remove @username from your contact list',array('@username'=>$account->name)), 'buddy/delete/'.$account->uid, array(), drupal_get_destination(), NULL, FALSE, FALSE) : print l(t('Add @username to your contact list',array('@username'=>$account->name)), 'buddy/add/'.$account->uid, array(), drupal_get_destination(), NULL, FALSE, FALSE);
}
?>    
esllou’s picture

that worked perfectly. Thanks a million.

What about that last block I asked about:

<?php
$contacts = $account->buddies;
if($contacts){
    $n=1;
    foreach($contacts as $contact){
    if($n<=3) {
    echo "<li>";
        print l($contact[name], 'user/'.$contact[uid]);
    echo "</li>";
    }
    $n++;
    }
}
else {
    echo "<li>$account->name has no contacts</li>";
}
?>

I have Paul and Sharon on my buddylist and the html that block is outputting is:

<li><a href="/user/P">P</a></li>
<li><a href="/user/S">S</a></li>

so, for some reason, it's only getting the first letter from each of their names and linking to that letter too, so the problem seems to be here:


        print l($contact[name], 'user/'.$contact[uid]);

could you send me your paypal address to me via my contact form....I would like to offer something to you as you've gone beyond the normal support level. Thanks.

criz’s picture

Assigned: Unassigned » criz
Status: Active » Fixed

No problem, I just want to help people switching to buddylist2.

Listing 3 buddies in a block can be also done using views, usernodes and the buddylist argument handler.
But your snippet should be no great deal too. Something like the following should work:

<?php
$contacts = buddy_api_get_buddies($account->uid);
if($contacts){
  $n=1;
  foreach(array_keys($contacts) as $contact){
  if($n<=3) {
    $contact_user = user_load(array('uid' => $contact));
    echo "<li>";
    print l($contact_user->name, 'user/'.$contact);
    echo "</li>";
    }
  $n++;
  }
}
else {
  echo "<li>$account->name has no contacts</li>";
}
?>
Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

millions’s picture

Hi Criz,

I'm new to drupal, and bl2. I've been installing and tweaking modules like crazy the past few days (i'm not much of a coder, certainly not php). I'm using panels2, advanced profile w/bio, and now BL2 and I can't get the add/delete links to show up. They worked with BL but not BL2. It looks like I just need to paste this code somewhere, but I don't have a user_profile.tpl.php in my themes folder. Any suggestions on where to paste your code snippet would be GREATLY appreciated! Thanks.

Nick

criz’s picture

Hi Nick,

sorry for the late answer. I'm not really into Bio/Advanced Profile, but there also should be a tpl file for your user profile. For example node-bio.tpl.php.

chris