3 if (module_exists('views')) {
4 include_once(drupal_get_path('module', 'buddylist').
'/buddylist_views.inc');
8 define('BUDDYLIST_ADD_SUBJECT', variable_get('buddylist_add_subject', "[@site] You are @adder_name's newest @buddy"));
9 define('BUDDYLIST_REMOVE_SUBJECT', variable_get('buddylist_remove_subject', "[@site] You have been removed from @adder_name's @buddylist"));
10 define('BUDDYLIST_REQUEST_SUBJECT', variable_get('buddylist_request_subject', "[@site] @adder_name has requested to add you to his/her @buddylist"));
11 define('BUDDYLIST_APPROVAL_SUBJECT', variable_get('buddylist_approval_subject', "[@site] @addee_name has approved your @buddylist add request"));
14 * returns an array of common translation placeholders
16 function buddylist_translation() {
17 $translations = array(
18 '@buddy' => t('buddy'),
19 '@Buddy' => t('Buddy'),
21 '@buddylist' => t('buddylist'),
22 '@Buddylist' => t('Buddylist'),
24 '@buddies' => t('buddies'),
25 '@Buddies' => t('Buddies'),
27 '@buddyof' => t('buddy of'),
28 '@Buddyof' => t('Buddy of'),
30 return variable_get('buddylist_translation', $translations);
35 * Implementation of hook_help
37 function buddylist_help($section) {
39 case
'admin/help#buddylist':
41 <p>@Buddy list enables users to keep a list of @buddies from their social network in their user account.
42 Users can also track what their @buddies are posting to the site.
43 Furthermore, they can track their <i>@buddies'</i> @buddies and thereby explore their social network.</p>
44 <p>If the administrator has enabled the profile module, users can add @buddies via their @buddies' user profiles.
45 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.
46 If a user is already in your @buddylist, the 'delete' action will remove the @buddy. Administrators can also enable the @buddylist block.
47 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>
50 <li>add a @buddy by looking at their profile: <a href=\"@userprofiles\" title=\"View user profiles\">view user profiles</a></li>
51 <li>allow users to view profiles in <a href=\"@setaccesspermissions\" title=\"set access permissions\">administer » access control</a></li>
52 <li>enable the @buddylist block at <a href=\"@blockadministration\" title=\"block administration\">administer » block</a></li>
53 <li>administer the @buddylist block at <a href=\"@buddylistsettings\" title=\"@buddylist settings\">administer » settings » @buddylist</a></li>
56 <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>",
57 array('@userprofiles' => url('profile'),
58 '@setaccesspermissions' => url('admin/user/access'),
59 '@blockadministration' => url('admin/build/block'),
60 '@buddylistsettings' => url('admin/settings/buddylist')
61 ) + buddylist_translation());
68 * Implementation of hook_perm
70 function buddylist_perm() {
71 return array('maintain buddy list', 'view buddy lists');
75 * Implementation of hook_menu
77 function buddylist_menu($may_cache) {
81 $id = is_numeric(arg(1)) ?
arg(1) : $user->uid
;
84 // buddylist settings page
86 'path' => 'admin/settings/buddylist',
87 'title' => t('Buddylist'), // Note that this isn't translated on purpose since it is for the admin
88 'description' => t('Buddylist configuration options for blocks, email, etc.'),
89 'callback' => 'drupal_get_form',
90 'callback arguments' => 'buddylist_admin_settings',
91 'access' => user_access('administer site configuration'),
93 // my buddylist menu item
95 'path' => 'buddylist',
96 'title' => t('My @buddylist', buddylist_translation()),
97 'access' => (user_access('maintain buddy list') && $id),
98 'callback' => 'buddylist_buddylisting_page',
99 'callback arguments' => array($user->uid
, 'buddies'),
103 // 'edit access' only granted to user's own buddy list or to administrative users
105 ($id == $user->uid
&& user_access('maintain buddy list') && $user->uid
)
106 || user_access('administer users'));
108 $approval_required = variable_get('buddylist_require_approval', 0);
111 'path' => 'buddy/add',
112 'title' => t('Add to @buddylist', buddylist_translation()),
113 'access' => $editAccess,
114 'callback' => 'drupal_get_form',
115 'callback arguments' => array('buddylist_addbuddy', arg(2)),
116 'type' => MENU_CALLBACK
,
119 'path' => 'buddy/delete',
120 'title' => t('Delete from @buddylist', buddylist_translation()),
121 'access' => $editAccess,
122 'callback' => 'drupal_get_form',
123 'callback arguments' => array('buddylist_deletebuddy', arg(2)),
124 'type' => MENU_CALLBACK
,
128 $viewAccess = (($id == $user->uid
&& user_access('maintain buddy list')) || user_access('view buddy lists'));
130 // If buddylist approval is required, then upon approval, both parties become buddies of each other.
131 // So, in effect, idea 'buddyof' becomes redundant.
132 if (!$approval_required) {
134 'path' => 'buddylist/'.
$id .
'/buddies',
135 'title' => t('@Buddies', buddylist_translation()),
136 'access' => $viewAccess,
137 'callback' => 'buddylist_buddylisting_page',
138 'type' => MENU_DEFAULT_LOCAL_TASK
,
140 'callback arguments' => array($id)
143 'path' => 'buddylist/'.
$id .
'/buddyof',
144 'title' => t('@Buddyof', buddylist_translation()),
145 'access' => $viewAccess,
146 'callback' => 'buddylist_buddylisting_page',
147 'type' => MENU_LOCAL_TASK
,
149 'callback arguments' => array($id, 'buddyof')
155 'path' => 'buddylist/'.
$id .
'/buddies/list',
156 'title' => t('@Buddylist', buddylist_translation()),
157 'access' => $viewAccess,
158 'callback' => 'buddylist_buddylisting_page',
159 'type' => MENU_DEFAULT_LOCAL_TASK
,
161 'callback arguments' => array($id),
164 'path' => 'buddylist/'.
$id .
'/buddies/recent',
165 'title' => t('Recent posts'),
166 'access' => ($viewAccess && module_exists('tracker')),
167 'callback' => 'buddylist_buddiesrecent_page',
168 'type' => MENU_LOCAL_TASK
,
170 'callback arguments' => array($id),
172 if (variable_get('buddylist_buddygroups', FALSE
)) {
174 'path' => 'buddylist/'.
$id .
'/buddies/groups/view',
175 'title' => t('View groups'),
176 'access' => $viewAccess,
177 'callback' => 'buddylist_buddiesgroups_page',
178 'type' => MENU_LOCAL_TASK
,
180 'callback arguments' => array($id),
183 'path' => 'buddylist/'.
$id .
'/buddies/groups/edit',
184 'title' => t('Edit groups'),
185 'access' => $editAccess,
186 'callback' => 'buddylist_buddiesgroups_edit',
187 'type' => MENU_LOCAL_TASK
,
189 'callback arguments' => array($id),
194 if ($approval_required && $editAccess) {
196 'path' => 'buddylist/'.
$id .
'/buddies/requests',
197 'title' => t('Pending requests'),
198 'access' => $editAccess,
199 'callback' => 'theme',
200 'type' => MENU_LOCAL_TASK
,
202 'callback arguments' => array('buddylist_pending_requests', $id)
206 'path' => 'buddylist/'.
$id .
'/buddies/requested/accept',
207 'title' => t('Accept Request'),
208 'access' => $editAccess,
209 'type' => MENU_CALLBACK
,
210 'callback' => 'drupal_get_form',
211 'callback arguments' => array('buddylist_pending_requested_accept',$id),
215 'path' => 'buddylist/'.
$id .
'/buddies/requested/deny',
216 'title' => t('Deny Request'),
217 'access' => $editAccess,
218 'type' => MENU_CALLBACK
,
219 'callback' => 'drupal_get_form',
220 'callback arguments' => array('buddylist_pending_requested_deny',$id),
224 'path' => 'buddylist/'.
$id .
'/buddies/request/cancel',
225 'title' => t('Cancel Request'),
226 'access' => $editAccess,
227 'type' => MENU_CALLBACK
,
228 'callback' => 'drupal_get_form',
229 'callback arguments' => array('buddylist_cancel_request',$id),
235 if ($id != $user->uid
) {
236 // This callback can interfere with the 'my buddylist' menu item,
237 // so we only load it if the user is viewing another user's list.
239 'path' => 'buddylist/'.
$id,
240 'title' => t('@Buddylist', buddylist_translation()),
241 'access' => (($viewAccess || $editAccess) && $id),
242 'callback' => 'buddylist_buddylisting_page',
243 'type' => MENU_CALLBACK
,
244 'callback arguments' => array($id),
248 'path' => 'buddylist/'.
$id .
'/buddies/recent/feed',
249 'title' => t('Xml feed'),
250 'access' => $viewAccess,
251 'callback' => 'buddylist_buddyfeed',
252 'type' => MENU_CALLBACK
,
253 'callback arguments' => array($id),
261 * Buddylist administration settings page
263 function buddylist_admin_settings() {
264 $form['general'] = array(
265 '#type' => 'fieldset',
266 '#title' => t('General settings'),
269 $form['general']['buddylist_require_approval'] = array(
271 '#title' => t('Require approval'),
272 '#default_value' => variable_get('buddylist_require_approval', 0),
273 '#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()),
274 '#options' => array(1 => t('Yes'), 0 => t('No'))
276 $form['general']['buddylist_buddygroups'] = array(
277 '#type' => 'checkbox',
278 '#title' => t('Enable @buddy groups', buddylist_translation()),
279 '#description' => t('Enables @buddylist @buddy groups. Users will be able to create @buddy groups to manage their @buddies.', buddylist_translation()),
280 '#default_value' => variable_get('buddylist_buddygroups', FALSE
),
283 // User profile page settings
284 $form['profile_settings'] = array(
285 '#type' => 'fieldset',
286 '#title' => t('Profile page options'),
288 $form['profile_settings']['buddylist_prof_buddies'] = array(
290 '#title' => t('Number of @buddies and users who\'ve added me', buddylist_translation()),
291 '#default_value' => variable_get('buddylist_prof_buddies', 5),
292 '#options' => drupal_map_assoc(range(0, 10)),
293 '#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()),
296 // TODO: move these to block settings
297 $form['block_settings'] = array(
298 '#type' => 'fieldset',
299 '#title' => t('@Buddylist block options', buddylist_translation()),
301 $form['block_settings']['buddylist_blocklisting_size'] = array(
303 '#title' => t("Number of @buddies to list in the user's @buddy block", buddylist_translation()),
304 '#default_value' => variable_get('buddylist_blocklisting_size', 5),
305 '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)),
306 '#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()),
308 $form['block_settings']['buddylist_posts_block'] = array(
310 '#title' => t("Number of posts to list in the @buddies' recent posts block", buddylist_translation()),
311 '#default_value' => variable_get('buddylist_posts_block', 7),
312 '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)),
313 '#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()),
315 $form['block_settings']['buddylist_block_title'] = array(
316 '#type' => 'textfield',
317 '#title' => t('"My @buddies\' recent posts" block title', buddylist_translation()),
318 '#default_value' => variable_get('buddylist_block_title', t("My @buddies' recent posts", buddylist_translation())),
321 '#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()),
323 $form['block_settings']['buddylist_status_block_title'] = array(
324 '#type' => 'textfield',
325 '#title' => t('"My @buddylist status" block title', buddylist_translation()),
326 '#default_value' => variable_get('buddylist_status_block_title', t("My @buddylist status", buddylist_translation())),
329 '#description' => t('This will be the title for the @buddylist status block. If none is specified, "My @buddylist status" will be used.', buddylist_translation()),
331 $form['block_settings']['buddylist_list_block_title'] = array(
332 '#type' => 'textfield',
333 '#title' => t('"My @buddies list" block title', buddylist_translation()),
335 '#default_value' => variable_get('buddylist_list_block_title', t('My @buddylist', buddylist_translation())),
338 '#description' => t('This will be the title for the "My @buddylist" block. If none is specified, "My @buddylist" will be used.', buddylist_translation()),
340 $form['block_settings']['buddylist_block_if_no_buddies'] = array(
341 '#type' => 'checkbox',
342 '#title' => t('Show "My @buddies list" block even if @buddylist is empty', buddylist_translation()),
344 '#default_value' => variable_get('buddylist_block_if_no_buddies', FALSE
),
345 '#description' => t('If a user has no @buddies, the @buddy block can show a message.', buddylist_translation()),
347 $form['block_settings']['buddylist_empty_text'] = array(
348 '#type' => 'textarea',
349 '#title' => t('Show this text in "My @buddies list" if @buddylist is empty', buddylist_translation()),
351 '#default_value' => variable_get('buddylist_empty_text', t(buddylist_empty_text_default(), buddylist_translation())),
352 '#description' => t('If a user has no @buddies and the above checkbox is checked, this message is shown instead of a list.', buddylist_translation()),
355 $form['mail'] = array(
356 '#type' => 'fieldset',
357 '#title' => t('email'),
361 $macros = implode(', ', array_keys(buddylist_mail_replacements($user, $user)));
362 $approval_macros = implode(', ', array_keys(buddylist_approval_mail_replacements($user, $user)));
364 $form['mail']['buddylist_user_mail'] = array(
365 '#type' => 'checkbox',
366 '#title' => t('Allow users to turn off buddylist messages'),
367 '#default_value' => variable_get('buddylist_user_mail', FALSE
),
368 '#description' => t('If you check this, users will have a new setting on their account edit page.'),
371 $form['mail']['buddylist_send_add'] = array(
372 '#type' => 'checkbox',
373 '#title' => t('Send add messages'),
374 '#default_value' => variable_get('buddylist_send_add', FALSE
),
377 $form['mail']['buddylist_add_subject'] = array(
378 '#type' => 'textfield',
379 '#title' => t('Added @buddy email subject', buddylist_translation()),
380 '#default_value' => BUDDYLIST_ADD_SUBJECT
,
383 $form['mail']['buddylist_add_message'] = array(
384 '#type' => 'textarea',
385 '#title' => t('Added @buddy email message', buddylist_translation()),
386 '#default_value' => variable_get('buddylist_add_message', buddylist_mail_add_default()),
387 '#description' => t('Replacement strings are: %macros', array('%macros' => $macros)),
390 $form['mail']['buddylist_send_remove'] = array(
391 '#type' => 'checkbox',
392 '#title' => t('Send remove messages'),
393 '#default_value' => variable_get('buddylist_send_remove', FALSE
),
396 $form['mail']['buddylist_remove_subject'] = array(
397 '#type' => 'textfield',
398 '#title' => t('Removed @buddy email subject', buddylist_translation()),
399 '#default_value' => BUDDYLIST_REMOVE_SUBJECT
,
402 $form['mail']['buddylist_remove_message'] = array(
403 '#type' => 'textarea',
404 '#title' => t('Removed @buddy email message', buddylist_translation()),
405 '#default_value' => variable_get('buddylist_remove_message', buddylist_mail_remove_default()),
406 '#description' => t('Replacement strings are: %macros', array('%macros' => $macros)),
409 $form['mail']['buddylist_send_request'] = array(
410 '#type' => 'checkbox',
411 '#title' => t('Send request messages.'),
412 '#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.'),
413 '#default_value' => variable_get('buddylist_send_request', FALSE
)
416 $form['mail']['buddylist_request_subject'] = array(
417 '#type' => 'textfield',
418 '#title' => t('@buddy request email subject', buddylist_translation()),
419 '#default_value' => BUDDYLIST_REQUEST_SUBJECT
,
422 $form['mail']['buddylist_request_message'] = array(
423 '#type' => 'textarea',
424 '#title' => t('@buddy request email message', buddylist_translation()),
425 '#default_value' => variable_get('buddylist_request_message', buddylist_mail_request_default()),
426 '#description' => t('Replacement strings are: %macros', array('%macros' => $macros)),
429 $form['mail']['buddylist_send_approval'] = array(
430 '#type' => 'checkbox',
431 '#title' => t('Send approval messages'),
432 '#default_value' => variable_get('buddylist_send_approval', FALSE
),
433 '#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())
436 $form['mail']['buddylist_approval_subject'] = array(
437 '#type' => 'textfield',
438 '#title' => t('@buddy request email subject', buddylist_translation()),
439 '#default_value' => BUDDYLIST_APPROVAL_SUBJECT
,
442 $form['mail']['buddylist_approval_message'] = array(
443 '#type' => 'textarea',
444 '#title' => t('@buddy approval email message', buddylist_translation()),
445 '#default_value' => variable_get('buddylist_approval_message', buddylist_mail_approval_default()),
446 '#description' => t('Replacement strings are: %macros', array('%macros' => $approval_macros)),
449 return system_settings_form($form);
454 * Implementation of hook_user
456 function buddylist_user($type, &$edit, &$thisuser, $category = NULL
) {
460 // show any buddylist notifications upon login and upon viewing own profile
461 if (user_access('maintain buddy list') && (($type == 'login') || ($type == 'view') && ($thisuser->uid
== $user->uid
))) {
462 buddylist_setmsg_received($thisuser);
464 if ($type == 'view') {
465 if ($list = buddylist_get_buddylist($thisuser)) {
466 $output[] = array('title' => t('@Buddies', buddylist_translation()), 'value' => $list, 'class' => 'buddylist');
468 if ($list = buddylist_get_buddylist($thisuser, TRUE
)) {
469 $output[] = array('title' => t('@buddy of', buddylist_translation()), 'value' => $list, 'class' => 'buddyoflist');
471 if ($actions = buddylist_get_buddy_actions($user, $thisuser)) {
472 $output[] = array('title' => t('@Buddy actions', buddylist_translation()), 'value' => theme('item_list', $actions), 'class' => 'buddylist_actions');
474 if(count($output) > 0) {
475 return array(t('@Buddy List', buddylist_translation()) => $output);
478 else if ($type == 'delete') {
479 db_query("DELETE FROM {buddylist} WHERE uid = %d OR buddy = %d", $thisuser->uid
, $thisuser->uid
);
480 db_query("DELETE FROM {buddylist_buddy_group} WHERE uid = %d OR buddy = %d", $thisuser->uid
, $thisuser->uid
);
481 db_query("DELETE FROM {buddylist_groups} WHERE uid = %d", $thisuser->uid
);
482 db_query("DELETE FROM {buddylist_pending_requests} WHERE requester_uid = %d OR requestee_uid = %d", $thisuser->uid
, $thisuser->uid
);
484 else if ($type == 'load') {
485 $thisuser->buddies
= buddylist_get_buddies($thisuser->uid
);
487 else if ($type == 'form' && $category == 'account' && variable_get('buddylist_user_mail', FALSE
) && user_access('maintain buddy list', $thisuser)) {
488 // when user tries to edit his own data
489 $form['buddylist_settings'] = array(
490 '#type' => 'fieldset',
491 '#title' => t('Buddylist settings'),
493 $form['buddylist_settings']['buddylist_mail'] = array(
494 '#type' => 'checkbox',
495 '#title' => t('Receive @buddylist notification mails', buddylist_translation()),
496 '#default_value' => isset($edit['buddylist_mail']) ?
$edit['buddylist_mail'] : 1,
497 '#description' => t('If you check this, you will be notified about important actions regarding your @Buddylist.', buddylist_translation()));
503 * Return a formatted list of buddies for the given user
504 * @param $buddy_of If set to TRUE, a formatted list of users is returned, for whom this user is a buddy.
506 function buddylist_get_buddylist($user, $buddy_of = FALSE
) {
508 if (user_access('view buddy lists') && !$buddy_of) {
510 if ($buddies = buddylist_get_buddies($user->uid
)) {
511 foreach(array_keys($buddies) as
$buddy) {
512 $account = user_load(array('uid' => $buddy));
513 $listbuddies[] = $account;
515 if ($i > variable_get('buddylist_prof_buddies', 5)) {
519 return theme('user_list', $listbuddies);
522 else if (user_access('view buddy lists') && !variable_get('buddylist_require_approval', 0)) {
523 // This portion of code is used to see if this $thisuser is a buddy of others and, if s/he is, returns a list
524 // of people s/he is a buddy of.
525 // Note the distinction between having a buddy and being someone else's buddy (i.e., 'buddyof')
526 // Of course, this distinction doesn't exist if approval is required to add a buddy (in which case, buddy relationships are symmetric)
527 $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';
528 $result = db_query_range($sql, $user->uid
, 0, variable_get('buddylist_prof_buddies', 5));
529 while ($row = db_fetch_object($result)) {
530 $listbuddyof[$row->uid
] = $row;
533 return theme('user_list', $listbuddyof);
540 * Returns an array of posible actions (html) for the viewing user,
541 * e.g. a link to make the viewed user a buddy
543 function buddylist_get_buddy_actions(&$viewing_user, &$viewed_user) {
546 if (!user_access('maintain buddy list') || $viewing_user->uid
== $viewed_user->uid
) {
550 if (variable_get('buddylist_require_approval', FALSE
) && in_array($viewed_user->uid
, array_keys(buddylist_get_requestees($viewing_user->uid
)))) {
551 $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());
553 else if (in_array($viewed_user->uid
, array_keys(buddylist_get_buddies($viewing_user->uid
)))) {
554 $actions[] = theme('remove_from_buddylist_link', $viewed_user);
556 else if (in_array($viewing_user->uid
, array_keys(buddylist_get_requestees($viewed_user->uid
)))) {
557 $actions[] = t('This user has requested to add you to your @buddylist.', buddylist_translation()) .
558 drupal_get_form('buddylist_approval_form', $viewing_user->uid
, $viewed_user->uid
);
561 $actions[] = theme('add_to_buddylist_link', $viewed_user);
569 * Implementation for hook_block
571 function buddylist_block($op = 'list', $delta = 0) {
575 $block[0]['info'] = variable_get('buddylist_list_block_title', t('My @buddylist', buddylist_translation()));
576 $block[1]['info'] = variable_get('buddylist_block_title', t('My @buddies\' recent posts', buddylist_translation()));
577 if (variable_get('buddylist_require_approval', 0)) {
578 $block[2]['info'] = variable_get('buddylist_status_block_title', t('My @buddy status', buddylist_translation()));
582 else if ($op == 'view' && user_access('access content') && user_access('maintain buddy list') && $user->uid
> 0) {
584 case
0 : // Shows buddylist block
585 if ($buddies = buddylist_get_buddies()) {
586 // we have buddies defined and generate the list
588 foreach (array_keys($buddies) as
$buddy) {
589 $users[] = user_load(array('uid' => $buddy));
591 if ($i == variable_get('buddylist_blocklisting_size', 5)) {
595 $block['content'] = theme('user_list', $users);
597 // buddylist is empty
598 if(variable_get('buddylist_block_if_no_buddies', FALSE
)) {
599 // Show a message that we have no buddies yet
600 $block['content'] = variable_get('buddylist_empty_text', t(buddylist_empty_text_default(), buddylist_translation()));
602 // If no buddies defined and no message available, end 'case' without returning block.
606 // this is the same output whether buddylist or not
607 $block['subject'] = variable_get('buddylist_list_block_title', t('My @buddylist', buddylist_translation()));
609 // check if a "more" link should generated by seeing if there are more buddies than the specified $upperlimit
610 if (count($buddies) > variable_get('buddylist_blocklisting_size', 5)) {
611 $block['content'] .
= '<div class="more-link">' .
l(t('more'), 'buddylist', array('title' => t('View more.'))) .
'</div>';
616 case
1: // Shows my buddies recent posts block
617 $buddies = buddylist_get_buddies();
618 $keys = array_keys($buddies);
619 if (count($keys) > 0) {
620 $str_buddies = implode(',', $keys);
621 $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));
623 if (db_num_rows($result)) {
624 $block['subject'] = variable_get('buddylist_block_title', t('My @buddies\' recent posts', buddylist_translation()));
625 $block['content'] = node_title_list($result);
627 // check if a "more" link should generated by seeing if there are more buddies than the specified $upperlimit
628 $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
);
629 $countresult = db_fetch_object($result);
631 if (module_exists('tracker') && variable_get('buddylist_posts_block', 7) < $countresult->node_count
) {
632 $block['content'] .
= '<div class="more-link">'.
l(t('more'), 'buddylist/'.
$user->uid .
'/buddies/recent', array('title' => t('View more.'))) .
'</div>';
639 case
2: // Buddylist status
640 $block['subject'] = variable_get('buddylist_status_block_title', t('My @buddy status', buddylist_translation()));
642 $count = db_result(db_query("SELECT COUNT(uid) FROM {buddylist} WHERE uid = %d", $user->uid
));
643 $sent = db_result(db_query("SELECT COUNT(requester_uid) FROM {buddylist_pending_requests} WHERE requester_uid = %d", $user->uid
));
644 $received = db_result(db_query("SELECT COUNT(requestee_uid) FROM {buddylist_pending_requests} WHERE requestee_uid = %d", $user->uid
));
646 $block['content'] = theme('buddylist_status_block', $count, $received, $sent);
654 * Public API for retrieving buddies. Feel free to use this from other
656 * $key can be 'uid' or 'label'.
658 function buddylist_get_buddies($uid = NULL
, $key = 'uid') {
665 if (!isset($buddies[$key][$uid])) {
666 $buddies[$key][$uid] = array();
667 $sql = 'SELECT b.buddy, u.name, u.mail, u.uid FROM {buddylist} b
668 INNER JOIN {users} u ON b.buddy = u.uid
670 $result = db_query($sql, $uid);
671 while ($row = db_fetch_object($result)) {
672 $buddies[$key][$uid][$row->buddy
]['uid'] = $row->uid
;
673 $buddies[$key][$uid][$row->buddy
]['name'] = $row->name
;
674 $buddies[$key][$uid][$row->buddy
]['mail'] = $row->mail;
675 $buddies[$key][$uid][$row->buddy
]['groups'] = buddylist_get_buddy_groups($uid, $row->buddy
);
676 $buddies[$key][$uid][$row->buddy
]['online'] = 0;
677 $selectlist .
= $row->buddy.
",";
679 // Add the online flag
680 if (db_num_rows($result)) {
681 $sql = 'SELECT uid FROM {sessions} WHERE uid IN (%s) AND timestamp > %d';
682 $result = db_query($sql, substr($selectlist,0,-1), time()-1800);
683 while ($row = db_fetch_object($result)) {
684 $buddies[$key][$uid][$row->uid
]['online'] = 1;
689 return $buddies[$key][$uid];
693 * Returns an array of uid => name of people that user with param $uid has made a buddy request to
695 function buddylist_get_requestees($uid) {
698 $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);
700 while ($row = db_fetch_object($result)) {
701 $buddies[$row->requestee_uid
] = $row->name
;
707 function buddylist_setmsg_received($thisuser) {
710 if (variable_get('buddylist_require_approval', 0)) {
711 // Go through and find new buddylist add-requests, (i.e., the ones in {buddylist_pending_requests} w/ received column == 0
712 $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
);
713 $acknowledged_uids = array();
714 while ($row = db_fetch_object($result)) {
715 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()));
716 $acknowledged_uids[] = $row->uid
;
718 if (count($acknowledged_uids)) {
719 db_query('UPDATE {buddylist_pending_requests} SET received = 1 WHERE requestee_uid = %d AND requester_uid IN (%s)', $user->uid
, implode(',', $acknowledged_uids));
723 $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
);
724 while ($rec = db_fetch_object($check_received)) {
725 if (($rec->received
) and ($thisuser->uid
== $user->uid
)) {
726 // TODO: This is where integration with Privatemsg could happen. If enabled, send a private message instead.
727 drupal_set_message(t('!linktouser has added you to his/her @buddylist.', array('!linktouser' => theme('username', $rec)) + buddylist_translation()));
728 db_query('UPDATE {buddylist} SET received = 0 WHERE buddy = %d', $user->uid
);
735 * expose add and remove links to theming.
737 function theme_remove_from_buddylist_link($buddyuser) {
738 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
);
741 function theme_add_to_buddylist_link($buddyuser) {
742 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
);
745 function theme_buddylist_accept_request_link($requestee_uid, $requester_uid) {
746 return l(t('Accept'), 'buddylist/' .
$requestee_uid .
'/buddies/requested/accept/' .
$requester_uid, array('title' => 'Accept'), drupal_get_destination(), NULL
, FALSE
, TRUE
);
749 function theme_buddylist_deny_request_link($requestee_uid, $requester_uid) {
750 return l(t('Deny'), 'buddylist/' .
$requestee_uid .
'/buddies/requested/deny/' .
$requester_uid, array('title' => 'Deny'), drupal_get_destination(), NULL
, FALSE
, TRUE
);
753 function theme_buddylist_sent_requests_cancel_link($requestee_uid, $requester_uid) {
754 return l(t('Cancel'), 'buddylist/' .
$requestee_uid .
'/buddies/request/cancel/' .
$requester_uid, array('title' => 'Cancel'), drupal_get_destination(), NULL
, FALSE
, TRUE
);
758 * Displays a list of a given user's buddies.
760 function buddylist_buddylisting_page($uid = NULL
, $mode = 'buddies') {
766 // Check that the uid is valid, not the anonymous user, and the user exists
767 if (!(is_numeric($uid) && ($uid > 0) && $thisuser = user_load(array('uid' => $uid)))) {
772 $viewing_own_account = ($user->uid
== $uid);
774 if (user_access('maintain buddy list') && $viewing_own_account) {
775 buddylist_setmsg_received($thisuser);
778 drupal_set_title(t('%username\'s @buddylist', array('%username' => $thisuser->name
) + buddylist_translation()));
780 $buddies_per_page = 20;
782 //TODO: use the get_buddies function instead
783 if ($mode == 'buddies') {
784 $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";
787 $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";
789 $result = pager_query($sql, $buddies_per_page, 0 , NULL
, $uid);
791 $header = array(t('@buddy', buddylist_translation()), t('online'));
792 $online_interval = time() - variable_get('user_block_seconds_online', 180);
794 if (db_num_rows($result)) {
795 while ($account = db_fetch_object($result)) {
796 $online = $account->access
> $online_interval;
797 $rows[] = array(theme('username', user_load(array('uid' => $account->buddy
))), theme('buddylist_online', $online));
799 $output .
= theme('table', $header, $rows);
802 $output .
= variable_get('buddylist_empty_text', t(buddylist_empty_text_default(), buddylist_translation()));
805 $output .
= theme('pager', NULL
, $buddies_per_page);
811 * Returns a list of people who've requested to be added to the given user's buddylist
812 * and a list of people who this given user has requested to be buddies with.
814 function theme_buddylist_pending_requests($id) {
816 $thisuser = user_load(array('uid' => $id));
817 drupal_set_title(t('@username\'s @buddylist', array('@username' => $thisuser->name
) + buddylist_translation()));
819 return theme('box', t('Received requests'), buddylist_pending_requester_list($thisuser)) .
820 theme('box', t('Sent requests'), buddylist_pending_requested_list($thisuser));
824 * Returns a list of people who've requested to be added to the given user's buddylist
826 function buddylist_pending_requester_list(&$account) {
829 $viewing_own_account = ($user->uid
== $account->uid
);
830 $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
);
832 if (!db_num_rows($result)) {
833 $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>';
836 $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>';
838 $html_rows = array();
839 while ($row = db_fetch_object($result)) {
841 $html_row[] = theme('username', $row);
842 $html_row[] = theme('buddylist_accept_request_link', $account->uid
, $row->uid
) .
" | " .
theme('buddylist_deny_request_link', $account->uid
, $row->uid
);
843 $html_rows[] = $html_row;
846 $output .
= theme('table', NULL
, $html_rows);
853 * Returns a list of user's who this given user has requested to be buddies with.
855 function buddylist_pending_requested_list(&$account) {
858 $viewing_own_account = ($user->uid
== $account->uid
);
859 $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
);
861 if (!db_num_rows($result)) {
862 $output = t('!Person !do_or_does not have any pending @buddy requests that !person !have_or_has made.',
864 '!Person' => ($viewing_own_account ?
t('You') : $account->name
),
865 '!do_or_does' => ($viewing_own_account ?
t('do') : t('does')),
866 '!have_or_has' => ($viewing_own_account ?
t('have') : t('has')),
867 '!person' => ($viewing_own_account ?
t('you') : $account->name
) ) + buddylist_translation());
870 $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());
872 $html_rows = array();
873 while ($row = db_fetch_object($result)) {
875 $html_row[] = theme('username', $row);
876 $html_row[] = theme('buddylist_sent_requests_cancel_link', $account->uid
, $row->uid
);
877 $html_rows[] = $html_row;
880 $output .
= theme('table', NULL
, $html_rows);
886 function buddylist_pending_requested_accept($requestee_uid, $requester_uid) {
887 $requestee_account = user_load(array('uid' => $requestee_uid));
888 $requester_account = user_load(array('uid' => $requester_uid));
889 $output = confirm_form(
890 buddylist_confirm_form($requester_account, $requestee_account),
892 'buddylist/'.
$requestee_uid.
'/buddies/requests',
893 t("Are you sure you want to accept the request from !name?", array('!name' => theme('username', $requester_account))),
895 'buddylist_request_accept_confirm');
900 function buddylist_cancel_request($requestee_uid, $requester_uid){
901 $requestee_account = user_load(array('uid' => $requestee_uid));
902 $requester_account = user_load(array('uid' => $requester_uid));
903 $output = confirm_form(
904 buddylist_request_cancel_form($requestee_uid, $requester_uid),
906 'buddylist/'.
$requestee_uid.
'/buddies/requests',
907 t("Are you sure you want to cancel the request to !name?", array('!name' => theme('username', $requester_account))),
909 'buddylist_cancel_request_confirm');
915 function buddylist_cancel_request_submit($form_id, $form_values){
916 db_query('DELETE FROM {buddylist_pending_requests} WHERE requestee_uid = %d AND requester_uid = %d', $form_values['requester_uid'], $form_values['requestee_uid']);
918 $former_potential_buddy = user_load(array('uid' => $form_values['requester_uid']));
919 drupal_set_message(t('The request to add !user_name has been cancelled.', array('!user_name' => theme('username', $former_potential_buddy))));
921 return 'buddylist/'.
$form_values['requester_uid'] .
'/buddies/requests';
924 function buddylist_pending_requested_deny($requestee_uid, $requester_uid) {
925 $requestee_account = user_load(array('uid' => $requestee_uid));
926 $requester_account = user_load(array('uid' => $requester_uid));
927 $output = confirm_form(
928 buddylist_confirm_form($requester_account, $requestee_account),
930 'buddylist/'.
$requestee_uid.
'/buddies/requests',
931 t("Are you sure you want to deny the request from !name?", array('!name' => theme('username', $requester_account))),
933 'buddylist_request_deny_confirm');
938 function buddylist_pending_requested_accept_submit($form_id, $form_values) {
939 $requestee_account = $form_values['requestee_account']; // most likely global user, unless admin looking
940 $requester_account = $form_values['requester_account'];
942 // Delete pending request from {buddylist_penging_requests}
943 $result = db_query('SELECT * FROM {buddylist_pending_requests} WHERE requestee_uid = %d AND requester_uid = %d', $requestee_account->uid
, $requester_account->uid
);
945 if (db_num_rows($result) == 0) {
946 // The other user cancelled since viewing this page
947 drupal_set_message(t('!linktouser has cancelled the request to join your @buddylist.', array('!linktouser' => theme('username', $requester_account)) + buddylist_translation()));
948 return 'buddylist/'.
$requestee_account->uid .
'/buddies/requests';
951 db_query('DELETE FROM {buddylist_pending_requests} WHERE requestee_uid = %d AND requester_uid = %d', $requestee_account->uid
, $requester_account->uid
);
953 // Make sure, for some weird reason, we don't already have these guys marked as buddies of each other in the database
954 $result = db_query('SELECT * FROM {buddylist} WHERE uid = %d AND buddy = %d', $requestee_account->uid
, $requester_account->uid
);
958 if (!db_num_rows($result)) {
959 db_query('INSERT INTO {buddylist} (uid, buddy, timestamp, received) VALUES (%d, %d, %d, %d)', $requestee_account->uid
, $requester_account->uid
, $time, 1);
962 $result = db_query('SELECT * FROM {buddylist} WHERE uid = %d AND buddy = %d', $requester_account->uid
, $requestee_account->uid
);
963 if (!db_num_rows($result)) {
964 db_query('INSERT INTO {buddylist} (uid, buddy, timestamp, received) VALUES (%d, %d, %d, %d)', $requester_account->uid
, $requestee_account->uid
, $time, 1);
967 if (variable_get('buddylist_send_approval', FALSE
)) {
968 buddylist_mail_user('approval', $requester_account, $requestee_account);
971 drupal_set_message(t('Congratulations! !linktouser is now your buddy.', array('!linktouser' => theme('username', $requester_account)) + buddylist_translation()));
973 return 'buddylist/'.
$requestee_account->uid .
'/buddies/requests';
976 function buddylist_pending_requested_deny_submit($form_id, $form_values) {
977 $requestee_account = $form_values['requestee_account']; // most likely global user, unless admin looking
978 $requester_account = $form_values['requester_account'];
980 // Delete pending request from {buddylist_penging_requests}
981 db_query('DELETE FROM {buddylist_pending_requests} WHERE requestee_uid = %d AND requester_uid = %d', $requestee_account->uid
, $requester_account->uid
);
983 drupal_set_message(t("!user's request to be your buddy has been denied.", array('!user' => theme('username', $requester_account)) + buddylist_translation()));
985 return 'buddylist/'.
$requestee_account->uid .
'/buddies/requests';
988 function buddylist_confirm_form($requester_account, $requestee_account) {
991 $form['requester_account'] = array(
993 '#value' => $requester_account
996 $form['requestee_account'] = array(
998 '#value' => $requestee_account
1004 function buddylist_confirm_form_submit($form_id, $form_values) {
1005 $requestee_account = user_load(array('uid' => $form_values['requestee_uid'])); // most likely global user, unless admin looking
1008 // Delete pending request from {buddylist_penging_requests}
1009 db_query('DELETE FROM {buddylist_pending_requests} WHERE requestee_uid = %d AND requester_uid = %d', $form_values['requestee_uid'], $form_values['requester_uid']);
1011 $requester_account = user_load(array('uid' => $form_values['requester_uid']));
1013 if ($form_values['op'] == t('Approve')) {
1014 // Make sure, for some weird reason, we don't already have these guys marked as buddies of each other in the database
1015 $result = db_query('SELECT * FROM {buddylist} WHERE uid = %d AND buddy = %d', $form_values['requestee_uid'], $form_values['reqeuster_uid']);
1019 if (!db_num_rows($result)) {
1020 db_query('INSERT INTO {buddylist} (uid, buddy, timestamp, received) VALUES (%d, %d, %d, %d)', $form_values['requestee_uid'], $form_values['requester_uid'], $time, 1);
1023 $result = db_query('SELECT * FROM {buddylist} WHERE uid = %d AND buddy = %d', $form_values['requester_uid'], $form_values['requestee_uid']);
1024 if (!db_num_rows($result)) {
1025 db_query('INSERT INTO {buddylist} (uid, buddy, timestamp, received) VALUES (%d, %d, %d, %d)', $form_values['requester_uid'], $form_values['requestee_uid'], $time, 1);
1028 if (variable_get('buddylist_send_approval', FALSE
)) {
1029 buddylist_mail_user('approval', $requester_account, $requestee_account);
1032 drupal_set_message(t('Congratulations! !linktouser is now your @buddy.', array('!linktouser' => theme('username', $requester_account)) + buddylist_translation()));
1035 drupal_set_message(t("!user's request to be your @buddy has been denied.", array('!user' => theme('username', $requester_account)) + buddylist_translation()));
1039 function buddylist_approval_form($requestee_uid, $requester_uid) {
1042 $form['requestee_uid'] = array(
1043 '#type' => 'hidden',
1044 '#value' => $requestee_uid,
1047 $form['requester_uid'] = array(
1048 '#type' => 'hidden',
1049 '#value' => $requester_uid
1052 $form['approve'] = array(
1053 '#type' => 'submit',
1054 '#value' => t('Approve')
1057 $form['deny'] = array(
1058 '#type' => 'submit',
1059 '#value' => t('Deny'),
1065 function buddylist_request_cancel_form($requestee_uid, $requester_uid) {
1066 $form['requestee_uid'] = array(
1067 '#type' => 'hidden',
1068 '#value' => $requestee_uid,
1071 $form['requester_uid'] = array(
1072 '#type' => 'hidden',
1073 '#value' => $requester_uid
1079 function buddylist_request_cancel_form_submit($form_id, $form_values) {
1080 db_query('DELETE FROM {buddylist_pending_requests} WHERE requester_uid = %d AND requestee_uid = %d', $form_values['requester_uid'], $form_values['requestee_uid']);
1082 $former_potential_buddy = user_load(array('uid' => $form_values['requestee_uid']));
1083 drupal_set_message(t('The request to add !user_name has been cancelled.', array('!user_name' => theme('username', $former_potential_buddy))));
1085 return 'buddylist/'.
$form_values['requester_uid'] .
'/buddies/requests';
1088 function buddylist_form_buddiesrecent_page($buddies) {
1089 foreach ($buddies as
$user_id => $buddy) {
1090 $form[] = array('#type' => 'fieldset',
1091 '#title' => $buddy['name'],
1092 '#collapsible' => 'true',
1093 '#value' => tracker_page($user_id),
1099 function buddylist_buddiesrecent_page($uid) {
1102 $thisuser = user_load(array('uid' => $uid));
1103 drupal_set_title(t('%username\'s @buddylist', array('%username' => $thisuser->name
) + buddylist_translation()));
1105 $buddies = buddylist_get_buddies($uid);
1106 $output .
= drupal_get_form('buddylist_form_buddiesrecent_page', $buddies);
1107 $output .
= theme('xml_icon', url('buddylist/'.
$uid .
'/buddies/recent/feed'));
1108 drupal_set_html_head('<link rel="alternate" type="application/rss+xml" title="'.
t('RSS - @buddies posts', buddylist_translation()).
'" href="'.
url('buddylist/'.
$user->uid .
'/buddies/recent/feed') .
'" />');
1113 function buddylist_buddiesgroups_page($uid) {
1115 $thisuser = user_load(array('uid' => $uid));
1116 drupal_set_title(t('%username\'s @buddy groups', array('%username' => $thisuser->name
) + buddylist_translation()));
1118 $headers = array(t('@buddy', buddylist_translation()), t('online'), t('# of @buddies', buddylist_translation()), t("@buddy's posts", buddylist_translation()));
1120 $result = db_query('SELECT label, label_id FROM {buddylist_groups} WHERE uid = %d ORDER BY label ASC', $thisuser->uid
);
1122 while ($row = db_fetch_object($result)) {
1123 $groups[$row->label_id
] = $row->label
;
1126 if (count($groups) == 0) {
1127 if ($thisuser->uid
== $user->uid
) {
1129 t("To organize your @buddies into groups, visit the !edit_groups page",
1130 array('!edit_groups' =>
1131 l(t('edit groups'), "buddylist/$uid/buddies/groups/edit")) + buddylist_translation())
1134 return t('No groups found.');
1137 $online_interval = time() - variable_get('user_block_seconds_online', 180);
1138 $buddies = buddylist_get_buddies($thisuser->uid
);
1140 foreach ($groups as
$label_id => $label) {
1141 $result = pager_query('SELECT bg.buddy, u.access FROM {buddylist_buddy_group} bg INNER JOIN {users} u ON bg.buddy = u.uid WHERE bg.uid = %d and bg.label_id = %d', 10, 0, NULL
, $thisuser->uid
, $label_id);
1143 while ($row = db_fetch_object($result)) {
1144 $online = $row->access
> $online_interval;
1146 theme('username', (object)$buddies[$row->buddy
]),
1147 theme('buddylist_online', $online),
1148 buddylist_count_buddies($row->buddy
),
1149 l(t('view posts'), 'user/'.
$row->buddy.
'/track')
1152 $output .
= theme('box', check_plain($label), $rows ?
theme('table', $headers, $rows) : t('Group is empty.'));
1157 function buddylist_count_buddies($uid) {
1158 $result = db_query("SELECT count(DISTINCT buddy) AS buddies FROM {buddylist} WHERE uid = %d", $uid);
1159 return db_result($result);
1163 function buddylist_get_buddy_groups($uid, $buddy = NULL
) {
1164 if (isset($buddy)) {
1165 $result = db_query("SELECT bg.label_id, bg.label, bg.visible FROM {buddylist_groups} bg INNER JOIN {buddylist_buddy_group} bbg ON bbg.uid = bg.uid WHERE bbg.uid = %d AND bg.label_id = bbg.label_id AND bbg.buddy = %d ", $uid, $buddy);
1168 $result = db_query("SELECT * FROM {buddylist_groups} WHERE uid=%d", $uid);
1170 $buddy_groups = array();
1171 while ($row = db_fetch_array($result)) {
1172 $buddy_groups[$row['label_id']] = $buddy ?
$row : $row['label'];
1174 return $buddy_groups;
1178 function buddylist_form_edit_groups_add() {
1180 $form['add_group'] = array(
1181 '#type' => 'textfield',
1182 '#title' => t('Add new group'),
1183 '#description' => t('Groups are a way to keep your @buddies organized. Groups can be named whatever you like.', buddylist_translation()),
1186 $form['submit'] = array(
1187 '#type' => 'submit',
1188 '#value' => t('Add'),
1194 function buddylist_form_edit_groups_remove($all_groups) {
1195 // Make a form to remove groups
1196 $form['remove']['groups'] = array(
1197 '#type' => 'checkboxes',
1198 '#return_value' => 1,
1200 '#default_value' => null
,
1201 '#options' => array_map('check_plain', $all_groups),
1204 $form['remove']['submit'] = array(
1205 '#type' => 'submit',
1206 '#value' => t('Remove'),
1212 function buddylist_form_edit_groups_table($buddies, $all_groups, $thisuser) {
1213 // Build the table with buddies and their groups
1214 $form['table']['groups'] = array ('#tree' => true
);
1215 foreach ($buddies as
$uid => $buddy) {
1217 foreach ($buddy['groups'] as
$group) {
1218 $items[] = $group['label_id'];
1221 if (count($all_groups) > 0) {
1222 $form['table']['groups'][$uid] = array(
1223 '#type' => 'checkboxes',
1225 '#default_value' => $items,
1226 '#options' => array_map('check_plain', $all_groups),
1231 $form['table']['user'] = array(
1233 '#value' => $thisuser->uid
,
1236 if (count($form['table']['groups']) > 0) {
1237 $form['table']['submit'] = array(
1238 '#type' => 'submit',
1239 '#value' => t('Submit'),
1247 * Callback for the buddygroup editing page
1249 function buddylist_buddiesgroups_edit($uid) {
1250 $thisuser = user_load(array('uid' => $uid));
1251 drupal_set_title(t('%username\'s @buddy groups', array('%username' => $thisuser->name
) + buddylist_translation()));
1253 if ($buddies = buddylist_get_buddies($thisuser->uid
)) {
1254 $output['add'] = drupal_get_form('buddylist_form_edit_groups_add');
1256 $groups = buddylist_get_buddy_groups($uid);
1258 if (count($groups) > 0) {
1259 $output['remove'] = drupal_get_form('buddylist_form_edit_groups_remove', $groups);
1260 $output['table'] = drupal_get_form('buddylist_form_edit_groups_table', $buddies, $groups, $thisuser);
1263 drupal_set_message(t("You don't have any groups defined."));
1266 return theme('buddylist_edit_groups', $output);
1269 drupal_set_message(t('Unable to edit @buddy groups. Add @buddies to your @buddylist before making groups.', buddylist_translation()));
1270 return t('No @buddies found.', buddylist_translation());
1274 function theme_buddylist_edit_groups($forms) {
1275 $output = '<span id="buddylist-group-add-form">'.
1278 '<span id="buddylist-group-remove-form">'.
1281 '<span id="buddlist-groups-form">'.
1287 function theme_buddylist_form_edit_groups_table($form) {
1290 foreach ($form['table']['groups'] as
$key => $value) {
1291 if(is_numeric($key)) {
1292 $rows[] = array(theme('username', user_load(array('uid' => $key))), drupal_render($form['table']['groups'][$key]));
1296 $headers = array(t('buddy'), t('@buddy groups', buddylist_translation()));
1297 $output .
= theme('table', $headers, $rows);
1299 $output .
= drupal_render($form);
1304 function theme_buddylist_status_block($count, $received_count, $sent_count) {
1307 $stats .
= "<div class='buddylist-status'>";
1308 $stats .
= "<div class='total'>" .
l(format_plural($count, '@count buddy', '@count buddies'), 'buddylist') .
"</div>";
1309 $stats .
= "<div class='pending_received'>" .
l(format_plural($received_count, '@count pending received request', '@count pending received requests'), 'buddylist/'.
$user->uid .
'/buddies/requests') .
"</div>";
1310 $stats .
= "<div class='pending_sent'>" .
l(format_plural($sent_count, '@count pending sent request', '@count pending sent requests'), 'buddylist/'.
$user->uid .
'/buddies/requests') .
"</div>";
1316 function buddylist_form_edit_groups_add_submit($form_id, $form_values) {
1318 $label_id = buddylist_buddygroup_new($user->uid
, $form_values['add_group']);
1321 function buddylist_form_edit_groups_remove_submit($form_id, $form_values) {
1323 foreach ($form_values['groups'] as
$label_id => $remove) {
1325 buddylist_buddygroup_remove($user->uid
, $label_id);
1330 function buddylist_form_edit_groups_table_submit($form_id, $form_values) {
1331 $userid = $form_values['user'];
1332 foreach ($form_values['groups'] as
$buddy => $groups) {
1333 foreach ($groups as
$label_id => $checked) {
1334 if ($checked == 0) {
1335 buddylist_buddygroup_remove_buddy($userid, $buddy, $label_id);
1338 buddylist_buddygroup_add_buddy($userid, $buddy, $label_id);
1343 drupal_set_message(t('@buddy groups saved.', buddylist_translation()));
1347 * Creates a new buddy group for a user
1349 * @param $uid user id of the user to whom the group will belong.
1350 * @param $group string; name of the group
1351 * @param $visible determines whether the user's buddies can see which groups they've been put in.
1353 * @return $label_id the existing or newly created id for the name of this group.
1355 function buddylist_buddygroup_new($uid, $group, $visible = TRUE
) {
1356 $label_id = db_result(db_query("SELECT label_id FROM {buddylist_groups} WHERE uid = %d AND label = '%s'", $uid, $group));
1357 if ($label_id === FALSE
) {
1358 $new_label_id = db_next_id('buddygroup');
1359 db_query("INSERT INTO {buddylist_groups} VALUES (%d, %d, '%s', %d)", $uid, $new_label_id, $group, $visible);
1360 return $new_label_id;
1368 * Removes a buddy group for a user
1370 * @param $uid user id of the user to whom the group belongs.
1371 * @param $label_id id of the group
1373 function buddylist_buddygroup_remove($uid, $label_id) {
1374 db_query("DELETE FROM {buddylist_groups} WHERE uid = %d AND label_id = %d", $uid, $label_id);
1375 db_query("DELETE FROM {buddylist_buddy_group} WHERE uid = %d AND label_id = %d", $uid, $label_id);
1378 function buddylist_buddygroup_remove_buddy($uid, $buddy, $label_id) {
1379 db_query("DELETE FROM {buddylist_buddy_group} WHERE uid = %d AND buddy = %d AND label_id = %d", $uid, $buddy, $label_id);
1382 function buddylist_buddygroup_add_buddy($uid, $buddy, $label_id) {
1383 db_lock_table('buddylist_buddy_group');
1384 buddylist_buddygroup_remove_buddy($uid, $buddy, $label_id);
1385 db_query('INSERT INTO {buddylist_buddy_group} VALUES (%d, %d, %d)', $uid, $buddy, $label_id);
1390 * Feed for buddies recent posts
1392 function buddylist_buddyfeed($uid) {
1393 if (!(is_numeric($uid) && $uid > 0)) {
1394 return drupal_not_found();
1398 $buddy_ids = array_keys(buddylist_get_buddies($uid));
1400 // false query to be used if no posts from buddies are available (as in this user has no buddies).
1401 $result = db_query('SELECT nid FROM {node} WHERE 0');
1402 if (count($buddy_ids)) {
1403 $buddy_ids_str = '('.
implode(',', $buddy_ids).
')';
1404 $result = db_query(db_rewrite_sql('SELECT nid FROM {node} WHERE status = 1 AND uid IN %s ORDER BY nid DESC'), $buddy_ids_str);
1406 $channel['title'] = t('@buddies recent posts on %site', array('%site' => variable_get('site_name', 'drupal')) + buddylist_translation());
1407 $channel['link'] = url('buddylist/'.
$uid .
'/buddies/recent', NULL
, NULL
, TRUE
);
1409 node_feed($result, $channel);
1412 function buddylist_addbuddy($uid) {
1414 $buddy = user_load(array('uid' => $uid));
1416 if (empty($buddy->name
)) {
1417 drupal_set_message(t('This user does not exist'));
1419 elseif (in_array($uid, array_keys(buddylist_get_buddies($user->uid
)))) {
1420 drupal_set_message(t('This user is already on your @buddylist', buddylist_translation()));
1422 elseif ($user->uid
== $uid) {
1423 drupal_set_message(t('Cannot add yourself to @buddylist', buddylist_translation()));
1426 $form['uid'] = array('#type' => 'hidden', '#value' => $uid);
1427 $form['name'] = array('#type' => 'hidden', '#value' => $buddy->name
);
1428 $output = confirm_form(
1430 t('Add user %name to your @buddylist?', array('%name' => $buddy->name
) + buddylist_translation()),
1431 $_GET['destination'],
1433 t('Add'), t('Cancel'),
1434 'buddylist_addbuddy_confirm');
1439 //function buddylist_form_alter($a, $b) {dprint_r($a); dprint_r($b);}
1442 * Confirm and add a buddy.
1444 function buddylist_addbuddy_submit($form_id, $form_values) {
1445 if (variable_get('buddylist_require_approval', 0)) {
1446 buddylist_add_request($form_values['uid']);
1449 buddylist_add($form_values['uid']);
1450 drupal_set_message(t('%name will be be notified the next time s/he logs in.', array('%name' => $form_values['name'])));
1456 * Removes the user $uid from the global user's account.
1457 * TODO: generalize this so that two uids can be given
1459 function buddylist_deletebuddy($uid) {
1461 $buddy = user_load(array('uid' => $uid));
1463 if (empty($buddy->name
)) {
1464 drupal_set_message('This user does not exist');
1466 else if (!in_array($uid, array_keys(buddylist_get_buddies($user->uid
)))) {
1467 drupal_set_message('This user is not on your @buddylist', buddylist_translation());
1470 $form['uid'] = array('#type' => 'hidden', '#value' => $uid);
1471 $form['name'] = array('#type' => 'hidden', '#value' => $buddy->name
);
1472 $output = confirm_form(
1474 t('Remove user %name from your @buddylist?', array('%name' => $buddy->name
) + buddylist_translation()),
1475 $_GET['destination'],
1477 t('Remove'), t('Cancel'),
1478 'buddylist_deletebuddy_confirm');
1485 * Confirm and add a buddy.
1487 function buddylist_deletebuddy_submit($form_id, $form_values) {
1488 buddylist_remove($form_values['uid']);
1489 drupal_set_message(t('@name will be be notified of being removed.', array('@name' => $form_values['name'])));
1493 function buddylist_add($id) {
1495 $user_to_add = user_load(array('uid' => $id));
1497 if (!in_array($id, array_keys(buddylist_get_buddies($user->uid
)))) {
1498 db_query('INSERT INTO {buddylist} (received, uid, buddy, timestamp) VALUES (1, %d, %d, %d)' , $user->uid
, $id , time());
1499 // DB value buddylist.received set to 1, meaning buddy has a message waiting
1500 // letting them know you added them as a buddy
1501 // buddylist.received set back to 0 when user logs in along with being informed of new buddy
1502 if (variable_get('buddylist_send_add', FALSE
)) {
1503 buddylist_mail_user('add', $user_to_add);
1505 drupal_set_message(t('%username has been added to your @buddylist', array('%username' => $user_to_add->name
) + buddylist_translation()));
1508 drupal_set_message(t('%username is already on your @buddylist', array('%username' => $user_to_add->name
) + buddylist_translation()));
1512 function buddylist_add_request($id) {
1514 $user_to_add = user_load(array('uid' => $id));
1516 $already_requested = in_array($id, array_keys(buddylist_get_requestees($user->uid
)));
1517 $already_buddies = in_array($id, array_keys(buddylist_get_buddies($user->uid
)));
1519 if (!$already_requested && !$already_buddies) {
1520 db_query('INSERT INTO {buddylist_pending_requests} (requester_uid, requestee_uid, received) VALUES (%d, %d, %d)', $user->uid
, $id, 0);
1522 if (variable_get('buddylist_send_request', FALSE
)) {
1523 buddylist_mail_user('request', $user_to_add);
1525 drupal_set_message(t('Your request to add %username to your @buddylist has been submitted. %username will be notified.', array('%username' => $user_to_add->name
) + buddylist_translation()));
1528 if ($already_requested) {
1529 drupal_set_message(t('You have already requested to add %username to your @buddylist', array('%username' => $user_to_add->name
) + buddylist_translation()));
1532 if ($already_buddies) {
1533 drupal_set_message(t('%username is already on your @buddylist', array('%username' => $user_to_add->name
) + buddylist_translation()));
1538 function buddylist_remove($id) {
1540 db_query('DELETE FROM {buddylist} WHERE uid = %d AND buddy = %d' , $user->uid
, $id);
1541 db_query('DELETE FROM {buddylist_buddy_group} WHERE uid = %d AND buddy = %d' , $user->uid
, $id);
1542 $thisuser = user_load(array('uid' => $id));
1543 if (variable_get('buddylist_send_remove', FALSE
)) {
1544 buddylist_mail_user('remove', $thisuser);
1546 drupal_set_message(t('%username has been removed from your @buddylist', array('%username' => $thisuser->name
) + buddylist_translation()));
1548 if (variable_get('buddylist_require_approval', 0)) {
1549 db_query('DELETE FROM {buddylist} WHERE uid = %d AND buddy = %d', $id, $user->uid
);
1550 db_query('DELETE FROM {buddylist_buddy_group} WHERE uid = %d AND buddy = %d', $id, $user->uid
);
1554 function buddylist_cancel_add($id) {
1555 $thisuser = user_load(array('uid' => $id));
1556 drupal_set_message(t('User %name was NOT added to your @buddylist.', array('%name' => $thisuser->name
) + buddylist_translation()));
1559 function buddylist_cancel_remove($id) {
1560 $thisuser = user_load(array('uid' => $id));
1561 drupal_set_message(t('User %name was NOT removed from your @buddylist.', array('%name' => $thisuser->name
) + buddylist_translation()));
1564 function theme_buddylist_online($online) {
1565 return $online ?
t('yes') : t('no');
1569 * Sends mail to a buddy, which is added/removed/deleted/requested by $user
1571 function buddylist_mail_user($op, $buddy, $user = NULL
) {
1573 if (variable_get('buddylist_user_mail', FALSE
) && isset($buddy->buddylist_mail
) && !$buddy->buddylist_mail
) {
1574 return; //the user has turned mails off
1577 if (is_null($user)){
1582 $subject = BUDDYLIST_ADD_SUBJECT
;
1583 $message = variable_get('buddylist_add_message', buddylist_mail_add_default());
1586 $subject = BUDDYLIST_REMOVE_SUBJECT
;
1587 $message = variable_get('buddylist_remove_message', buddylist_mail_remove_default());
1590 $subject = BUDDYLIST_REQUEST_SUBJECT
;
1591 $message = variable_get('buddylist_request_message', buddylist_mail_request_default());
1594 $subject = BUDDYLIST_APPROVAL_SUBJECT
;
1595 $message = variable_get('buddylist_approval_message', buddylist_mail_approval_default());
1599 $replacements = buddylist_translation() + (($op == 'approval') ?
buddylist_approval_mail_replacements($user, $buddy) : buddylist_mail_replacements($buddy, $user));
1600 $subject = t($subject, $replacements);
1601 $message = t($message, $replacements);
1603 $site_mail = variable_get('site_mail', "");
1604 if (!strlen($site_mail)) {
1605 if (user_access('administer site configuration')){
1606 drupal_set_message(t('You should create an !link for your site.', array('!link' => l(t('an administrator mail address'), 'admin/settings/site-information'))), 'warning');
1608 $site_mail = 'nobody@localhost';
1612 if ($op != 'approval') {
1613 if (drupal_mail("buddylist_mail_user_$op", $buddy->mail, $subject, $message, $site_mail)) {
1614 $message = t('%type message was sent to %username', array('%type' => $op, '%username' => $buddy->name
));
1615 watchdog('buddylist', $message);
1618 $message = t('There was a problem sending the %type message to %username', array('%type' => $op, '%username' => $buddy->name
));
1619 watchdog('buddylist', $message, WATCHDOG_WARNING
);
1623 if (drupal_mail("buddylist_mail_user_$op", $buddy->mail, $subject, $message, $site_mail)) {
1624 $message = t('%type message was sent to %username', array('%type' => $op, '%username' => $buddy->name
));
1625 watchdog('buddylist', $message);
1628 $message = t('There was a problem sending the %type message to %username', array('%type' => $op, '%username' => $buddy->name
));
1629 watchdog('buddylist', $message, WATCHDOG_WARNING
);
1634 function buddylist_mail_add_default() {
1638 You are @adder_name
's newest @buddy.
1640 Here's a
link to @adder_name
's profile. If you'd like
, you can add them as one of your @buddies
:
1649 function buddylist_mail_remove_default() {
1653 You have been removed from @adder_name
's @buddylist.
1655 Here's a
link to @adder_name
's profile:
1659 Enjoy your new freedom!
1666 function buddylist_mail_request_default() {
1670 @adder_name has requested to add you to his/her @buddylist.
1672 Here's a
link to @adder_name
's profile:
1676 To approve/deny this request, log in to @siteurl and see your pending @buddy requests
1677 at @pending_requests_link.
1679 Enjoy your new freedom!
1686 function buddylist_mail_approval_default() {
1690 @addee_name has approved your request to join his/her @buddylist.
1692 Here's a
link to your buddylist
:
1696 Enjoy your new freedom
!
1704 function buddylist_mail_replacements(&$buddy, &$user){
1706 '@adder_name' => $user->name
,
1707 '@adder_link' => url("user/".
$user->uid
, NULL
, NULL
, TRUE
),
1708 '@adder_uid' => $user->uid
,
1709 '@addee_name' => $buddy->name
,
1710 '@addee_link' => url("user/".
$buddy->uid
, NULL
, NULL
, TRUE
),
1711 '@addee_uid' => $buddy->uid
,
1712 '@site' => variable_get("site_name", "Drupal"),
1713 '@siteurl' => $GLOBALS["base_url"],
1714 '@adder_list_link' => url("buddylist/".
$user->uid .
"/buddies/list", NULL
, NULL
, TRUE
),
1715 '@pending_requests_link' => url("buddylist/".
$buddy->uid .
"/buddies/requests", NULL
, NULL
, TRUE
),
1721 function buddylist_approval_mail_replacements(&$buddy, &$user) {
1723 '@adder_name' => $user->name
,
1724 '@adder_link' => url("user/".
$user->uid
, NULL
, NULL
, TRUE
),
1725 '@adder_uid' => $user->uid
,
1726 '@addee_name' => $buddy->name
,
1727 '@addee_link' => url("user/".
$buddy->uid
, NULL
, NULL
, TRUE
),
1728 '@addee_uid' => $buddy->uid
,
1729 '@site' => variable_get("site_name", "Drupal"),
1730 '@siteurl' => $GLOBALS["base_url"],
1731 '@adder_list_link' => url("buddylist/".
$user->uid .
"/buddies/list", NULL
, NULL
, TRUE
),
1735 function buddylist_empty_text_default() {
1737 <p
>You haven
't made any @buddies, yet.</p>
1739 <p>You can add a @buddy by visiting a user's profile page.
</p
>