Newer
Older
adrian
committed
<?php
/**
* Implementation of hook_help
*/
function buddylist_help($field) {
switch ($field) {
case 'admin/modules#description':
return t('Enable buddy list functionality.');
}
adrian
committed
/**
* 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
*/
$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')) ));
$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.'));
$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;
if ($user->uid == 0) {
return null;
}
if (($type == 'login') || ($type == 'load') || ($type == 'view')) {
buddylist_setmsg_received($type, $edit, $thisuser);
adrian
committed
}
// This portion of code runs when a user's profile is viewed
// Returns a list of $thisuser's buddies
// the only purpose of $friends and $has_friends is to determine whether any friends exist for $thisuser
$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')) {
$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));
adrian
committed
}
}
adrian
committed
function buddylist_update_session() {
global $user;
$_SESSION['buddylist'] = array();
if (isset($user->uid)) {
$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;
}
adrian
committed
/**
* Implementation for hook_block
*/
function buddylist_block($op = 'list', $delta = 0) {
adrian
committed
if ($op == 'list') {
$block[0]['info'] = t('Buddy list');
$block[1]['info'] = t('Buddies\' recent posts');
else if ($op == 'view' && user_access('access content') && $user->uid > 0) {
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));
$account->link = l((strlen($account->name) > 15 ? substr($account->name, 0, 15) .'...' : $account->name), 'user/'. $account->uid);
adrian
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>';
}
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));
$content_list[] .= l($list->title, 'node/view/'. $list->nid) .' ('. l($list->name , 'user/'. $list->uid) . ') ';
}
$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
*/
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;
if (empty($op)) {
$op = arg(1);
}
case 'feed':
buddylist_feed_recentposts();
break;
case 'add':
$title = t('Confirm addition to buddy list?');
$output = buddylist_addbuddy_form(arg(2));
break;
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)) );
$thisuser = db_fetch_object(db_query('SELECT name FROM {users} WHERE uid = %d', arg(2)));
$output = '<div class="members">'. buddylist_buddyoflist(arg(2)) .'</div>';
drupal_set_title(t('Users who added you as a buddy'));
$output = '<div class="members">'. buddylist_buddyoflist($user->uid) .'</div>';
case 'save':
$output = buddylist_add($edit['buddy_id']);
drupal_goto('buddylist');
$title = t('Confirm removal from buddy list?');
$output = buddylist_deletebuddy_form(arg(2));
break;
adrian
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');
adrian
committed
case 'recent':
//$title = 'My buddies recent posts.';
$output = buddylist_recentposts();
break;
case '':
$output = buddylist_buddylist($user->uid);
$output = '<div class="members">'. buddylist_buddylist(arg(1)) .'</div>';
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);
$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;
}
/**
* 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));
adrian
committed
}
else if (in_array($uid, $_SESSION['buddylist'])) {
return t('This user is already on your buddy list');
adrian
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)));
$form .= form_submit(t('Add user'));
adrian
committed
}
adrian
committed
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');
adrian
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)));
$form .= form_submit(t('Remove user'));
adrian
committed
}
}
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;
db_query('DELETE FROM {buddylist} where uid = %d and buddy = %d' , $user->uid , $id);
adrian
committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
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;
}