--- buddylist_old.module	2006-03-30 17:05:45.000000000 -0800
+++ buddylist_new.module	2006-05-06 09:51:12.281250000 -0700
@@ -1,5 +1,5 @@
 <?php
-// $Id: buddylist.module,v 1.48.2.6 2006/03/30 20:55:02 robertDouglass Exp $
+// $Id: buddylist.module,v 1.58 2006/03/30 20:50:07 robertDouglass Exp $
 
 /**
  * returns an array of common translation placeholders
@@ -9,9 +9,10 @@
 	  '%Buddy' => t('Buddy'),
 	  '%buddylist' => t('buddylist'),
 	  '%buddies' => t('buddies'),
-	  '%Buddies' => t('Buddies'),
+	  '%Buddies' => t('Friends'),
 	  '%buddiesof' => t('buddies of'),
 	  '%Buddylist' => t('Buddylist'),
+	  '%friendship_requests' => t('Friendship invitations sent to:'),
 	);
 }
 
@@ -151,7 +152,7 @@
   }
 
   if ($type == 'view' && user_access('view buddy lists')) {
-    // if thisuser has friends, show friends
+    // if thisuser has friends, show friends MOD NOTE: friends are NOW determined by *mutual* buddy invite status
     $cnt = variable_get('buddylist_prof_buddies', 5);
     $i = 0;
     if ($buddies = buddylist_get_buddies($thisuser->uid)) {
@@ -163,29 +164,61 @@
           break;
         }
       }
-      $output[] = array('title' => t('%Buddies', buddylist_translation()), 'value' => theme('user_list', $listbuddies), 'class' => 'buddylist',);
+      $output[] = array('title' => t('%username\'s %Buddies', array('%username' => $thisuser->name) + buddylist_translation()), 'value' => theme('user_list', $listbuddies), 'class' => 'buddylist',);
     }
-
-    // This portion of code is used to see if this $thisuser is a buddy of others and, if s/he is, returns a list
-    // of people s/he is a buddy of.
-    // Note the distinction between having a buddy and being someone else's buddy (i.e., 'buddyof')
-    $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';
-    $result = db_query_range($sql, $thisuser->uid, 0, $cnt);
-    while ($row = db_fetch_object($result)) {
-      $listbuddiesof[$row->uid] = $row;
-    }
-    if ($listbuddiesof) {
-      $output[] = array('title' => t('%Buddy of', buddylist_translation()), 'value' => theme('user_list', $listbuddiesof));
+	
+	// **MOD alternately reinterpret the old "$thisuser has buddies" as "$thisuser has pending outgoing friendship requests" ajwwong
+	// Show pending outgoing friend requests *only* if you are looking at your own profile
+	if ($thisuser->uid == $user->uid) {
+		$cnt = variable_get('buddylist_prof_buddies', 5);
+		$i = 0;
+	//    if ($buddies = buddylist_get_buddies($thisuser->uid)) {
+		if ($buddies = buddylist_get_friendship_requests($thisuser->uid)) {
+		  foreach(array_keys($buddies) as $buddy) {
+			$account = user_load(array('uid' => $buddy));
+					$list_friendship_requests[] = $account;
+			$i++;
+			if ($i > $cnt) {
+			  break;
+			}
+		  }
+	//      $output[] = array('title' => t('%Buddies', buddylist_translation()), 'value' => theme('user_list', $listbuddies), 'class' => 'buddylist',);
+		  $output[] = array('title' => t('%friendship_requests', buddylist_translation()), 'value' => theme('user_list', $list_friendship_requests), 'class' => 'buddylist',);
+		}
+	
+		// This portion of code is used to see if this $thisuser is a buddy of others and, if s/he is, returns a list
+		// of people s/he is a buddy of.
+		// Note the distinction between having a buddy and being someone else's buddy (i.e., 'buddyof')
+		
+		// ** MOD reinterpret the "this user is a 'buddy of'" as "someone else has invited this user to be friends" BUT not yet MUTUAL ajwwong
+	//    $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';
+		$sql = 'SELECT b1.uid, u.name FROM {buddylist} b1 INNER JOIN {users} u ON b1.uid = u.uid WHERE b1.buddy = %d AND b1.buddy != ALL (SELECT b2.uid from `buddylist` b2 where b2.buddy = b1.uid) ORDER BY u.access DESC';
+		$result = db_query_range($sql, $thisuser->uid, 0, $cnt);
+		while ($row = db_fetch_object($result)) {
+		  $listbuddiesof[$row->uid] = $row;
+		}
+		if ($listbuddiesof) {
+		  $output[] = array('title' => t('New Friends Requests from:', buddylist_translation()), 'value' => theme('user_list', $listbuddiesof));
+		}
     }
+// **MOD/ if global $user is not viewing *own* profile, then check to see if $thisuser is *already* invited, to be a friend of $user.  
+// If not already invited, create a link that allows an invitation.  If already invited, create a link that allows "removal" of invitation.
 
     // Check to see whether or not $thisuser is in global $user's buddy list
     // If $thisuser is already in $user's buddy list, a link offering to delete $thisuser from $user's buddy list is generated
     // If $thisuser is not on $user's buddy list, and $thisuser != $user, then a link offering to add $thisuser to $user's buddy list
     // is generated.
+//    if (@in_array($thisuser->uid, array_keys(buddylist_get_buddies($user->uid))) && user_access('maintain buddy list')) {
     if (@in_array($thisuser->uid, array_keys(buddylist_get_buddies($user->uid))) && user_access('maintain buddy list')) {
       $actions[] = theme('remove_from_buddylist_link', $thisuser);
     }
-    else {
+	else if (@in_array($thisuser->uid, array_keys(buddylist_get_friendship_requests($user->uid))) && user_access('maintain buddy list')) {
+      $actions[] = theme('retract_friendship_request_link', $thisuser);
+    }
+    else if (@in_array($user->uid, array_keys(buddylist_get_friendship_requests($thisuser->uid))) && user_access('maintain buddy list')) {
+      $actions[] = theme('accept_friendship_request_link', $thisuser);
+    }
+	else {
       if ($user->uid != $thisuser->uid && user_access('maintain buddy list')) {
         $actions[] = theme('add_to_buddylist_link', $thisuser);
       }
@@ -312,6 +345,8 @@
  * modules.
  * $key can be 'uid' or 'label'.
 */
+/*MOD -- this version now uses MUTUAL buddy status for "friend determination".  The old buddylist_get_buddies is now buddylist_get_friendship_reqeusts */
+
 function buddylist_get_buddies($uid = NULL, $key = 'uid') {
   static $buddies;
 
@@ -321,9 +356,52 @@
   }
   if (!isset($buddies[$key][$uid])) {
     $buddies[$key][$uid] = array();
+/* old version   
     $sql = 'SELECT b.buddy, u.name, u.mail FROM {buddylist} b
             INNER JOIN {users} u ON b.buddy = u.uid
             WHERE b.uid = %d';
+    $result = db_query($sql, $uid);*/
+/* NEW VERSION buddies only when **MUTUAL** invitation */
+    $sql = 'SELECT b1.buddy, u.name, u.mail FROM {buddylist} b1
+            INNER JOIN {users} u INNER JOIN {buddylist} b2 ON (b1.buddy = u.uid AND b1.buddy = b2.uid AND b2.buddy = b1.uid)
+            WHERE b1.uid = %d';
+    $result = db_query($sql, $uid);
+    while ($row = db_fetch_object($result)) {
+			$buddies[$key][$uid][$row->buddy]['name'] = $row->name;
+			$buddies[$key][$uid][$row->buddy]['mail'] = $row->mail;
+			$buddies[$key][$uid][$row->buddy]['groups'] = buddylist_get_buddy_groups($uid, $row->buddy);
+    }
+  }
+
+  return $buddies[$key][$uid];
+}
+
+/**  	
+ * Public API for retrieving friendship requests. Feel free to use this from other
+ * modules.
+ * $key can be 'uid' or 'label'.
+ * --- NOTE THIS IS JUST THE OLD "get_buddies" function, but re-interpreted so that it is now friendship requests -- but modified so 
+ * so that it's *PENDING* friends requests, i.e., friendship requests that haven't been already acted upon.
+*/
+function buddylist_get_friendship_requests($uid = NULL, $key = 'uid') {
+  static $buddies;
+
+  if (!$uid) {
+    global $user;
+    $uid = $user->uid;
+  }
+  if (!isset($buddies[$key][$uid])) {
+    $buddies[$key][$uid] = array();
+/*    $sql = 'SELECT b.buddy, u.name, u.mail FROM {buddylist} b
+            INNER JOIN {users} u ON b.buddy = u.uid
+            WHERE b.uid = %d'; */
+// This query below only gives us the PENDING friendship requests, that haven't been *actualized* as yet as fully mutual buddy status
+    $sql = 'SELECT b1.buddy, u.name, u.mail FROM {buddylist} b1 
+            INNER JOIN {users} u ON (b1.buddy = u.uid)
+            WHERE b1.uid = %d AND b1.buddy != ALL (SELECT b2.uid from `buddylist` b2 where b2.buddy = b1.uid)' ; 			
+/*    $sql = 'SELECT b1.buddy, u.name, u.mail FROM {users} u 
+            INNER JOIN {buddylist} b1 LEFT JOIN {buddylist} b2 ON (b1.buddy = u.uid  AND b1.buddy = b2.uid AND b2.buddy = b1.uid) 
+            WHERE b1.uid = %d AND b2.buddy IS NULL' ; 		This was an attempt to use RobRoy's sql, but I couldn't figure out how to make it work ajwwong*/	
     $result = db_query($sql, $uid);
     while ($row = db_fetch_object($result)) {
 			$buddies[$key][$uid][$row->buddy]['name'] = $row->name;
@@ -353,8 +431,14 @@
   while ($rec = db_fetch_object($check_received)) {
     if (($rec->received) and ($thisuser->uid == $user->uid)) {
       // TODO: This is where integration with Privatemsg could happen. If enabled, send a private message instead.
-      drupal_set_message(t('%linktouser has added you to his/her %buddylist.', array('%linktouser' => l($rec->name, 'user/'. $rec->uid)) + buddylist_translation()));
-      db_query('UPDATE {buddylist} SET received = 0 WHERE buddy = %d', $user->uid);
+      // Also TODO:   This should be where the user invites OR ACCEPTS the invitation... depending on database values...  DONE ajwwong
+ 	  if (!in_array($user->uid, array_keys(buddylist_get_buddies($rec->uid)))){ // if these guys are not already friends
+	    drupal_set_message(t('%linktouser has invited you to be his/her friend.', array('%linktouser' => l($rec->name, 'user/'. $rec->uid)) + buddylist_translation()));
+      }
+	  else {
+	    drupal_set_message(t('%linktouser has accepted your friendship invitation.', array('%linktouser' => l($rec->name, 'user/'. $rec->uid)) + buddylist_translation()));
+      }
+	  db_query('UPDATE {buddylist} SET received = 0 WHERE buddy = %d', $user->uid);
     }
   }
 }
@@ -363,13 +447,20 @@
  * expose add and remove links to theming.
  */
 function theme_remove_from_buddylist_link($buddyuser) {
-  return l(t('Remove %name from my %buddy list', array('%name' => theme('placeholder', $buddyuser->name)) + buddylist_translation()), 'buddy/delete/' . $buddyuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
+  return l(t('Remove %name from %buddy list ', array('%name' => theme('placeholder', $buddyuser->name)) + buddylist_translation()), 'buddy/delete/' . $buddyuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
 }
 
 function theme_add_to_buddylist_link($buddyuser) {
-	return l(t('Add %name to my %buddy list', array('%name' => theme('placeholder', $buddyuser->name)) + buddylist_translation()), 'buddy/add/' . $buddyuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
+	return l(t('Invite %name to my %buddy list', array('%name' => theme('placeholder', $buddyuser->name)) + buddylist_translation()), 'buddy/add/' . $buddyuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
+}
+
+function theme_retract_friendship_request_link($buddyuser) {
+  return l(t('Retract friendship invitation to %name', array('%name' => theme('placeholder', $buddyuser->name)) + buddylist_translation()), 'buddy/delete/' . $buddyuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
 }
 
+function theme_accept_friendship_request_link($buddyuser) {
+  return l(t('Accept friendship invitation from %name', array('%name' => theme('placeholder', $buddyuser->name)) + buddylist_translation()), 'buddy/add/' . $buddyuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
+}
 
 /**
  * Displays a list of a given user's buddies.
@@ -712,7 +803,7 @@
 
 function buddylist_addbuddy($uid) {
   global $user;
-  $uid = (int)$uid[0];
+  //$uid = (int)$uid[0];
   $buddy = user_load(array('uid' => $uid));
 
   if (empty($buddy->name)) {
@@ -721,18 +812,34 @@
   elseif (in_array($uid, array_keys(buddylist_get_buddies($user->uid)))) {
     return t('This user is already on your %buddy list', buddylist_translation());
   }
+  // ADD this CHECK below to see if you've already INVITED this person ajwwong
+  elseif (in_array($uid, array_keys(buddylist_get_friendship_requests($user->uid)))) {
+    return t('This user has already been invited to your %buddy list', buddylist_translation());
+  }
   elseif ($user->uid == $uid) {
     return t('Cannot add yourself to %buddy list', buddylist_translation());
   }
 
   $form['uid'] = array('#type' => 'hidden', '#value' => $uid);
   $form['name'] = array('#type' => 'hidden', '#value' => $buddy->name);
-  return confirm_form('buddylist_addbuddy_confirm',
-    $form,
-    t('Add user %name to your %buddy list?', array('%name' => theme('placeholder', $buddy->name)) + buddylist_translation()),
-    $_GET['destination'],
-    ' ',
-    t('Add'), t('Cancel'));
+  //MOD:  Check to see if you've already been invited -- then *accept* their invitation, else "invite" them as friends
+  if (in_array($user->uid, array_keys(buddylist_get_friendship_requests($uid)))) {
+	  return confirm_form('buddylist_addbuddy_confirm',
+		$form,
+		t('Accept user %name as your friend?', array('%name' => theme('placeholder', $buddy->name)) + buddylist_translation()),
+		$_GET['destination'],
+		' ',
+		t('Accept'), t('Cancel'));
+  }
+  else {
+	  return confirm_form('buddylist_addbuddy_confirm',
+		$form,
+		t('Invite user %name to your %buddy list?', array('%name' => theme('placeholder', $buddy->name)) + buddylist_translation()),
+		$_GET['destination'],
+		' ',
+		t('Invite'), t('Cancel'));
+  }
+  
 }
 
 /**
@@ -746,24 +853,40 @@
 
 function buddylist_deletebuddy($uid) {
   global $user;
-  $uid = (int)$uid[0];
+  //$uid = (int)$uid[0];
   $buddy = user_load(array('uid' => $uid));
 
   if (empty($buddy->name)) {
     return t('This user does not exist');
   }
-  else if (!in_array($uid, array_keys(buddylist_get_buddies($user->uid)))) {
-    return t('This user is not on your %buddy list', buddylist_translation());
+//  else if (!in_array($uid, array_keys(buddylist_get_buddies($user->uid)))) {
+//    return t('This user is not on your %buddy list', buddylist_translation());
+  else if ((!in_array($uid, array_keys(buddylist_get_friendship_requests($user->uid)))) && (!in_array($uid, array_keys(buddylist_get_buddies($user->uid))))) {
+    return t('This user has not been invited to your %buddy list', buddylist_translation());
   }
-
+  
   $form['uid'] = array('#type' => 'hidden', '#value' => $uid);
   $form['name'] = array('#type' => 'hidden', '#value' => $buddy->name);
-  return confirm_form('buddylist_deletebuddy_confirm',
-    $form,
-    t('Remove user %name from your %buddy list?', array('%name' => theme('placeholder', $buddy->name)) + buddylist_translation()),
-    $_GET['destination'],
-    ' ',
-    t('Remove'), t('Cancel'));
+  // MOD -- Check to see if you are either *removing the person* as a friend, or simply retracting the friendship invitation / ajwwong
+  if (in_array($uid, array_keys(buddylist_get_buddies($user->uid)))) {
+	  return confirm_form('buddylist_deletebuddy_confirm',
+		$form,
+		t('Remove user %name from your %buddy list?', array('%name' => theme('placeholder', $buddy->name)) + buddylist_translation()),
+	//    t('Retract %buddy list invitation to %name?', array('%name' => theme('placeholder', $buddy->name)) + buddylist_translation()),
+		$_GET['destination'],
+		' ',
+		t('Remove'), t('Cancel'));
+  }
+  else {
+	  return confirm_form('buddylist_deletebuddy_confirm',
+		$form,
+	//	t('Remove user %name from your %buddy list?', array('%name' => theme('placeholder', $buddy->name)) + buddylist_translation()),
+	    t('Retract friendship invitation to %name?', array('%name' => theme('placeholder', $buddy->name)) + buddylist_translation()),
+		$_GET['destination'],
+		' ',
+		t('Retract'), t('Cancel'));
+  }
+
 }
 
 
@@ -772,7 +895,8 @@
  */
 function buddylist_deletebuddy_confirm_submit($form_id, $form_values) {
 	buddylist_remove($form_values['uid']);
-	drupal_set_message(t('%name will be be notified of being removed.', array('%name' => theme('placeholder', $form_values['name']))));
+//  Commenting out this next line... it seems rather anti-social to "notify" people of their "removal"  ajwwong
+//	drupal_set_message(t('%name will be be notified of being removed.', array('%name' => theme('placeholder', $form_values['name']))));
   return 'user';
 };
 
@@ -781,13 +905,21 @@
   global $user;
   $user_to_add = user_load(array('uid' => $id));
 
-  if (!in_array($id, array_keys(buddylist_get_buddies($user->uid)))) {
+//  if (!in_array($id, array_keys(buddylist_get_buddies($user->uid)))) {
+// MOD  this is just to make sure we've not *invited* them, already...  ajwwong
+  if (!in_array($id, array_keys(buddylist_get_friendship_requests($user->uid))))  {
     db_query('INSERT INTO {buddylist} (received, uid, buddy, timestamp) VALUES (1, %d, %d, %d)' , $user->uid , $id , time());
     // DB value buddylist.received set to 1, meaning buddy has a message waiting
     // letting them know you added them as a buddy
     // buddylist.received set back to 0 when user logs in along with being informed of new buddy
-
-    drupal_set_message(t('%username has been added to your %buddy list', array('%username' => theme('placeholder', $user_to_add->name)) + buddylist_translation()));
+    
+	//MOD:  Now, when you "add", you are either "inviting" the other as friend or "accepting" the friendship request
+	if (!in_array($user->uid, array_keys(buddylist_get_friendship_requests($id)))) {  //Assume you've not been invited by other person already / ajwwong
+      drupal_set_message(t('%username has been invited to join your %buddy list', array('%username' => theme('placeholder', $user_to_add->name)) + buddylist_translation()));
+    }
+	else {
+	  drupal_set_message(t('You have accepted %username\'s friendship invitation', array('%username' => theme('placeholder', $user_to_add->name)) + buddylist_translation()));
+    }
   }
   else {
     drupal_set_message(t('%username is already on your %buddylist', array('%username' => theme('placeholder', $user_to_add->name)) + buddylist_translation()));
@@ -798,9 +930,18 @@
   global $user;
   db_query('DELETE FROM {buddylist} WHERE uid = %d AND buddy = %d' , $user->uid , $id);
   $thisuser = user_load(array('uid' => $id));
-  drupal_set_message(t('%username has been removed from your %buddylist', array('%username' => theme('placeholder', $thisuser->name )) + buddylist_translation()));
-}
 
+// Add this line below to break *both* connections ... this seems like the right thing to do, otherwise, there's a "hanging invitation" left
+   if (db_num_rows(db_query('SELECT * FROM {buddylist} WHERE uid = %d AND buddy = %d' , $id, $user->uid)) == 1) {
+       db_query('DELETE FROM {buddylist} WHERE uid = %d AND buddy = %d' , $id, $user->uid);
+ //And this conditional sequence has been added, because sometimes, only the *invitation* has only been retracted, ... rather than full-on friendship removed
+       drupal_set_message(t('%username has been removed from your %buddylist', array('%username' => theme('placeholder', $thisuser->name )) + buddylist_translation()));
+   }
+   else {
+       drupal_set_message(t('Your friendship invitation to %username has been retracted', array('%username' => theme('placeholder', $thisuser->name )) + buddylist_translation()));
+   }
+}   
+   
 function buddylist_cancel_add($id) {
   $thisuser = user_load(array('uid' => $id));
   drupal_set_message(t('User %name was NOT added to your %buddylist.', array('%name' => theme('placeholder', $thisuser->name)) + buddylist_translation()));
@@ -810,3 +951,4 @@
   $thisuser = user_load(array('uid' => $id));
   drupal_set_message(t('User %name was NOT removed from your %buddylist.', array('%name' => theme('placeholder', $thisuser->name)) + buddylist_translation()));
 }
+
