summaryrefslogtreecommitdiffstats
path: root/buddylist_ui/buddylist_ui.module
blob: ac02ec0d4b5272522e042d790a0c697e64f6414a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<?php


/**
 * Implementation of hook_help
 * @return string, help text
 */
function buddylist_ui_help($section) {
  switch ($section) {
    case 'admin/help#buddylist_ui' :
      $output = t("buddylist_ui help text");
      return $output;
  }
}

/**
 * returns an array of common translation placeholders
 * @return array, translations
 */
function buddylist_ui_translation() {
  $translations = array (
    '@buddy' => t('buddy'), 
    '@Buddy' => t('Buddy'), 
    '@buddylist' => t('buddylist'), 
    '@Buddylist' => t('Buddylist'), 
    '@buddies' => t('buddies'), 
    '@Buddies' => t('Buddies'),);
  return variable_get('buddylist_ui_translation', $translations);
}

/**
 * Implementation of hook_perm
 * @return array, perms
 */
function buddylist_ui_perm() {
  return array ('view buddy lists', 'maintain buddy list');
}

/**
 * Implementation of hook_menu
 */
function buddylist_ui_menu($may_cache) {
  global $user;
  $items = array ();
  $id = is_numeric(arg(1)) ? arg(1) : $user->uid;
  $editAccess = (($id == $user->uid && user_access('maintain buddy list') && $user->uid) || user_access('administer users'));

  // Menu structure loosely based on user.module
  if ($may_cache) {
    $items[] = array (
      'path' => 'buddylist',
      'title' => t('My @buddylist', buddylist_ui_translation()), 
      'callback' => 'theme',
      'callback arguments' => array('buddylist_ui_page_buddylist', $id),
      'type' => MENU_NORMAL_ITEM,
      'weight' => 0,
      'access' => $editAccess,
      );

    // user page: buddylist
    $items[] = array (
      'path' => 'buddylist/confirmed',
      'title' => t('My @Buddies', buddylist_ui_translation()),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => 1,
      );
      
    // user page: pending requests
    $items[] = array(
      'path' => 'buddylist/requests',
      'title' => t('My @Buddy Requests', buddylist_ui_translation()),
      'access' => $editAccess,
      'callback' => 'theme',
      'type' => MENU_LOCAL_TASK,
      'weight' => 2,
      'callback arguments' => array('buddylist_ui_page_pending_requests', $id)
    );      

    // admin backend
    $items[] = array(
      'path' => 'admin/settings/buddylist_ui',
      'title' => t('Buddylist UI Settings'), 
      'description' => t('Buddylist UI Admin Settings'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'buddylist_ui_admin_settings',
      'access' => user_access('administer site configuration'),
    );
  }
  else {
    $items[] = array (
      'path' => 'buddy/add',
      'title' => t('Add to @buddylist', buddylist_ui_translation()), 
      'access' => $editAccess, 
      'callback' => 'drupal_get_form', 
      'callback arguments' => array ('buddylist_ui_addbuddy', arg(2)), 'type' => MENU_CALLBACK,
      );

    $items[] = array (
      'path' => 'buddy/delete',
      'title' => t('Delete from @buddylist', buddylist_ui_translation()), 
      'access' => $editAccess, 
      'callback' => 'drupal_get_form', 
      'callback arguments' => array ('buddylist_ui_deletebuddy', arg(2)), 'type' => MENU_CALLBACK,
      );
      
    $items[] = array (
      'path' => 'buddylist/' . $id . '/buddies/requested/accept',
      'title' => t('Accept Request'), 
      'access' => $editAccess, 
      'type' => MENU_CALLBACK, 
      'callback' => 'drupal_get_form', 
      'callback arguments' => array ('buddylist_ui_pending_requested_accept', $id),
      );

    $items[] = array (
      'path' => 'buddylist/' . $id . '/buddies/requested/deny',
      'title' => t('Deny Request'), 
      'access' => $editAccess, 
      'type' => MENU_CALLBACK, 
      'callback' => 'drupal_get_form', 
      'callback arguments' => array ('buddylist_ui_pending_requested_deny', $id),
      );

    $items[] = array (
      'path' => 'buddylist/' . $id . '/buddies/request/cancel',
      'title' => t('Cancel Request'), 
      'access' => $editAccess, 
      'type' => MENU_CALLBACK, 
      'callback' => 'drupal_get_form', 
      'callback arguments' => array ('buddylist_ui_cancel_request', $id),
      );
  }
  return $items;
}

/**
 * budbuddylist_ui_admin_settings administration settings page
 */
function buddylist_ui_admin_settings() {
  $form['general'] = array(
    '#type' => 'fieldset',
    '#title' => t('General settings'),
  );

  $form['general']['buddylist_ui_oneway'] = array(    
    '#type' => 'checkbox',
    '#default_value' => variable_get('buddylist_ui_oneway', 0),
    '#title' => t('One way connection mode'),
    '#description' => t("oneway means, that userA can add userB without approval. in the case of twoway a request to userB is sent, which can be denied or approved."),
  
  );

  return system_settings_form($form);
}

/**
 * @param $requestee_uid, user id 
 * @param $requester_uid, user id
 * @return
 */
function buddylist_ui_pending_requested_deny($requestee_uid, $requester_uid) {
  $requestee_account = user_load(array (
    'uid' => $requestee_uid
  ));
  $requester_account = user_load(array (
    'uid' => $requester_uid
  ));
  $output = confirm_form(buddylist_ui_confirm_form($requester_account, $requestee_account), t('Deny Request'), 'buddylist/requests', 
  t("Are you sure you want to deny the request from !name?", array (
    '!name' => theme('username',
    $requester_account
  ))), t('Yes'), t('No'), 'buddylist_ui_request_deny_confirm');

  return $output;
}

/**
 * @param $form_id
 * @param $form_values
 * @return
 */
function buddylist_ui_pending_requested_deny_submit($form_id, $form_values) {
  buddy_api_deny_request($form_values['requestee_account'], $form_values['requester_account']);
  return 'user/' . $form_values['requester_account']->uid;
}

/**
 * @param $requestee_uid, user id 
 * @param $requester_uid, user id
 * @return
 */
function buddylist_ui_pending_requested_accept($requestee_uid, $requester_uid) {
  $requestee_account = user_load(array (
    'uid' => $requestee_uid
  ));
  $requester_account = user_load(array (
    'uid' => $requester_uid
  ));
  $output = confirm_form(buddylist_ui_confirm_form($requester_account, $requestee_account), t('Accept Request'), 'buddylist/requests', 
  t("Are you sure you want to accept the request from !name?", array (
    '!name' => theme('username',
    $requester_account
  ))), t('Yes'), t('No'), 'buddylist_ui_request_accept_confirm');

  return $output;
}

/**
 * @param $form_id
 * @param $form_values
 * @return
 */
function buddylist_ui_pending_requested_accept_submit($form_id, $form_values) {
  buddy_api_accept_request($form_values['requestee_account'], $form_values['requester_account']);
  return 'user/' . $form_values['requester_account']->uid;
}

/**
 * @param $requestee_uid, user id 
 * @param $requester_uid, user id
 * @return
 */
function buddylist_ui_confirm_form($requester_account, $requestee_account) {
  $form = array ();

  $form['requester_account'] = array (
    '#type' => 'value',
    '#value' => $requester_account
  );

  $form['requestee_account'] = array (
    '#type' => 'value',
    '#value' => $requestee_account
  );

  return $form;
}
/**
 * @param $requestee_uid, user id 
 * @param $requester_uid, user id
 * @return
 */
function buddylist_ui_cancel_request($requester_uid, $requestee_uid) {
  $requester_account = user_load(array (
    'uid' => $requester_uid
  ));
  $requestee_account = user_load(array (
    'uid' => $requestee_uid
  ));
  $output = confirm_form(buddylist_ui_cancel_request_form($requestee_account, $requester_account), t('Cancel Request'), 'buddylist/requests', 
  t("Are you sure you want to cancel the request to !name?", array (
    '!name' => theme('username',
    $requestee_account
  ))), t('Yes'), t('No'), 'buddylist_ui_cancel_request_confirm');

  return $output;
}

/**
 * @param $form_id
 * @param $form_values
 * @return
 */
function buddylist_ui_cancel_request_submit($form_id, $form_values) {
  buddy_api_cancel_request($form_values['requester_account'], $form_values['requestee_account']);
  return 'user/' . $form_values['requestee_account']->uid;
}

/**
 * @param $requestee_uid, user id 
 * @param $requester_uid, user id
 * @return
 */
function buddylist_ui_cancel_request_form($requestee_account, $requester_account) {
  $form['requestee_account'] = array (
    '#type' => 'value',
    '#value' => $requestee_account,
  );

  $form['requester_account'] = array (
    '#type' => 'value',
    '#value' => $requester_account
  );

  return $form;
}

/**
 * Returns an array of posible actions (html) for the viewing user,
 * e.g. a link to make the viewed user a buddy
 * @param $viewing_user
 * @param $viewed_user
 * @return
 */
function buddylist_ui_get_buddy_actions(& $viewing_user, & $viewed_user) {

  $actions = array ();
  if (!user_access('maintain buddy list') || $viewing_user->uid == $viewed_user->uid) {
    return $actions;
  }

  if (in_array($viewed_user->uid, array_keys(buddy_api_get_requestees($viewing_user->uid)))) {
    $actions['buddy_requested'] = theme('buddylist_ui_action_sent_request', $viewed_user);
  } 
  else
    if (in_array($viewed_user->uid, array_keys(buddy_api_get_buddies($viewing_user->uid)))) {
      $actions['buddy_remove'] = theme('buddylist_ui_action_remove_buddy', $viewed_user);
    } 
    else
      if (in_array($viewing_user->uid, array_keys(buddy_api_get_requestees($viewed_user->uid)))) {
        $actions['buddy_confirm'] = theme('buddylist_ui_action_confirm_request', $viewed_user);
      } 
      else {
        $actions['buddy_add'] = theme('buddylist_ui_action_add_buddy', $viewed_user);
      }

  return $actions;
}

/**
 * Implementation of hook_user
 * @return
 */
function buddylist_ui_user($type, & $edit, & $thisuser, $category = NULL) {
  
  if ($type == 'view') {
    $output = array();
    $output[] = theme('buddylist_ui_user_buddies', $thisuser);
    $output[] = theme('buddylist_ui_user_actions', $thisuser);    
    if (count($output) > 0)
      return array (t('@Buddylist', buddylist_ui_translation()) => $output);
  } 
  else
    if ($type == 'delete') {
      db_query("DELETE FROM {buddylist} WHERE uid = %d OR buddy = %d", $thisuser->uid, $thisuser->uid);
      db_query("DELETE FROM {buddylist_pending_requests} WHERE requester_uid = %d OR requestee_uid = %d", $thisuser->uid, $thisuser->uid);
    } 
    else
      if ($type == 'load') {
        // Do not load buddies as objects to avoid recursion
        $thisuser->buddies = buddy_api_get_buddies($thisuser->uid);
      }
}


/**
 * @param $uid
 * @return
 */
function buddylist_ui_addbuddy($uid) {
  global $user;
  $buddy = user_load(array (
    'uid' => $uid
  ));

  if (empty ($buddy->name)) {
    drupal_set_message(t('This user does not exist'));
  }
  elseif (in_array($uid, array_keys(buddy_api_get_buddies($user->uid)))) {
    drupal_set_message(t('This user is already on your @buddylist', buddylist_ui_translation()));
  }
  elseif ($user->uid == $uid) {
    drupal_set_message(t('Cannot add yourself to @buddylist', buddylist_ui_translation()));
  } 
  else {
    $form['requester_account'] = array (
      '#type' => 'value',
      '#value' => $user
    );
    $form['requestee_account'] = array (
      '#type' => 'value',
      '#value' => $buddy
    );
    $form['message'] = array (
      '#type' => 'textfield',
         '#title' => t('Message'),
         '#description' => t('Message, which is sent by mail and shown at pending requests.'),
    );
    $output = confirm_form($form, t('Add user %name to your @buddylist?', array (
      '%name' => $buddy->name
    ) + buddylist_ui_translation()), $_GET['destination'], '', t('Add'), t('Cancel'), 'buddylist_ui_addbuddy_confirm');
    return $output;
  }
  drupal_goto();
}

/**
 * Confirm and add a buddy.
 * @param $form_id
 * @param $form_values
 * @return
 */
function buddylist_ui_addbuddy_submit($form_id, $form_values) {
  $buddyinfo = array();
  $buddyinfo['message'] = $form_values['message'];
  $buddyinfo['oneway'] = variable_get('buddylist_ui_oneway', 0);
  buddy_api_add($form_values['requester_account'], $form_values['requestee_account'], $buddyinfo);
  return 'user';
};

/**
 * Removes the user $uid from the global user's account.
 * TODO: generalize this so that two uids can be given
 * @param $uid
 * @return
 */
function buddylist_ui_deletebuddy($uid) {
  global $user;
  $buddy = user_load(array (
    'uid' => $uid
  ));

  if (empty ($buddy->name)) {
    drupal_set_message('This user does not exist');
  } 
  else
    if (!in_array($uid, array_keys(buddy_api_get_buddies($user->uid)))) {
      drupal_set_message('This user is not on your @buddylist', buddylist_ui_translation());
    } 
    else {
      $form['requester_account'] = array (
        '#type' => 'value',
        '#value' => $user
      );
      $form['requestee_account'] = array (
        '#type' => 'value',
        '#value' => $buddy
      );
      $output = confirm_form($form, t('Remove user %name from your @buddylist?', array (
        '%name' => $buddy->name
      ) + buddylist_ui_translation()), $_GET['destination'], '', t('Remove'), t('Cancel'), 'buddylist_ui_deletebuddy_confirm');
      return $output;
    }
  drupal_goto();
}

/**
 * @param $form_id
 * @param $form_values
 */
function buddylist_ui_deletebuddy_submit($form_id, $form_values) {
  buddy_api_remove($form_values['requester_account'], $form_values['requestee_account']);
};

/**
 * @param $buddyuser
 * @return
 */
function theme_buddylist_ui_action_add_buddy($buddyuser) {
  return l(t('Add %name to my @buddylist', array (
    '%name' => $buddyuser->name
  ) + buddylist_ui_translation()), 'buddy/add/' . $buddyuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
}

/**
 * @param $buddyuser
 * @return
 */
function theme_buddylist_ui_action_remove_buddy($buddyuser) {
  return l(t('Remove %name from my @buddylist', array (
    '%name' => $buddyuser->name
  ) + buddylist_ui_translation()), 'buddy/delete/' . $buddyuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
}

/**
 * @param $buddyuser
 * @return
 */
function theme_buddylist_ui_action_sent_request($buddyuser) {
  return t('You have requested to add %name to your @buddylist. (See !your_pending_requests)', array (
    '%name' => $buddyuser->name,
    '!your_pending_requests' => l(t('your pending requests'
  ), 'buddylist/requests')) + buddylist_ui_translation());
}

/**
 * @param $buddyuser
 * @return
 */
function theme_buddylist_ui_action_confirm_request($buddyuser) {
  return t('%name has requested to add you to your @buddylist. (See !your_pending_requests)', array (
    '%name' => $buddyuser->name,
    '!your_pending_requests' => l(t('your pending requests'
  ), 'buddylist/requests')) + buddylist_ui_translation());
}

/**
 * Main page for displaying confirmed buddylist
 * 
 * @return string, html formatted output
 */
function theme_buddylist_ui_page_buddylist() {
  drupal_set_title(t('@Buddylist', buddylist_ui_translation()));
  $view = views_get_view('buddy_api_buddylist'); 
  $title = views_get_title($view);
  $view_buddylist = views_build_view('embed', $view, array(), FALSE);
  if (!strlen($view_buddylist)){
    $output .= '<p>'. t('NONE') .'</p>';
  }
  $output .= $view_buddylist;
  
  return theme_box($title, $output, $region = 'main');
}

/**
 * Main page for displaying pending buddy requests (sent & received)
 * 
 * @return string, html formatted output
 */
function theme_buddylist_ui_page_pending_requests($id) {
  drupal_set_title(t('Pending Requests'));
  
  $view = views_get_view('buddy_api_received_requests'); 
  $title = views_get_title($view);
  $view_received_requests = views_build_view('embed', $view, array(), FALSE);
  if (!strlen($view_received_requests)){
  	$view_received_requests .= '<p>'. t('NONE') .'</p>';
  }
  $themed_received_requests = theme_box($title, $view_received_requests, $region = 'main');
  
  $view = views_get_view('buddy_api_sent_requests'); 
  $title = views_get_title($view);
  $view_sent_requests = views_build_view('embed', $view, array(), FALSE); 
  if (!strlen($view_sent_requests)){
  	$view_sent_requests .= '<p>'. t('NONE') .'</p>';
  }
  $themed_sent_requests = theme_box($title, $view_sent_requests, $region = 'main');
  
  return $themed_received_requests . $themed_sent_requests;     
}
/**
 * Themes default display of buddies on user profile
 * 
 * @return string, html formatted output
 */
function theme_buddylist_ui_user_buddies($buddyuser) {

  // Permission check first
  global $user;
  if ( !user_access('view buddy lists') && $buddyuser->uid != $user->uid) return;

  // Display entire buddylist page
  $view = views_get_view('buddy_api_buddylist');
  $view_buddylist = views_build_view('embed', $view, array(), FALSE);
  if (!strlen($view_buddylist)){
    $output .= '<p>'. t('NONE') .'</p>';
  }
  $output .= $view_buddylist;

  return array ('title' => t('@Buddies', buddylist_ui_translation()), 'value' => theme_box('', $output, $region = 'main'));
}

/**
 * Themes default display of buddy actions on user profile
 * 
 * @return string, html formatted output
 */
function theme_buddylist_ui_user_actions($buddyuser) {

  // Retrieve all actions
  global $user;
  $actions = buddylist_ui_get_buddy_actions($user, $buddyuser);

  // Return actions as a list, if there are any
  if ($actions)
    return array ('title' => t('@Buddy actions', buddylist_ui_translation()), 'value' => theme('item_list', $actions), 'class' => 'buddylist_actions');
}