--- buddylist.module.cvs	Tue Jan 31 22:31:40 2006
+++ buddylist.module	Thu Feb  2 09:33:35 2006
@@ -94,16 +94,56 @@ function buddylist_groups_select($uid, $
  */
 function buddylist_settings() {
   // TODO: move these to block settings
-  $group .= form_select(t('Number of buddies to list in the user\'s buddy block'), 'buddylist_blocklisting_size', variable_get('buddylist_blocklisting_size', 5), drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)), 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/block')) ));
-  $group .= form_select(t('Number of posts to list in the buddies\' recent posts block'), 'buddylist_posts_block', variable_get('buddylist_posts_block', 7), drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)), 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/block'))  ));
-  $group .= form_textfield(t('Block title'), 'buddylist_block_title', variable_get('buddylist_block_title', t('My buddies\' recent posts')), 70, 128, t('This will be the title for the recent buddies post block. If none is specified, the default will be used.'));
-  $output .= form_group(t('Buddylist block options'), $group);
-
-  // User profile page settings
-  $group = form_select(t('Number of buddies and users who\'ve added me'), 'buddylist_prof_buddies', variable_get('buddylist_prof_buddies', 5), drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), t('The default maximum number of buddies and users who\'ve added me as a buddy to display on a user\'s profile page.'));
-  $output .= form_group(t('Profile page options'), $group);
+/* Buddylist block settings group */
+  $form['block'] = array(
+    '#type' => 'fieldset', 
+    '#title' => t('Buddylist block options'), 
+    //'#tree' => TRUE,
+  );
+  
+  $form['block']['buddylist_blocklisting_size'] = array(
+    '#type' => 'select',
+    '#title' => t('Number of buddies to list in the user\'s buddy block'),
+    '#default_value' => variable_get('buddylist_blocklisting_size', 5),
+    '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)),
+    '#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/block')) ),
+  );
+  
+  $form['block']['buddylist_posts_block'] = array(
+    '#type' => 'select',
+    '#title' => t('Number of posts to list in the buddies\' recent posts block'),
+    '#default_value' => variable_get('buddylist_posts_block', 7),
+    '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)),
+    '#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/block'))  ),
+  );
+  
+  $form['block']['buddylist_block_title'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Block title'),
+    '#default_value' => variable_get('buddylist_block_title', t('My buddies\' recent posts')),
+    '#size' => 70,
+    '#maxlength' => 128,
+    '#description' => t('This will be the title for the recent buddies post block. If none is specified, the default will be used.'),
+  );
+  
+  /* profile page settings group */
+  $form['profile'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Profile page options'),
+  );
+  
+  $form['profile']['buddylist_prof_buddies'] = array(
+    '#type' => 'select',
+    '#title' => t('Number of buddies and users who\'ve added me'),
+    '#default_value' => variable_get('buddylist_prof_buddies', 5),
+    '#options' => drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
+    '#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.'),
+  );
+  
+  return $form;
 
-  return $output;
 }
 
 
@@ -142,7 +182,10 @@ function buddylist_user($type, &$edit, &
           break;
         }
       }
-      $output .= form_item(t('Buddies'), theme('user_list', $listbuddies));
+      $fields[] = array('title' => t('Buddies'),
+        'value' => theme('item_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
@@ -154,25 +197,34 @@ function buddylist_user($type, &$edit, &
       $listbuddiesof[$row->uid] = theme('username', $row);
     }
     if ($listbuddiesof) {
-      $output .= form_item(t('Buddy of'), theme('item_list', $listbuddiesof));
+      $fields[] = array('title' => t('Buddy Of'),
+        'value' => theme('item_list', $listbuddiesof),
+        'class' => 'buddylist',
+      );
+
     }
 
     // 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')) {
-      $actions[] = l(t('Remove %name from my buddy list', array('%name' => theme('placeholder', $thisuser->name))), 'buddy/delete/' . $thisuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
+    if (@in_array($thisuser->uid, array_keys(buddylist_get_buddies($user->uid))) && user_access('edit buddy lists')) {
+      $actions[] = l(t('Remove %name from my buddy list', array('%name' => theme('placeholder', $thisuser->name))), 'buddy/delete/' . $thisuser->uid, NULL, NULL, NULL, FALSE, TRUE);
     }
     else {
-      if ($user->uid != $thisuser->uid && user_access('maintain buddy list')) {
-        $actions[] = l(t('Add %name to my buddy list', array('%name' => theme('placeholder', $thisuser->name))), 'buddy/add/' . $thisuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
+      if ($user->uid != $thisuser->uid && user_access('edit buddy lists')) {
+        $actions[] = l(t('Add %name to my buddy list', array('%name' => theme('placeholder', $thisuser->name))), 'buddy/add/' . $thisuser->uid, NULL, NULL, NULL, FALSE, TRUE);
+
       }
     }
+
     if ($actions) {
-      $output .= form_item(t('Buddy actions'), theme('item_list', $actions));
+      $fields[] = array('title' => t('Buddy Actions'),
+        'value' => theme('item_list', $actions),
+        'class' => 'buddylist',
+      );
     }
-    return array (t('Buddy List') => array($output));
+    return array(t('Buddy List') => $fields);
   }
 }
 
@@ -212,8 +264,8 @@ function buddylist_block($op = 'list', $
         break;
 
       case 1: // Shows my buddies recent posts block
-				$buddies = buddylist_get_buddies();
-				$str_buddies = implode(',', array_keys($buddies));
+        $buddies = buddylist_get_buddies();
+        $str_buddies = implode(',', array_keys($buddies));
         $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));
 
         if (db_num_rows($result)) {
@@ -238,7 +290,7 @@ function buddylist_block($op = 'list', $
  * Implementation of hook_perm
  */
 function buddylist_perm() {
-  return array('maintain buddy list', 'view buddy lists');
+  return array('edit buddy lists', 'view buddy lists');
 }
 
 /**
@@ -248,11 +300,11 @@ function buddylist_menu($may_cache) {
   global $user;
 
   $links = array();
-  $id = arg(1) ? arg(1) : $user->uid;
+  $id = (arg(1) ? arg(1) : $user->uid);
   if ($may_cache) {
-    $links[] = array('path' => 'buddylist', 'title' => t('my buddylist'), 'access' => user_access('view buddy lists'), 'callback' => 'buddylist_buddylisting_page');
+    $links[] = array('path' => 'buddylist', 'title' => t('my buddylist'), 'access' => (user_access('view buddy lists') && $user->uid), 'callback' => 'buddylist_buddylisting_page');
   }
-  elseif ($id == $user->uid || user_access('administer users')) {
+  elseif ($id == $user->uid || user_access('administer users') || user_access('edit buddy lists')) {
 		//tabs
 	    $links[] = array('path' => 'buddylist/'. $id .'/buddies', 'title' => t('buddies'), 'access' => user_access('view buddy lists'), 'callback' => 'buddylist_buddylisting_page', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -1, 'callback arguments' => array($id));
 	    $links[] = array('path' => 'buddylist/'. $id .'/buddiesof', 'title' => t('buddies of'), 'access' => user_access('view buddy lists'), 'callback' => 'buddylist_buddylisting_page','type' => MENU_LOCAL_TASK, 'weight' => 1, 'callback arguments' => array($id, 'buddiesof'));
@@ -264,8 +316,8 @@ function buddylist_menu($may_cache) {
 	    // RSS feeds
 	    $links[] = array('path' => 'buddylist/'. $id .'/buddies/recent/feed', 'title' => t('xml feed'), 'access' => user_access('view buddy lists'), 'callback' => 'buddylist_buddyfeed', 'type' => MENU_CALLBACK, 'callback arguments' => array($id));
 	    // other callbacks
-	    $links[] = array('path' => 'buddy/add', 'title' => t('add to buddylist'), 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_addbuddy', 'type' => MENU_CALLBACK);
-	    $links[] = array('path' => 'buddy/delete', 'title' => t('delete from buddylist'), 'access' => user_access('maintain buddy list'), 'callback' => 'buddylist_deletebuddy', 'type' => MENU_CALLBACK);
+	    $links[] = array('path' => 'buddy/add', 'title' => t('add to buddylist'), 'access' => user_access('edit buddy lists'), 'callback' => 'buddylist_addbuddy', 'type' => MENU_CALLBACK);
+	    $links[] = array('path' => 'buddy/delete', 'title' => t('delete from buddylist'), 'access' => user_access('edit buddy lists'), 'callback' => 'buddylist_deletebuddy', 'type' => MENU_CALLBACK);
 	  }
   return $links;
 }
@@ -282,8 +334,8 @@ function buddylist_buddylisting_page($ui
   if (empty($uid)) {
     $uid = $user->uid;
   }
-	elseif ($uid != $user->uid && !user_access('administer users')) {
-    // a normal user caan only view own buddylist
+  elseif ($uid != $user->uid && !user_access('administer users')) {
+    // a normal user can only view own buddylist
     drupal_access_denied();
     exit();
   }
@@ -335,7 +387,7 @@ function buddylist_buddiesrecent_page($u
   drupal_set_title(t('%username\'s buddylist', array('%username' => $thisuser->name)));
 
   $output = '';
-  $result = pager_query(db_rewrite_sql('SELECT n.nid, n.type, n.status FROM {node} n LEFT JOIN {buddylist} b ON n.uid = b.buddy WHERE n.status = 1 AND b.uid = %d ORDER BY n.nid DESC'), variable_get('default_nodes_main', 10), 0, NULL, $uid);
+  $result = pager_query(db_rewrite_sql('SELECT n.nid, n.type, n.status FROM {node} n LEFT JOIN {buddylist} b ON n.uid = b.buddy WHERE n.status = 1 AND b.uid = %d and b.label = \'all\' ORDER BY n.nid DESC'), variable_get('default_nodes_main', 10), 0, NULL, $uid);
 
   while ($node = db_fetch_object($result)) {
     $output .= node_view(node_load($node->nid), TRUE);
@@ -370,42 +422,74 @@ function buddylist_buddiesgroups_form($u
   $thisuser = user_load(array('uid' => $uid));
   drupal_set_title(t('%username\'s buddy groups', array('%username' => $thisuser->name)));
 
-	if ($edit = $_POST['edit']) {
-	  $sql = "DELETE FROM {buddylist} WHERE uid=%d AND label != 'all'";
-		$result = db_query($sql, $uid);
-	  foreach ($edit['buddylist_labels'] as $buddy => $str_labels) {
-	    $labels = explode(',', $str_labels);
-	    foreach ($labels as $label) {
-			  $sql = "INSERT INTO buddylist (uid, buddy, label, timestamp, received) VALUES (%d, %d, '%s', %d, 0)";
-				db_query($sql, $uid, $buddy, trim($label), time());
-			}
-	  }
-		drupal_set_message(t('buddy groups saved.'));
-	}
+  if ($edit = $_POST['edit'] && count($_POST['buddylist_labels'])) {
+    $sql = "DELETE FROM {buddylist} WHERE uid=%d AND label != 'all'";
+    $result = db_query($sql, $uid);
+    for($cnt = 0; $cnt < count($_POST['buddylist_labels']); $cnt++) {
+      foreach($_POST['buddylist_labels'][$cnt] as $buddy => $str_labels) {
+        $labels = explode(',', $str_labels);
+        foreach ($labels as $label) {
+          $sql = "INSERT INTO buddylist (uid, buddy, label, timestamp, received) VALUES (%d, %d, '%s', %d, 0)";
+          db_query($sql, $uid, $buddy, trim($label), time());
+        }
+      }
+    }
+    drupal_set_message(t('buddy groups saved.'));
+  }
 	
-	if ($buddies = buddylist_get_buddies($userid)) {
-	  foreach ($buddies as $uid => $accounts) {
-	    $items = array();
-			foreach ($accounts as $account) {
-			  if ($account->label != 'all') {
-					$items[] = $account->label;	
-			  }
-			}
-			if (!$val = $edit['buddylist_labels'][$account->uid]) {
-			  $val = implode(',', $items);
-			}
-			$rows[] = array(theme('username', $account), form_textfield('', "buddylist_labels]". $account->uid, $val, 70, 255));
-		}
-		$headers = array(t('buddy'), t('buddy groups'));
-		$output .= theme('table', $headers, $rows);
-		$output .= form_button(t('Submit'));
-		return form($output);
-	}
-	else {
-	  drupal_set_message(t('No buddy groups'));
-	}
+  if ($buddies = buddylist_get_buddies($userid)) {
+
+    $table_output = do_buddygroup_form($buddies,$uid,$accounts,$edit);
+
+    $form['table'] = array(
+      '#type' => 'markup',
+      '#value' => $table_output,
+    );
+
+    $form['submit'] = array(
+      '#type' => 'submit',
+      '#value' => t('Submit'),
+    );
+    $output .= drupal_get_form('',$form);
+    return $output;
+      
+  }
+  else {
+    return theme('placeholder',t('Unable to edit buddy groups. No buddies found.'));
+  }
+}
+
+
+
+function do_buddygroup_form($buddies,$uid,$accounts,$edit) {
+    
+  foreach ($buddies as $uid => $accounts) {
+    $items = array();
+    foreach ($accounts as $account) {
+      if ($account->label != 'all') {
+        $items[] = $account->label;	
+      }
+    }
+    $val = implode(',', $items);
+
+    $form['buddylist_labels'] = array(
+      '#type' => 'textfield',
+      '#title' => t('buddy groups'),
+      '#name' => "buddylist_labels[][$account->uid]",
+      '#value' => $val,
+      '#size' => 70,
+      '#maxlength' => 255,
+    );
+
+    $rows[] = array(theme('username', $account),form_render($form['buddylist_labels'])); 
+  }
+    
+  $headers = array(t('buddy'), t('buddy groups'));
+  $output .= theme('table', $headers, $rows);
+  return $output;
 }
 
+
 /**
  * Feed for buddies recent posts
  */
@@ -422,7 +506,7 @@ function buddylist_buddyfeed($uid) {
 
 function buddylist_addbuddy($uid) {
   global $user;
-
+  
   $buddy = user_load(array('uid' => $uid));
   if (empty($buddy->name)) {
     return t('This user does not exist');
@@ -433,13 +517,16 @@ function buddylist_addbuddy($uid) {
   elseif ($user->uid == $uid) {
     return t('Cannot add yourself to buddy list');
   }
-  elseif (($_POST['op'] == t('add user')) && $_POST['edit']['confirm']) {
+  elseif (($_POST['op'] == t('add user')) && $_POST['edit']['add buddy']) {
     buddylist_add($uid);
     drupal_goto('buddylist');
   }
+  
+  return confirm_form('confirm add buddy', NULL, t('Add user %name to your buddy list?', array('%name' => $buddy->name)), 
+                      'user', t('%name will be be notified the next time s/he logs in.', array('%name' => $buddy->name)),
+                       t('add user'), NULL, 'add buddy');
+  
 
-  return theme('confirm', t('Add user %name to your buddy list?', array('%name' => $buddy->name)), 'user',
-           t('%name will be be notified the next time s/he logs in.', array('%name' => $buddy->name)), t('add user'));
 }
 
 function buddylist_deletebuddy($uid) {
@@ -452,13 +539,14 @@ function buddylist_deletebuddy($uid) {
   else if (!in_array($uid, array_keys(buddylist_get_buddies($user->uid)))) {
     return t('This user is not on your buddy list');
   }
-  elseif (($_POST['op'] == t('remove user')) && $_POST['edit']['confirm']) {
+  elseif (($_POST['op'] == t('remove buddy')) && $_POST['edit']['remove buddy']) {
     buddylist_remove($uid);
     drupal_goto('buddylist');
   }
 
-  return theme('confirm', t('Remove user %name from your buddy list?', array('%name' => $buddy->name)), 'user',
-           t('%name will be not be notified the next time s/he logs in.', array('%name' => $buddy->name)), t('remove user'));
+  return confirm_form('confirm remove buddy', NULL, t('Remove user %name from your buddy list?', array('%name' => $buddy->name)), 
+                      'user', t('%name will be be notified the next time s/he logs in.', array('%name' => $buddy->name)),
+                       t('remove buddy'), NULL, 'remove buddy');
 }
 
 function buddylist_add($id) {
@@ -492,5 +580,5 @@ function buddylist_cancel_add($id) {
 
 function buddylist_cancel_remove($id) {
   $thisuser = user_load(array('uid' => $id));
-  drupal_set_message(t('User %name was NOT removed to your buddylist.', array('%name' => theme('placeholder', $thisuser->name))));
+  drupal_set_message(t('User %name was NOT removed from your buddylist.', array('%name' => theme('placeholder', $thisuser->name))));
 }
