4 if (module_exists('views')) {
5 include_once('buddylist_views.inc');
9 define('BUDDYLIST_ADD_SUBJECT', variable_get('buddylist_add_subject', "[@site] You are @adder_name's newest @buddy"));
10 define('BUDDYLIST_REMOVE_SUBJECT', variable_get('buddylist_remove_subject', "[@site] You have been removed from @adder_name's @buddy list"));
11 define('BUDDYLIST_REQUEST_SUBJECT', variable_get('buddylist_request_subject', "[@site] @adder_name has requested to add you to his/her @buddy list"));
12 define('BUDDYLIST_APPROVAL_SUBJECT', variable_get('buddylist_approval_subject', "[@site] @addee_name has approved your @buddy list add request"));
15 * returns an array of common translation placeholders
17 function buddylist_translation() {
18 $translations = array(
19 '@buddy' => t('buddy'),
20 '@Buddy' => t('Buddy'),
22 '@buddylist' => t('buddylist'),
23 '@Buddylist' => t('Buddylist'),
25 '@buddies' => t('buddies'),
26 '@Buddies' => t('Buddies'),
28 '@buddyof' => t('buddy of'),
29 '@Buddyof' => t('Buddy of'),
31 return variable_get('buddylist_translation', $translations);
36 * Implementation of hook_help
38 function buddylist_help($section) {
40 case
'admin/settings/modules#description':
41 return t('Enable @buddy list functionality.', buddlist_translation());
42 case
'admin/help#buddylist':
44 <p>@Buddy list enables users to keep a list of @buddies from their social network in their user account.
45 Users can also track what their @buddies are posting to the site.
46 Furthermore, they can track their <i>@buddies'</i> @buddies and thereby explore their social network.</p>
47 <p>If the administrator has enabled the profile module, users can add @buddies via their @buddies' user profiles.
48 On the \"View\" tab of each user's profile, there is a \"@Buddy list\" section. Select the 'add @buddy' action to add the user to your @buddy list.
49 If a user is already in your @buddy list, the 'delete' action will remove the @buddy. Administrators can also enable the @buddylist block.
50 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 @buddy lists with other FOAF-aware social networking applications.</p>
53 <li>add a @buddy by looking at their profile: <a href=\"@userprofiles\" title=\"View user profiles\">view user profiles</a></li>
54 <li>allow users to view profiles in <a href=\"@setaccesspermissions\" title=\"set access permissions\">administer » access control</a></li>
55 <li>enable the @buddy list block at <a href=\"@blockadministration\" title=\"block administration\">administer » block</a></li>
56 <li>administer the @buddy list block at <a href=\"@buddylistsettings\" title=\"@buddylist settings\">administer » settings » @buddylist</a></li>
59 <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>",
60 array('@userprofiles' => url('profile'),
61 '@setaccesspermissions' => url('admin/user/access'),
62 '@blockadministration' => url('admin/build/block'),
63 '@buddylistsettings' => url('admin/settings/buddylist')
64 ) + buddylist_translation());
71 * Implementation of hook_perm
73 function buddylist_perm() {
74 return array('maintain buddy list', 'view buddy lists');
78 * Implementation of hook_menu
80 function buddylist_menu($may_cache) {
84 $id = is_numeric(arg(1)) ?
arg(1) : $user->uid
;
87 // buddylist settings page
89 'path' => 'admin/settings/buddylist',
90 'title' => t('Buddylist'), // Note that this isn't translated on purpose since it is for the admin
91 'description' => t('Buddylist configuration options for blocks, email, etc.'),
92 'callback' => 'drupal_get_form',
93 'callback arguments' => 'buddylist_admin_settings',
94 'access' => user_access('administer site configuration'),
96 // my buddylist menu item
98 'path' => 'buddylist',
99 'title' => t('My @buddylist', buddylist_translation()),
100 'access' => (user_access('maintain buddy list') && $id),
101 'callback' => 'buddylist_buddylisting_page',
102 'callback arguments' => array($user->uid
, 'buddies'),
106 // 'edit access' only granted to user's own buddy list or to administrative users
108 ($id == $user->uid
&& user_access('maintain buddy list') && $user->uid
)
109 || user_access('administer users'));
111 $approval_required = variable_get('buddylist_require_approval', 0);
114 'path' => 'buddy/add',
115 'title' => t('Add to @buddylist', buddylist_translation()),
116 'access' => $editAccess,
117 'callback' => 'drupal_get_form',
118 'callback arguments' => array('buddylist_addbuddy', arg(2)),
119 'type' => MENU_CALLBACK
,
122 'path' => 'buddy/delete',
123 'title' => t('Delete from @buddylist', buddylist_translation()),
124 'access' => $editAccess,
125 'callback' => 'drupal_get_form',
126 'callback arguments' => array('buddylist_deletebuddy', arg(2)),
127 'type' => MENU_CALLBACK
,
131 $viewAccess = (($id == $user->uid
&& user_access('maintain buddy list')) || user_access('view buddy lists'));
133 // If buddylist approval is required, then upon approval, both parties become buddies of each other.
134 // So, in effect, idea 'buddyof' becomes redundant.
135 if (!$approval_required) {
137 'path' => 'buddylist/'.
$id .
'/buddies',
138 'title' => t('@Buddies', buddylist_translation()),
139 'access' => $viewAccess,
140 'callback' => 'buddylist_buddylisting_page',
141 'type' => MENU_DEFAULT_LOCAL_TASK
,
143 'callback arguments' => array($id)
146 'path' => 'buddylist/'.
$id .
'/buddyof',
147 'title' => t('@Buddyof', buddylist_translation()),
148 'access' => $viewAccess,
149 'callback' => 'buddylist_buddylisting_page',
150 'type' => MENU_LOCAL_TASK
,
152 'callback arguments' => array($id, 'buddyof')
158 'path' => 'buddylist/'.
$id .
'/buddies/list',
159 'title' => t('@Buddylist', buddylist_translation()),
160 'access' => $viewAccess,
161 'callback' => 'buddylist_buddylisting_page',
162 'type' => MENU_DEFAULT_LOCAL_TASK
,
164 'callback arguments' => array($id),
167 'path' => 'buddylist/'.
$id .
'/buddies/recent',
168 'title' => t('Recent posts'),
169 'access' => ($viewAccess && module_exists('tracker')),
170 'callback' => 'buddylist_buddiesrecent_page',
171 'type' => MENU_LOCAL_TASK
,
173 'callback arguments' => array($id),
175 if (variable_get('buddylist_buddygroups', FALSE
)) {
177 'path' => 'buddylist/'.
$id .
'/buddies/groups/view',
178 'title' => t('View groups'),
179 'access' => $viewAccess,
180 'callback' => 'buddylist_buddiesgroups_page',
181 'type' => MENU_LOCAL_TASK
,
183 'callback arguments' => array($id),
186 'path' => 'buddylist/'.
$id .
'/buddies/groups/edit',
187 'title' => t('Edit groups'),
188 'access' => $editAccess,
189 'callback' => 'buddylist_buddiesgroups_edit',
190 'type' => MENU_LOCAL_TASK
,
192 'callback arguments' => array($id),
197 if ($approval_required && $editAccess) {
199 'path' => 'buddylist/'.
$id .
'/buddies/requests',
200 'title' => t('Pending requests'),
201 'access' => $editAccess,
202 'callback' => 'theme',
203 'type' => MENU_LOCAL_TASK
,
205 'callback arguments' => array('buddylist_pending_requests', $id)
210 if ($id != $user->uid
) {
211 // This callback can interfere with the 'my buddylist' menu item,
212 // so we only load it if the user is viewing another user's list.
214 'path' => 'buddylist/'.
$id,
215 'title' => t('@Buddylist', buddylist_translation()),
216 'access' => (($viewAccess || $editAccess) && $id),
217 'callback' => 'buddylist_buddylisting_page',
218 'type' => MENU_CALLBACK
,
219 'callback arguments' => array($id),
223 'path' => 'buddylist/'.
$id .
'/buddies/recent/feed',
224 'title' => t('Xml feed'),
225 'access' => $viewAccess,
226 'callback' => 'buddylist_buddyfeed',
227 'type' => MENU_CALLBACK
,
228 'callback arguments' => array($id),
236 * Buddylist administration settings page
238 function buddylist_admin_settings() {
239 $form['general'] = array(
240 '#type' => 'fieldset',
241 '#title' => t('General settings'),
244 $form['general']['buddylist_require_approval'] = array(
246 '#title' => t('Require approval'),
247 '#default_value' => variable_get('buddylist_require_approval', 0),
248 '#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()),
249 '#options' => array(1 => t('Yes'), 0 => t('No'))
251 $form['general']['buddylist_buddygroups'] = array(
252 '#type' => 'checkbox',
253 '#title' => t('Enable @buddy groups', buddylist_translation()),
254 '#description' => t('Enables @buddylist @buddy groups. Users will be able to create @buddy groups to manage their @buddies.', buddylist_translation()),
255 '#default_value' => variable_get('buddylist_buddygroups', FALSE
),
258 // User profile page settings
259 $form['profile_settings'] = array(
260 '#type' => 'fieldset',
261 '#title' => t('Profile page options'),
263 $form['profile_settings']['buddylist_prof_buddies'] = array(
265 '#title' => t('Number of @buddies and users who\'ve added me', buddylist_translation()),
266 '#default_value' => variable_get('buddylist_prof_buddies', 5),
267 '#options' => drupal_map_assoc(range(0, 10)),
268 '#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()),
271 // TODO: move these to block settings
272 $form['block_settings'] = array(
273 '#type' => 'fieldset',
274 '#title' => t('@Buddylist block options', buddylist_translation()),
276 $form['block_settings']['buddylist_blocklisting_size'] = array(
278 '#title' => t("Number of @buddies to list in the user's @buddy block", buddylist_translation()),
279 '#default_value' => variable_get('buddylist_blocklisting_size', 5),
280 '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)),
281 '#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()),
283 $form['block_settings']['buddylist_posts_block'] = array(
285 '#title' => t("Number of posts to list in the @buddies' recent posts block", buddylist_translation()),
286 '#default_value' => variable_get('buddylist_posts_block', 7),
287 '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)),
288 '#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()),
290 $form['block_settings']['buddylist_list_block_title'] = array(
291 '#type' => 'textfield',
292 '#title' => t('"My @buddies list" block title', buddylist_translation()),
293 '#default_value' => variable_get('buddylist_list_block_title', t('My @buddy list', buddylist_translation())),
296 '#description' => t('This will be the title for the "My @buddy list" block. If none is specified, "My @buddy list" will be used.', buddylist_translation()),
298 $form['block_settings']['buddylist_block_title'] = array(
299 '#type' => 'textfield',
300 '#title' => t('"My @buddies\' recent posts" block title', buddylist_translation()),
301 '#default_value' => variable_get('buddylist_block_title', t("My @buddies' recent posts", buddylist_translation())),
304 '#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()),
306 $form['block_settings']['buddylist_status_block_title'] = array(
307 '#type' => 'textfield',
308 '#title' => t('"My @buddy list status" block title', buddylist_translation()),
309 '#default_value' => variable_get('buddylist_status_block_title', t("My @buddy list status", buddylist_translation())),
312 '#description' => t('This will be the title for the @buddy list status block. If none is specified, "My @buddy list status" will be used.', buddylist_translation()),
315 $form['mail'] = array(
316 '#type' => 'fieldset',
317 '#title' => t('email'),
321 $macros = implode(', ', array_keys(buddylist_mail_replacements($user, $user)));
322 $approval_macros = implode(', ', array_keys(buddylist_approval_mail_replacements($user, $user)));
324 $form['mail']['buddylist_user_mail'] = array(
325 '#type' => 'checkbox',
326 '#title' => t('Allow users to turn off buddylist messages'),
327 '#default_value' => variable_get('buddylist_user_mail', FALSE
),
328 '#description' => t('If you check this, users will have a new setting on their account edit page.'),
331 $form['mail']['buddylist_send_add'] = array(
332 '#type' => 'checkbox',
333 '#title' => t('Send add messages'),
334 '#default_value' => variable_get('buddylist_send_add', FALSE
),
337 $form['mail']['buddylist_add_subject'] = array(
338 '#type' => 'textfield',
339 '#title' => t('Added @buddy email subject', buddylist_translation()),
340 '#default_value' => BUDDYLIST_ADD_SUBJECT
,
343 $form['mail']['buddylist_add_message'] = array(
344 '#type' => 'textarea',
345 '#title' => t('Added @buddy email message', buddylist_translation()),
346 '#default_value' => variable_get('buddylist_add_message', buddylist_mail_add_default()),
347 '#description' => t('Replacement strings are: %macros', array('%macros' => $macros)),
350 $form['mail']['buddylist_send_remove'] = array(
351 '#type' => 'checkbox',
352 '#title' => t('Send remove messages'),
353 '#default_value' => variable_get('buddylist_send_remove', FALSE
),
356 $form['mail']['buddylist_remove_subject'] = array(
357 '#type' => 'textfield',
358 '#title' => t('Removed @buddy email subject', buddylist_translation()),
359 '#default_value' => BUDDYLIST_REMOVE_SUBJECT
,
362 $form['mail']['buddylist_remove_message'] = array(
363 '#type' => 'textarea',
364 '#title' => t('Removed @buddy email message', buddylist_translation()),
365 '#default_value' => variable_get('buddylist_remove_message', buddylist_mail_remove_default()),
366 '#description' => t('Replacement strings are: %macros', array('%macros' => $macros)),
369 $form['mail']['buddylist_send_request'] = array(
370 '#type' => 'checkbox',
371 '#title' => t('Send request messages.'),
372 '#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.'),
373 '#default_value' => variable_get('buddylist_send_request', FALSE
)
376 $form['mail']['buddylist_request_subject'] = array(
377 '#type' => 'textfield',
378 '#title' => t('@buddy request email subject', buddylist_translation()),
379 '#default_value' => BUDDYLIST_REQUEST_SUBJECT
,
382 $form['mail']['buddylist_request_message'] = array(
383 '#type' => 'textarea',
384 '#title' => t('@buddy request email message', buddylist_translation()),
385 '#default_value' => variable_get('buddylist_request_message', buddylist_mail_request_default()),
386 '#description' => t('Replacement strings are: %macros', array('%macros' => $macros)),
389 $form['mail']['buddylist_send_approval'] = array(
390 '#type' => 'checkbox',
391 '#title' => t('Send approval messages'),
392 '#default_value' => variable_get('buddylist_send_approval', FALSE
),
393 '#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())
396 $form['mail']['buddylist_approval_subject'] = array(
397 '#type' => 'textfield',
398 '#title' => t('@buddy request email subject', buddylist_translation()),
399 '#default_value' => BUDDYLIST_APPROVAL_SUBJECT
,
402 $form['mail']['buddylist_approval_message'] = array(
403 '#type' => 'textarea',
404 '#title' => t('@buddy approval email message', buddylist_translation()),
405 '#default_value' => variable_get('buddylist_approval_message', buddylist_mail_approval_default()),
406 '#description' => t('Replacement strings are: %macros', array('%macros' => $approval_macros)),
409 return system_settings_form($form);
414 * Implementation of hook_user
416 function buddylist_user($type, &$edit, &$thisuser, $category = NULL
) {
420 // show any buddylist notifications upon login and upon viewing own profile
421 if (user_access('maintain buddy list') && (($type == 'login') || ($type == 'view') && ($thisuser->uid
== $user->uid
))) {
422 buddylist_setmsg_received($thisuser);
424 if ($type == 'view') {
425 if ($list = buddylist_get_buddylist($thisuser)) {
426 $output[] = array('title' => t('@Buddies', buddylist_translation()), 'value' => $list, 'class' => 'buddylist');
428 if ($list = buddylist_get_buddylist($thisuser, TRUE
)) {
429 $output[] = array('title' => t('@buddy of', buddylist_translation()), 'value' => $list, 'class' => 'buddyoflist');
431 if ($actions = buddylist_get_buddy_actions($user, $thisuser)) {
432 $output[] = array('title' => t('@Buddy actions', buddylist_translation()), 'value' => theme('item_list', $actions), 'class' => 'buddylist_actions');
434 if(count($output) > 0) {
435 return array(t('@Buddy List', buddylist_translation()) => $output);
438 else if ($type == 'delete') {
439 db_query("DELETE FROM {buddylist} WHERE uid = %d OR buddy = %d", $thisuser->uid
, $thisuser->uid
);
440 db_query("DELETE FROM {buddylist_buddy_group} WHERE uid = %d OR buddy = %d", $thisuser->uid
, $thisuser->uid
);
441 db_query("DELETE FROM {buddylist_groups} WHERE uid = %d", $thisuser->uid
);
442 db_query("DELETE FROM {buddylist_pending_requests} WHERE requester_uid = %d OR requestee_uid = %d", $thisuser->uid
, $thisuser->uid
);
444 else if ($type == 'load') {
445 $thisuser->buddies
= buddylist_get_buddies($thisuser->uid
);
447 else if ($type == 'form' && $category == 'account' && variable_get('buddylist_user_mail', FALSE
) && user_access('maintain buddy list', $thisuser)) {
448 // when user tries to edit his own data
449 $form['buddylist_settings'] = array(
450 '#type' => 'fieldset',
451 '#title' => t('Buddylist settings'),
453 $form['buddylist_settings']['buddylist_mail'] = array(
454 '#type' => 'checkbox',
455 '#title' => t('Receive @buddylist notification mails', buddylist_translation()),
456 '#default_value' => isset($edit['buddylist_mail']) ?
$edit['buddylist_mail'] : 1,
457 '#description' => t('If you check this, you will be notified about important actions regarding your @Buddylist.', buddylist_translation()));
463 * Return a formatted list of buddies for the given user
464 * @param $buddy_of If set to TRUE, a formatted list of users is returned, for whom this user is a buddy.
466 function buddylist_get_buddylist($user, $buddy_of = FALSE
) {
468 if (user_access('view buddy lists') && !$buddy_of) {
470 if ($buddies = buddylist_get_buddies($user->uid
)) {
471 foreach(array_keys($buddies) as
$buddy) {
472 $account = user_load(array('uid' => $buddy));
473 $listbuddies[] = $account;
475 if ($i > variable_get('buddylist_prof_buddies', 5)) {
479 return theme('user_list', $listbuddies);
482 else if (user_access('view buddy lists') && !variable_get('buddylist_require_approval', 0)) {
483 // This portion of code is used to see if this $thisuser is a buddy of others and, if s/he is, returns a list
484 // of people s/he is a buddy of.
485 // Note the distinction between having a buddy and being someone else's buddy (i.e., 'buddyof')
486 // Of course, this distinction doesn't exist if approval is required to add a buddy (in which case, buddy relationships are symmetric)
487 $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';
488 $result = db_query_range($sql, $user->uid
, 0, variable_get('buddylist_prof_buddies', 5));
489 while ($row = db_fetch_object($result)) {
490 $listbuddyof[$row->uid
] = $row;
493 return theme('user_list', $listbuddyof);
500 * Returns an array of posible actions (html) for the viewing user,
501 * e.g. a link to make the viewed user a buddy
503 function buddylist_get_buddy_actions(&$viewing_user, &$viewed_user) {
506 if (!user_access('maintain buddy list') || $viewing_user->uid
== $viewed_user->uid
) {
510 if (variable_get('buddylist_require_approval', FALSE
) && in_array($viewed_user->uid
, array_keys(buddylist_get_requestees($viewing_user->uid
)))) {
511 $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());
513 else if (in_array($viewed_user->uid
, array_keys(buddylist_get_buddies($viewing_user->uid
)))) {
514 $actions[] = theme('remove_from_buddylist_link', $viewed_user);
516 else if (in_array($viewing_user->uid
, array_keys(buddylist_get_requestees($viewed_user->uid
)))) {
517 $actions[] = t('This user has requested to add you to your @buddylist.', buddylist_translation()) .
518 drupal_get_form('buddylist_approval_form', $viewing_user->uid
, $viewed_user->uid
);
521 $actions[] = theme('add_to_buddylist_link', $viewed_user);
529 * Implementation for hook_block
531 function buddylist_block($op = 'list', $delta = 0) {
535 $block[0]['info'] = variable_get('buddylist_list_block_title', t('My @buddy list', buddylist_translation()));
536 $block[1]['info'] = variable_get('buddylist_block_title', t('My @buddies\' recent posts', buddylist_translation()));
537 if (variable_get('buddylist_require_approval', 0)) {
538 $block[2]['info'] = variable_get('buddylist_status_block_title', t('My @buddy status', buddylist_translation()));
542 else if ($op == 'view' && user_access('access content') && user_access('maintain buddy list') && $user->uid
> 0) {
544 case
0 : // Shows buddylist block
545 if ($buddies = buddylist_get_buddies()) {
547 foreach (array_keys($buddies) as
$buddy) {
548 $users[] = user_load(array('uid' => $buddy));
550 if ($i == variable_get('buddylist_blocklisting_size', 5)) {
555 $block['content'] = theme('user_list', $users);
556 $block['subject'] = variable_get('buddylist_list_block_title', t('My @buddy list', buddylist_translation()));
558 // check if a "more" link should generated by seeing if there are more buddies than the specified $upperlimit
559 if (count($buddies) > variable_get('buddylist_blocklisting_size', 5)) {
560 $block['content'] .
= '<div class="more-link">' .
l(t('more'), 'buddylist', array('title' => t('View more.'))) .
'</div>';
566 case
1: // Shows my buddies recent posts block
567 $buddies = buddylist_get_buddies();
568 $keys = array_keys($buddies);
569 if (count($keys) > 0) {
570 $str_buddies = implode(',', $keys);
571 $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));
573 if (db_num_rows($result)) {
574 $block['subject'] = variable_get('buddylist_block_title', t('My @buddies\' recent posts', buddylist_translation()));
575 $block['content'] = node_title_list($result);
577 // check if a "more" link should generated by seeing if there are more buddies than the specified $upperlimit
578 $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
);
579 $countresult = db_fetch_object($result);
581 if (variable_get('buddylist_posts_block', 7) < $countresult->node_count
) {
582 $block['content'] .
= '<div class="more-link">'.
l(t('more'), 'buddylist/'.
$user->uid .
'/buddies/recent', array('title' => t('View more.'))) .
'</div>';
589 case
2: // Buddylist status
590 $block['subject'] = variable_get('buddylist_status_block_title', t('My @buddy status', buddylist_translation()));
592 $count = db_result(db_query("SELECT COUNT(uid) FROM {buddylist} WHERE uid = %d", $user->uid
));
593 $sent = db_result(db_query("SELECT COUNT(requester_uid) FROM {buddylist_pending_requests} WHERE requester_uid = %d", $user->uid
));
594 $received = db_result(db_query("SELECT COUNT(requestee_uid) FROM {buddylist_pending_requests} WHERE requestee_uid = %d", $user->uid
));
596 $block['content'] = theme('buddylist_status_block', $count, $received, $sent);
604 * Public API for retrieving buddies. Feel free to use this from other
606 * $key can be 'uid' or 'label'.
608 function buddylist_get_buddies($uid = NULL
, $key = 'uid') {
615 if (!isset($buddies[$key][$uid])) {
616 $buddies[$key][$uid] = array();
617 $sql = 'SELECT b.buddy, u.name, u.mail, u.uid FROM {buddylist} b
618 INNER JOIN {users} u ON b.buddy = u.uid
620 $result = db_query($sql, $uid);
621 while ($row = db_fetch_object($result)) {
622 $buddies[$key][$uid][$row->buddy
]['uid'] = $row->uid
;
623 $buddies[$key][$uid][$row->buddy
]['name'] = $row->name
;
624 $buddies[$key][$uid][$row->buddy
]['mail'] = $row->mail;
625 $buddies[$key][$uid][$row->buddy
]['groups'] = buddylist_get_buddy_groups($uid, $row->buddy
);
626 $buddies[$key][$uid][$row->buddy
]['online'] = 0;
627 $selectlist .
= $row->buddy.
",";
629 // Add the online flag
630 if (db_num_rows($result)) {
631 $sql = 'SELECT uid FROM {sessions} WHERE uid IN (%s) AND timestamp > %d';
632 $result = db_query($sql, substr($selectlist,0,-1), time()-1800);
633 while ($row = db_fetch_object($result)) {
634 $buddies[$key][$uid][$row->uid
]['online'] = 1;
639 return $buddies[$key][$uid];
643 * Returns an array of uid => name of people that user with param $uid has made a buddy request to
645 function buddylist_get_requestees($uid) {
648 $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);
650 while ($row = db_fetch_object($result)) {
651 $buddies[$row->requestee_uid
] = $row->name
;
657 function buddylist_setmsg_received($thisuser) {
660 if (variable_get('buddylist_require_approval', 0)) {
661 // Go through and find new buddylist add-requests, (i.e., the ones in {buddylist_pending_requests} w/ received column == 0
662 $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
);
663 $acknowledged_uids = array();
664 while ($row = db_fetch_object($result)) {
665 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()));
666 $acknowledged_uids[] = $row->uid
;
668 if (count($acknowledged_uids)) {
669 db_query('UPDATE {buddylist_pending_requests} SET received = 1 WHERE requestee_uid = %d AND requester_uid IN (%s)', $user->uid
, implode(',', $acknowledged_uids));
673 $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
);
674 while ($rec = db_fetch_object($check_received)) {
675 if (($rec->received
) and ($thisuser->uid
== $user->uid
)) {
676 // TODO: This is where integration with Privatemsg could happen. If enabled, send a private message instead.
677 drupal_set_message(t('!linktouser has added you to his/her @buddylist.', array('!linktouser' => theme('username', $rec)) + buddylist_translation()));
678 db_query('UPDATE {buddylist} SET received = 0 WHERE buddy = %d', $user->uid
);
685 * expose add and remove links to theming.
687 function theme_remove_from_buddylist_link($buddyuser) {
688 return l(t('Remove %name from my @buddy list', array('%name' => $buddyuser->name
) + buddylist_translation()), 'buddy/delete/' .
$buddyuser->uid
, NULL
, drupal_get_destination(), NULL
, FALSE
, TRUE
);
691 function theme_add_to_buddylist_link($buddyuser) {
692 return l(t('Add %name to my @buddy list', array('%name' => $buddyuser->name
) + buddylist_translation()), 'buddy/add/' .
$buddyuser->uid
, NULL
, drupal_get_destination(), NULL
, FALSE
, TRUE
);
697 * Displays a list of a given user's buddies.
699 function buddylist_buddylisting_page($uid = NULL
, $mode = 'buddies') {
705 // Check that the uid is valid, not the anonymous user, and the user exists
706 if (!(is_numeric($uid) && ($uid > 0) && $thisuser = user_load(array('uid' => $uid)))) {
711 drupal_set_title(t('%username\'s @buddylist', array('%username' => $thisuser->name
) + buddylist_translation()));
713 $buddies_per_page = 20;
715 //TODO: use the get_buddies function instead
716 if ($mode == 'buddies') {
717 $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";
720 $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";
722 $result = pager_query($sql, $buddies_per_page, 0 , NULL
, $uid);
724 $header = array(t('@buddy', buddylist_translation()), t('online'));
725 $online_interval = time() - variable_get('user_block_seconds_online', 180);
727 if (db_num_rows($result)) {
728 while ($account = db_fetch_object($result)) {
729 $online = $account->access
> $online_interval;
730 $rows[] = array(theme('username', user_load(array('uid' => $account->buddy
))), theme('buddylist_online', $online));
732 $output .
= theme('table', $header, $rows);
735 $output .
= t('No @buddies found.', buddylist_translation());
738 $output .
= theme('pager', NULL
, $buddies_per_page);
744 * Returns a list of people who've requested to be added to the given user's buddylist
745 * and a list of people who this given user has requested to be buddies with.
747 function theme_buddylist_pending_requests($id) {
749 $thisuser = user_load(array('uid' => $id));
750 drupal_set_title(t('@username\'s @buddylist', array('@username' => $thisuser->name
) + buddylist_translation()));
752 return theme('box', t('Received requests'), buddylist_pending_requester_list($thisuser)) .
753 theme('box', t('Sent requests'), buddylist_pending_requested_list($thisuser));
757 * Returns a list of people who've requested to be added to the given user's buddylist
759 function buddylist_pending_requester_list(&$account) {
762 $viewing_own_account = ($user->uid
== $account->uid
);
763 $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
);
765 if (!db_num_rows($result)) {
766 $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>';
769 $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>';
771 $html_rows = array();
772 while ($row = db_fetch_object($result)) {
774 $html_row[] = theme('username', $row);
775 $html_row[] = drupal_get_form('buddylist_approval_form', $account->uid
, $row->uid
, $row->name
);
776 $html_rows[] = $html_row;
779 $output .
= theme('table', NULL
, $html_rows);
786 * Returns a list of user's who this given user has requested to be buddies with.
788 function buddylist_pending_requested_list(&$account) {
791 $viewing_own_account = ($user->uid
== $account->uid
);
792 $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
);
794 if (!db_num_rows($result)) {
795 $output = t('!Person !do_or_does not have any pending @buddy requests that !person !have_or_has made.',
797 '!Person' => ($viewing_own_account ?
t('You') : $account->name
),
798 '!do_or_does' => ($viewing_own_account ?
t('do') : t('does')),
799 '!have_or_has' => ($viewing_own_account ?
t('have') : t('has')),
800 '!person' => ($viewing_own_account ?
t('you') : $account->name
) ) + buddylist_translation());
803 $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());
805 $html_rows = array();
806 while ($row = db_fetch_object($result)) {
808 $html_row[] = theme('username', $row);
809 $html_row[] = drupal_get_form('buddylist_request_cancel_form', $row->uid
, $account->uid
);
810 $html_rows[] = $html_row;
813 $output .
= theme('table', NULL
, $html_rows);
820 function buddylist_approval_form($requestee_uid, $requester_uid) {
823 $form['requestee_uid'] = array(
825 '#value' => $requestee_uid,
828 $form['requester_uid'] = array(
830 '#value' => $requester_uid
833 $form['approve'] = array(
835 '#value' => t('Approve')
838 $form['deny'] = array(
840 '#value' => t('Deny'),
846 function buddylist_approval_form_submit($form_id, $form_values) {
847 $requestee_account = user_load(array('uid' => $form_values['requestee_uid'])); // most likely global user, unless admin looking
849 // Delete pending request from {buddylist_penging_requests}
850 db_query('DELETE FROM {buddylist_pending_requests} WHERE requestee_uid = %d AND requester_uid = %d', $form_values['requestee_uid'], $form_values['requester_uid']);
852 $requester_account = user_load(array('uid' => $form_values['requester_uid']));
854 if ($form_values['op'] == t('Approve')) {
855 // Make sure, for some weird reason, we don't already have these guys marked as buddies of each other in the database
856 $result = db_query('SELECT * FROM {buddylist} WHERE uid = %d AND buddy = %d', $form_values['requestee_uid'], $form_values['reqeuster_uid']);
860 if (!db_num_rows($result)) {
861 db_query('INSERT INTO {buddylist} (uid, buddy, timestamp, received) VALUES (%d, %d, %d, %d)', $form_values['requestee_uid'], $form_values['requester_uid'], $time, 1);
864 $result = db_query('SELECT * FROM {buddylist} WHERE uid = %d AND buddy = %d', $form_values['requester_uid'], $form_values['requestee_uid']);
865 if (!db_num_rows($result)) {
866 db_query('INSERT INTO {buddylist} (uid, buddy, timestamp, received) VALUES (%d, %d, %d, %d)', $form_values['requester_uid'], $form_values['requestee_uid'], $time, 1);
869 if (variable_get('buddylist_send_approval', FALSE
)) {
870 buddylist_mail_user('approval', $requester_account, $requestee_account);
873 drupal_set_message(t('Congratulations! !linktouser is now your @buddy.', array('!linktouser' => theme('username', $requester_account)) + buddylist_translation()));
876 drupal_set_message(t("!user's request to be your @buddy has been denied.", array('!user' => theme('username', $requester_account)) + buddylist_translation()));
880 function buddylist_request_cancel_form($requestee_uid, $requester_uid) {
881 $form['requestee_uid'] = array(
883 '#value' => $requestee_uid,
886 $form['requester_uid'] = array(
888 '#value' => $requester_uid
891 $form['cancel'] = array(
893 '#value' => t('Cancel')
899 function buddylist_request_cancel_form_submit($form_id, $form_values) {
900 db_query('DELETE FROM {buddylist_pending_requests} WHERE requester_uid = %d AND requestee_uid = %d', $form_values['requester_uid'], $form_values['requestee_uid']);
902 $former_potential_buddy = user_load(array('uid' => $form_values['requestee_uid']));
903 drupal_set_message(t('The request to add !user_name has been cancelled.', array('!user_name' => theme('username', $former_potential_buddy))));
905 return 'buddylist/'.
$form_values['requester_uid'] .
'/buddies/requests';
908 function buddylist_form_buddiesrecent_page($buddies) {
909 foreach ($buddies as
$user_id => $buddy) {
910 $form[] = array('#type' => 'fieldset',
911 '#title' => $buddy['name'],
912 '#collapsible' => 'true',
913 '#value' => tracker_page($user_id),
919 function buddylist_buddiesrecent_page($uid) {
922 $thisuser = user_load(array('uid' => $uid));
923 drupal_set_title(t('%username\'s @buddylist', array('%username' => $thisuser->name
) + buddylist_translation()));
925 $buddies = buddylist_get_buddies($uid);
926 $output .
= drupal_get_form('buddylist_form_buddiesrecent_page', $buddies);
927 $output .
= theme('xml_icon', url('buddylist/'.
$uid .
'/buddies/recent/feed'));
928 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') .
'" />');
933 function buddylist_buddiesgroups_page($uid) {
935 $thisuser = user_load(array('uid' => $uid));
936 drupal_set_title(t('%username\'s @buddy groups', array('%username' => $thisuser->name
) + buddylist_translation()));
938 $headers = array(t('@buddy', buddylist_translation()), t('online'), t('# of @buddies', buddylist_translation()), t("@buddy's posts", buddylist_translation()));
940 $result = db_query('SELECT label, label_id FROM {buddylist_groups} WHERE uid = %d ORDER BY label ASC', $thisuser->uid
);
942 while ($row = db_fetch_object($result)) {
943 $groups[$row->label_id
] = $row->label
;
946 if (count($groups) == 0) {
947 if ($thisuser->uid
== $user->uid
) {
949 t("To organize your @buddies into groups, visit the !edit_groups page",
950 array('!edit_groups' =>
951 l(t('edit groups'), "buddylist/$uid/buddies/groups/edit")) + buddylist_translation())
954 return t('No groups found.');
957 $online_interval = time() - variable_get('user_block_seconds_online', 180);
958 $buddies = buddylist_get_buddies($thisuser->uid
);
960 foreach ($groups as
$label_id => $label) {
961 $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);
963 while ($row = db_fetch_object($result)) {
964 $online = $row->access
> $online_interval;
966 theme('username', (object)$buddies[$row->buddy
]),
967 theme('buddylist_online', $online),
968 buddylist_count_buddies($row->buddy
),
969 l(t('view posts'), 'user/'.
$row->buddy.
'/track')
972 $output .
= theme('box', check_plain($label), $rows ?
theme('table', $headers, $rows) : t('Group is empty.'));
977 function buddylist_count_buddies($uid) {
978 $result = db_query("SELECT count(DISTINCT buddy) AS buddies FROM {buddylist} WHERE uid = %d", $uid);
979 return db_result($result);
983 function buddylist_get_buddy_groups($uid, $buddy = NULL
) {
985 $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);
988 $result = db_query("SELECT * FROM {buddylist_groups} ", $uid);
990 $buddy_groups = array();
991 while ($row = db_fetch_array($result)) {
992 $buddy_groups[$row['label_id']] = $buddy ?
$row : $row['label'];
994 return $buddy_groups;
998 function buddylist_form_edit_groups_add() {
1000 $form['add_group'] = array(
1001 '#type' => 'textfield',
1002 '#title' => t('Add new group'),
1003 '#description' => t('Groups are a way to keep your @buddies organized. Groups can be named whatever you like.', buddylist_translation()),
1006 $form['submit'] = array(
1007 '#type' => 'submit',
1008 '#value' => t('Add'),
1014 function buddylist_form_edit_groups_remove($all_groups) {
1015 // Make a form to remove groups
1016 $form['remove']['groups'] = array(
1017 '#type' => 'checkboxes',
1018 '#return_value' => 1,
1020 '#default_value' => null
,
1021 '#options' => array_map('check_plain', $all_groups),
1024 $form['remove']['submit'] = array(
1025 '#type' => 'submit',
1026 '#value' => t('Remove'),
1032 function buddylist_form_edit_groups_table($buddies, $all_groups, $thisuser) {
1033 // Build the table with buddies and their groups
1034 $form['table']['groups'] = array ('#tree' => true
);
1035 foreach ($buddies as
$uid => $buddy) {
1037 foreach ($buddy['groups'] as
$group) {
1038 $items[] = $group['label_id'];
1041 if (count($all_groups) > 0) {
1042 $form['table']['groups'][$uid] = array(
1043 '#type' => 'checkboxes',
1045 '#default_value' => $items,
1046 '#options' => array_map('check_plain', $all_groups),
1051 $form['table']['user'] = array(
1053 '#value' => $thisuser->uid
,
1056 if (count($form['table']['groups']) > 0) {
1057 $form['table']['submit'] = array(
1058 '#type' => 'submit',
1059 '#value' => t('Submit'),
1067 * Callback for the buddygroup editing page
1069 function buddylist_buddiesgroups_edit($uid) {
1070 $thisuser = user_load(array('uid' => $uid));
1071 drupal_set_title(t('%username\'s @buddy groups', array('%username' => $thisuser->name
) + buddylist_translation()));
1073 if ($buddies = buddylist_get_buddies($thisuser->uid
)) {
1074 $output['add'] = drupal_get_form('buddylist_form_edit_groups_add');
1076 $groups = buddylist_get_buddy_groups($uid);
1078 if (count($groups) > 0) {
1079 $output['remove'] = drupal_get_form('buddylist_form_edit_groups_remove', $groups);
1080 $output['table'] = drupal_get_form('buddylist_form_edit_groups_table', $buddies, $groups, $thisuser);
1083 drupal_set_message(t("You don't have any groups defined."));
1086 return theme('buddylist_edit_groups', $output);
1089 drupal_set_message(t('Unable to edit @buddy groups. Add @buddies to your @buddylist before making groups.', buddylist_translation()));
1090 return t('No @buddies found.', buddylist_translation());
1094 function theme_buddylist_edit_groups($forms) {
1095 $output = '<span id="buddylist-group-add-form">'.
1098 '<span id="buddylist-group-remove-form">'.
1101 '<span id="buddlist-groups-form">'.
1107 function theme_buddylist_form_edit_groups_table($form) {
1110 foreach ($form['table']['groups'] as
$key => $value) {
1111 if(is_numeric($key)) {
1112 $rows[] = array(theme('username', user_load(array('uid' => $key))), drupal_render($form['table']['groups'][$key]));
1116 $headers = array(t('buddy'), t('@buddy groups', buddylist_translation()));
1117 $output .
= theme('table', $headers, $rows);
1119 $output .
= drupal_render($form);
1124 function theme_buddylist_status_block($count, $received_count, $sent_count) {
1127 $stats .
= "<div class='buddylist-status'>";
1128 $stats .
= "<div class='total'>" .
l(format_plural($count, '@count buddy', '@count buddies'), 'buddylist') .
"</div>";
1129 $stats .
= "<div class='pending_received'>" .
l(format_plural($received_count, '@count pending received request', '@count pending received requests'), 'buddylist/'.
$user->uid .
'/buddies/requests') .
"</div>";
1130 $stats .
= "<div class='pending_sent'>" .
l(format_plural($sent_count, '@count pending sent request', '@count pending sent requests'), 'buddylist/'.
$user->uid .
'/buddies/requests') .
"</div>";
1136 function buddylist_form_edit_groups_add_submit($form_id, $form_values) {
1138 $label_id = buddylist_buddygroup_new($user->uid
, $form_values['add_group']);
1141 function buddylist_form_edit_groups_remove_submit($form_id, $form_values) {
1143 foreach ($form_values['groups'] as
$label_id => $remove) {
1145 buddylist_buddygroup_remove($user->uid
, $label_id);
1150 function buddylist_form_edit_groups_table_submit($form_id, $form_values) {
1151 $userid = $form_values['user'];
1152 foreach ($form_values['groups'] as
$buddy => $groups) {
1153 foreach ($groups as
$label_id => $checked) {
1154 if ($checked == 0) {
1155 buddylist_buddygroup_remove_buddy($userid, $buddy, $label_id);
1158 buddylist_buddygroup_add_buddy($userid, $buddy, $label_id);
1163 drupal_set_message(t('@buddy groups saved.', buddylist_translation()));
1167 * Creates a new buddy group for a user
1169 * @param $uid user id of the user to whom the group will belong.
1170 * @param $group string; name of the group
1171 * @param $visible determines whether the user's buddies can see which groups they've been put in.
1173 * @return $label_id the existing or newly created id for the name of this group.
1175 function buddylist_buddygroup_new($uid, $group, $visible = TRUE
) {
1176 $label_id = db_result(db_query("SELECT label_id FROM {buddylist_groups} WHERE uid = %d AND label = '%s'", $uid, $group));
1177 if ($label_id === FALSE
) {
1178 $new_label_id = db_next_id('buddygroup');
1179 db_query("INSERT INTO {buddylist_groups} VALUES (%d, %d, '%s', %d)", $uid, $new_label_id, $group, $visible);
1180 return $new_label_id;
1188 * Removes a buddy group for a user
1190 * @param $uid user id of the user to whom the group belongs.
1191 * @param $label_id id of the group
1193 function buddylist_buddygroup_remove($uid, $label_id) {
1194 db_query("DELETE FROM {buddylist_groups} WHERE uid = %d AND label_id = %d", $uid, $label_id);
1195 db_query("DELETE FROM {buddylist_buddy_group} WHERE uid = %d AND label_id = %d", $uid, $label_id);
1198 function buddylist_buddygroup_remove_buddy($uid, $buddy, $label_id) {
1199 db_query("DELETE FROM {buddylist_buddy_group} WHERE uid = %d AND buddy = %d AND label_id = %d", $uid, $buddy, $label_id);
1202 function buddylist_buddygroup_add_buddy($uid, $buddy, $label_id) {
1203 db_lock_table('buddylist_buddy_group');
1204 buddylist_buddygroup_remove_buddy($uid, $buddy, $label_id);
1205 db_query('INSERT INTO {buddylist_buddy_group} VALUES (%d, %d, %d)', $uid, $buddy, $label_id);
1210 * Feed for buddies recent posts
1212 function buddylist_buddyfeed($uid) {
1213 if (!(is_numeric($uid) && $uid > 0)) {
1214 return drupal_not_found();
1218 $buddy_ids = array_keys(buddylist_get_buddies($uid));
1220 // false query to be used if no posts from buddies are available (as in this user has no buddies).
1221 $result = db_query('SELECT nid FROM {node} WHERE 0');
1222 if (count($buddy_ids)) {
1223 $buddy_ids_str = '('.
implode(',', $buddy_ids).
')';
1224 $result = db_query(db_rewrite_sql('SELECT nid FROM {node} WHERE status = 1 AND uid IN %s ORDER BY nid DESC'), $buddy_ids_str);
1226 $channel['title'] = t('@buddies recent posts on %site', array('%site' => variable_get('site_name', 'drupal')) + buddylist_translation());
1227 $channel['link'] = url('buddylist/'.
$uid .
'/buddies/recent', NULL
, NULL
, TRUE
);
1229 node_feed($result, $channel);
1232 function buddylist_addbuddy($uid) {
1234 $buddy = user_load(array('uid' => $uid));
1236 if (empty($buddy->name
)) {
1237 drupal_set_message(t('This user does not exist'));
1239 elseif (in_array($uid, array_keys(buddylist_get_buddies($user->uid
)))) {
1240 drupal_set_message(t('This user is already on your @buddy list', buddylist_translation()));
1242 elseif ($user->uid
== $uid) {
1243 drupal_set_message(t('Cannot add yourself to @buddy list', buddylist_translation()));
1246 $form['uid'] = array('#type' => 'hidden', '#value' => $uid);
1247 $form['name'] = array('#type' => 'hidden', '#value' => $buddy->name
);
1248 $output = confirm_form(
1250 t('Add user %name to your @buddy list?', array('%name' => $buddy->name
) + buddylist_translation()),
1251 $_GET['destination'],
1253 t('Add'), t('Cancel'),
1254 'buddylist_addbuddy_confirm');
1259 //function buddylist_form_alter($a, $b) {dprint_r($a); dprint_r($b);}
1262 * Confirm and add a buddy.
1264 function buddylist_addbuddy_submit($form_id, $form_values) {
1265 if (variable_get('buddylist_require_approval', 0)) {
1266 buddylist_add_request($form_values['uid']);
1269 buddylist_add($form_values['uid']);
1270 drupal_set_message(t('%name will be be notified the next time s/he logs in.', array('%name' => $form_values['name'])));
1276 * Removes the user $uid from the global user's account.
1277 * TODO: generalize this so that two uids can be given
1279 function buddylist_deletebuddy($uid) {
1281 $buddy = user_load(array('uid' => $uid));
1283 if (empty($buddy->name
)) {
1284 drupal_set_message('This user does not exist');
1286 else if (!in_array($uid, array_keys(buddylist_get_buddies($user->uid
)))) {
1287 drupal_set_message('This user is not on your @buddy list', buddylist_translation());
1290 $form['uid'] = array('#type' => 'hidden', '#value' => $uid);
1291 $form['name'] = array('#type' => 'hidden', '#value' => $buddy->name
);
1292 $output = confirm_form(
1294 t('Remove user %name from your @buddy list?', array('%name' => $buddy->name
) + buddylist_translation()),
1295 $_GET['destination'],
1297 t('Remove'), t('Cancel'),
1298 'buddylist_deletebuddy_confirm');
1305 * Confirm and add a buddy.
1307 function buddylist_deletebuddy_submit($form_id, $form_values) {
1308 buddylist_remove($form_values['uid']);
1309 drupal_set_message(t('@name will be be notified of being removed.', array('@name' => $form_values['name'])));
1313 function buddylist_add($id) {
1315 $user_to_add = user_load(array('uid' => $id));
1317 if (!in_array($id, array_keys(buddylist_get_buddies($user->uid
)))) {
1318 db_query('INSERT INTO {buddylist} (received, uid, buddy, timestamp) VALUES (1, %d, %d, %d)' , $user->uid
, $id , time());
1319 // DB value buddylist.received set to 1, meaning buddy has a message waiting
1320 // letting them know you added them as a buddy
1321 // buddylist.received set back to 0 when user logs in along with being informed of new buddy
1322 if (variable_get('buddylist_send_add', FALSE
)) {
1323 buddylist_mail_user('add', $user_to_add);
1325 drupal_set_message(t('%username has been added to your @buddy list', array('%username' => $user_to_add->name
) + buddylist_translation()));
1328 drupal_set_message(t('%username is already on your @buddylist', array('%username' => $user_to_add->name
) + buddylist_translation()));
1332 function buddylist_add_request($id) {
1334 $user_to_add = user_load(array('uid' => $id));
1336 $already_requested = in_array($id, array_keys(buddylist_get_requestees($user->uid
)));
1337 $already_buddies = in_array($id, array_keys(buddylist_get_buddies($user->uid
)));
1339 if (!$already_requested && !$already_buddies) {
1340 db_query('INSERT INTO {buddylist_pending_requests} (requester_uid, requestee_uid, received) VALUES (%d, %d, %d)', $user->uid
, $id, 0);
1342 if (variable_get('buddylist_send_request', FALSE
)) {
1343 buddylist_mail_user('request', $user_to_add);
1345 drupal_set_message(t('Your request to add %username to your @buddy list has been submitted. %username will be notified.', array('%username' => $user_to_add->name
) + buddylist_translation()));
1348 if ($already_requested) {
1349 drupal_set_message(t('You have already requested to add %username to your @buddylist', array('%username' => $user_to_add->name
) + buddylist_translation()));
1352 if ($already_buddies) {
1353 drupal_set_message(t('%username is already on your @buddylist', array('%username' => $user_to_add->name
) + buddylist_translation()));
1358 function buddylist_remove($id) {
1360 db_query('DELETE FROM {buddylist} WHERE uid = %d AND buddy = %d' , $user->uid
, $id);
1361 db_query('DELETE FROM {buddylist_buddy_group} WHERE uid = %d AND buddy = %d' , $user->uid
, $id);
1362 $thisuser = user_load(array('uid' => $id));
1363 if (variable_get('buddylist_send_remove', FALSE
)) {
1364 buddylist_mail_user('remove', $thisuser);
1366 drupal_set_message(t('%username has been removed from your @buddylist', array('%username' => $thisuser->name
) + buddylist_translation()));
1368 if (variable_get('buddylist_require_approval', 0)) {
1369 db_query('DELETE FROM {buddylist} WHERE uid = %d AND buddy = %d', $id, $user->uid
);
1370 db_query('DELETE FROM {buddylist_buddy_group} WHERE uid = %d AND buddy = %d', $id, $user->uid
);
1374 function buddylist_cancel_add($id) {
1375 $thisuser = user_load(array('uid' => $id));
1376 drupal_set_message(t('User %name was NOT added to your @buddylist.', array('%name' => $thisuser->name
) + buddylist_translation()));
1379 function buddylist_cancel_remove($id) {
1380 $thisuser = user_load(array('uid' => $id));
1381 drupal_set_message(t('User %name was NOT removed from your @buddylist.', array('%name' => $thisuser->name
) + buddylist_translation()));
1384 function theme_buddylist_online($online) {
1385 return $online ?
t('yes') : t('no');
1389 * Sends mail to a buddy, which is added/removed/deleted/requested by $user
1391 function buddylist_mail_user($op, $buddy, $user = NULL
) {
1393 if (variable_get('buddylist_user_mail', FALSE
) && isset($buddy->buddylist_mail
) && !$buddy->buddylist_mail
) {
1394 return; //the user has turned mails off
1397 if (is_null($user)){
1402 $subject = BUDDYLIST_ADD_SUBJECT
;
1403 $message = variable_get('buddylist_add_message', buddylist_mail_add_default());
1406 $subject = BUDDYLIST_REMOVE_SUBJECT
;
1407 $message = variable_get('buddylist_remove_message', buddylist_mail_remove_default());
1410 $subject = BUDDYLIST_REQUEST_SUBJECT
;
1411 $message = variable_get('buddylist_request_message', buddylist_mail_request_default());
1414 $subject = BUDDYLIST_APPROVAL_SUBJECT
;
1415 $message = variable_get('buddylist_approval_message', buddylist_mail_approval_default());
1419 $replacements = buddylist_translation() + (($op == 'approval') ?
buddylist_approval_mail_replacements($buddy, $user) : buddylist_mail_replacements($buddy, $user));
1420 $subject = t($subject, $replacements);
1421 $message = t($message, $replacements);
1423 $site_mail = variable_get('site_mail', "");
1424 if (!strlen($site_mail)) {
1425 if (user_access('administer site configuration')){
1426 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');
1428 $site_mail = 'nobody@localhost';
1432 if ($op != 'approval') {
1433 if (drupal_mail("buddylist_mail_user_$op", $buddy->mail, $subject, $message, $site_mail)) {
1434 $message = t('%type message was sent to %username', array('%type' => $op, '%username' => $buddy->name
));
1435 watchdog('buddylist', $message);
1438 $message = t('There was a problem sending the %type message to %username', array('%type' => $op, '%username' => $buddy->name
));
1439 watchdog('buddylist', $message, WATCHDOG_WARNING
);
1443 if (drupal_mail("buddylist_mail_user_$op", $buddy->mail, $subject, $message, $site_mail)) {
1444 $message = t('%type message was sent to %username', array('%type' => $op, '%username' => $buddy->name
));
1445 watchdog('buddylist', $message);
1448 $message = t('There was a problem sending the %type message to %username', array('%type' => $op, '%username' => $buddy->name
));
1449 watchdog('buddylist', $message, WATCHDOG_WARNING
);
1454 function buddylist_mail_add_default() {
1458 You are @adder_name
's newest @buddy.
1460 Here's a
link to @adder_name
's profile. If you'd like
, you can add them as one of your @buddies
:
1469 function buddylist_mail_remove_default() {
1473 You have been removed from @adder_name
's @buddy list.
1475 Here's a
link to @adder_name
's profile:
1479 Enjoy your new freedom!
1486 function buddylist_mail_request_default() {
1490 @adder_name has requested to add you to his/her @buddy list.
1492 Here's a
link to @adder_name
's profile:
1496 To approve/deny this request, log in to @siteurl and see your pending @buddy requests
1497 at @pending_requests_link.
1499 Enjoy your new freedom!
1506 function buddylist_mail_approval_default() {
1510 @addee_name has approved your request to join his/her @buddy list.
1512 Here's a
link to your buddylist
:
1516 Enjoy your new freedom
!
1524 function buddylist_mail_replacements(&$buddy, &$user){
1526 '@adder_name' => $user->name
,
1527 '@adder_link' => url("user/".
$user->uid
, NULL
, NULL
, TRUE
),
1528 '@adder_uid' => $user->uid
,
1529 '@addee_name' => $buddy->name
,
1530 '@addee_link' => url("user/".
$buddy->uid
, NULL
, NULL
, TRUE
),
1531 '@addee_uid' => $buddy->uid
,
1532 '@site' => variable_get("site_name", "Drupal"),
1533 '@siteurl' => $GLOBALS["base_url"],
1534 '@adder_list_link' => url("buddylist/".
$user->uid .
"/buddies/list", NULL
, NULL
, TRUE
),
1535 '@pending_requests_link' => url("buddylist/".
$buddy->uid .
"/buddies/requests", NULL
, NULL
, TRUE
),
1541 function buddylist_approval_mail_replacements(&$buddy, &$user) {
1543 '@adder_name' => $user->name
,
1544 '@adder_link' => url("user/".
$user->uid
, NULL
, NULL
, TRUE
),
1545 '@adder_uid' => $user->uid
,
1546 '@addee_name' => $buddy->name
,
1547 '@addee_link' => url("user/".
$buddy->uid
, NULL
, NULL
, TRUE
),
1548 '@addee_uid' => $buddy->uid
,
1549 '@site' => variable_get("site_name", "Drupal"),
1550 '@siteurl' => $GLOBALS["base_url"],
1551 '@adder_list_link' => url("buddylist/".
$user->uid .
"/buddies/list", NULL
, NULL
, TRUE
),