78c845a93474d7be9ff9062bbaf4a48436ba46ea
[project/buddylist.git] / buddylist.module
1 <?php
2 // $Id$
3
4 if (module_exists('views')) {
5 include_once('buddylist_views.inc');
6 }
7
8
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"));
13
14 /**
15 * returns an array of common translation placeholders
16 */
17 function buddylist_translation() {
18 $translations = array(
19 '@buddy' => t('buddy'),
20 '@Buddy' => t('Buddy'),
21
22 '@buddylist' => t('buddylist'),
23 '@Buddylist' => t('Buddylist'),
24
25 '@buddies' => t('buddies'),
26 '@Buddies' => t('Buddies'),
27
28 '@buddyof' => t('buddy of'),
29 '@Buddyof' => t('Buddy of'),
30 );
31 return variable_get('buddylist_translation', $translations);
32 }
33
34
35 /**
36 * Implementation of hook_help
37 */
38 function buddylist_help($section) {
39 switch ($section) {
40 case 'admin/settings/modules#description':
41 return t('Enable @buddy list functionality.', buddlist_translation());
42 case 'admin/help#buddylist':
43 $output = t("
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>
51 <p>You can:</p>
52 <ul>
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 &raquo; access control</a></li>
55 <li>enable the @buddy list block at <a href=\"@blockadministration\" title=\"block administration\">administer &raquo; block</a></li>
56 <li>administer the @buddy list block at <a href=\"@buddylistsettings\" title=\"@buddylist settings\">administer &raquo; settings &raquo; @buddylist</a></li>
57 </ul>
58
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());
65 return $output;
66 }
67 }
68
69
70 /**
71 * Implementation of hook_perm
72 */
73 function buddylist_perm() {
74 return array('maintain buddy list', 'view buddy lists');
75 }
76
77 /**
78 * Implementation of hook_menu
79 */
80 function buddylist_menu($may_cache) {
81 global $user;
82
83 $items = array();
84 $id = is_numeric(arg(1)) ? arg(1) : $user->uid;
85
86 if ($may_cache) {
87 // buddylist settings page
88 $items[] = array(
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'),
95 );
96 // my buddylist menu item
97 $items[] = array(
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'),
103 );
104 }
105 else {
106 // 'edit access' only granted to user's own buddy list or to administrative users
107 $editAccess = (
108 ($id == $user->uid && user_access('maintain buddy list') && $user->uid)
109 || user_access('administer users'));
110
111 $approval_required = variable_get('buddylist_require_approval', 0);
112
113 $items[] = array(
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,
120 );
121 $items[] = array(
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,
128 );
129
130 // 'view only' tabs
131 $viewAccess = (($id == $user->uid && user_access('maintain buddy list')) || user_access('view buddy lists'));
132
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) {
136 $items[] = array(
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,
142 'weight' => -1,
143 'callback arguments' => array($id)
144 );
145 $items[] = array(
146 'path' => 'buddylist/'. $id .'/buddyof',
147 'title' => t('@Buddyof', buddylist_translation()),
148 'access' => $viewAccess,
149 'callback' => 'buddylist_buddylisting_page',
150 'type' => MENU_LOCAL_TASK,
151 'weight' => 1,
152 'callback arguments' => array($id, 'buddyof')
153 );
154 }
155
156 // subtabs
157 $items[] = array(
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,
163 'weight' => -1,
164 'callback arguments' => array($id),
165 );
166 $items[] = array(
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,
172 'weight' => 3,
173 'callback arguments' => array($id),
174 );
175 if (variable_get('buddylist_buddygroups', FALSE)) {
176 $items[] = array(
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,
182 'weight' => 5,
183 'callback arguments' => array($id),
184 );
185 $items[] = array(
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,
191 'weight' => 6,
192 'callback arguments' => array($id),
193 );
194 }
195
196 // sub-subtabs
197 if ($approval_required && $editAccess) {
198 $items[] = array(
199 'path' => 'buddylist/'. $id .'/buddies/requests',
200 'title' => t('Pending requests'),
201 'access' => $editAccess,
202 'callback' => 'theme',
203 'type' => MENU_LOCAL_TASK,
204 'weight' => 0,
205 'callback arguments' => array('buddylist_pending_requests', $id)
206 );
207 }
208
209 // other callbacks
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.
213 $items[] = array(
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),
220 );
221 }
222 $items[] = array(
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),
229 );
230 }
231
232 return $items;
233 }
234
235 /**
236 * Buddylist administration settings page
237 */
238 function buddylist_admin_settings() {
239 $form['general'] = array(
240 '#type' => 'fieldset',
241 '#title' => t('General settings'),
242 );
243
244 $form['general']['buddylist_require_approval'] = array(
245 '#type' => 'radios',
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'))
250 );
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),
256 );
257
258 // User profile page settings
259 $form['profile_settings'] = array(
260 '#type' => 'fieldset',
261 '#title' => t('Profile page options'),
262 );
263 $form['profile_settings']['buddylist_prof_buddies'] = array(
264 '#type' => 'select',
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()),
269 );
270
271 // TODO: move these to block settings
272 $form['block_settings'] = array(
273 '#type' => 'fieldset',
274 '#title' => t('@Buddylist block options', buddylist_translation()),
275 );
276 $form['block_settings']['buddylist_blocklisting_size'] = array(
277 '#type' => 'select',
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()),
282 );
283 $form['block_settings']['buddylist_posts_block'] = array(
284 '#type' => 'select',
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()),
289 );
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())),
294 '#size' => 70,
295 '#maxlength' => 128,
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()),
297 );
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())),
302 '#size' => 70,
303 '#maxlength' => 128,
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()),
305 );
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())),
310 '#size' => 70,
311 '#maxlength' => 128,
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()),
313 );
314
315 $form['mail'] = array(
316 '#type' => 'fieldset',
317 '#title' => t('email'),
318 );
319
320 global $user;
321 $macros = implode(', ', array_keys(buddylist_mail_replacements($user, $user)));
322 $approval_macros = implode(', ', array_keys(buddylist_approval_mail_replacements($user, $user)));
323
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.'),
329 );
330
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),
335 );
336
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,
341 );
342
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)),
348 );
349
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),
354 );
355
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,
360 );
361
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)),
367 );
368
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)
374 );
375
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,
380 );
381
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)),
387 );
388
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())
394 );
395
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,
400 );
401
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)),
407 );
408
409 return system_settings_form($form);
410 }
411
412
413 /**
414 * Implementation of hook_user
415 */
416 function buddylist_user($type, &$edit, &$thisuser, $category = NULL) {
417 global $user;
418
419 $output = array();
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);
423 }
424 if ($type == 'view') {
425 if ($list = buddylist_get_buddylist($thisuser)) {
426 $output[] = array('title' => t('@Buddies', buddylist_translation()), 'value' => $list, 'class' => 'buddylist');
427 }
428 if ($list = buddylist_get_buddylist($thisuser, TRUE)) {
429 $output[] = array('title' => t('@buddy of', buddylist_translation()), 'value' => $list, 'class' => 'buddyoflist');
430 }
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');
433 }
434 if(count($output) > 0) {
435 return array(t('@Buddy List', buddylist_translation()) => $output);
436 }
437 }
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);
443 }
444 else if ($type == 'load') {
445 $thisuser->buddies = buddylist_get_buddies($thisuser->uid);
446 }
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'),
452 '#weight' => 5);
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()));
458 return $form;
459 }
460 }
461
462 /*
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.
465 */
466 function buddylist_get_buddylist($user, $buddy_of = FALSE) {
467
468 if (user_access('view buddy lists') && !$buddy_of) {
469 $i = 0;
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;
474 $i++;
475 if ($i > variable_get('buddylist_prof_buddies', 5)) {
476 break;
477 }
478 }
479 return theme('user_list', $listbuddies);
480 }
481 }
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;
491 }
492 if ($listbuddyof) {
493 return theme('user_list', $listbuddyof);
494 }
495 }
496 }
497
498
499 /*
500 * Returns an array of posible actions (html) for the viewing user,
501 * e.g. a link to make the viewed user a buddy
502 */
503 function buddylist_get_buddy_actions(&$viewing_user, &$viewed_user) {
504
505 $actions = array();
506 if (!user_access('maintain buddy list') || $viewing_user->uid == $viewed_user->uid) {
507 return $actions;
508 }
509
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());
512 }
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);
515 }
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);
519 }
520 else {
521 $actions[] = theme('add_to_buddylist_link', $viewed_user);
522 }
523
524 return $actions;
525 }
526
527
528 /**
529 * Implementation for hook_block
530 */
531 function buddylist_block($op = 'list', $delta = 0) {
532 global $user;
533
534 if ($op == 'list') {
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()));
539 }
540 return $block;
541 }
542 else if ($op == 'view' && user_access('access content') && user_access('maintain buddy list') && $user->uid > 0) {
543 switch ($delta) {
544 case 0 : // Shows buddylist block
545 if ($buddies = buddylist_get_buddies()) {
546 $i = 0;
547 foreach (array_keys($buddies) as $buddy) {
548 $users[] = user_load(array('uid' => $buddy));
549 $i++;
550 if ($i == variable_get('buddylist_blocklisting_size', 5)) {
551 break;
552 }
553 }
554
555 $block['content'] = theme('user_list', $users);
556 $block['subject'] = variable_get('buddylist_list_block_title', t('My @buddy list', buddylist_translation()));
557
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>';
561 }
562 return $block;
563 }
564 break;
565
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));
572
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);
576
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);
580
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>';
583 }
584 return $block;
585 }
586 }
587 break;
588
589 case 2: // Buddylist status
590 $block['subject'] = variable_get('buddylist_status_block_title', t('My @buddy status', buddylist_translation()));
591
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));
595
596 $block['content'] = theme('buddylist_status_block', $count, $received, $sent);
597 return $block;
598 break;
599 }
600 }
601 }
602
603 /**
604 * Public API for retrieving buddies. Feel free to use this from other
605 * modules.
606 * $key can be 'uid' or 'label'.
607 */
608 function buddylist_get_buddies($uid = NULL, $key = 'uid') {
609 static $buddies;
610
611 if (!$uid) {
612 global $user;
613 $uid = $user->uid;
614 }
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
619 WHERE b.uid = %d';
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.",";
628 }
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;
635 }
636 }
637 }
638
639 return $buddies[$key][$uid];
640 }
641
642 /**
643 * Returns an array of uid => name of people that user with param $uid has made a buddy request to
644 */
645 function buddylist_get_requestees($uid) {
646 $buddies = array();
647
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);
649
650 while ($row = db_fetch_object($result)) {
651 $buddies[$row->requestee_uid] = $row->name;
652 }
653
654 return $buddies;
655 }
656
657 function buddylist_setmsg_received($thisuser) {
658 global $user;
659
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;
667 }
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));
670 }
671 }
672 else {
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);
679 }
680 }
681 }
682 }
683
684 /**
685 * expose add and remove links to theming.
686 */
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);
689 }
690
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);
693 }
694
695
696 /**
697 * Displays a list of a given user's buddies.
698 */
699 function buddylist_buddylisting_page($uid = NULL, $mode = 'buddies') {
700 global $user;
701
702 if (empty($uid)) {
703 $uid = $user->uid;
704 }
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)))) {
707 drupal_not_found();
708 exit();
709 }
710
711 drupal_set_title(t('%username\'s @buddylist', array('%username' => $thisuser->name) + buddylist_translation()));
712
713 $buddies_per_page = 20;
714
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";
718 }
719 else {
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";
721 }
722 $result = pager_query($sql, $buddies_per_page, 0 , NULL, $uid);
723
724 $header = array(t('@buddy', buddylist_translation()), t('online'));
725 $online_interval = time() - variable_get('user_block_seconds_online', 180);
726
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));
731 }
732 $output .= theme('table', $header, $rows);
733 }
734 else {
735 $output .= t('No @buddies found.', buddylist_translation());
736 }
737
738 $output .= theme('pager', NULL, $buddies_per_page);
739
740 return $output;
741 }
742
743 /**
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.
746 */
747 function theme_buddylist_pending_requests($id) {
748
749 $thisuser = user_load(array('uid' => $id));
750 drupal_set_title(t('@username\'s @buddylist', array('@username' => $thisuser->name) + buddylist_translation()));
751
752 return theme('box', t('Received requests'), buddylist_pending_requester_list($thisuser)) .
753 theme('box', t('Sent requests'), buddylist_pending_requested_list($thisuser));
754 }
755
756 /**
757 * Returns a list of people who've requested to be added to the given user's buddylist
758 */
759 function buddylist_pending_requester_list(&$account) {
760 global $user;
761
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);
764
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>';
767 }
768 else {
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>';
770
771 $html_rows = array();
772 while ($row = db_fetch_object($result)) {
773 $html_row = array();
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;
777 }
778
779 $output .= theme('table', NULL, $html_rows);
780 }
781
782 return $output;
783 }
784
785 /**
786 * Returns a list of user's who this given user has requested to be buddies with.
787 */
788 function buddylist_pending_requested_list(&$account) {
789 global $user;
790
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);
793
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.',
796 array(
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());
801 }
802 else {
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());
804
805 $html_rows = array();
806 while ($row = db_fetch_object($result)) {
807 $html_row = array();
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;
811 }
812
813 $output .= theme('table', NULL, $html_rows);
814 }
815
816 return $output;
817 }
818
819
820 function buddylist_approval_form($requestee_uid, $requester_uid) {
821 $form = array();
822
823 $form['requestee_uid'] = array(
824 '#type' => 'hidden',
825 '#value' => $requestee_uid,
826 );
827
828 $form['requester_uid'] = array(
829 '#type' => 'hidden',
830 '#value' => $requester_uid
831 );
832
833 $form['approve'] = array(
834 '#type' => 'submit',
835 '#value' => t('Approve')
836 );
837
838 $form['deny'] = array(
839 '#type' => 'submit',
840 '#value' => t('Deny'),
841 );
842
843 return $form;
844 }
845
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
848
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']);
851
852 $requester_account = user_load(array('uid' => $form_values['requester_uid']));
853
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']);
857
858 $time = time();
859
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);
862 }
863
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);
867 }
868
869 if (variable_get('buddylist_send_approval', FALSE)) {
870 buddylist_mail_user('approval', $requester_account, $requestee_account);
871 }
872
873 drupal_set_message(t('Congratulations! !linktouser is now your @buddy.', array('!linktouser' => theme('username', $requester_account)) + buddylist_translation()));
874 }
875 else {
876 drupal_set_message(t("!user's request to be your @buddy has been denied.", array('!user' => theme('username', $requester_account)) + buddylist_translation()));
877 }
878 }
879
880 function buddylist_request_cancel_form($requestee_uid, $requester_uid) {
881 $form['requestee_uid'] = array(
882 '#type' => 'hidden',
883 '#value' => $requestee_uid,
884 );
885
886 $form['requester_uid'] = array(
887 '#type' => 'hidden',
888 '#value' => $requester_uid
889 );
890
891 $form['cancel'] = array(
892 '#type' => 'submit',
893 '#value' => t('Cancel')
894 );
895
896 return $form;
897 }
898
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']);
901
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))));
904
905 return 'buddylist/'. $form_values['requester_uid'] .'/buddies/requests';
906 }
907
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),
914 );
915 }
916 return $form;
917 }
918
919 function buddylist_buddiesrecent_page($uid) {
920 global $user;
921
922 $thisuser = user_load(array('uid' => $uid));
923 drupal_set_title(t('%username\'s @buddylist', array('%username' => $thisuser->name) + buddylist_translation()));
924
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') .'" />');
929
930 return $output;
931 }
932
933 function buddylist_buddiesgroups_page($uid) {
934 global $user;
935 $thisuser = user_load(array('uid' => $uid));
936 drupal_set_title(t('%username\'s @buddy groups', array('%username' => $thisuser->name) + buddylist_translation()));
937
938 $headers = array(t('@buddy', buddylist_translation()), t('online'), t('# of @buddies', buddylist_translation()), t("@buddy's posts", buddylist_translation()));
939
940 $result = db_query('SELECT label, label_id FROM {buddylist_groups} WHERE uid = %d ORDER BY label ASC', $thisuser->uid);
941 $groups = array();
942 while ($row = db_fetch_object($result)) {
943 $groups[$row->label_id] = $row->label;
944 }
945
946 if (count($groups) == 0) {
947 if ($thisuser->uid == $user->uid) {
948 drupal_set_message(
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())
952 );
953 }
954 return t('No groups found.');
955 }
956
957 $online_interval = time() - variable_get('user_block_seconds_online', 180);
958 $buddies = buddylist_get_buddies($thisuser->uid);
959
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);
962 $rows = array();
963 while ($row = db_fetch_object($result)) {
964 $online = $row->access > $online_interval;
965 $rows[] = array(
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')
970 );
971 }
972 $output .= theme('box', check_plain($label), $rows ? theme('table', $headers, $rows) : t('Group is empty.'));
973 }
974 return $output;
975 }
976
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);
980 }
981
982
983 function buddylist_get_buddy_groups($uid, $buddy = NULL) {
984 if (isset($buddy)) {
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);
986 }
987 else {
988 $result = db_query("SELECT * FROM {buddylist_groups} ", $uid);
989 }
990 $buddy_groups = array();
991 while ($row = db_fetch_array($result)) {
992 $buddy_groups[$row['label_id']] = $buddy ? $row : $row['label'];
993 }
994 return $buddy_groups;
995 }
996
997
998 function buddylist_form_edit_groups_add() {
999 // Add group form
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()),
1004 );
1005
1006 $form['submit'] = array(
1007 '#type' => 'submit',
1008 '#value' => t('Add'),
1009 );
1010
1011 return $form;
1012 }
1013
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,
1019 '#title' => '',
1020 '#default_value' => null,
1021 '#options' => array_map('check_plain', $all_groups),
1022 );
1023
1024 $form['remove']['submit'] = array(
1025 '#type' => 'submit',
1026 '#value' => t('Remove'),
1027 );
1028
1029 return $form;
1030 }
1031
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) {
1036 $items = array();
1037 foreach ($buddy['groups'] as $group) {
1038 $items[] = $group['label_id'];
1039 }
1040
1041 if (count($all_groups) > 0) {
1042 $form['table']['groups'][$uid] = array(
1043 '#type' => 'checkboxes',
1044 '#title' => '',
1045 '#default_value' => $items,
1046 '#options' => array_map('check_plain', $all_groups),
1047 );
1048 }
1049 }
1050
1051 $form['table']['user'] = array(
1052 '#type' => 'value',
1053 '#value' => $thisuser->uid,
1054 );
1055
1056 if (count($form['table']['groups']) > 0) {
1057 $form['table']['submit'] = array(
1058 '#type' => 'submit',
1059 '#value' => t('Submit'),
1060 );
1061 }
1062
1063 return $form;
1064 }
1065
1066 /**
1067 * Callback for the buddygroup editing page
1068 */
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()));
1072
1073 if ($buddies = buddylist_get_buddies($thisuser->uid)) {
1074 $output['add'] = drupal_get_form('buddylist_form_edit_groups_add');
1075
1076 $groups = buddylist_get_buddy_groups($uid);
1077
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);
1081 }
1082 else {
1083 drupal_set_message(t("You don't have any groups defined."));
1084 }
1085
1086 return theme('buddylist_edit_groups', $output);
1087 }
1088 else {
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());
1091 }
1092 }
1093
1094 function theme_buddylist_edit_groups($forms) {
1095 $output = '<span id="buddylist-group-add-form">'.
1096 $forms['add'].
1097 '</span>'.
1098 '<span id="buddylist-group-remove-form">'.
1099 $forms['remove'].
1100 '</span>'.
1101 '<span id="buddlist-groups-form">'.
1102 $forms['table'].
1103 '</span>';
1104 return $output;
1105 }
1106
1107 function theme_buddylist_form_edit_groups_table($form) {
1108 $rows = array();
1109
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]));
1113 }
1114 }
1115
1116 $headers = array(t('buddy'), t('@buddy groups', buddylist_translation()));
1117 $output .= theme('table', $headers, $rows);
1118
1119 $output .= drupal_render($form);
1120
1121 return $output;
1122 }
1123
1124 function theme_buddylist_status_block($count, $received_count, $sent_count) {
1125 global $user;
1126
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>";
1131 $stats .= "</div>";
1132
1133 return $stats;
1134 }
1135
1136 function buddylist_form_edit_groups_add_submit($form_id, $form_values) {
1137 global $user;
1138 $label_id = buddylist_buddygroup_new($user->uid, $form_values['add_group']);
1139 }
1140
1141 function buddylist_form_edit_groups_remove_submit($form_id, $form_values) {
1142 global $user;
1143 foreach ($form_values['groups'] as $label_id => $remove) {
1144 if ($remove > 0) {
1145 buddylist_buddygroup_remove($user->uid, $label_id);
1146 }
1147 }
1148 }
1149
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);
1156 }
1157 else {
1158 buddylist_buddygroup_add_buddy($userid, $buddy, $label_id);
1159 }
1160 }
1161 }
1162
1163 drupal_set_message(t('@buddy groups saved.', buddylist_translation()));
1164 }
1165
1166 /**
1167 * Creates a new buddy group for a user
1168 *
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.
1172 *
1173 * @return $label_id the existing or newly created id for the name of this group.
1174 */
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;
1181 }
1182 else {
1183 return $label_id;
1184 }
1185 }
1186
1187 /**
1188 * Removes a buddy group for a user
1189 *
1190 * @param $uid user id of the user to whom the group belongs.
1191 * @param $label_id id of the group
1192 */
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);
1196 }
1197
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);
1200 }
1201
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);
1206 db_unlock_tables();
1207 }
1208
1209 /**
1210 * Feed for buddies recent posts
1211 */
1212 function buddylist_buddyfeed($uid) {
1213 if (!(is_numeric($uid) && $uid > 0)) {
1214 return drupal_not_found();
1215 exit();
1216 }
1217
1218 $buddy_ids = array_keys(buddylist_get_buddies($uid));
1219
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);
1225 }
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);
1228
1229 node_feed($result, $channel);
1230 }
1231
1232 function buddylist_addbuddy($uid) {
1233 global $user;
1234 $buddy = user_load(array('uid' => $uid));
1235
1236 if (empty($buddy->name)) {
1237 drupal_set_message(t('This user does not exist'));
1238 }
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()));
1241 }
1242 elseif ($user->uid == $uid) {
1243 drupal_set_message(t('Cannot add yourself to @buddy list', buddylist_translation()));
1244 }
1245 else {
1246 $form['uid'] = array('#type' => 'hidden', '#value' => $uid);
1247 $form['name'] = array('#type' => 'hidden', '#value' => $buddy->name);
1248 $output = confirm_form(
1249 $form,
1250 t('Add user %name to your @buddy list?', array('%name' => $buddy->name) + buddylist_translation()),
1251 $_GET['destination'],
1252 '',
1253 t('Add'), t('Cancel'),
1254 'buddylist_addbuddy_confirm');
1255 return $output;
1256 }
1257 drupal_goto();
1258 }
1259 //function buddylist_form_alter($a, $b) {dprint_r($a); dprint_r($b);}
1260
1261 /**
1262 * Confirm and add a buddy.
1263 */
1264 function buddylist_addbuddy_submit($form_id, $form_values) {
1265 if (variable_get('buddylist_require_approval', 0)) {
1266 buddylist_add_request($form_values['uid']);
1267 }
1268 else {
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'])));
1271 }
1272 return 'user';
1273 };
1274
1275 /**
1276 * Removes the user $uid from the global user's account.
1277 * TODO: generalize this so that two uids can be given
1278 */
1279 function buddylist_deletebuddy($uid) {
1280 global $user;
1281 $buddy = user_load(array('uid' => $uid));
1282
1283 if (empty($buddy->name)) {
1284 drupal_set_message('This user does not exist');
1285 }
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());
1288 }
1289 else {
1290 $form['uid'] = array('#type' => 'hidden', '#value' => $uid);
1291 $form['name'] = array('#type' => 'hidden', '#value' => $buddy->name);
1292 $output = confirm_form(
1293 $form,
1294 t('Remove user %name from your @buddy list?', array('%name' => $buddy->name) + buddylist_translation()),
1295 $_GET['destination'],
1296 '',
1297 t('Remove'), t('Cancel'),
1298 'buddylist_deletebuddy_confirm');
1299 return $output;
1300 }
1301 drupal_goto();
1302 }
1303
1304 /**
1305 * Confirm and add a buddy.
1306 */
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'])));
1310 return 'user';
1311 };
1312
1313 function buddylist_add($id) {
1314 global $user;
1315 $user_to_add = user_load(array('uid' => $id));
1316
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);
1324 }
1325 drupal_set_message(t('%username has been added to your @buddy list', array('%username' => $user_to_add->name) + buddylist_translation()));
1326 }
1327 else {
1328 drupal_set_message(t('%username is already on your @buddylist', array('%username' => $user_to_add->name) + buddylist_translation()));
1329 }
1330 }
1331
1332 function buddylist_add_request($id) {
1333 global $user;
1334 $user_to_add = user_load(array('uid' => $id));
1335
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)));
1338
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);
1341
1342 if (variable_get('buddylist_send_request', FALSE)) {
1343 buddylist_mail_user('request', $user_to_add);
1344 }
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()));
1346 }
1347 else {
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()));
1350 }
1351
1352 if ($already_buddies) {
1353 drupal_set_message(t('%username is already on your @buddylist', array('%username' => $user_to_add->name) + buddylist_translation()));
1354 }
1355 }
1356 }
1357
1358 function buddylist_remove($id) {
1359 global $user;
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);
1365 }
1366 drupal_set_message(t('%username has been removed from your @buddylist', array('%username' => $thisuser->name) + buddylist_translation()));
1367
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);
1371 }
1372 }
1373
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()));
1377 }
1378
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()));
1382 }
1383
1384 function theme_buddylist_online($online) {
1385 return $online ? t('yes') : t('no');
1386 }
1387
1388 /*
1389 * Sends mail to a buddy, which is added/removed/deleted/requested by $user
1390 */
1391 function buddylist_mail_user($op, $buddy, $user = NULL) {
1392
1393 if (variable_get('buddylist_user_mail', FALSE) && isset($buddy->buddylist_mail) && !$buddy->buddylist_mail) {
1394 return; //the user has turned mails off
1395 }
1396
1397 if (is_null($user)){
1398 global $user;
1399 }
1400 switch($op) {
1401 case 'add':
1402 $subject = BUDDYLIST_ADD_SUBJECT;
1403 $message = variable_get('buddylist_add_message', buddylist_mail_add_default());
1404 break;
1405 case 'remove':
1406 $subject = BUDDYLIST_REMOVE_SUBJECT;
1407 $message = variable_get('buddylist_remove_message', buddylist_mail_remove_default());
1408 break;
1409 case 'request':
1410 $subject = BUDDYLIST_REQUEST_SUBJECT;
1411 $message = variable_get('buddylist_request_message', buddylist_mail_request_default());
1412 break;
1413 case 'approval':
1414 $subject = BUDDYLIST_APPROVAL_SUBJECT;
1415 $message = variable_get('buddylist_approval_message', buddylist_mail_approval_default());
1416 break;
1417 }
1418
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);
1422
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');
1427 }
1428 $site_mail = 'nobody@localhost';
1429 }
1430
1431 // send the email
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);
1436 }
1437 else {
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);
1440 }
1441 }
1442 else {
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);
1446 }
1447 else {
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);
1450 }
1451 }
1452 }
1453
1454 function buddylist_mail_add_default() {
1455 return <<<MESSAGE
1456 Hi @addee_name,
1457
1458 You are @adder_name's newest @buddy.
1459
1460 Here's a link to @adder_name's profile. If you'd like, you can add them as one of your @buddies:
1461
1462 @adder_link
1463
1464 Regards,
1465 The @site team
1466 MESSAGE;
1467 }
1468
1469 function buddylist_mail_remove_default() {
1470 return <<<MESSAGE
1471 Hi @addee_name,
1472
1473 You have been removed from @adder_name's @buddy list.
1474
1475 Here's a link to @adder_name's profile:
1476
1477 @adder_link
1478
1479 Enjoy your new freedom!
1480
1481 Regards,
1482 The @site team
1483 MESSAGE;
1484 }
1485
1486 function buddylist_mail_request_default() {
1487 return <<<MESSAGE
1488 Hi @addee_name,
1489
1490 @adder_name has requested to add you to his/her @buddy list.
1491
1492 Here's a link to @adder_name's profile:
1493
1494 @adder_link
1495
1496 To approve/deny this request, log in to @siteurl and see your pending @buddy requests
1497 at @pending_requests_link.
1498
1499 Enjoy your new freedom!
1500
1501 Regards,
1502 The @site team
1503 MESSAGE;
1504 }
1505
1506 function buddylist_mail_approval_default() {
1507 return <<<MESSAGE
1508 Hi @adder_name,
1509
1510 @addee_name has approved your request to join his/her @buddy list.
1511
1512 Here's a link to your buddylist:
1513
1514 @adder_list_link
1515
1516 Enjoy your new freedom!
1517
1518 Regards,
1519 The @site team
1520
1521 MESSAGE;
1522 }
1523
1524 function buddylist_mail_replacements(&$buddy, &$user){
1525 return array(
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),
1536 );
1537 }
1538
1539
1540
1541 function buddylist_approval_mail_replacements(&$buddy, &$user) {
1542 return array(
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),
1552 );
1553 }