Skip to content
buddylist.module 18.5 KiB
Newer Older
Matt Westgate's avatar
Matt Westgate committed

/**
 * Implementation of hook_help
 */
function buddylist_help($field) {
  switch ($field) {
    case 'admin/modules#description':
      return t('Enable buddy list functionality.');
  }
/**
 * Implementation of hook_init
 */
function buddylist_init() {
  variable_set('buddylist_prof_buddies', variable_get('buddylist_prof_buddies', 4));
  variable_set('buddylist_blocklisting_size', variable_get('buddylist_blocklisting_size', 5));
}

/**
 * Implementation of hook_settings
 */
Neil Drumm's avatar
Neil Drumm committed
function buddylist_settings() {
  $group .= form_select(t('Number of buddies to list in the user\'s buddy block'), 'buddylist_blocklisting_size', variable_get('buddylist_blocklisting_size', 5), drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)), t('This setting controls the maximum number of buddies displayed in a user\'s "buddylist block" given that the "buddylist block" is enabled in the %link.', array('%link' => l(t('block settings'), 'admin/block')) ));
  $group .= form_select(t('Number of posts to list in the buddies\' recent posts block'), 'buddylist_posts_block', variable_get('buddylist_posts_block', 10), drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)), t('This setting controls the maximum number of posts to display in a user\'s "buddy recent posts" block given that the "buddies\' recent posts" block is enabled in the %link.', array('%link' => l(t('block settings'), 'admin/block'))  ));
Neil Drumm's avatar
Neil Drumm committed
  $group .= form_textfield(t('Block title'), 'buddylist_block_title', variable_get('buddylist_block_title', t('My buddies\' recent posts')), 70, 128, t('This will be the title for the recent buddies post block. If none is specified, the default will be used.'));
  $output .= form_group(t('Buddylist block options'), $group);

  // User profile page settings
  $group = form_select(t('Number of buddies and buddy-ofs for the user profile page'), 'buddylist_prof_buddies', variable_get('buddylist_prof_buddies', 4), drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), t('The default maximum number of buddies and buddy-ofs to display on a user\'s profile page.'));
Neil Drumm's avatar
Neil Drumm committed
  $output .= form_group(t('Profile page options'), $group);

  return $output;
}

function buddylist_setmsg_received($type, &$edit, &$thisuser) {
  global $user;

  $check_received = db_query('SELECT received, b.uid as uid, u.name FROM {buddylist} b LEFT JOIN {users} u on u.uid = b.uid WHERE buddy = '. $thisuser->uid .' AND received = 1');
  while ($rec = db_fetch_object($check_received)) {
    if (($rec->received) and ($thisuser->uid == $user->uid)) {
      drupal_set_message(l($rec->name, 'user/'. $rec->uid) .' has added you to his/her buddylist.');
      db_query('UPDATE {buddylist} SET received = 0 WHERE buddy = '. $user->uid); 
    }
  }
}

/**
 * Implementation of hook_user
 */
function buddylist_user($type, &$edit, &$thisuser) {
  global $user;
Neil Drumm's avatar
Neil Drumm committed
  $output = '';
  if ($user->uid == 0) {
    return null;
  }
  if (($type == 'login') || ($type == 'load') || ($type == 'view')) {
    buddylist_setmsg_received($type, $edit, $thisuser);
Neil Drumm's avatar
Neil Drumm committed
  }

  if (!isset($_SESSION['buddylist'])) {
    buddylist_update_session();
  // This portion of code runs when a user's profile is viewed
  // Returns a list of $thisuser's buddies
  if ($type == 'view') {
    // the only purpose of $friends and $has_friends is to determine whether any friends exist for $thisuser
Neil Drumm's avatar
Neil Drumm committed
    $friends = db_query('SELECT uid FROM {buddylist} WHERE uid = %d', $thisuser->uid);
    $has_friends = db_fetch_object($friends);
    // if thisuser has friends, show friends
    if ($has_friends->uid == $thisuser->uid && variable_get('buddylist_prof_buddies', 0) != 0) {
      $output .= form_item(t('Buddies'), _buddylist_firstfew_buddies($thisuser->uid)); 
  // This portion of code is used to see if this $thisuser is a buddy of others and, if s/he is, returns a list
  // of people s/he is a buddy of.
  // Note the distinction between having a buddy and being someone else's buddy (i.e., 'buddyof')
  if (isset($thisuser->uid) && ($type == 'view')) {
Neil Drumm's avatar
Neil Drumm committed
    $friends = db_query('SELECT buddy FROM {buddylist} WHERE buddy = %d', $thisuser->uid);
    $has_friends = db_fetch_object($friends);
    if ($has_friends->buddy == $thisuser->uid && variable_get('buddylist_prof_buddyofs', 0) != 0) {
      $output .= form_item(t('Buddy of'), _buddylist_firstfew_buddyof($thisuser->uid));
  // Check to see whether or not $thisuser is in global $user's buddy list
  // If $thisuser is already in $user's buddy list, a link offering to delete $thisuser from $user's buddy list is generated
  // If $thisuser is not on $user's buddy list, and $thisuser != $user, then a link offering to add $thisuser to $user's buddy list
  // is generated.
  if (isset($thisuser->uid) && ($type == 'view')) {
    if (in_array($thisuser->uid, $_SESSION['buddylist'])) {
      $output .= form_item(t('Buddy list'), l(t('Remove from buddy list'), 'buddylist/delete/' . $thisuser->uid));
      if ($user->uid != $thisuser->uid) {
        $output .= form_item(t('Buddy list'), l(t('Add to buddy list'), 'buddylist/add/' . $thisuser->uid));
  return array ('' => $output);
function buddylist_update_session() {
  global $user;
  $_SESSION['buddylist'] = array();
  if (isset($user->uid)) {
Neil Drumm's avatar
Neil Drumm committed
    $result = db_query('SELECT buddy FROM {buddylist} WHERE uid = %d ORDER BY timestamp DESC', $user->uid);
    while ($buddy = db_fetch_object($result)) {
      $_SESSION['buddylist'][] = $buddy->buddy;
    }
Neil Drumm's avatar
Neil Drumm committed
  }
/**
 * Implementation for hook_block
 */
Neil Drumm's avatar
Neil Drumm committed
function buddylist_block($op = 'list', $delta = 0) {
  global $user;
Neil Drumm's avatar
Neil Drumm committed
  if ($op == 'list') {
    $block[0]['info'] = t('Buddy list');
    $block[1]['info'] = t('Buddies\' recent posts');
    return $block;
  } 
  else if ($op == 'view' && user_access('access content') && $user->uid > 0) {
Neil Drumm's avatar
Neil Drumm committed
    switch ($delta) {
      case 0 : // Shows buddylist block
        $result = db_query_range('SELECT b.buddy as uid, u.name FROM {buddylist} b, {users} u WHERE b.uid = %d AND u.uid = b.buddy ORDER BY b.timestamp DESC', $user->uid, 0, variable_get('buddylist_blocklisting_size', 5));
Neil Drumm's avatar
Neil Drumm committed
        while ($account = db_fetch_object($result)) {
          $account->link = l((strlen($account->name) > 15 ? substr($account->name, 0, 15) .'...' : $account->name), 'user/'. $account->uid);
Neil Drumm's avatar
Neil Drumm committed
          $users[] = $account->link;
        }
Neil Drumm's avatar
Neil Drumm committed
        $block['content'] = theme('user_list', $users);
        $block['subject'] = t('My buddy list');
	
        // check if a "more" link should generated by seeing if there are more buddies than the specified $upperlimit
        $queryresult = db_query('SELECT count(buddy) as buddycount FROM {buddylist} WHERE uid = %d', $user->uid);
        $countresult = db_fetch_object($queryresult);
        $morelink = '';
        
	if ($countresult->buddycount > variable_get('buddylist_blocklisting_size', 5)) {
           $block['content'] .=  '<div class="more-link">' . l(t('more'), 'buddylist', array('title' => t('View more.'))) . '</div>';
        }
	
Neil Drumm's avatar
Neil Drumm committed
        return $block;
        break;
Neil Drumm's avatar
Neil Drumm committed
      case 1: // Shows my buddies recent posts block
        $result = db_query_range('SELECT distinct n.nid, u.uid, u.name, n.created, n.title FROM {buddylist} b LEFT JOIN {node} n ON n.uid=b.buddy LEFT JOIN {users} u ON n.uid = u.uid WHERE n.status = 1 AND b.uid=%d ORDER BY n.nid DESC', $user->uid, 0, variable_get('buddylist_posts_block', 10));
Neil Drumm's avatar
Neil Drumm committed
        while ($list = db_fetch_object($result)) {
          $content_list[] .= l($list->title, 'node/view/'. $list->nid) .' ('. l($list->name , 'user/'. $list->uid) . ') ';
Neil Drumm's avatar
Neil Drumm committed
        }
        $block['subject'] = variable_get('buddylist_block_title', t('My buddies\' recent posts')); 
        $block['content'] .= theme('item_list', $content_list);
        $block['content'] .= '<div class="more-link">'. l(t('more'), 'buddylist/recent', array('title' => t('View more.'))) .'</div>';
        return $block;
        break;
    }
/**
 * Implementation of hook_perm
 */
Neil Drumm's avatar
Neil Drumm committed
function buddylist_perm() {
  return array('maintain buddy list');
/**
 +* Implementation of hook_menu
 */
function buddylist_menu() {
  global $user;
  $links = array();
  
  $links[] = array('path' => 'buddylist/feed', 'title' => t('xml feed'), 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_page', 'type' => MENU_CALLBACK); 
  $links[] = array('path' => 'buddylist', 'title' => t('my buddylist'), 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_page');
  $links[] = array('path' => 'buddylist/buddyof/', 'title' => t('buddy of'), 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_page', 'type' => MENU_CALLBACK);
  $links[] = array('path' => 'buddylist/buddyof/'. $user->uid, 'title' => t('who added you as a buddy'), 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_page');
  $links[] = array('path' => 'buddylist/recent', 'title' => t('buddies recent posts'), 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_page');
  $links[] = array('path' => 'buddylist/add', 'title' => t('add to buddylist'), 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_page', 'type' => MENU_CALLBACK);
  $links[] = array('path' => 'buddylist/save', 'title' => '', 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_page', 'type' => MENU_CALLBACK);
  $links[] = array('path' => 'buddylist/delete', 'title' => t('delete buddylist'), 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_page', 'type' => MENU_CALLBACK);
  $links[] = array('path' => 'buddylist/remove', 'title' => '', 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_page', 'type' => MENU_CALLBACK);

  return $links;
}


function buddylist_page() {
  global $user;
Neil Drumm's avatar
Neil Drumm committed
  $edit = $_POST['edit'];
  $op = $_POST['op'];
  if (empty($op)) {
    $op = arg(1);
  }
  switch ($op) {
Neil Drumm's avatar
Neil Drumm committed
    case 'feed':
      buddylist_feed_recentposts();
      break;

    case 'add':
      $title = t('Confirm addition to buddy list?');
      $output = buddylist_addbuddy_form(arg(2));
      break;
Neil Drumm's avatar
Neil Drumm committed

    case 'buddyof':
      if (arg(2)) {
        $thisuser = db_fetch_object(db_query('SELECT name FROM {users} WHERE uid = %d', arg(2)));
        drupal_set_title(t('Users who added %username as a buddy', array('%username' => $thisuser->name)) );
Neil Drumm's avatar
Neil Drumm committed
        $thisuser = db_fetch_object(db_query('SELECT name FROM {users} WHERE uid = %d', arg(2)));
	$output = '<div class="members">'. buddylist_buddyoflist(arg(2)) .'</div>';
Neil Drumm's avatar
Neil Drumm committed
      }
      else {
        drupal_set_title(t('Users who added you as a buddy'));
        $output = '<div class="members">'. buddylist_buddyoflist($user->uid) .'</div>';
Neil Drumm's avatar
Neil Drumm committed
    case 'save':
      $output = buddylist_add($edit['buddy_id']);
Neil Drumm's avatar
Neil Drumm committed
    case 'delete':
      $title = t('Confirm removal from buddy list?');
      $output = buddylist_deletebuddy_form(arg(2));
      break;
    case t('Remove user'):
Neil Drumm's avatar
Neil Drumm committed
    case 'remove' :
      $output = buddylist_remove($edit['buddy_id']);
      $thisuser = user_load(array('uid' => $edit['buddy_id']) );
      drupal_set_message(t('user %username removed from buddylist',array('%username' => '<em>'. $thisuser->name . '</em>') ));
      drupal_goto('buddylist');
Neil Drumm's avatar
Neil Drumm committed
    case 'recent':
      //$title = 'My buddies recent posts.';
      $output = buddylist_recentposts();
      break;

    case '':
      $output = buddylist_buddylist($user->uid);
Neil Drumm's avatar
Neil Drumm committed
      break;

    default:
      $output = '<div class="members">'. buddylist_buddylist(arg(1)) .'</div>';
Neil Drumm's avatar
Neil Drumm committed
      break;
  }
  if ($op != 'feed') {
    print theme('page',  $output);
  }
}

/**
 * Displays a page for recent posts by buddies
 */
function buddylist_recentposts() {
  global $user;

  $output = '';
  $result = pager_query('SELECT n.nid FROM {node} n LEFT JOIN {buddylist} b ON n.uid = b.buddy WHERE n.status = 1 AND b.uid = '. $user->uid .' ORDER BY n.nid DESC', variable_get('default_nodes_main', 10));

  while ($node = db_fetch_object($result)) {
    $output .= node_view(node_load(array('nid' => $node->nid)), 1);
Neil Drumm's avatar
Neil Drumm committed
  $output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
  $output .= theme('xml_icon', url('buddylist/feed'));

  drupal_set_html_head('<link rel="alternate" type="application/rss+xml" title="RSS - buddies posts" href="'. url('buddylist/feed') .'" />');
  return $output;
}
Neil Drumm's avatar
Neil Drumm committed
/**
 * Feed for buddies recent posts
 */
function buddylist_feed_recentposts() {
  global $user;
  if (arg(2)) { 
    $result = db_query('SELECT n.nid, n.title, n.teaser, n.created, u.name, u.uid FROM {node} n INNER JOIN {users} u ON n.uid = u.uid LEFT JOIN {buddylist} b ON b.buddy = u.uid WHERE n.status = 1 AND b.uid = '. arg(2) .' ORDER BY n.nid DESC');
    $channel['title'] = t('Buddies recent posts on %site', array('%site' => variable_get('site_name', 'drupal')));
    $channel['link'] = url('buddylist/recent');
    $channel['description'] = $term->description;
    node_feed($result, $channel);
  }
function buddylist_addbuddy_form($uid) {
  global $user;
  buddylist_update_session();
  $buddy = user_load(array('uid' => $uid));
  if (empty($buddy->name)) {
Neil Drumm's avatar
Neil Drumm committed
    return t('This user does not exist');
Neil Drumm's avatar
Neil Drumm committed
  else if (in_array($uid, $_SESSION['buddylist'])) {
    return t('This user is already on your buddy list');
Neil Drumm's avatar
Neil Drumm committed
  else if ($user->uid == $uid) {
    return t('Cannot add yourself to buddy list');
    $form  = t('Add user <b>%linktouser</b> to buddy list?<br /><br />', array('%linktouser' => l($buddy->name, 'user/' . $uid)));
Neil Drumm's avatar
Neil Drumm committed
    $form .= form_hidden('buddy_id', $uid);
    $form .= form_submit(t('Add user'));
Neil Drumm's avatar
Neil Drumm committed
    return form($form,'post',url('buddylist/save'));
function buddylist_deletebuddy_form($uid) {
  global $user;
  buddylist_update_session();
  $buddy = user_load(array('uid' => $uid));

  if (empty($buddy->name)) {
    return t('This user does not exist');
Neil Drumm's avatar
Neil Drumm committed
  else if (!in_array($uid, $_SESSION['buddylist'])) {
    return t('This user is not on your buddy list');
    $form  = t('Remove user <b>%link</b> from buddy list?<br /><br />', array('%link' => l($buddy->name, 'user/' . $uid)));
Neil Drumm's avatar
Neil Drumm committed
    $form .= form_hidden('buddy_id', $uid);
    $form .= form_submit(t('Remove user'));  
Neil Drumm's avatar
Neil Drumm committed
    return form($form,'post',url('buddylist/remove'));
}

function buddylist_add($id) {
  global $user;
  $user_to_add = user_load(array('uid' => $id));
  
  if (!_buddylist_already_buddy($id)) {
    db_query('INSERT INTO {buddylist} (received, uid, buddy, timestamp) VALUES (1, %d, %d, %d)' , $user->uid , $id , time());
    // DB value buddylist.received set to 1, meaning buddy has a message waiting
    // letting them know you added them as a buddy
    // buddylist.received set back to 0 when user logs in along with being informed of new buddy
    buddylist_update_session();
    drupal_set_message(t('User %username has been added to your buddy list', array('%username' => '<em>'. $user_to_add->name .'</em>')));
  } else {
    drupal_set_message(t('User %username is already on your buddylist', array('%username' => '<em>'. $user_to_add->name .'</em>')));
  }
}

function buddylist_remove($id) {
  global $user;
Neil Drumm's avatar
Neil Drumm committed
  db_query('DELETE FROM {buddylist} where uid = %d and buddy = %d' , $user->uid , $id);
  buddylist_update_session();
}

function buddylist_buddylist($id) {
  global $user;
  
  if ($user->uid != $id) {
    $user = user_load(array('uid' => $id));
    drupal_set_title(t('%username\'s buddylist', array('%username' => $user->name)));
  }
  
  $buddies_per_page = 20;
  $result = pager_query("SELECT buddy FROM {buddylist} b INNER JOIN {users} u ON b.buddy = u.uid WHERE b.uid = ". $id ." ORDER BY u.changed DESC", $buddies_per_page, 0, NULL);

  $output = '<div id="profile">';
  while ($account = db_fetch_object($result)) {
    $output .= theme('profile_profile', user_load(array('uid' => $account->buddy)));
  }
  $output .= '</div>';
  $output .= theme('pager', NULL, $buddies_per_page); //RIGHT HERE TOO!!! (see IMPORTANT note above)
  return $output;
}

function buddylist_buddyoflist($id) {
  $buddies_per_page = 20;
  //$result = pager_query("SELECT uid FROM {buddylist} WHERE buddy = ". $id, $buddies_per_page, 0, NULL);
  $result = pager_query("SELECT u.uid as uid FROM {buddylist} b INNER JOIN {users} u ON b.uid = u.uid WHERE b.buddy = ". $id ." ORDER BY u.changed DESC", $buddies_per_page, 0 , NULL);
  $output = '<div id="profile">';
  while($account = db_fetch_object($result)) {
    $output .= theme('profile_profile', user_load(array('uid' => $account->uid)));
  }
  $output .= '</div>';
  $output .= theme('pager', NULL, $buddies_per_page);
  return $output;
}

function _buddylist_firstfew_buddies($id) { 
  // As of now, db_query_range() is called with a limit of 5. This argument should be configurable in the future.
  $upperlimit = variable_get('buddylist_prof_buddies', 4);
  
  $result = db_query_range('SELECT buddy FROM {buddylist} b INNER JOIN {users} u ON b.buddy = u.uid WHERE b.uid = %d ORDER BY u.changed DESC', $id, 0, $upperlimit);
    
  $output = '<div id="profile">';
  while ($currentbuddy = db_fetch_object($result)) {
    $output .= theme('profile_profile', user_load(array('uid' => $currentbuddy->buddy)));  
  }
  $output .= '</div>';
  
  // check if a "more" link should generated by seeing if there are more buddies than the specified $upperlimit
  $queryresult = db_query('SELECT count(buddy) as buddycount FROM {buddylist} WHERE uid = %d', $id);
  $countresult = db_fetch_object($queryresult);
  if ($countresult->buddycount > $upperlimit) {
      $output .=  l(t('see all buddies'), 'buddylist/'. $id, array('title' => t('View more.')));
  }
  
  return $output;
}

function _buddylist_firstfew_buddyof($id) {
  $upperlimit = variable_get('buddylist_prof_buddies', 4);
  
  $result = db_query_range('SELECT u.uid as uid FROM {buddylist} b INNER JOIN {users} u ON b.uid = u.uid WHERE b.buddy = %d ORDER BY u.changed DESC', $id, 0, $upperlimit);
  $output = '<div id="profile">';
  while ($currentbuddyof = db_fetch_object($result)) {
    $output .= theme('profile_profile', user_load(array('uid' => $currentbuddyof->uid)));
  }
  $output .= '</div>';
  
  // see if a "more" link should be generated by seeing if this person is a buddy of more people than can be listed here
  $queryresult = db_query('SELECT count(uid) as buddyofcount FROM {buddylist} WHERE buddy = %d', $id);
  $countresult = db_fetch_object($queryresult);
  if ($countresult->buddyofcount > $upperlimit) {
    $output .= l(t('see all buddy-ofs'), 'buddylist/buddyof/'. $id, array('title' => t('View more.')));
  }
  
  return $output;
}

function _buddylist_already_buddy($id) {
  global $user;
  buddylist_update_session();
  foreach ($_SESSION['buddylist'] as $buddyid) {
    if ($buddyid == $id) {
      return 1;
    }
  }
  return 0;
}