--- buddylist_views.inc.orig	Sun Jan 07 19:23:14 2007
+++ buddylist_views.inc	Fri Jul 20 18:28:41 2007
@@ -58,6 +58,7 @@ function buddylist_views_tables() {
         'name' => t('Buddylist: Author Is A Buddy Of Username'),
         'operator' => 'views_handler_operator_or',
         'list' => 'views_handler_filter_username',
+        'list-type' => 'select',
         'value-type' => 'array',
         'help' => t('This allows you to filter by buddies of a particular user.'),
       ),
@@ -66,11 +67,57 @@ function buddylist_views_tables() {
         'name' => t('Buddylist: Author Is A Buddy Of Current User'),
         'operator' => 'views_handler_operator_eqneq',
         'list' => 'views_handler_filter_usercurrent',
+        'list-type' => 'select',
         'help' => t('This allows you to filter by buddies of the current user.'),
         'cacheable' => 'no',
       ),
     ),
   );
+  
+  $tables['buddylist_buddy_group'] = array(
+    'name' => 'buddylist_buddy_group',
+    'join' => array(
+      'type' => 'inner',
+      'left' => array(
+        "table" => "users",
+        "field" => "uid",
+      ),
+      'right' => array(
+        'field' => 'buddy'
+      ),
+    ),
+    'fields' => array(
+      'buddy' => array(
+        'name' => t('Buddylist: Buddy Group Buddy'),
+        'help' => t('Buddy Group Buddy.'),
+      ),
+      'label_id' => array(
+        'name' => t('Buddylist: Buddy Group ID'),
+        'help' => t('Buddy Group ID.'),
+      ),
+    ),
+  );
+
+  $tables['buddylist_groups'] = array(
+    'name' => 'buddylist_groups',
+    'join' => array(
+      'type' => 'inner',
+      'left' => array(
+        "table" => "buddylist_buddy_group",
+        "field" => "label_id",
+      ),
+      'right' => array(
+        'field' => 'label_id'
+      ),
+    ),
+    'fields' => array(
+      'label_id' => array(
+        'name' => t('Buddylist: Buddy Group ID'),
+        'help' => t('Buddy Group ID.'),
+      ),
+    ),
+  );
+
   if (module_exists('usernode')) {
     $tables['usernode_buddylist'] = array(
       "name" => "buddylist",
@@ -88,6 +135,7 @@ function buddylist_views_tables() {
           'name' => t('Buddylist: Usernode Is A Buddy Of Username'),
           'operator' => 'views_handler_operator_or',
           'list' => 'views_handler_filter_username',
+          'list-type' => 'select',
           'value-type' => 'array',
           'help' => t('This allows you to filter by buddies of a particular user.'),
         ),
@@ -96,6 +144,7 @@ function buddylist_views_tables() {
           'name' => t('Buddylist: Usernode Is A Buddy Of Current User'),
           'operator' => 'views_handler_operator_eqneq',
           'list' => 'views_handler_filter_usercurrent',
+          'list-type' => 'select',
           'help' => t('This allows you to filter by buddies of the current user.'),
         ),
       ),
@@ -116,6 +165,7 @@ function buddylist_views_tables() {
           'name' => t('Buddylist: Username Is A Buddy Of Usernode'),
           'operator' => 'views_handler_operator_or',
           'list' => 'views_handler_filter_username',
+          'list-type' => 'select',
           'value-type' => 'array',
           'help' => t('This allows you to filter by buddies of a particular user.'),
         ),
@@ -124,6 +174,7 @@ function buddylist_views_tables() {
           'name' => t('Buddylist: Current User Is A Buddy Of Usernode'),
           'operator' => 'views_handler_operator_eqneq',
           'list' => 'views_handler_filter_usercurrent',
+          'list-type' => 'select',
           'help' => t('This allows you to filter by buddies of the current user.'),
         ),
       ),
@@ -144,6 +195,7 @@ function buddylist_views_tables() {
           'name' => t('Buddylist: Username Is A Buddy Of Usernode'),
           'operator' => 'views_handler_operator_or',
           'list' => 'views_handler_filter_username',
+          'list-type' => 'select',
           'value-type' => 'array',
           'help' => t('This allows you to filter by buddies of a particular user.'),
         ),
@@ -152,6 +204,7 @@ function buddylist_views_tables() {
           'name' => t('Buddylist: Current User Is A Buddy Of Usernode'),
           'operator' => 'views_handler_operator_eqneq',
           'list' => 'views_handler_filter_usercurrent',
+          'list-type' => 'select',
           'help' => t('This allows you to filter by buddies of the current user.'),
         ),
       ),
@@ -292,6 +345,9 @@ function buddylist_views_default_views()
  * Implementation of hook_views_arguments
  */
 function buddylist_views_arguments() {
+  // striving for minimal changes in the original code for easy integration
+  $arguments = array();
+
   if (module_exists('usernode')) {
     $arguments = array(
       'buddylist_buddies' => array(
@@ -305,8 +361,17 @@ function buddylist_views_arguments() {
         'help' => t('This will filter usernodes from users, of which the user with the ID passed to the view as argument is a buddy of.'),
       ),
     );
-    return $arguments;
+//    return $arguments;
   }
+
+  $arguments['buddylist_buddylists_buddyof'] =
+    array(
+      'name' => t('Buddylist: Buddylist ID'),
+      'handler' => 'buddylist_views_handler_arg_buddylists_buddyof',
+      'help' => t('The Buddylist ID argument allows users to filter to nodes authored by buddies who are in the the specified Buddylist.'), 
+    );
+
+  return $arguments;
 }
 
 /**
@@ -387,5 +452,87 @@ function buddylist_views_handler_arg_bud
       }
       $user = db_fetch_object(db_query("SELECT name FROM {users} WHERE uid = '%d'", $query));
       return check_plain($user->name);
+  }
+}
+
+
+/**
+ * Callback for buddylist_views_arguments
+ * "$op is the operation, $query is the query object being manipulated, and $a1
+ * and $a2 vary based upon the $op field."
+ */
+function buddylist_views_handler_arg_buddylists_buddyof($op, &$query, $a1, $a2 = '') {
+  switch($op) {
+    case 'summary':
+      /*
+        "$a1 is the argument type (the key to the array that defines the
+        argument), and $a2 is the option the user selected, if applicable.
+
+        The handler must create the summary query, which requires adding the
+        right fields and filters to the $query object and returning an array
+        of information that tells Views how to interpret the summary query
+        results. If needs to perform a $query->add_field() on any fields that
+        will be used to display the summary.
+      */
+      // TODO: do we need to add any tables here?
+      $query->add_table('buddylist_groups', true);
+      $query->add_table('buddylist_buddy_group', true);
+      $query->add_field('label_id', 'buddylist_groups');
+      $query->add_field('label', 'buddylist_groups');
+      $fieldinfo['field'] = "buddylist_groups.label";
+      return $fieldinfo;
+      break;
+    case 'sort':
+      /*
+        "$a1 is the sort direction; either ASC or DESC.
+        $a2 is unused.
+
+        The handler must apply the proper sorting. This handler will not always
+        be called, as the user can select "
+      */ 
+      $query->add_orderby('buddylist_groups', 'label', $a1);
+      break;
+    case 'filter':
+      /*
+        "$a1 is the full database information of the argument (including the
+        'options').
+        $a2 is the actual arg from the URL.
+
+        The handler must apply a filter for the given argument."
+      */
+      $where = db_escape_string($a2);
+      $query->add_where("buddylist_buddy_group.label_id = %d", $where);
+      break;
+    case 'link':
+      /*
+        "$query is not the query object in this case; it is actually the raw
+        data object returned from the query for this record in the summary view.
+
+        $a1 is the argument type.
+        $a2 is the base of the URL, which is usually the URL of the summary view.
+
+        During a summary view, this operation is called to provide a link to the
+        view with that argument for each result. This should be run through l()."
+      */
+
+      // $a1 is the key of the array returned by the implementation of
+      // hook_views_arguments.
+      // We have access to those fields in $query, which we added when
+      // $op == 'summary'.
+      return l($query->label, "$a2/$query->label_id");
+    case 'title':
+      /*
+        "$query is not the query object in this case; it is actually the raw
+        argument used.
+
+        $a1 is the argument type.
+        $a2 is unused.
+
+        When creating a view that uses an argument, or a breadcrumb based on
+        an argument, this is used to create the title. This is largely used to
+        translate the title into 'human readable' data."
+      */
+      $group = db_fetch_object(db_query("SELECT label FROM {buddylist_groups} WHERE label_id = %d", $query));
+      return check_plain($group->label);
   }
 }
