PLEASE NOTE! These snippets are user submitted. It is impossible to check them all, so please use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.

Description

This php snippet inserts the ADD TO BUDDYLIST link

Dependencies: buddylist.module must be installed and enabled. The snippet checks to see if the user viewing the profile page has the access permission to maintain buddy list before displaying the link

Usage

  • For use in your user profile page override
  • Using a text editor like NOTEPAD.EXE or an equivalent, copy and paste the code into your user_profile.tpl.php file
  • Change the div class name or the link text to suit.
  global $user;
  if ($user->uid != $account->uid) {
    if (@in_array($account->uid, array_keys(buddylist_get_buddies($user->uid))) && user_access('maintain buddy list')) {
      print l(t('Remove !username from your buddylist',array('!username' => $account->name)), 'buddy/delete/'. $account->uid);
    }
    else {
      if ($account->uid != $user->uid && user_access('maintain buddy list')) {
        print l(t('Add !username to your buddylist',array('!username' => $account->name)), 'buddy/add/'. $account->uid);
      }
    }
  }

Comments

pribeh’s picture

Several people I know are reporting that this doesn't work past 5.3. And I don't know why.

Would be nice to know how to get this to work in a block as well. Someone posted this as a starting point but I don't know php.

$uid = arg(1); // second part of the path user/1 on user profile page
Loading a user:
$account = user_load(array('uid' => $uid));
Get the name:
$name = $account->name;

Any help would be uber appreciated. I currently don't have a way other than the template fix for my users to add buddies.

pribeh’s picture

With the help of a few others the following code seems to do the trick:

<?php

if (@in_array($account->uid, array_keys(buddylist_get_buddies($user->uid))) && user_access('maintain buddy list')) {
  print "<a target=_blank href=\"?q=buddy/delete/".$account->uid."\">Remove " .$account->name. " from your buddylist</a>";
  }
else {
  if ($user->uid != $account->uid && user_access('maintain buddy list')) {
  print "<a target=_blank href=\"?q=buddy/add/".$account->uid."\">Add " .$account->name. " to your buddylist</a>";
  }
}
?>

Make sure to specify this at the beginning of your user_profile.tpl.php:

<?php
global $user;
?>

and also make sure to specify your code in template.php to focus on $account (the profile being viewed) as opposed to $user (logged-in user viewing profile):

/**
* Catch the theme_user_profile function, and redirect through the template api
*/
function phptemplate_user_profile($account, $fields = array()) {
  // Pass to phptemplate, including translating the parameters to an associative array. The element names are the names that the variables
  // will be assigned within your template.
  /* potential need for other code to extract field info */
return _phptemplate_callback('user_profile', array('account' => $account, 'fields' => $fields));
  }
  }

The above code was taken from Muslim Guy's posts and modified by Nevets.

merto@drupal.org’s picture

These will override theme_user_profile and will insert image as link to Remove or Add to my buddylist below user_picture without calling _phptemplate_callback. It will also give me the flexibility to style all category name.


function phptemplate_user_profile($account, $fields) {
  $output = '<div class="profile">';
  $output .= '<div class="picture-wrapper">';
  $output .= theme('user_picture', $account);
  	global $user;
		if (in_array($account->uid, array_keys(buddylist_get_buddies($user->uid))) && user_access('maintain buddy list')) {
  		$output .= l(theme('image', drupal_get_path('module', 'buddylist') .'/images/user_delete.png', t('Remove')), 'buddy/delete/' . $account->uid, array('title'=>t('remove from my buddylist')),drupal_get_destination(), NULL, NULL, TRUE, TRUE);
 		 }
		else {
  		if ($user->uid != $account->uid && user_access('maintain buddy list')) {
  		$output .= l(theme('image', drupal_get_path('module', 'buddylist') .'/images/user_add.png', t('Add')),'buddy/add/' . $account->uid, array('title'=>t('add to my buddylist')),drupal_get_destination(), NULL, NULL, TRUE, TRUE);
  }
}
 
  $output .= '</div>';
  $output .= '<div class= "profile-item-wrapper">';

  foreach ($fields as $category => $items) {
  	$category = str_replace(" ", "-", $category);
    if (strlen($category) > 0) {
    	    	$output .= '<dl class="'. check_plain($category ). '">'; 	
      $output .= '<h2 class="title">'. check_plain($category) .'</h2>';
    }
    
    foreach ($items as $item) {
      if (isset($item['title'])) {
        $output .= '<dt class="'. $item['class'] .'">'. $item['title'].' :' .'</dt>';
      }
      $output .= '<dd class="'. $item['class'] .'">'. $item['value'] .'</dd>';
    }
    $output .= '</dl>';
  }
  $output .= '</div>';
  $output .= '</div>';

  return $output;
}
tucspl’s picture

In order to get this to work for buddylist 2, try this snippet (this goes in your user_profile.tpl.php):

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

Note: If used the snippet on this page (the comment above this comment) to replace $user with $account in your template.php, replace that snippet with this (this goes in template.php):

/**
* Catch the theme_user_profile function, and redirect through the template api
*/
function phptemplate_user_profile($user, $fields = array()) {
  // Pass to phptemplate, including translating the parameters to an associative array. The element names are the names that the variables
  // will be assigned within your template.
  /* potential need for other code to extract field info */
return _phptemplate_callback('user_profile', array('user' => $user, 'fields' => $fields));
  }