Newer
Older
include_once(drupal_get_path('module', 'buddylist'). '/buddylist_views.inc');
}
define('BUDDYLIST_ADD_SUBJECT', variable_get('buddylist_add_subject', "[@site] You are @adder_name's newest @buddy"));
define('BUDDYLIST_REMOVE_SUBJECT', variable_get('buddylist_remove_subject', "[@site] You have been removed from @adder_name's @buddylist"));
define('BUDDYLIST_REQUEST_SUBJECT', variable_get('buddylist_request_subject', "[@site] @adder_name has requested to add you to his/her @buddylist"));
define('BUDDYLIST_APPROVAL_SUBJECT', variable_get('buddylist_approval_subject', "[@site] @addee_name has approved your @buddylist add request"));
/**
* returns an array of common translation placeholders
*/
function buddylist_translation() {
$translations = array(
'@buddy' => t('buddy'),
'@Buddy' => t('Buddy'),
'@buddylist' => t('buddylist'),
'@Buddylist' => t('Buddylist'),
'@buddies' => t('buddies'),
'@Buddies' => t('Buddies'),
'@buddyof' => t('buddy of'),
'@Buddyof' => t('Buddy of'),
);
Wolfgang Ziegler
committed
return variable_get('buddylist_translation', $translations);
}
/**
* Implementation of hook_help
*/
function buddylist_help($section) {
switch ($section) {
case 'admin/help#buddylist':
$output = t("
<p>@Buddy list enables users to keep a list of @buddies from their social network in their user account.
Users can also track what their @buddies are posting to the site.
Furthermore, they can track their <i>@buddies'</i> @buddies and thereby explore their social network.</p>
<p>If the administrator has enabled the profile module, users can add @buddies via their @buddies' user profiles.
On the \"View\" tab of each user's profile, there is a \"@Buddylist\" section. Select the 'add @buddy' action to add the user to your @buddylist.
If a user is already in your @buddylist, the 'delete' action will remove the @buddy. Administrators can also enable the @buddylist block.
This block allows you to see a list of your @buddies. If the <a href=\"http://drupal.org/project/foaf\">Friends Of A Friend (FOAF)</a> module is enabled, it will be possible to share @buddylists with other FOAF-aware social networking applications.</p>
<p>You can:</p>
<ul>
<li>add a @buddy by looking at their profile: <a href=\"@userprofiles\" title=\"View user profiles\">view user profiles</a></li>
<li>allow users to view profiles in <a href=\"@setaccesspermissions\" title=\"set access permissions\">administer » access control</a></li>
<li>enable the @buddylist block at <a href=\"@blockadministration\" title=\"block administration\">administer » block</a></li>
<li>administer the @buddylist block at <a href=\"@buddylistsettings\" title=\"@buddylist settings\">administer » settings » @buddylist</a></li>
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
</ul>
<p>For more information, read the configuration and customization handbook <a href=\"http://drupal.org/handbook/modules/Buddylist\" title=\"Buddylist page\">Buddylist page</a></p>",
array('@userprofiles' => url('profile'),
'@setaccesspermissions' => url('admin/user/access'),
'@blockadministration' => url('admin/build/block'),
'@buddylistsettings' => url('admin/settings/buddylist')
) + buddylist_translation());
return $output;
}
}
/**
* 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;
$items = array();
$id = is_numeric(arg(1)) ? arg(1) : $user->uid;
if ($may_cache) {
// buddylist settings page
$items[] = array(
'path' => 'admin/settings/buddylist',
'title' => t('Buddylist'), // Note that this isn't translated on purpose since it is for the admin
'description' => t('Buddylist configuration options for blocks, email, etc.'),
'callback' => 'drupal_get_form',
'callback arguments' => 'buddylist_admin_settings',
'access' => user_access('administer site configuration'),
);
// my buddylist menu item
$items[] = array(
'path' => 'buddylist',
'title' => t('My @buddylist', buddylist_translation()),
'access' => (user_access('maintain buddy list') && $id),
'callback' => 'buddylist_buddylisting_page',
Wolfgang Ziegler
committed
'callback arguments' => array($user->uid, 'buddies'),
);
}
else {
// 'edit access' only granted to user's own buddy list or to administrative users
$editAccess = (
($id == $user->uid && user_access('maintain buddy list') && $user->uid)
|| user_access('administer users'));
Robert Douglass
committed
Robert Douglass
committed
$approval_required = variable_get('buddylist_require_approval', 0);
$items[] = array(
'path' => 'buddy/add',
'title' => t('Add to @buddylist', buddylist_translation()),
'access' => $editAccess,
'callback' => 'drupal_get_form',
'callback arguments' => array('buddylist_addbuddy', arg(2)),
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'buddy/delete',
'title' => t('Delete from @buddylist', buddylist_translation()),
'access' => $editAccess,
'callback' => 'drupal_get_form',
'callback arguments' => array('buddylist_deletebuddy', arg(2)),
'type' => MENU_CALLBACK,
);
// 'view only' tabs
Wolfgang Ziegler
committed
$viewAccess = (($id == $user->uid && user_access('maintain buddy list')) || user_access('view buddy lists'));
Robert Douglass
committed
// If buddylist approval is required, then upon approval, both parties become buddies of each other.
Robert Douglass
committed
// So, in effect, idea 'buddyof' becomes redundant.
Robert Douglass
committed
if (!$approval_required) {
$items[] = array(
'path' => 'buddylist/'. $id .'/buddies',
'title' => t('@Buddies', buddylist_translation()),
'access' => $viewAccess,
'callback' => 'buddylist_buddylisting_page',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
'callback arguments' => array($id)
);
Robert Douglass
committed
$items[] = array(
'path' => 'buddylist/'. $id .'/buddyof',
'title' => t('@Buddyof', buddylist_translation()),
'access' => $viewAccess,
'callback' => 'buddylist_buddylisting_page',
'type' => MENU_LOCAL_TASK,
'weight' => 1,
'callback arguments' => array($id, 'buddyof')
);
}
// subtabs
$items[] = array(
'path' => 'buddylist/'. $id .'/buddies/list',
Wolfgang Ziegler
committed
'title' => t('@Buddylist', buddylist_translation()),
'access' => $viewAccess,
'callback' => 'buddylist_buddylisting_page',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
'callback arguments' => array($id),
);
$items[] = array(
'path' => 'buddylist/'. $id .'/buddies/recent',
'title' => t('Recent posts'),
'access' => ($viewAccess && module_exists('tracker')),
'callback' => 'buddylist_buddiesrecent_page',
'type' => MENU_LOCAL_TASK,
if (variable_get('buddylist_buddygroups', FALSE)) {
$items[] = array(
'path' => 'buddylist/'. $id .'/buddies/groups/view',
'title' => t('View groups'),
'access' => $viewAccess,
'callback' => 'buddylist_buddiesgroups_page',
'type' => MENU_LOCAL_TASK,
'callback arguments' => array($id),
);
$items[] = array(
'path' => 'buddylist/'. $id .'/buddies/groups/edit',
'title' => t('Edit groups'),
'access' => $editAccess,
'callback' => 'buddylist_buddiesgroups_edit',
'type' => MENU_LOCAL_TASK,
'callback arguments' => array($id),
);
}
Robert Douglass
committed
// sub-subtabs
if ($approval_required && $editAccess) {
$items[] = array(
'path' => 'buddylist/'. $id .'/buddies/requests',
'title' => t('Pending requests'),
Robert Douglass
committed
'access' => $editAccess,
'callback' => 'theme',
Robert Douglass
committed
'type' => MENU_LOCAL_TASK,
'weight' => 0,
'callback arguments' => array('buddylist_pending_requests', $id)
Robert Douglass
committed
);
Wolfgang Ziegler
committed
$items[] = array(
'path' => 'buddylist/'. $id .'/buddies/requested/accept',
'title' => t('Accept Request'),
'access' => $editAccess,
'type' => MENU_CALLBACK,
Wolfgang Ziegler
committed
'callback arguments' => array('buddylist_pending_requested_accept',$id),
);
Wolfgang Ziegler
committed
$items[] = array(
'path' => 'buddylist/'. $id .'/buddies/requested/deny',
'title' => t('Deny Request'),
'access' => $editAccess,
'type' => MENU_CALLBACK,
Wolfgang Ziegler
committed
'callback arguments' => array('buddylist_pending_requested_deny',$id),
);
Wolfgang Ziegler
committed
$items[] = array(
'path' => 'buddylist/'. $id .'/buddies/request/cancel',
'title' => t('Cancel Request'),
'access' => $editAccess,
'type' => MENU_CALLBACK,
Wolfgang Ziegler
committed
'callback arguments' => array('buddylist_cancel_request',$id),
);
Robert Douglass
committed
}
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// other callbacks
if ($id != $user->uid) {
// This callback can interfere with the 'my buddylist' menu item,
// so we only load it if the user is viewing another user's list.
$items[] = array(
'path' => 'buddylist/'. $id,
'title' => t('@Buddylist', buddylist_translation()),
'access' => (($viewAccess || $editAccess) && $id),
'callback' => 'buddylist_buddylisting_page',
'type' => MENU_CALLBACK,
'callback arguments' => array($id),
);
}
$items[] = array(
'path' => 'buddylist/'. $id .'/buddies/recent/feed',
'title' => t('Xml feed'),
'access' => $viewAccess,
'callback' => 'buddylist_buddyfeed',
'type' => MENU_CALLBACK,
'callback arguments' => array($id),
);
}
return $items;
}
/**
* Buddylist administration settings page
*/
function buddylist_admin_settings() {
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
$form['general'] = array(
'#type' => 'fieldset',
'#title' => t('General settings'),
);
$form['general']['buddylist_require_approval'] = array(
'#type' => 'radios',
'#title' => t('Require approval'),
'#default_value' => variable_get('buddylist_require_approval', 0),
'#description' => t("Select 'Yes' if a user's request to be someone's @buddy should be approved by the other user first. Upon approval, both parties will be @buddies of each other.", buddylist_translation()),
'#options' => array(1 => t('Yes'), 0 => t('No'))
);
$form['general']['buddylist_buddygroups'] = array(
'#type' => 'checkbox',
'#title' => t('Enable @buddy groups', buddylist_translation()),
'#description' => t('Enables @buddylist @buddy groups. Users will be able to create @buddy groups to manage their @buddies.', buddylist_translation()),
'#default_value' => variable_get('buddylist_buddygroups', FALSE),
);
// User profile page settings
$form['profile_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Profile page options'),
);
$form['profile_settings']['buddylist_prof_buddies'] = array(
'#type' => 'select',
'#title' => t('Number of @buddies and users who\'ve added me', buddylist_translation()),
'#default_value' => variable_get('buddylist_prof_buddies', 5),
'#options' => drupal_map_assoc(range(0, 10)),
'#description' => t('The default maximum number of @buddies and users who\'ve added me as a @buddy to display on a user\'s profile page.', buddylist_translation()),
);
// TODO: move these to block settings
$form['block_settings'] = array(
'#type' => 'fieldset',
'#title' => t('@Buddylist block options', buddylist_translation()),
);
$form['block_settings']['buddylist_blocklisting_size'] = array(
'#type' => 'select',
'#title' => t("Number of @buddies to list in the user's @buddy block", buddylist_translation()),
'#default_value' => variable_get('buddylist_blocklisting_size', 5),
'#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)),
'#description' => 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/build/block')) + buddylist_translation()),
);
$form['block_settings']['buddylist_posts_block'] = array(
'#type' => 'select',
'#title' => t("Number of posts to list in the @buddies' recent posts block", buddylist_translation()),
'#default_value' => variable_get('buddylist_posts_block', 7),
'#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)),
'#description' => 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/build/block')) + buddylist_translation()),
);
$form['block_settings']['buddylist_block_title'] = array(
'#type' => 'textfield',
'#title' => t('"My @buddies\' recent posts" block title', buddylist_translation()),
'#default_value' => variable_get('buddylist_block_title', t("My @buddies' recent posts", buddylist_translation())),
'#size' => 70,
'#maxlength' => 128,
'#description' => t('This will be the title for the recent @buddies post block. If none is specified, "My @buddies\' recent posts" will be used.', buddylist_translation()),
);
Wolfgang Ziegler
committed
$form['block_settings']['buddylist_status_block_title'] = array(
'#type' => 'textfield',
'#title' => t('"My @buddylist status" block title', buddylist_translation()),
'#default_value' => variable_get('buddylist_status_block_title', t("My @buddylist status", buddylist_translation())),
Wolfgang Ziegler
committed
'#size' => 70,
'#maxlength' => 128,
'#description' => t('This will be the title for the @buddylist status block. If none is specified, "My @buddylist status" will be used.', buddylist_translation()),
Wolfgang Ziegler
committed
);
Josh Koenig
committed
$form['block_settings']['buddylist_list_block_title'] = array(
'#type' => 'textfield',
'#title' => t('"My @buddies list" block title', buddylist_translation()),
'#weight' => 1,
'#default_value' => variable_get('buddylist_list_block_title', t('My @buddylist', buddylist_translation())),
Josh Koenig
committed
'#size' => 70,
'#maxlength' => 128,
'#description' => t('This will be the title for the "My @buddylist" block. If none is specified, "My @buddylist" will be used.', buddylist_translation()),
Josh Koenig
committed
);
$form['block_settings']['buddylist_block_if_no_buddies'] = array(
'#type' => 'checkbox',
'#title' => t('Show "My @buddies list" block even if @buddylist is empty', buddylist_translation()),
'#weight' => 2,
'#default_value' => variable_get('buddylist_block_if_no_buddies', FALSE),
'#description' => t('If a user has no @buddies, the @buddy block can show a message.', buddylist_translation()),
);
$form['block_settings']['buddylist_empty_text'] = array(
'#type' => 'textarea',
'#title' => t('Show this text in "My @buddies list" if @buddylist is empty', buddylist_translation()),
'#weight' => 3,
'#default_value' => variable_get('buddylist_empty_text', t(buddylist_empty_text_default(), buddylist_translation())),
'#description' => t('If a user has no @buddies and the above checkbox is checked, this message is shown instead of a list.', buddylist_translation()),
);
$form['mail'] = array(
'#type' => 'fieldset',
'#title' => t('email'),
);
Robert Douglass
committed
global $user;
$macros = implode(', ', array_keys(buddylist_mail_replacements($user, $user)));
$approval_macros = implode(', ', array_keys(buddylist_approval_mail_replacements($user, $user)));
Wolfgang Ziegler
committed
$form['mail']['buddylist_user_mail'] = array(
'#type' => 'checkbox',
'#title' => t('Allow users to turn off buddylist messages'),
'#default_value' => variable_get('buddylist_user_mail', FALSE),
'#description' => t('If you check this, users will have a new setting on their account edit page.'),
);
$form['mail']['buddylist_send_add'] = array(
'#type' => 'checkbox',
'#title' => t('Send add messages'),
'#default_value' => variable_get('buddylist_send_add', FALSE),
);
$form['mail']['buddylist_add_subject'] = array(
'#type' => 'textfield',
'#title' => t('Added @buddy email subject', buddylist_translation()),
'#default_value' => BUDDYLIST_ADD_SUBJECT,
);
$form['mail']['buddylist_add_message'] = array(
'#type' => 'textarea',
'#title' => t('Added @buddy email message', buddylist_translation()),
'#default_value' => variable_get('buddylist_add_message', buddylist_mail_add_default()),
'#description' => t('Replacement strings are: %macros', array('%macros' => $macros)),
);
$form['mail']['buddylist_send_remove'] = array(
'#type' => 'checkbox',
'#title' => t('Send remove messages'),
'#default_value' => variable_get('buddylist_send_remove', FALSE),
);
$form['mail']['buddylist_remove_subject'] = array(
'#type' => 'textfield',
'#title' => t('Removed @buddy email subject', buddylist_translation()),
'#default_value' => BUDDYLIST_REMOVE_SUBJECT,
);
$form['mail']['buddylist_remove_message'] = array(
Robert Douglass
committed
'#title' => t('Removed @buddy email message', buddylist_translation()),
'#default_value' => variable_get('buddylist_remove_message', buddylist_mail_remove_default()),
'#description' => t('Replacement strings are: %macros', array('%macros' => $macros)),
);
Robert Douglass
committed
Robert Douglass
committed
$form['mail']['buddylist_send_request'] = array(
'#type' => 'checkbox',
'#title' => t('Send request messages.'),
'#description' => t('Check this box if you want users to receive an email when someone requests to be their buddy. This setting only has effect if approval is required to be on someone\'s buddylist.'),
Robert Douglass
committed
'#default_value' => variable_get('buddylist_send_request', FALSE)
);
Robert Douglass
committed
Robert Douglass
committed
$form['mail']['buddylist_request_subject'] = array(
'#type' => 'textfield',
'#title' => t('@buddy request email subject', buddylist_translation()),
'#default_value' => BUDDYLIST_REQUEST_SUBJECT,
);
Robert Douglass
committed
$form['mail']['buddylist_request_message'] = array(
Robert Douglass
committed
'#type' => 'textarea',
'#title' => t('@buddy request email message', buddylist_translation()),
'#default_value' => variable_get('buddylist_request_message', buddylist_mail_request_default()),
Robert Douglass
committed
'#description' => t('Replacement strings are: %macros', array('%macros' => $macros)),
);
Robert Douglass
committed
Robert Douglass
committed
$form['mail']['buddylist_send_approval'] = array(
'#type' => 'checkbox',
'#title' => t('Send approval messages'),
'#default_value' => variable_get('buddylist_send_approval', FALSE),
'#description' => t('Check this box if you want users to receive an email to the requester when someone approves an add request. This setting only has effect if approval is required to be on someone\'s @buddylist.', buddylist_translation())
Robert Douglass
committed
);
Robert Douglass
committed
Robert Douglass
committed
$form['mail']['buddylist_approval_subject'] = array(
'#type' => 'textfield',
'#title' => t('@buddy request email subject', buddylist_translation()),
'#default_value' => BUDDYLIST_APPROVAL_SUBJECT,
);
Robert Douglass
committed
$form['mail']['buddylist_approval_message'] = array(
Robert Douglass
committed
'#type' => 'textarea',
'#title' => t('@buddy approval email message', buddylist_translation()),
'#default_value' => variable_get('buddylist_approval_message', buddylist_mail_approval_default()),
Robert Douglass
committed
'#description' => t('Replacement strings are: %macros', array('%macros' => $approval_macros)),
Robert Douglass
committed
);
/**
* Implementation of hook_user
*/
function buddylist_user($type, &$edit, &$thisuser, $category = NULL) {
global $user;
$output = array();
// show any buddylist notifications upon login and upon viewing own profile
if (user_access('maintain buddy list') && (($type == 'login') || ($type == 'view') && ($thisuser->uid == $user->uid))) {
buddylist_setmsg_received($thisuser);
}
if ($type == 'view') {
if ($list = buddylist_get_buddylist($thisuser)) {
$output[] = array('title' => t('@Buddies', buddylist_translation()), 'value' => $list, 'class' => 'buddylist');
}
if ($list = buddylist_get_buddylist($thisuser, TRUE)) {
$output[] = array('title' => t('@buddy of', buddylist_translation()), 'value' => $list, 'class' => 'buddyoflist');
}
if ($actions = buddylist_get_buddy_actions($user, $thisuser)) {
$output[] = array('title' => t('@Buddy actions', buddylist_translation()), 'value' => theme('item_list', $actions), 'class' => 'buddylist_actions');
}
if(count($output) > 0) {
return array(t('@Buddy List', buddylist_translation()) => $output);
}
}
else if ($type == 'delete') {
db_query("DELETE FROM {buddylist} WHERE uid = %d OR buddy = %d", $thisuser->uid, $thisuser->uid);
db_query("DELETE FROM {buddylist_buddy_group} WHERE uid = %d OR buddy = %d", $thisuser->uid, $thisuser->uid);
db_query("DELETE FROM {buddylist_groups} WHERE uid = %d", $thisuser->uid);
db_query("DELETE FROM {buddylist_pending_requests} WHERE requester_uid = %d OR requestee_uid = %d", $thisuser->uid, $thisuser->uid);
}
else if ($type == 'load') {
$thisuser->buddies = buddylist_get_buddies($thisuser->uid);
}
Wolfgang Ziegler
committed
else if ($type == 'form' && $category == 'account' && variable_get('buddylist_user_mail', FALSE) && user_access('maintain buddy list', $thisuser)) {
Wolfgang Ziegler
committed
// when user tries to edit his own data
$form['buddylist_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Buddylist settings'),
'#weight' => 5);
$form['buddylist_settings']['buddylist_mail'] = array(
'#type' => 'checkbox',
'#title' => t('Receive @buddylist notification mails', buddylist_translation()),
'#default_value' => isset($edit['buddylist_mail']) ? $edit['buddylist_mail'] : 1,
'#description' => t('If you check this, you will be notified about important actions regarding your @Buddylist.', buddylist_translation()));
return $form;
Robert Douglass
committed
}
}
/*
* Return a formatted list of buddies for the given user
* @param $buddy_of If set to TRUE, a formatted list of users is returned, for whom this user is a buddy.
*/
function buddylist_get_buddylist($user, $buddy_of = FALSE) {
if (user_access('view buddy lists') && !$buddy_of) {
if ($buddies = buddylist_get_buddies($user->uid)) {
foreach(array_keys($buddies) as $buddy) {
$account = user_load(array('uid' => $buddy));
$listbuddies[] = $account;
$i++;
if ($i > variable_get('buddylist_prof_buddies', 5)) {
return theme('user_list', $listbuddies);
}
else if (user_access('view buddy lists') && !variable_get('buddylist_require_approval', 0)) {
// 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')
Robert Douglass
committed
// Of course, this distinction doesn't exist if approval is required to add a buddy (in which case, buddy relationships are symmetric)
$sql = 'SELECT b.uid, u.name FROM {buddylist} b INNER JOIN {users} u ON b.uid = u.uid WHERE b.buddy = %d ORDER BY u.access DESC';
$result = db_query_range($sql, $user->uid, 0, variable_get('buddylist_prof_buddies', 5));
while ($row = db_fetch_object($result)) {
$listbuddyof[$row->uid] = $row;
}
if ($listbuddyof) {
return theme('user_list', $listbuddyof);
Wolfgang Ziegler
committed
/*
* Returns an array of posible actions (html) for the viewing user,
Robert Douglass
committed
* e.g. a link to make the viewed user a buddy
*/
function buddylist_get_buddy_actions(&$viewing_user, &$viewed_user) {
$actions = array();
if (!user_access('maintain buddy list') || $viewing_user->uid == $viewed_user->uid) {
return $actions;
if (variable_get('buddylist_require_approval', FALSE) && in_array($viewed_user->uid, array_keys(buddylist_get_requestees($viewing_user->uid)))) {
$actions[] = t('You have requested to add this user to your @buddylist. (See !your_pending_requests)', array('!your_pending_requests' => l(t('your pending requests'), 'buddylist/'. $viewing_user->uid .'/buddies/requests')) + buddylist_translation());
}
else if (in_array($viewed_user->uid, array_keys(buddylist_get_buddies($viewing_user->uid)))) {
$actions[] = theme('remove_from_buddylist_link', $viewed_user);
}
else if (in_array($viewing_user->uid, array_keys(buddylist_get_requestees($viewed_user->uid)))) {
$actions[] = t('This user has requested to add you to your @buddylist.', buddylist_translation()) .
drupal_get_form('buddylist_approval_form', $viewing_user->uid, $viewed_user->uid);
}
else {
$actions[] = theme('add_to_buddylist_link', $viewed_user);
}
return $actions;
}
/**
* Implementation for hook_block
*/
function buddylist_block($op = 'list', $delta = 0) {
global $user;
if ($op == 'list') {
$block[0]['info'] = variable_get('buddylist_list_block_title', t('My @buddylist', buddylist_translation()));
$block[1]['info'] = variable_get('buddylist_block_title', t('My @buddies\' recent posts', buddylist_translation()));
Wolfgang Ziegler
committed
if (variable_get('buddylist_require_approval', 0)) {
$block[2]['info'] = variable_get('buddylist_status_block_title', t('My @buddy status', buddylist_translation()));
}
Wolfgang Ziegler
committed
else if ($op == 'view' && user_access('access content') && user_access('maintain buddy list') && $user->uid > 0) {
switch ($delta) {
case 0 : // Shows buddylist block
if ($buddies = buddylist_get_buddies()) {
Josh Koenig
committed
// we have buddies defined and generate the list
$i = 0;
foreach (array_keys($buddies) as $buddy) {
$users[] = user_load(array('uid' => $buddy));
$i++;
if ($i == variable_get('buddylist_blocklisting_size', 5)) {
break;
}
}
$block['content'] = theme('user_list', $users);
Josh Koenig
committed
} else {
// buddylist is empty
if(variable_get('buddylist_block_if_no_buddies', FALSE)) {
// Show a message that we have no buddies yet
$block['content'] = variable_get('buddylist_empty_text', t(buddylist_empty_text_default(), buddylist_translation()));
} else {
// If no buddies defined and no message available, end 'case' without returning block.
break;
}
}
// this is the same output whether buddylist or not
$block['subject'] = variable_get('buddylist_list_block_title', t('My @buddylist', buddylist_translation()));
Josh Koenig
committed
// check if a "more" link should generated by seeing if there are more buddies than the specified $upperlimit
if (count($buddies) > variable_get('buddylist_blocklisting_size', 5)) {
$block['content'] .= '<div class="more-link">' . l(t('more'), 'buddylist', array('title' => t('View more.'))) . '</div>';
}
Josh Koenig
committed
return $block;
break;
case 1: // Shows my buddies recent posts block
$buddies = buddylist_get_buddies();
$keys = array_keys($buddies);
if (count($keys) > 0) {
$str_buddies = implode(',', $keys);
$result = db_query_range(db_rewrite_sql("SELECT n.nid, n.status, n.type, u.uid, u.name, n.created, n.title FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.status = 1 AND n.uid IN ($str_buddies) ORDER BY n.nid DESC"), 0, variable_get('buddylist_posts_block', 7));
if (db_num_rows($result)) {
$block['subject'] = variable_get('buddylist_block_title', t('My @buddies\' recent posts', buddylist_translation()));
$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(db_rewrite_sql('SELECT COUNT(n.nid) AS node_count 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'), $user->uid);
$countresult = db_fetch_object($result);
if (module_exists('tracker') && variable_get('buddylist_posts_block', 7) < $countresult->node_count) {
$block['content'] .= '<div class="more-link">'. l(t('more'), 'buddylist/'. $user->uid .'/buddies/recent', array('title' => t('View more.'))) .'</div>';
}
return $block;
}
}
break;
Robert Douglass
committed
Wolfgang Ziegler
committed
case 2: // Buddylist status
$block['subject'] = variable_get('buddylist_status_block_title', t('My @buddy status', buddylist_translation()));
$count = db_result(db_query("SELECT COUNT(uid) FROM {buddylist} WHERE uid = %d", $user->uid));
$sent = db_result(db_query("SELECT COUNT(requester_uid) FROM {buddylist_pending_requests} WHERE requester_uid = %d", $user->uid));
$received = db_result(db_query("SELECT COUNT(requestee_uid) FROM {buddylist_pending_requests} WHERE requestee_uid = %d", $user->uid));
Robert Douglass
committed
Wolfgang Ziegler
committed
$block['content'] = theme('buddylist_status_block', $count, $received, $sent);
return $block;
break;
}
}
}
/**
* Public API for retrieving buddies. Feel free to use this from other
* modules.
* $key can be 'uid' or 'label'.
*/
function buddylist_get_buddies($uid = NULL, $key = 'uid') {
static $buddies;
if (!$uid) {
global $user;
$uid = $user->uid;
}
if (!isset($buddies[$key][$uid])) {
$buddies[$key][$uid] = array();
Wolfgang Ziegler
committed
$sql = 'SELECT b.buddy, u.name, u.mail, u.uid FROM {buddylist} b
INNER JOIN {users} u ON b.buddy = u.uid
WHERE b.uid = %d';
$result = db_query($sql, $uid);
while ($row = db_fetch_object($result)) {
Wolfgang Ziegler
committed
$buddies[$key][$uid][$row->buddy]['uid'] = $row->uid;
$buddies[$key][$uid][$row->buddy]['name'] = $row->name;
$buddies[$key][$uid][$row->buddy]['mail'] = $row->mail;
$buddies[$key][$uid][$row->buddy]['groups'] = buddylist_get_buddy_groups($uid, $row->buddy);
$buddies[$key][$uid][$row->buddy]['online'] = 0;
$selectlist .= $row->buddy.",";
}
// Add the online flag
if (db_num_rows($result)) {
$sql = 'SELECT uid FROM {sessions} WHERE uid IN (%s) AND timestamp > %d';
$result = db_query($sql, substr($selectlist,0,-1), time()-1800);
while ($row = db_fetch_object($result)) {
$buddies[$key][$uid][$row->uid]['online'] = 1;
}
}
}
return $buddies[$key][$uid];
}
Wolfgang Ziegler
committed
/**
* Returns an array of uid => name of people that user with param $uid has made a buddy request to
*/
Robert Douglass
committed
function buddylist_get_requestees($uid) {
$buddies = array();
Robert Douglass
committed
Robert Douglass
committed
$result = db_query('SELECT bpr.requestee_uid, u.name FROM {buddylist_pending_requests} bpr INNER JOIN {users} u ON bpr.requestee_uid = u.uid WHERE requester_uid = %d', $uid);
Robert Douglass
committed
Robert Douglass
committed
while ($row = db_fetch_object($result)) {
$buddies[$row->requestee_uid] = $row->name;
}
Robert Douglass
committed
Robert Douglass
committed
return $buddies;
}
function buddylist_setmsg_received($thisuser) {
global $user;
Robert Douglass
committed
if (variable_get('buddylist_require_approval', 0)) {
// Go through and find new buddylist add-requests, (i.e., the ones in {buddylist_pending_requests} w/ received column == 0
Wolfgang Ziegler
committed
$result = db_query('SELECT bpr.requester_uid as uid, u.name FROM {buddylist_pending_requests} bpr INNER JOIN {users} u ON bpr.requester_uid = u.uid WHERE bpr.requestee_uid = %d AND bpr.received = 0', $user->uid);
Robert Douglass
committed
$acknowledged_uids = array();
while ($row = db_fetch_object($result)) {
Wolfgang Ziegler
committed
drupal_set_message(t('!linktouser has requested to add you to his/her @buddylist. Please view your !pending_buddy_requests to approve/deny.', array('!linktouser' => theme('username', $row), '!pending_buddy_requests' => l(t('pending buddy requests'), 'buddylist/'. $user->uid .'/buddies/requests')) + buddylist_translation()));
$acknowledged_uids[] = $row->uid;
Robert Douglass
committed
}
if (count($acknowledged_uids)) {
db_query('UPDATE {buddylist_pending_requests} SET received = 1 WHERE requestee_uid = %d AND requester_uid IN (%s)', $user->uid, implode(',', $acknowledged_uids));
}
}
else {
$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 = %d AND received = 1', $thisuser->uid);
while ($rec = db_fetch_object($check_received)) {
if (($rec->received) and ($thisuser->uid == $user->uid)) {
// TODO: This is where integration with Privatemsg could happen. If enabled, send a private message instead.
Wolfgang Ziegler
committed
drupal_set_message(t('!linktouser has added you to his/her @buddylist.', array('!linktouser' => theme('username', $rec)) + buddylist_translation()));
Robert Douglass
committed
db_query('UPDATE {buddylist} SET received = 0 WHERE buddy = %d', $user->uid);
}
}
}
}
/**
* expose add and remove links to theming.
*/
function theme_remove_from_buddylist_link($buddyuser) {
return l(t('Remove %name from my @buddylist', array('%name' => $buddyuser->name) + buddylist_translation()), 'buddy/delete/' . $buddyuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
}
function theme_add_to_buddylist_link($buddyuser) {
return l(t('Add %name to my @buddylist', array('%name' => $buddyuser->name) + buddylist_translation()), 'buddy/add/' . $buddyuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
Wolfgang Ziegler
committed
function theme_buddylist_accept_request_link($requestee_uid, $requester_uid) {
return l(t('Accept'), 'buddylist/' . $requestee_uid . '/buddies/requested/accept/' . $requester_uid, array('title' => 'Accept'), drupal_get_destination(), NULL, FALSE, TRUE);
}
function theme_buddylist_deny_request_link($requestee_uid, $requester_uid) {
return l(t('Deny'), 'buddylist/' . $requestee_uid . '/buddies/requested/deny/' . $requester_uid, array('title' => 'Deny'), drupal_get_destination(), NULL, FALSE, TRUE);
}
function theme_buddylist_sent_requests_cancel_link($requestee_uid, $requester_uid) {
return l(t('Cancel'), 'buddylist/' . $requestee_uid . '/buddies/request/cancel/' . $requester_uid, array('title' => 'Cancel'), drupal_get_destination(), NULL, FALSE, TRUE);
}
/**
* Displays a list of a given user's buddies.
*/
function buddylist_buddylisting_page($uid = NULL, $mode = 'buddies') {
global $user;
if (empty($uid)) {
$uid = $user->uid;
}
// Check that the uid is valid, not the anonymous user, and the user exists
if (!(is_numeric($uid) && ($uid > 0) && $thisuser = user_load(array('uid' => $uid)))) {
drupal_not_found();
exit();
}
$viewing_own_account = ($user->uid == $uid);
if (user_access('maintain buddy list') && $viewing_own_account) {
buddylist_setmsg_received($thisuser);
}
drupal_set_title(t('%username\'s @buddylist', array('%username' => $thisuser->name) + buddylist_translation()));
$buddies_per_page = 20;
//TODO: use the get_buddies function instead
if ($mode == 'buddies') {
$sql = "SELECT DISTINCT(b.buddy), u.access FROM {buddylist} b INNER JOIN {users} u ON b.buddy = u.uid WHERE b.uid = %d ORDER BY u.access DESC";
}
else {
$sql = "SELECT DISTINCT(u.uid) as buddy, u.access FROM {buddylist} b INNER JOIN {users} u ON b.uid = u.uid WHERE b.buddy = %d ORDER BY u.access DESC";
}
$result = pager_query($sql, $buddies_per_page, 0 , NULL, $uid);
$header = array(t('@buddy', buddylist_translation()), t('online'));
$online_interval = time() - variable_get('user_block_seconds_online', 180);
if (db_num_rows($result)) {
while ($account = db_fetch_object($result)) {
$online = $account->access > $online_interval;
$rows[] = array(theme('username', user_load(array('uid' => $account->buddy))), theme('buddylist_online', $online));
}
$output .= theme('table', $header, $rows);
}
else {
Josh Koenig
committed
$output .= variable_get('buddylist_empty_text', t(buddylist_empty_text_default(), buddylist_translation()));
}
$output .= theme('pager', NULL, $buddies_per_page);
return $output;
}
/**
* Returns a list of people who've requested to be added to the given user's buddylist
* and a list of people who this given user has requested to be buddies with.
*/
function theme_buddylist_pending_requests($id) {
$thisuser = user_load(array('uid' => $id));
drupal_set_title(t('@username\'s @buddylist', array('@username' => $thisuser->name) + buddylist_translation()));
Robert Douglass
committed
return theme('box', t('Received requests'), buddylist_pending_requester_list($thisuser)) .
theme('box', t('Sent requests'), buddylist_pending_requested_list($thisuser));
}
Robert Douglass
committed
/**
* Returns a list of people who've requested to be added to the given user's buddylist
Robert Douglass
committed
*/
function buddylist_pending_requester_list(&$account) {
Robert Douglass
committed
global $user;
Robert Douglass
committed
$viewing_own_account = ($user->uid == $account->uid);
$result = db_query('SELECT bpr.requester_uid as uid, u.name FROM {buddylist_pending_requests} bpr INNER JOIN {users} u ON bpr.requester_uid = u.uid WHERE requestee_uid = %d', $account->uid);
Robert Douglass
committed
Robert Douglass
committed
if (!db_num_rows($result)) {
$output = '<p>'. t("!someone currently !does_or_do not have any pending @buddy requests from other users.", array('!someone' => ($viewing_own_account ? t('You') : $account->name), '!does_or_do' => $viewing_own_account ? t('do') : t('does')) + buddylist_translation()) .'</p>';
}
else {
$output = '<p>'. t("The following people have requested to be !someones @buddy.", array('!someones' => ($viewing_own_account ? t('your') : $account->name ."'s ")) + buddylist_translation()) .'</p>';
Robert Douglass
committed
$html_rows = array();
while ($row = db_fetch_object($result)) {
$html_row = array();
Wolfgang Ziegler
committed
$html_row[] = theme('username', $row);
Wolfgang Ziegler
committed
$html_row[] = theme('buddylist_accept_request_link', $account->uid, $row->uid) . " | " . theme('buddylist_deny_request_link', $account->uid, $row->uid);
Robert Douglass
committed
$html_rows[] = $html_row;
}
Robert Douglass
committed
Robert Douglass
committed
$output .= theme('table', NULL, $html_rows);
}
Robert Douglass
committed
Robert Douglass
committed
return $output;
}
/**
* Returns a list of user's who this given user has requested to be buddies with.
*/
function buddylist_pending_requested_list(&$account) {
global $user;
Robert Douglass
committed
$viewing_own_account = ($user->uid == $account->uid);
$result = db_query('SELECT bpr.requestee_uid as uid, u.name FROM {buddylist_pending_requests} bpr INNER JOIN {users} u ON bpr.requestee_uid = u.uid WHERE requester_uid = %d', $account->uid);
Robert Douglass
committed
if (!db_num_rows($result)) {
Robert Douglass
committed
$output = t('!Person !do_or_does not have any pending @buddy requests that !person !have_or_has made.',
Robert Douglass
committed
'!Person' => ($viewing_own_account ? t('You') : $account->name),
'!do_or_does' => ($viewing_own_account ? t('do') : t('does')),
'!have_or_has' => ($viewing_own_account ? t('have') : t('has')),
'!person' => ($viewing_own_account ? t('you') : $account->name) ) + buddylist_translation());
}
else {
$output = t('!person !have_or_has requested to be added to the @buddylist of the following users.', array('!person' => ($viewing_own_account ? t('You') : $account->name), '!have_or_has' => ($viewing_own_account ? t('have') : t('has'))) + buddylist_translation());
Robert Douglass
committed
$html_rows = array();
while ($row = db_fetch_object($result)) {
$html_row = array();
$html_row[] = theme('username', $row);
Wolfgang Ziegler
committed
$html_row[] = theme('buddylist_sent_requests_cancel_link', $account->uid, $row->uid);
$html_rows[] = $html_row;
}
Robert Douglass
committed
$output .= theme('table', NULL, $html_rows);
}
return $output;
}
Wolfgang Ziegler
committed
function buddylist_pending_requested_accept($requestee_uid, $requester_uid) {
$requestee_account = user_load(array('uid' => $requestee_uid));
$requester_account = user_load(array('uid' => $requester_uid));
$output = confirm_form(
buddylist_confirm_form($requester_account, $requestee_account),
t('Accept Request'),
'buddylist/'.$requestee_uid.'/buddies/requests',
t("Are you sure you want to accept the request from !name?", array('!name' => theme('username', $requester_account))),
t('Yes'), t('No'),
'buddylist_request_accept_confirm');
Wolfgang Ziegler
committed
return $output;
}
Wolfgang Ziegler
committed
function buddylist_cancel_request($requestee_uid, $requester_uid){
$requestee_account = user_load(array('uid' => $requestee_uid));
$requester_account = user_load(array('uid' => $requester_uid));
$output = confirm_form(
buddylist_request_cancel_form($requestee_uid, $requester_uid),
t('Cancel Request'),
'buddylist/'.$requestee_uid.'/buddies/requests',
t("Are you sure you want to cancel the request to !name?", array('!name' => theme('username', $requester_account))),
t('Yes'), t('No'),
'buddylist_cancel_request_confirm');
Wolfgang Ziegler
committed
return $output;
Robert Douglass
committed
Wolfgang Ziegler
committed
}
Robert Douglass
committed
Wolfgang Ziegler
committed
function buddylist_cancel_request_submit($form_id, $form_values){
db_query('DELETE FROM {buddylist_pending_requests} WHERE requestee_uid = %d AND requester_uid = %d', $form_values['requester_uid'], $form_values['requestee_uid']);
Robert Douglass
committed
Wolfgang Ziegler
committed
$former_potential_buddy = user_load(array('uid' => $form_values['requester_uid']));
drupal_set_message(t('The request to add !user_name has been cancelled.', array('!user_name' => theme('username', $former_potential_buddy))));
Robert Douglass
committed
Wolfgang Ziegler
committed
return 'buddylist/'. $form_values['requester_uid'] .'/buddies/requests';
}
function buddylist_pending_requested_deny($requestee_uid, $requester_uid) {
$requestee_account = user_load(array('uid' => $requestee_uid));
$requester_account = user_load(array('uid' => $requester_uid));
$output = confirm_form(
buddylist_confirm_form($requester_account, $requestee_account),
t('Accept Request'),
'buddylist/'.$requestee_uid.'/buddies/requests',
t("Are you sure you want to deny the request from !name?", array('!name' => theme('username', $requester_account))),
t('Yes'), t('No'),
'buddylist_request_deny_confirm');
Wolfgang Ziegler
committed
return $output;
}
function buddylist_pending_requested_accept_submit($form_id, $form_values) {
$requestee_account = $form_values['requestee_account']; // most likely global user, unless admin looking
$requester_account = $form_values['requester_account'];
// Delete pending request from {buddylist_penging_requests}
$result = db_query('SELECT * FROM {buddylist_pending_requests} WHERE requestee_uid = %d AND requester_uid = %d', $requestee_account->uid, $requester_account->uid);
if (db_num_rows($result) == 0) {
// The other user cancelled since viewing this page
drupal_set_message(t('!linktouser has cancelled the request to join your @buddylist.', array('!linktouser' => theme('username', $requester_account)) + buddylist_translation()));
Wolfgang Ziegler
committed
return 'buddylist/'. $requestee_account->uid .'/buddies/requests';
}
Wolfgang Ziegler
committed
db_query('DELETE FROM {buddylist_pending_requests} WHERE requestee_uid = %d AND requester_uid = %d', $requestee_account->uid, $requester_account->uid);
Wolfgang Ziegler
committed
// Make sure, for some weird reason, we don't already have these guys marked as buddies of each other in the database
$result = db_query('SELECT * FROM {buddylist} WHERE uid = %d AND buddy = %d', $requestee_account->uid, $requester_account->uid);
Wolfgang Ziegler
committed
$time = time();
Wolfgang Ziegler
committed
if (!db_num_rows($result)) {
db_query('INSERT INTO {buddylist} (uid, buddy, timestamp, received) VALUES (%d, %d, %d, %d)', $requestee_account->uid, $requester_account->uid, $time, 1);
}
Wolfgang Ziegler
committed
$result = db_query('SELECT * FROM {buddylist} WHERE uid = %d AND buddy = %d', $requester_account->uid, $requestee_account->uid);
if (!db_num_rows($result)) {
db_query('INSERT INTO {buddylist} (uid, buddy, timestamp, received) VALUES (%d, %d, %d, %d)', $requester_account->uid, $requestee_account->uid, $time, 1);
}
Wolfgang Ziegler
committed
if (variable_get('buddylist_send_approval', FALSE)) {
buddylist_mail_user('approval', $requester_account, $requestee_account);
}
drupal_set_message(t('Congratulations! !linktouser is now your buddy.', array('!linktouser' => theme('username', $requester_account)) + buddylist_translation()));
Wolfgang Ziegler
committed
return 'buddylist/'. $requestee_account->uid .'/buddies/requests';
}
function buddylist_pending_requested_deny_submit($form_id, $form_values) {
$requestee_account = $form_values['requestee_account']; // most likely global user, unless admin looking
$requester_account = $form_values['requester_account'];
Robert Douglass
committed
Wolfgang Ziegler
committed
// Delete pending request from {buddylist_penging_requests}
db_query('DELETE FROM {buddylist_pending_requests} WHERE requestee_uid = %d AND requester_uid = %d', $requestee_account->uid, $requester_account->uid);
Wolfgang Ziegler
committed
drupal_set_message(t("!user's request to be your buddy has been denied.", array('!user' => theme('username', $requester_account)) + buddylist_translation()));
Wolfgang Ziegler
committed
return 'buddylist/'. $requestee_account->uid .'/buddies/requests';
}
function buddylist_confirm_form($requester_account, $requestee_account) {
$form = array();
Wolfgang Ziegler
committed
$form['requester_account'] = array(
'#type' => 'value',
'#value' => $requester_account
);
Wolfgang Ziegler
committed
$form['requestee_account'] = array(
'#type' => 'value',
'#value' => $requestee_account
);