Index: user_relationships_actions.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/user_relationships/user_relationships_actions.inc,v
retrieving revision 1.6
diff -u -r1.6 user_relationships_actions.inc
--- user_relationships_actions.inc	14 Sep 2007 21:44:20 -0000	1.6
+++ user_relationships_actions.inc	18 Sep 2007 00:46:05 -0000
@@ -214,3 +214,84 @@
 
   return 'relationships/'. $form_values['viewed_id'];
 }
+
+/**
+ * Validate settings on the block configuration page(s)
+ */
+function _user_relationships_block_validate_num_rels($form) {
+  $num_rels = $form['#value'];
+  if (!is_numeric($num_rels) || (int)$num_rels != $num_rels || (int)$num_rels <= 0) {
+    form_set_error('user_relationships_block_num_rels', t('Number of users must be an integer greater than 0.'));
+    return FALSE;
+  } 
+  return TRUE;
+} // function _user_relationships_block()
+
+/**
+ * function _user_relationships_block_select_relationships
+ *   given a $user object, select which of their relationships to display in the block
+ * 
+ * @param $user - user object of user whose relationships we want to select
+ * @param $rtid - type id of relationships we're interested in, or 'ALL' for all relationships
+ * @param $how_many - how many relationships to return
+ * @param $options - array of (text) options presented to admins to allow them to select which n relationships to display
+ * @param $options_index - index into the $options array indicating which option the admin has selected for this type
+ *
+ * @return array of relationship objects
+ */
+function _user_relationships_block_select_relationships($user, $rtid, $how_many, $options, $options_index) {
+
+  if (is_null($user->relationships)) return NULL;
+
+  $relationships = $user->relationships;
+
+  // get rid of irrelevant relationships
+  if ('ALL' != $rtid) {
+    for ($i = 0; isset($relationships[$i]); $i++) {
+      if ($relationships[$i]->rtid != $rtid) unset($relationships[$i]);
+    } // for ($i = 0; isset($relationships[$i]); $i++)
+  } // if ('ALL' != $rtid)
+
+  if (0 == count($relationships)) return NULL;
+
+  // trim the list as indicated by $how_many and $options[$options_index]
+  if (count($relationships) > $how_many) {
+    switch (drupal_strtolower($options[$options_index])) {
+      case 'newest':
+	usort($relationships, '_user_relationships_compare_newest_first');
+	$relationships = array_slice($relationships, 0, $how_many);
+	break;
+      case 'oldest':
+	usort($relationships, '_user_relationships_compare_oldest_first');
+	$relationships = array_slice($relationships, 0, $how_many);
+	break;
+      case 'random':
+	$random_rels = array();
+	$keys = array_rand($relationships, $how_many);
+	foreach ($keys as $key) {
+	  $random_rels[] = $relationships[$key];
+	} // foreach ($keys as $key)
+	$relationships = $random_rels;
+	break;
+      default:
+	// should never get here!!!!
+	break;
+    } // switch ($options[$option_index])
+  } // if (count(relationships) > $how_many)
+
+  return $relationships;
+} // function _user_relationships_block_select_relatees($user, $rtid, $how_many, $options, $options_index)
+
+/**
+ * sort relationships by date in ascending order
+ */
+function _user_relationships_compare_oldest_first($r1, $r2) {
+  return strcmp($r1->updated_at, $r2->updated_at);
+} // function _user_relationships_compare_oldest_first($r1, $r2)
+
+/**
+ * sort relationships by date in descending order
+ */
+function _user_relationships_compare_newest_first($r1, $r2) {
+  return -strcmp($r1->updated_at, $r2->updated_at);
+} // function _user_relationships_compare_newest_first($r1, $r2)
Index: user_relationships_hooks.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/user_relationships/user_relationships_hooks.inc,v
retrieving revision 1.3
diff -u -r1.3 user_relationships_hooks.inc
--- user_relationships_hooks.inc	12 Sep 2007 20:27:47 -0000	1.3
+++ user_relationships_hooks.inc	18 Sep 2007 00:46:05 -0000
@@ -306,4 +306,109 @@
   // remember when we last expired relationships
   variable_set('user_relationships_last_expire', $now);
   return TRUE;
-}
\ No newline at end of file
+}
+
+/**
+ * Implementation for hook_block
+ */
+function user_relationships_block($op = 'list', $delta = 0, $edit = array()) {
+  $all_types = 'ALL';
+
+  if (0 != strcmp($op, 'list')) {
+    $block_rtype = explode('|', $delta);
+    $block =  $block_rtype[0]; // 'MY' or 'USER'
+    $rtype =  $block_rtype[1]; // valid rtid or $all_types
+  } // if (!strcmp($op, 'list')
+
+  $which_rels_options = array(1 => 'Newest',
+			      2 => 'Oldest',
+			      3 => 'Random'
+			      );
+  $default_option = 1;
+
+  switch ($op) {
+    case 'list':
+      // return list of all blocks defined by the module
+      $relationships = user_relationships_relationship_types_load();
+      $block = array();
+      foreach ($relationships as $relationship) {
+        $block["MY|{$relationship->rtid}"]['info']   = t('My Relationships: @type',
+							 array('@type' => drupal_ucfirst($relationship->plural_name)));
+        $block["USER|{$relationship->rtid}"]['info'] = t('User Relationships: @type',
+							 array('@type' => drupal_ucfirst($relationship->plural_name)));
+      }
+      $block["MY|{$all_types}"]['info']   = t('My Relationships: All relationships');
+      $block["USER|{$all_types}"]['info'] = t('User Relationships: All relationships');
+      return $block;
+      break;
+
+    case 'configure':
+      $form = array();
+      $form['user_relationships_block_num_rels'] = array(
+	'#type'          => 'textfield',
+	'#title'         => t('Number of relationships to display in block'),
+	'#description'   => t('Enter the maximum number of relationships to display in this block.'),
+	'#size'          => 4,
+	'#default_value' => variable_get("user_relationships_block_{$block}_t_{$rtype}_num_rels", 8),
+	'#validate'      => array('_user_relationships_block_validate_num_rels' => array())
+	);
+      $form['user_relationships_block_which_rels'] = array(
+	'#type'          => 'radios',
+	'#title'         => t('Which relationships should be displayed'),
+	'#options'       => $which_rels_options,
+	'#default_value' => variable_get("user_relationships_block_{$block}_t_{$rtype}_which_rels", $default_option),
+      );
+      return $form;
+      break;
+
+    case 'save':
+      variable_set("user_relationships_block_{$block}_t_{$rtype}_num_rels",
+		   (int)$edit['user_relationships_block_num_rels']);
+      variable_set("user_relationships_block_{$block}_t_{$rtype}_which_rels",
+		   (int)$edit['user_relationships_block_which_rels']);
+      break;
+
+    case 'view':
+      global $user;
+      // determine which user's relationships to display
+      if (0 == strcmp($block,'MY')) {
+	$viewing_user =& $user;
+      }
+      else {
+	// if displaying a block for a user other than the person logged in, only display it on user nodes
+	if (module_exists('usernode')) {
+	  if (0 == strcmp(arg(0), 'node') && is_numeric(arg(1))) {
+	    $node = node_load(arg(1));
+	    if (0 == strcmp($node->type, 'usernode')) {
+	      $viewing_user = user_load(array('uid' =>$node->uid));
+	    } // if (0 == strcmp($node->type, 'usernode'))
+	  } // if (0 == strcmp(arg(0), 'node') && is_numeric(arg(1)))
+	} // if (module_exists('usernode'))
+      } // if (0  == strcmp($block,'USER'))
+
+      // if no user, bail
+      if (!isset($viewing_user)) return NULL;
+
+      // select the appropriate set of relationships based on admin's configuration settings
+      $relationships = _user_relationships_block_select_relationships(
+		         $viewing_user,
+			 $rtype,
+			 variable_get("user_relationships_block_{$block}_t_{$rtype}_num_rels", 8),
+			 $which_rels_options,
+			 variable_get("user_relationships_block_{$block}_t_{$rtype}_which_rels", $default_option)
+			 );
+
+      // get the content of the block
+      $blk['subject'] = theme('user_relationships_relationships_block_subject',
+			      $viewing_user,
+			      $rtype,
+			      (($rtype == $all_types) ? TRUE : FALSE));
+      $blk['content'] = ($relationships != NULL)
+	? theme('user_relationships_relationships_block_content',
+		$viewing_user, $relationships, (($rtype == $all_types) ? TRUE : FALSE))
+	: theme('user_relationships_relationships_block_empty',
+		$viewing_user, $rtype, (($rtype == $all_types) ? TRUE : FALSE));
+      return $blk;
+      break;
+  } // switch ($op)
+} // function user_relationships_block()
Index: user_relationships_theme.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/user_relationships/user_relationships_theme.inc,v
retrieving revision 1.6
diff -u -r1.6 user_relationships_theme.inc
--- user_relationships_theme.inc	17 Sep 2007 18:44:40 -0000	1.6
+++ user_relationships_theme.inc	18 Sep 2007 00:46:05 -0000
@@ -178,3 +178,74 @@
 
   return url($url, NULL, NULL, TRUE); 
 }
+
+/**
+ * Generate the title of a My/User Relationships block
+ */
+function theme_user_relationships_relationships_block_subject($viewing_user, $rtid, $all_types = FALSE) {
+  global $user;
+  $user_name = ($viewing_user->uid == $user->uid) ? 'My' : $viewing_user->name . "'s";
+  if ($all_types) {
+    $type_name = 'Relationships';
+  }
+  else {
+    $rtype = user_relationships_relationship_type_load(array('rtid' => $rtid));
+    $type_name = drupal_ucfirst($rtype->plural_name);
+  }
+  $output = t('@user @type', array('@user' => $user_name, '@type' => $type_name));
+  return $output;
+} // function theme_user_relationships_relationships_block_subject()
+
+/**
+ * Generate the content of a (non-empty) My/User Relationships block
+ *
+ * Only called if there are relationships to display
+ */
+function theme_user_relationships_relationships_block_content($viewing_user, $relationships, $all_types) {
+  $rows[] = array();
+
+  if ($all_types) {
+    // build an array of relationship type names indexed by rtid
+    $rtypes = user_relationships_relationship_types_load();
+    foreach ($rtypes as $rtype) {
+      $relationship_type[$rtype->rtid] = $rtype->name;
+    }
+  }
+
+  foreach ($relationships as $relationship) {
+    $relatee_id = ($viewing_user->uid == $relationship->requester_id)
+      ? $relationship->requestee_id
+      : $relationship->requester_id;
+    $relatee = user_load(array('uid' => $relatee_id));
+    $rows[] = $all_types
+      ? array(t('!name', array('!name' => theme('username', $relatee))),
+	      t('@type', array('@type' => $relationship_type[$relationship->rtid])))
+      : array(t('!name', array('!name' => theme('username', $relatee))));
+  }
+  $output = theme('table', array(), $rows);
+
+  return $output;
+} // function theme_user_relationships_relationships_block_content($relationships)
+
+/**
+ * Generate the content of an empty My/User Relationships block
+ */
+function theme_user_relationships_relationships_block_empty($viewing_user, $rtid, $all_types) {
+  global $user;
+
+  if ($all_types) {
+    $rtype_name = 'relationships';
+  }
+  else {
+    $rtype = user_relationships_relationship_type_load(array('rtid' => $rtid));
+    $rtype_name = $rtype->plural_name;
+  }
+
+  if ($viewing_user->uid == $user->uid) {
+    return t('You have no @rels', array('@rels' => $rtype_name));
+  }
+  else {
+    return t('!name has no @rels', array('!name' => theme('username', $viewing_user), '@rels' => $rtype_name));
+  }
+  
+} // function theme_user_relationships_relationships_block_empty($user, $rtid)
