Stripping CVS keywords
[project/buddylist.git] / buddylist.module
1 <?php
2
3 if (module_exists('views')) {
4 include_once(drupal_get_path('module', 'buddylist'). '/buddylist_views.inc');
5 }
6
7
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"));
12
13 /**
14 * returns an array of common translation placeholders
15 */
16 function buddylist_translation() {
17 $translations = array(
18 '@buddy' => t('buddy'),
19 '@Buddy' => t('Buddy'),
20
21 '@buddylist' => t('buddylist'),
22 '@Buddylist' => t('Buddylist'),
23
24 '@buddies' => t('buddies'),
25 '@Buddies' => t('Buddies'),
26
27 '@buddyof' => t('buddy of'),
28 '@Buddyof' => t('Buddy of'),
29 );
30 return variable_get('buddylist_translation', $translations);
31 }
32
33
34 /**
35 * Implementation of hook_help
36 */
37 function buddylist_help($section) {
38 switch ($section) {
39 case 'admin/help#buddylist':
40 $output = t("
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>
48 <p>You can:</p>
49 <ul>
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 &raquo; access control</a></li>
52 <li>enable the @buddylist block at <a href=\"@blockadministration\" title=\"block administration\">administer &raquo; block</a></li>
53 <li>administer the @buddylist block at <a href=\"@buddylistsettings\" title=\"@buddylist settings\">administer &raquo; settings &raquo; @buddylist</a></li>
54 </ul>
55
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());
62 return $output;
63 }
64 }
65
66
67 /**
68 * Implementation of hook_perm
69 */
70 function buddylist_perm() {
71 return array('maintain buddy list', 'view buddy lists');
72 }
73
74 /**
75 * Implementation of hook_menu
76 */
77 function buddylist_menu($may_cache) {
78 global $user;
79
80 $items = array();
81 $id = is_numeric(arg(1)) ? arg(1) : $user->uid;
82
83 if ($may_cache) {
84 // buddylist settings page
85 $items[] = array(
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'),
92 );
93 // my buddylist menu item
94 $items[] = array(
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'),
100 );
101 }
102 else {
103 // 'edit access' only granted to user's own buddy list or to administrative users
104 $editAccess = (
105 ($id == $user->uid && user_access('maintain buddy list') && $user->uid)
106 || user_access('administer users'));
107
108 $approval_required = variable_get('buddylist_require_approval', 0);
109
110 $items[] = array(
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,
117 );
118 $items[] = array(
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,
125 );
126
127 // 'view only' tabs
128 $viewAccess = (($id == $user->uid && user_access('maintain buddy list')) || user_access('view buddy lists'));
129
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) {
133 $items[] = array(
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,
139 'weight' => -1,
140 'callback arguments' => array($id)
141 );
142 $items[] = array(
143 'path' => 'buddylist/'. $id .'/buddyof',
144 'title' => t('@Buddyof', buddylist_translation()),
145 'access' => $viewAccess,
146 'callback' => 'buddylist_buddylisting_page',
147 'type' => MENU_LOCAL_TASK,
148 'weight' => 1,
149 'callback arguments' => array($id, 'buddyof')
150 );
151 }
152
153 // subtabs
154 $items[] = array(
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,
160 'weight' => -1,
161 'callback arguments' => array($id),
162 );
163 $items[] = array(
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,
169 'weight' => 3,
170 'callback arguments' => array($id),
171 );
172 if (variable_get('buddylist_buddygroups', FALSE)) {
173 $items[] = array(
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,
179 'weight' => 5,
180 'callback arguments' => array($id),
181 );
182 $items[] = array(
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,
188 'weight' => 6,
189 'callback arguments' => array($id),
190 );
191 }
192
193 // sub-subtabs
194 if ($approval_required && $editAccess) {
195 $items[] = array(
196 'path' => 'buddylist/'. $id .'/buddies/requests',
197 'title' => t('Pending requests'),
198 'access' => $editAccess,
199 'callback' => 'theme',
200 'type' => MENU_LOCAL_TASK,
201 'weight' => 0,
202 'callback arguments' => array('buddylist_pending_requests', $id)
203 );
204
205 $items[] = array(
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),
212 );
213
214 $items[] = array(
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),
221 );
222
223 $items[] = array(
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),
230 );
231
232 }
233
234 // other callbacks
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.
238 $items[] = array(
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),
245 );
246 }
247 $items[] = array(
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),
254 );
255 }
256
257 return $items;
258 }
259
260 /**
261 * Buddylist administration settings page
262 */
263 function buddylist_admin_settings() {
264 $form['general'] = array(
265 '#type' => 'fieldset',
266 '#title' => t('General settings'),
267 );
268
269 $form['general']['buddylist_require_approval'] = array(
270 '#type' => 'radios',
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'))
275 );
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),
281 );
282
283 // User profile page settings
284 $form['profile_settings'] = array(
285 '#type' => 'fieldset',
286 '#title' => t('Profile page options'),
287 );
288 $form['profile_settings']['buddylist_prof_buddies'] = array(
289 '#type' => 'select',
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()),
294 );
295
296 // TODO: move these to block settings
297 $form['block_settings'] = array(
298 '#type' => 'fieldset',
299 '#title' => t('@Buddylist block options', buddylist_translation()),
300 );
301 $form['block_settings']['buddylist_blocklisting_size'] = array(
302 '#type' => 'select',
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()),
307 );
308 $form['block_settings']['buddylist_posts_block'] = array(
309 '#type' => 'select',
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()),
314 );
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())),
319 '#size' => 70,
320 '#maxlength' => 128,
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()),
322 );
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())),
327 '#size' => 70,
328 '#maxlength' => 128,
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()),
330 );
331 $form['block_settings']['buddylist_list_block_title'] = array(
332 '#type' => 'textfield',
333 '#title' => t('"My @buddies list" block title', buddylist_translation()),
334 '#weight' => 1,
335 '#default_value' => variable_get('buddylist_list_block_title', t('My @buddylist', buddylist_translation())),
336 '#size' => 70,
337 '#maxlength' => 128,
338 '#description' => t('This will be the title for the "My @buddylist" block. If none is specified, "My @buddylist" will be used.', buddylist_translation()),
339 );
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()),
343 '#weight' => 2,
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()),
346 );
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()),
350 '#weight' => 3,
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()),
353 );
354
355 $form['mail'] = array(
356 '#type' => 'fieldset',
357 '#title' => t('email'),
358 );
359
360 global $user;
361 $macros = implode(', ', array_keys(buddylist_mail_replacements($user, $user)));
362 $approval_macros = implode(', ', array_keys(buddylist_approval_mail_replacements($user, $user)));
363
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.'),
369 );
370
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),
375 );
376
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,
381 );
382
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)),
388 );
389
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),
394 );
395
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,
400 );
401
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)),
407 );
408
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)
414 );
415
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,
420 );
421
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)),
427 );
428
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())
434 );
435
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,
440 );
441
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)),
447 );
448
449 return system_settings_form($form);
450 }
451
452
453 /**
454 * Implementation of hook_user
455 */
456 function buddylist_user($type, &$edit, &$thisuser, $category = NULL) {
457 global $user;
458
459 $output = array();
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);
463 }
464 if ($type == 'view') {
465 if ($list = buddylist_get_buddylist($thisuser)) {
466 $output[] = array('title' => t('@Buddies', buddylist_translation()), 'value' => $list, 'class' => 'buddylist');
467 }
468 if ($list = buddylist_get_buddylist($thisuser, TRUE)) {
469 $output[] = array('title' => t('@buddy of', buddylist_translation()), 'value' => $list, 'class' => 'buddyoflist');
470 }
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');
473 }
474 if(count($output) > 0) {
475 return array(t('@Buddy List', buddylist_translation()) => $output);
476 }
477 }
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);
483 }
484 else if ($type == 'load') {
485 $thisuser->buddies = buddylist_get_buddies($thisuser->uid);
486 }
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'),
492 '#weight' => 5);
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()));
498 return $form;
499 }
500 }
501
502 /*
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.
505 */
506 function buddylist_get_buddylist($user, $buddy_of = FALSE) {
507
508 if (user_access('view buddy lists') && !$buddy_of) {
509 $i = 0;
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;
514 $i++;
515 if ($i > variable_get('buddylist_prof_buddies', 5)) {
516 break;
517 }
518 }
519 return theme('user_list', $listbuddies);
520 }
521 }
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;
531 }
532 if ($listbuddyof) {
533 return theme('user_list', $listbuddyof);
534 }
535 }
536 }
537
538
539 /*
540 * Returns an array of posible actions (html) for the viewing user,
541 * e.g. a link to make the viewed user a buddy
542 */
543 function buddylist_get_buddy_actions(&$viewing_user, &$viewed_user) {
544
545 $actions = array();
546 if (!user_access('maintain buddy list') || $viewing_user->uid == $viewed_user->uid) {
547 return $actions;
548 }
549
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());
552 }
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);
555 }
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);
559 }
560 else {
561 $actions[] = theme('add_to_buddylist_link', $viewed_user);
562 }
563
564 return $actions;
565 }
566
567
568 /**
569 * Implementation for hook_block
570 */
571 function buddylist_block($op = 'list', $delta = 0) {
572 global $user;
573
574 if ($op == 'list') {
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()));
579 }
580 return $block;
581 }
582 else if ($op == 'view' && user_access('access content') && user_access('maintain buddy list') && $user->uid > 0) {
583 switch ($delta) {
584 case 0 : // Shows buddylist block
585 if ($buddies = buddylist_get_buddies()) {
586 // we have buddies defined and generate the list
587 $i = 0;
588 foreach (array_keys($buddies) as $buddy) {
589 $users[] = user_load(array('uid' => $buddy));
590 $i++;
591 if ($i == variable_get('buddylist_blocklisting_size', 5)) {
592 break;
593 }
594 }
595 $block['content'] = theme('user_list', $users);
596 } else {
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()));
601 } else {
602 // If no buddies defined and no message available, end 'case' without returning block.
603 break;
604 }
605 }
606 // this is the same output whether buddylist or not
607 $block['subject'] = variable_get('buddylist_list_block_title', t('My @buddylist', buddylist_translation()));
608
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>';
612 }
613 return $block;
614 break;
615
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));
622
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);
626
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);
630
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>';
633 }
634 return $block;
635 }
636 }
637 break;
638
639 case 2: // Buddylist status
640 $block['subject'] = variable_get('buddylist_status_block_title', t('My @buddy status', buddylist_translation()));
641
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));
645
646 $block['content'] = theme('buddylist_status_block', $count, $received, $sent);
647 return $block;
648 break;
649 }
650 }
651 }
652
653 /**
654 * Public API for retrieving buddies. Feel free to use this from other
655 * modules.
656 * $key can be 'uid' or 'label'.
657 */
658 function buddylist_get_buddies($uid = NULL, $key = 'uid') {
659 static $buddies;
660
661 if (!$uid) {
662 global $user;
663 $uid = $user->uid;
664 }
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
669 WHERE b.uid = %d';
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.",";
678 }
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;
685 }
686 }
687 }
688
689 return $buddies[$key][$uid];
690 }
691
692 /**
693 * Returns an array of uid => name of people that user with param $uid has made a buddy request to
694 */
695 function buddylist_get_requestees($uid) {
696 $buddies = array();
697
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);
699
700 while ($row = db_fetch_object($result)) {
701 $buddies[$row->requestee_uid] = $row->name;
702 }
703
704 return $buddies;
705 }
706
707 function buddylist_setmsg_received($thisuser) {
708 global $user;
709
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;
717 }
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));
720 }
721 }
722 else {
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);
729 }
730 }
731 }
732 }
733
734 /**
735 * expose add and remove links to theming.
736 */
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);
739 }
740
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);
743 }
744
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);
747 }
748
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);
751 }
752
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);
755 }
756
757 /**
758 * Displays a list of a given user's buddies.
759 */
760 function buddylist_buddylisting_page($uid = NULL, $mode = 'buddies') {
761 global $user;
762
763 if (empty($uid)) {
764 $uid = $user->uid;
765 }
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)))) {
768 drupal_not_found();
769 exit();
770 }
771
772 $viewing_own_account = ($user->uid == $uid);
773
774 if (user_access('maintain buddy list') && $viewing_own_account) {
775 buddylist_setmsg_received($thisuser);
776 }
777
778 drupal_set_title(t('%username\'s @buddylist', array('%username' => $thisuser->name) + buddylist_translation()));
779
780 $buddies_per_page = 20;
781
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";
785 }
786 else {
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";
788 }
789 $result = pager_query($sql, $buddies_per_page, 0 , NULL, $uid);
790
791 $header = array(t('@buddy', buddylist_translation()), t('online'));
792 $online_interval = time() - variable_get('user_block_seconds_online', 180);
793
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));
798 }
799 $output .= theme('table', $header, $rows);
800 }
801 else {
802 $output .= variable_get('buddylist_empty_text', t(buddylist_empty_text_default(), buddylist_translation()));
803 }
804
805 $output .= theme('pager', NULL, $buddies_per_page);
806
807 return $output;
808 }
809
810 /**
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.
813 */
814 function theme_buddylist_pending_requests($id) {
815
816 $thisuser = user_load(array('uid' => $id));
817 drupal_set_title(t('@username\'s @buddylist', array('@username' => $thisuser->name) + buddylist_translation()));
818
819 return theme('box', t('Received requests'), buddylist_pending_requester_list($thisuser)) .
820 theme('box', t('Sent requests'), buddylist_pending_requested_list($thisuser));
821 }
822
823 /**
824 * Returns a list of people who've requested to be added to the given user's buddylist
825 */
826 function buddylist_pending_requester_list(&$account) {
827 global $user;
828
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);
831
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>';
834 }
835 else {
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>';
837
838 $html_rows = array();
839 while ($row = db_fetch_object($result)) {
840 $html_row = array();
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;
844 }
845
846 $output .= theme('table', NULL, $html_rows);
847 }
848
849 return $output;
850 }
851
852 /**
853 * Returns a list of user's who this given user has requested to be buddies with.
854 */
855 function buddylist_pending_requested_list(&$account) {
856 global $user;
857
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);
860
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.',
863 array(
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());
868 }
869 else {
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());
871
872 $html_rows = array();
873 while ($row = db_fetch_object($result)) {
874 $html_row = array();
875 $html_row[] = theme('username', $row);
876 $html_row[] = theme('buddylist_sent_requests_cancel_link', $account->uid, $row->uid);
877 $html_rows[] = $html_row;
878 }
879
880 $output .= theme('table', NULL, $html_rows);
881 }
882
883 return $output;
884 }
885
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),
891 t('Accept Request'),
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))),
894 t('Yes'), t('No'),
895 'buddylist_request_accept_confirm');
896
897 return $output;
898 }
899
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),
905 t('Cancel Request'),
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))),
908 t('Yes'), t('No'),
909 'buddylist_cancel_request_confirm');
910
911 return $output;
912
913 }
914
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']);
917
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))));
920
921 return 'buddylist/'. $form_values['requester_uid'] .'/buddies/requests';
922 }
923
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),
929 t('Accept Request'),
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))),
932 t('Yes'), t('No'),
933 'buddylist_request_deny_confirm');
934
935 return $output;
936 }
937
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'];
941
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);
944
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';
949 }
950
951 db_query('DELETE FROM {buddylist_pending_requests} WHERE requestee_uid = %d AND requester_uid = %d', $requestee_account->uid, $requester_account->uid);
952
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);
955
956 $time = time();
957
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);
960 }
961
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);
965 }
966
967 if (variable_get('buddylist_send_approval', FALSE)) {
968 buddylist_mail_user('approval', $requester_account, $requestee_account);
969 }
970
971 drupal_set_message(t('Congratulations! !linktouser is now your buddy.', array('!linktouser' => theme('username', $requester_account)) + buddylist_translation()));
972
973 return 'buddylist/'. $requestee_account->uid .'/buddies/requests';
974 }
975
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'];
979
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);
982
983 drupal_set_message(t("!user's request to be your buddy has been denied.", array('!user' => theme('username', $requester_account)) + buddylist_translation()));
984
985 return 'buddylist/'. $requestee_account->uid .'/buddies/requests';
986 }
987
988 function buddylist_confirm_form($requester_account, $requestee_account) {
989 $form = array();
990
991 $form['requester_account'] = array(
992 '#type' => 'value',
993 '#value' => $requester_account
994 );
995
996 $form['requestee_account'] = array(
997 '#type' => 'value',
998 '#value' => $requestee_account
999 );
1000
1001 return $form;
1002 }
1003
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
1006
1007
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']);
1010
1011 $requester_account = user_load(array('uid' => $form_values['requester_uid']));
1012
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']);
1016
1017 $time = time();
1018
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);
1021 }
1022
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);
1026 }
1027
1028 if (variable_get('buddylist_send_approval', FALSE)) {
1029 buddylist_mail_user('approval', $requester_account, $requestee_account);
1030 }
1031
1032 drupal_set_message(t('Congratulations! !linktouser is now your @buddy.', array('!linktouser' => theme('username', $requester_account)) + buddylist_translation()));
1033 }
1034 else {
1035 drupal_set_message(t("!user's request to be your @buddy has been denied.", array('!user' => theme('username', $requester_account)) + buddylist_translation()));
1036 }
1037 }
1038
1039 function buddylist_approval_form($requestee_uid, $requester_uid) {
1040 $form = array();
1041
1042 $form['requestee_uid'] = array(
1043 '#type' => 'hidden',
1044 '#value' => $requestee_uid,
1045 );
1046
1047 $form['requester_uid'] = array(
1048 '#type' => 'hidden',
1049 '#value' => $requester_uid
1050 );
1051
1052 $form['approve'] = array(
1053 '#type' => 'submit',
1054 '#value' => t('Approve')
1055 );
1056
1057 $form['deny'] = array(
1058 '#type' => 'submit',
1059 '#value' => t('Deny'),
1060 );
1061
1062 return $form;
1063 }
1064
1065 function buddylist_request_cancel_form($requestee_uid, $requester_uid) {
1066 $form['requestee_uid'] = array(
1067 '#type' => 'hidden',
1068 '#value' => $requestee_uid,
1069 );
1070
1071 $form['requester_uid'] = array(
1072 '#type' => 'hidden',
1073 '#value' => $requester_uid
1074 );
1075
1076 return $form;
1077 }
1078
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']);
1081
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))));
1084
1085 return 'buddylist/'. $form_values['requester_uid'] .'/buddies/requests';
1086 }
1087
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),
1094 );
1095 }
1096 return $form;
1097 }
1098
1099 function buddylist_buddiesrecent_page($uid) {
1100 global $user;
1101
1102 $thisuser = user_load(array('uid' => $uid));
1103 drupal_set_title(t('%username\'s @buddylist', array('%username' => $thisuser->name) + buddylist_translation()));
1104
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') .'" />');
1109
1110 return $output;
1111 }
1112
1113 function buddylist_buddiesgroups_page($uid) {
1114 global $user;
1115 $thisuser = user_load(array('uid' => $uid));
1116 drupal_set_title(t('%username\'s @buddy groups', array('%username' => $thisuser->name) + buddylist_translation()));
1117
1118 $headers = array(t('@buddy', buddylist_translation()), t('online'), t('# of @buddies', buddylist_translation()), t("@buddy's posts", buddylist_translation()));
1119
1120 $result = db_query('SELECT label, label_id FROM {buddylist_groups} WHERE uid = %d ORDER BY label ASC', $thisuser->uid);
1121 $groups = array();
1122 while ($row = db_fetch_object($result)) {
1123 $groups[$row->label_id] = $row->label;
1124 }
1125
1126 if (count($groups) == 0) {
1127 if ($thisuser->uid == $user->uid) {
1128 drupal_set_message(
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())
1132 );
1133 }
1134 return t('No groups found.');
1135 }
1136
1137 $online_interval = time() - variable_get('user_block_seconds_online', 180);
1138 $buddies = buddylist_get_buddies($thisuser->uid);
1139
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);
1142 $rows = array();
1143 while ($row = db_fetch_object($result)) {
1144 $online = $row->access > $online_interval;
1145 $rows[] = array(
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')
1150 );
1151 }
1152 $output .= theme('box', check_plain($label), $rows ? theme('table', $headers, $rows) : t('Group is empty.'));
1153 }
1154 return $output;
1155 }
1156
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);
1160 }
1161
1162
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);
1166 }
1167 else {
1168 $result = db_query("SELECT * FROM {buddylist_groups} WHERE uid=%d", $uid);
1169 }
1170 $buddy_groups = array();
1171 while ($row = db_fetch_array($result)) {
1172 $buddy_groups[$row['label_id']] = $buddy ? $row : $row['label'];
1173 }
1174 return $buddy_groups;
1175 }
1176
1177
1178 function buddylist_form_edit_groups_add() {
1179 // Add group form
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()),
1184 );
1185
1186 $form['submit'] = array(
1187 '#type' => 'submit',
1188 '#value' => t('Add'),
1189 );
1190
1191 return $form;
1192 }
1193
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,
1199 '#title' => '',
1200 '#default_value' => null,
1201 '#options' => array_map('check_plain', $all_groups),
1202 );
1203
1204 $form['remove']['submit'] = array(
1205 '#type' => 'submit',
1206 '#value' => t('Remove'),
1207 );
1208
1209 return $form;
1210 }
1211
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) {
1216 $items = array();
1217 foreach ($buddy['groups'] as $group) {
1218 $items[] = $group['label_id'];
1219 }
1220
1221 if (count($all_groups) > 0) {
1222 $form['table']['groups'][$uid] = array(
1223 '#type' => 'checkboxes',
1224 '#title' => '',
1225 '#default_value' => $items,
1226 '#options' => array_map('check_plain', $all_groups),
1227 );
1228 }
1229 }
1230
1231 $form['table']['user'] = array(
1232 '#type' => 'value',
1233 '#value' => $thisuser->uid,
1234 );
1235
1236 if (count($form['table']['groups']) > 0) {
1237 $form['table']['submit'] = array(
1238 '#type' => 'submit',
1239 '#value' => t('Submit'),
1240 );
1241 }
1242
1243 return $form;
1244 }
1245
1246 /**
1247 * Callback for the buddygroup editing page
1248 */
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()));
1252
1253 if ($buddies = buddylist_get_buddies($thisuser->uid)) {
1254 $output['add'] = drupal_get_form('buddylist_form_edit_groups_add');
1255
1256 $groups = buddylist_get_buddy_groups($uid);
1257
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);
1261 }
1262 else {
1263 drupal_set_message(t("You don't have any groups defined."));
1264 }
1265
1266 return theme('buddylist_edit_groups', $output);
1267 }
1268 else {
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());
1271 }
1272 }
1273
1274 function theme_buddylist_edit_groups($forms) {
1275 $output = '<span id="buddylist-group-add-form">'.
1276 $forms['add'].
1277 '</span>'.
1278 '<span id="buddylist-group-remove-form">'.
1279 $forms['remove'].
1280 '</span>'.
1281 '<span id="buddlist-groups-form">'.
1282 $forms['table'].
1283 '</span>';
1284 return $output;
1285 }
1286
1287 function theme_buddylist_form_edit_groups_table($form) {
1288 $rows = array();
1289
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]));
1293 }
1294 }
1295
1296 $headers = array(t('buddy'), t('@buddy groups', buddylist_translation()));
1297 $output .= theme('table', $headers, $rows);
1298
1299 $output .= drupal_render($form);
1300
1301 return $output;
1302 }
1303
1304 function theme_buddylist_status_block($count, $received_count, $sent_count) {
1305 global $user;
1306
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>";
1311 $stats .= "</div>";
1312
1313 return $stats;
1314 }
1315
1316 function buddylist_form_edit_groups_add_submit($form_id, $form_values) {
1317 global $user;
1318 $label_id = buddylist_buddygroup_new($user->uid, $form_values['add_group']);
1319 }
1320
1321 function buddylist_form_edit_groups_remove_submit($form_id, $form_values) {
1322 global $user;
1323 foreach ($form_values['groups'] as $label_id => $remove) {
1324 if ($remove > 0) {
1325 buddylist_buddygroup_remove($user->uid, $label_id);
1326 }
1327 }
1328 }
1329
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);
1336 }
1337 else {
1338 buddylist_buddygroup_add_buddy($userid, $buddy, $label_id);
1339 }
1340 }
1341 }
1342
1343 drupal_set_message(t('@buddy groups saved.', buddylist_translation()));
1344 }
1345
1346 /**
1347 * Creates a new buddy group for a user
1348 *
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.
1352 *
1353 * @return $label_id the existing or newly created id for the name of this group.
1354 */
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;
1361 }
1362 else {
1363 return $label_id;
1364 }
1365 }
1366
1367 /**
1368 * Removes a buddy group for a user
1369 *
1370 * @param $uid user id of the user to whom the group belongs.
1371 * @param $label_id id of the group
1372 */
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);
1376 }
1377
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);
1380 }
1381
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);
1386 db_unlock_tables();
1387 }
1388
1389 /**
1390 * Feed for buddies recent posts
1391 */
1392 function buddylist_buddyfeed($uid) {
1393 if (!(is_numeric($uid) && $uid > 0)) {
1394 return drupal_not_found();
1395 exit();
1396 }
1397
1398 $buddy_ids = array_keys(buddylist_get_buddies($uid));
1399
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);
1405 }
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);
1408
1409 node_feed($result, $channel);
1410 }
1411
1412 function buddylist_addbuddy($uid) {
1413 global $user;
1414 $buddy = user_load(array('uid' => $uid));
1415
1416 if (empty($buddy->name)) {
1417 drupal_set_message(t('This user does not exist'));
1418 }
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()));
1421 }
1422 elseif ($user->uid == $uid) {
1423 drupal_set_message(t('Cannot add yourself to @buddylist', buddylist_translation()));
1424 }
1425 else {
1426 $form['uid'] = array('#type' => 'hidden', '#value' => $uid);
1427 $form['name'] = array('#type' => 'hidden', '#value' => $buddy->name);
1428 $output = confirm_form(
1429 $form,
1430 t('Add user %name to your @buddylist?', array('%name' => $buddy->name) + buddylist_translation()),
1431 $_GET['destination'],
1432 '',
1433 t('Add'), t('Cancel'),
1434 'buddylist_addbuddy_confirm');
1435 return $output;
1436 }
1437 drupal_goto();
1438 }
1439 //function buddylist_form_alter($a, $b) {dprint_r($a); dprint_r($b);}
1440
1441 /**
1442 * Confirm and add a buddy.
1443 */
1444 function buddylist_addbuddy_submit($form_id, $form_values) {
1445 if (variable_get('buddylist_require_approval', 0)) {
1446 buddylist_add_request($form_values['uid']);
1447 }
1448 else {
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'])));
1451 }
1452 return 'user';
1453 };
1454
1455 /**
1456 * Removes the user $uid from the global user's account.
1457 * TODO: generalize this so that two uids can be given
1458 */
1459 function buddylist_deletebuddy($uid) {
1460 global $user;
1461 $buddy = user_load(array('uid' => $uid));
1462
1463 if (empty($buddy->name)) {
1464 drupal_set_message('This user does not exist');
1465 }
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());
1468 }
1469 else {
1470 $form['uid'] = array('#type' => 'hidden', '#value' => $uid);
1471 $form['name'] = array('#type' => 'hidden', '#value' => $buddy->name);
1472 $output = confirm_form(
1473 $form,
1474 t('Remove user %name from your @buddylist?', array('%name' => $buddy->name) + buddylist_translation()),
1475 $_GET['destination'],
1476 '',
1477 t('Remove'), t('Cancel'),
1478 'buddylist_deletebuddy_confirm');
1479 return $output;
1480 }
1481 drupal_goto();
1482 }
1483
1484 /**
1485 * Confirm and add a buddy.
1486 */
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'])));
1490 return 'user';
1491 };
1492
1493 function buddylist_add($id) {
1494 global $user;
1495 $user_to_add = user_load(array('uid' => $id));
1496
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);
1504 }
1505 drupal_set_message(t('%username has been added to your @buddylist', array('%username' => $user_to_add->name) + buddylist_translation()));
1506 }
1507 else {
1508 drupal_set_message(t('%username is already on your @buddylist', array('%username' => $user_to_add->name) + buddylist_translation()));
1509 }
1510 }
1511
1512 function buddylist_add_request($id) {
1513 global $user;
1514 $user_to_add = user_load(array('uid' => $id));
1515
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)));
1518
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);
1521
1522 if (variable_get('buddylist_send_request', FALSE)) {
1523 buddylist_mail_user('request', $user_to_add);
1524 }
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()));
1526 }
1527 else {
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()));
1530 }
1531
1532 if ($already_buddies) {
1533 drupal_set_message(t('%username is already on your @buddylist', array('%username' => $user_to_add->name) + buddylist_translation()));
1534 }
1535 }
1536 }
1537
1538 function buddylist_remove($id) {
1539 global $user;
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);
1545 }
1546 drupal_set_message(t('%username has been removed from your @buddylist', array('%username' => $thisuser->name) + buddylist_translation()));
1547
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);
1551 }
1552 }
1553
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()));
1557 }
1558
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()));
1562 }
1563
1564 function theme_buddylist_online($online) {
1565 return $online ? t('yes') : t('no');
1566 }
1567
1568 /*
1569 * Sends mail to a buddy, which is added/removed/deleted/requested by $user
1570 */
1571 function buddylist_mail_user($op, $buddy, $user = NULL) {
1572
1573 if (variable_get('buddylist_user_mail', FALSE) && isset($buddy->buddylist_mail) && !$buddy->buddylist_mail) {
1574 return; //the user has turned mails off
1575 }
1576
1577 if (is_null($user)){
1578 global $user;
1579 }
1580 switch($op) {
1581 case 'add':
1582 $subject = BUDDYLIST_ADD_SUBJECT;
1583 $message = variable_get('buddylist_add_message', buddylist_mail_add_default());
1584 break;
1585 case 'remove':
1586 $subject = BUDDYLIST_REMOVE_SUBJECT;
1587 $message = variable_get('buddylist_remove_message', buddylist_mail_remove_default());
1588 break;
1589 case 'request':
1590 $subject = BUDDYLIST_REQUEST_SUBJECT;
1591 $message = variable_get('buddylist_request_message', buddylist_mail_request_default());
1592 break;
1593 case 'approval':
1594 $subject = BUDDYLIST_APPROVAL_SUBJECT;
1595 $message = variable_get('buddylist_approval_message', buddylist_mail_approval_default());
1596 break;
1597 }
1598
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);
1602
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');
1607 }
1608 $site_mail = 'nobody@localhost';
1609 }
1610
1611 // send the email
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);
1616 }
1617 else {
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);
1620 }
1621 }
1622 else {
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);
1626 }
1627 else {
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);
1630 }
1631 }
1632 }
1633
1634 function buddylist_mail_add_default() {
1635 return <<<MESSAGE
1636 Hi @addee_name,
1637
1638 You are @adder_name's newest @buddy.
1639
1640 Here's a link to @adder_name's profile. If you'd like, you can add them as one of your @buddies:
1641
1642 @adder_link
1643
1644 Regards,
1645 The @site team
1646 MESSAGE;
1647 }
1648
1649 function buddylist_mail_remove_default() {
1650 return <<<MESSAGE
1651 Hi @addee_name,
1652
1653 You have been removed from @adder_name's @buddylist.
1654
1655 Here's a link to @adder_name's profile:
1656
1657 @adder_link
1658
1659 Enjoy your new freedom!
1660
1661 Regards,
1662 The @site team
1663 MESSAGE;
1664 }
1665
1666 function buddylist_mail_request_default() {
1667 return <<<MESSAGE
1668 Hi @addee_name,
1669
1670 @adder_name has requested to add you to his/her @buddylist.
1671
1672 Here's a link to @adder_name's profile:
1673
1674 @adder_link
1675
1676 To approve/deny this request, log in to @siteurl and see your pending @buddy requests
1677 at @pending_requests_link.
1678
1679 Enjoy your new freedom!
1680
1681 Regards,
1682 The @site team
1683 MESSAGE;
1684 }
1685
1686 function buddylist_mail_approval_default() {
1687 return <<<MESSAGE
1688 Hi @adder_name,
1689
1690 @addee_name has approved your request to join his/her @buddylist.
1691
1692 Here's a link to your buddylist:
1693
1694 @adder_list_link
1695
1696 Enjoy your new freedom!
1697
1698 Regards,
1699 The @site team
1700
1701 MESSAGE;
1702 }
1703
1704 function buddylist_mail_replacements(&$buddy, &$user){
1705 return array(
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),
1716 );
1717 }
1718
1719
1720
1721 function buddylist_approval_mail_replacements(&$buddy, &$user) {
1722 return array(
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),
1732 );
1733 }
1734
1735 function buddylist_empty_text_default() {
1736 return <<<MESSAGE
1737 <p>You haven't made any @buddies, yet.</p>
1738
1739 <p>You can add a @buddy by visiting a user's profile page.</p>
1740 MESSAGE;
1741 }