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', 7), 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 users who\'ve added me.'), 'buddylist_prof_buddies', variable_get('buddylist_prof_buddies', 5), drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), t('The default maximum number of buddies and users who\'ve added me as a buddy 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; $output = ''; if (!user_access('view buddy lists')) { return null; } if (($type == 'login') || ($type == 'load') || ($type == 'view')) { buddylist_setmsg_received($type, $edit, $thisuser); } if (!isset($_SESSION['buddylist'])) { buddylist_update_session(); } // This portion of code runs when a user's profile is viewed if ($type == 'view') { // 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', 5) != 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') $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_buddies', 5) != 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. $_SESSION['buddylist_op_destination'] = $_SERVER['HTTP_REFERER']; if (in_array($thisuser->uid, $_SESSION['buddylist']) && user_access('maintain buddy list')) { $output .= form_item(t('Buddy list'), l(t('Remove from buddy list'), 'buddylist/'. $user->uid .'/delete/' . $thisuser->uid)); } else { if ($user->uid != $thisuser->uid && user_access('maintain buddy list')) { $output .= form_item(t('Buddy list'), l(t('Add to buddy list'), 'buddylist/'. $user->uid .'/add/' . $thisuser->uid)); } } } return array ('' => $output); } 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; } } } /** * Implementation for hook_block */ function buddylist_block($op = 'list', $delta = 0) { global $user; 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) { 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)); while ($account = db_fetch_object($result)) { $account->link = l((strlen($account->name) > 15 ? substr($account->name, 0, 15) .'...' : $account->name), 'user/'. $account->uid); $users[] = $account->link; } $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'] .= ''; } return $block; break; case 1: // Shows my buddies recent posts block $result = db_query_range('SELECT DISTINCT n.nid, n.format, n.status, n.type, u.uid, u.name, n.created, n.title FROM {buddylist} b LEFT JOIN {node} n '. node_access_join_sql() .' ON n.uid=b.buddy LEFT JOIN {users} u ON n.uid = u.uid WHERE n.status = 1 AND b.uid=%d AND '. node_access_where_sql() .' ORDER BY n.nid DESC', $user->uid, 0, variable_get('buddylist_posts_block', 7)); $block['subject'] = variable_get('buddylist_block_title', t('My buddies\' recent posts')); $block['content'] = node_title_list($result); // check if a "more" link should generated by seeing if there are more buddies than the specified $upperlimit $result = db_query('SELECT COUNT(DISTINCT(n.nid)) as node_count FROM {buddylist} b LEFT JOIN {node} n '. node_access_join_sql() .' ON n.uid=b.buddy LEFT JOIN {users} u ON n.uid = u.uid WHERE n.status = 1 AND b.uid=%d AND '. node_access_where_sql(), $user->uid); $countresult = db_fetch_object($result); if (variable_get('buddylist_posts_block', 7) < $countresult->node_count) { $block['content'] .= ''; } return $block; break; } } } /** * Implementation of hook_perm */ function buddylist_perm() { return array('maintain buddy list', 'view buddy lists'); } /** * Implementation of hook_menu */ function buddylist_menu($may_cache) { global $user; $links = array(); $id = arg(1); if ($may_cache) { $links[] = array('path' => 'buddylist', 'title' => t('my buddylist'), 'access' => user_access('view buddy lists'), 'callback' => 'buddylist_buddylisting_page'); $links[] = array('path' => 'buddylist/help', 'title' => t(''), 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_help', 'type' => MENU_CALLBACK); } else if (!empty($id) && is_numeric($id)) { $thisuser = user_load(array('uid' => $id)); $links[] = array('path' => 'buddylist/'. $id, 'title' => t('my buddylist'), 'access' => user_access('view buddy lists'), 'callback' => 'buddylist_buddylisting_page', 'type' => MENU_CALLBACK); //tabs $links[] = array('path' => 'buddylist/'. $id .'/buddies', 'title' => t('buddies'), 'access' => user_access('view buddy lists'), 'callback' => 'buddylist_buddylisting_page', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -1); $links[] = array('path' => 'buddylist/'. $id .'/buddiesof', 'title' => t('buddies of'), 'access' => user_access('view buddy lists'), 'callback' => 'buddylist_buddiesoflisting_page','type' => MENU_LOCAL_TASK, 'weight' => 1); // subtabs $links[] = array('path' => 'buddylist/'. $id .'/buddies/list', 'title' => t('list'), 'access' => user_access('view buddy lists'), 'callback' => 'buddylist_buddylisting_page', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -1); $links[] = array('path' => 'buddylist/'. $id .'/buddies/recent', 'title' => t('recent posts'), 'access' => user_access('view buddy lists'), 'callback' => 'buddylist_buddiesrecent_page', 'type' => MENU_LOCAL_TASK, 'weight' => 1); // RSS feeds $links[] = array('path' => 'buddylist/'. $id .'/buddies/recent/feed', 'title' => t('xml feed'), 'access' => user_access('view buddy lists'), 'callback' => 'buddylist_buddyfeed', 'type' => MENU_CALLBACK); // other callbacks $links[] = array('path' => 'buddylist/'. $id .'/add', 'title' => t('add to buddylist'), 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_addbuddy_page', 'type' => MENU_CALLBACK); $links[] = array('path' => 'buddylist/'. $id .'/save', 'title' => '', 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_savebuddy', 'type' => MENU_CALLBACK); $links[] = array('path' => 'buddylist/'. $id .'/delete', 'title' => t('delete from buddylist'), 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_deletebuddy_page', 'type' => MENU_CALLBACK); $links[] = array('path' => 'buddylist/'. $id .'/remove', 'title' => '', 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_removebuddy', 'type' => MENU_CALLBACK); } return $links; } function buddylist_buddylisting_page() { global $user; $userid = arg(1); if (empty($userid)) { drupal_goto('buddylist/'. $user->uid); } $thisuser = user_load(array('uid' => $userid)); drupal_set_title(t('%username\'s buddylist', array('%username' => $thisuser->name))); $output = '
'; $buddies_per_page = 20; // Compile a list of fields to show $fields = array(); $result = db_query('SELECT name, title, type FROM {profile_fields} WHERE visibility = %d', PROFILE_PUBLIC_LISTINGS); while ($record = db_fetch_object($result)) { $fields[] = $record; } $result = pager_query("SELECT buddy FROM {buddylist} b INNER JOIN {users} u ON b.buddy = u.uid WHERE b.uid = ". $userid ." ORDER BY u.changed DESC", $buddies_per_page, 0, NULL); $output .= '
'; while ($account = db_fetch_object($result)) { $output .= theme('profile_profile', user_load(array('uid' => $account->buddy)), $fields); } $output .= '
'; $output .= theme('pager', NULL, $buddies_per_page); $output .= '
'; print theme('page', $output); } function buddylist_buddiesoflisting_page() { global $user; $userid = arg(1); $thisuser = user_load(array('uid' => $userid)); drupal_set_title(t('%username\'s buddylist', array('%username' => $thisuser->name))); $output = '
'; $buddies_per_page = 20; $result = pager_query("SELECT u.uid as uid FROM {buddylist} b INNER JOIN {users} u ON b.uid = u.uid WHERE b.buddy = ". $userid ." ORDER BY u.changed DESC", $buddies_per_page, 0 , NULL); $output = '
'; while($account = db_fetch_object($result)) { $output .= theme('profile_profile', user_load(array('uid' => $account->uid))); } $output .= '
'; $output .= theme('pager', NULL, $buddies_per_page); $ouput .= '
'; print theme('page', $output); } function buddylist_buddiesrecent_page() { global $user; $userid = arg(1); $thisuser = user_load(array('uid' => $userid)); drupal_set_title(t('%username\'s buddylist', array('%username' => $thisuser->name))); $output = ''; $result = pager_query('SELECT n.nid, n.type, n.format, n.status FROM {node} n '. node_access_join_sql() .' LEFT JOIN {buddylist} b ON n.uid = b.buddy WHERE n.status = 1 AND b.uid = '. $userid .' AND '. node_access_where_sql() .' 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/'. $userid .'/buddies/recent/feed')); drupal_set_html_head(''); print theme('page', $output); } /** * Feed for buddies recent posts */ function buddylist_buddyfeed() { global $user; $userid = arg(1); $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 = '. $userid .' ORDER BY n.nid DESC'); $channel['title'] = t('Buddies recent posts on %site', array('%site' => variable_get('site_name', 'drupal'))); $channel['link'] = url('buddylist/'. $userid .'/buddies/recent'); $channel['description'] = $term->description; node_feed($result, $channel); } function buddylist_addbuddy_page() { $title = t('Confirm addition to buddy list?'); $output = buddylist_addbuddy_form(arg(3)); print theme('page', $output); } function buddylist_savebuddy() { global $user; $edit = $_POST['edit']; $op = $_POST['op']; switch ($op) { case t('Add user'): buddylist_add($edit['buddy_id']); buddylist_goto_referrer(); break; case t('Cancel add'): default: buddylist_cancel_add($edit['buddy_id']); buddylist_goto_referrer(); break; } } function buddylist_deletebuddy_page() { $title = t('Confirm deletion from buddy list?'); $output = buddylist_deletebuddy_form(arg(3)); print theme('page', $output); } function buddylist_removebuddy() { global $user; $edit = $_POST['edit']; $op = $_POST['op']; switch ($op) { case t('Remove user'): buddylist_remove($edit['buddy_id']); buddylist_goto_referrer(); break; case t('Cancel remove'): default: buddylist_cancel_remove($edit['buddy_id']); buddylist_goto_referrer(); break; } } function buddylist_addbuddy_form($uid) { global $user; buddylist_update_session(); $buddy = user_load(array('uid' => $uid)); if (empty($buddy->name)) { return t('This user does not exist'); } else if (in_array($uid, $_SESSION['buddylist'])) { return t('This user is already on your buddy list'); } else if ($user->uid == $uid) { return t('Cannot add yourself to buddy list'); } else { $form = t('Add user %linktouser to buddy list?

', array('%linktouser' => l($buddy->name, 'user/' . $uid))); $form .= t('The user will be notified the next time s/he is logged in.

'); $form .= form_hidden('buddy_id', $uid); $form .= form_submit(t('Add user')); $form .= form_submit(t('Cancel add')); return form($form,'post',url('buddylist/'. $uid .'/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'); } else if (!in_array($uid, $_SESSION['buddylist'])) { return t('This user is not on your buddy list'); } else { $form = t('Remove user %link from buddy list?

', array('%link' => l($buddy->name, 'user/' . $uid))); $form .= form_hidden('buddy_id', $uid); $form .= form_submit(t('Remove user')); $form .= form_submit(t('Cancel remove')); return form($form,'post',url('buddylist/'. $user->uid .'/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' => ''. $user_to_add->name .''))); } else { drupal_set_message(t('User %username is already on your buddylist', array('%username' => ''. $user_to_add->name .''))); } } function buddylist_remove($id) { global $user; db_query('DELETE FROM {buddylist} where uid = %d and buddy = %d' , $user->uid , $id); $thisuser = user_load(array('uid' => $id)); drupal_set_message(t('user %username removed from buddylist',array('%username' => ''. $thisuser->name . '') )); buddylist_update_session(); } function _buddylist_firstfew_buddies($id) { $upperlimit = variable_get('buddylist_prof_buddies', 5); $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 = '
'; while ($currentbuddy = db_fetch_object($result)) { $output .= theme('profile_profile', user_load(array('uid' => $currentbuddy->buddy))); } $output .= '
'; // 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', 5); $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 = '
'; while ($currentbuddyof = db_fetch_object($result)) { $output .= theme('profile_profile', user_load(array('uid' => $currentbuddyof->uid))); } $output .= '
'; // 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 buddies of'), 'buddylist/'. $id .'/buddiesof', 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; } function buddylist_cancel_add($id) { $thisuser = user_load(array('uid' => $id)); drupal_set_message(t('User %name was NOT added to your buddylist.', array('%name' => ''. $thisuser->name .''))); } function buddylist_cancel_remove($id) { $thisuser = user_load(array('uid' => $id)); drupal_set_message(t('User %name was NOT removed to your buddylist.', array('%name' => ''. $thisuser->name . ''))); } /** * This code is used to redirect a browser back to the url that referred the browser * to the buddylist_add() or buddylist_remove() actions. * * Until a patch comes out that allows us to directly pass a complete * URL to drupal_goto(), we take the code from drupal_goto, and change * the last parameter to str_replace() from a call to url() to the * actual url we want to forward to (and have stored in a session variable) */ function buddylist_goto_referrer() { // Translate & to simply & in the absolute URL $url = str_replace('&', '&', $_SESSION['buddylist_op_destination']); if (ini_get('session.use_trans_sid') && session_id() && !strstr($url, session_id())) { $sid = session_name() . '=' . session_id(); if (strstr($url, '?') && !strstr($url, $sid)) { $url = $url .'&'. $sid; } else { $url = $url .'?'. $sid; } } // Before the redirect, allow modules to react to the end of the page request. module_invoke_all('exit', $url); unset($_SESSION['buddylist_op_destination']); header('Location: '. $url); // The "Location" header sends a REDIRECT status code to the http // daemon. In some cases this can go wrong, so we make sure none // of the code below the drupal_goto() call gets executed when we redirect. exit(); } ?>