From f6a77908ca23fed702e17f0bfe86c8c064c4f630 Mon Sep 17 00:00:00 2001
From: Sascha Grossenbacher <saschagros@gmail.com>
Date: Sat, 2 Apr 2011 18:14:53 +0200
Subject: [PATCH] Issue #1102476 by Berdir: Revamp relationship listings and local tasks using renderable arrays and alter hooks instead of templates.

---
 .../user_relationship_elaborations.module          |   23 +-
 user_relationships.module                          |   13 +-
 .../templates/user_relationships.tpl.php           |   34 ---
 .../user_relationships_pending_requests.tpl.php    |   53 -----
 .../user_relationships_ui.api.php                  |   38 +++
 .../user_relationships_ui.install                  |   53 +++++
 user_relationships_ui/user_relationships_ui.module |  238 ++++++++++++--------
 .../user_relationships_ui.pages.inc                |  205 ++++++++++++++++-
 8 files changed, 460 insertions(+), 197 deletions(-)
 delete mode 100644 user_relationships_ui/templates/user_relationships.tpl.php
 delete mode 100644 user_relationships_ui/templates/user_relationships_pending_requests.tpl.php
 create mode 100644 user_relationships_ui/user_relationships_ui.api.php

diff --git a/user_relationship_elaborations/user_relationship_elaborations.module b/user_relationship_elaborations/user_relationship_elaborations.module
index b6da2ba..bb4726d 100644
--- a/user_relationship_elaborations/user_relationship_elaborations.module
+++ b/user_relationship_elaborations/user_relationship_elaborations.module
@@ -91,25 +91,28 @@ function _user_relationship_elaborations_walk_recursive($action, &$relationships
           $data[$relationship->rid] = $relationship->rid;
         }
       }
-      elseif (isset($data[$relationship->rid]) && $data[$relationship->rid]) {
-        $relationship->elaboration = $data[$relationship->rid];
+      else {
+        $relationship->elaboration = isset($data[$relationship->rid]) ? $data[$relationship->rid] : NULL;
       }
     }
   }
 }
 
-
 /**
- * Add elaborations to relationships page through MODULE_preprocess_HOOK
+ * Implements hook_user_relationships_ui_table_header_alter().
  */
-function user_relationship_elaborations_preprocess_user_relationships(&$variables) {
-  if (!variable_get('user_relationships_elaborations_api_only', FALSE)) {
-    foreach ($variables['relationships'] as $rid => $relationship) {
-      $variables['relationships'][$rid]->extra_for_display .= isset($variables['relationships'][$rid]->elaboration) ? check_plain($variables['relationships'][$rid]->elaboration) : '';
-    }
-  }
+function user_relationship_elaborations_user_relationships_ui_table_header_alter(&$header) {
+  $insert_index = array_search(t('Relationship'), $header) + 1;
+  $header = array_merge(array_slice($header, 0, $insert_index), array(t('Comments')), array_slice($header, $insert_index));
 }
 
+/**
+ * Implements hook_user_relationships_ui_table_header_alter().
+ */
+function user_relationship_elaborations_user_relationships_ui_table_row_alter(&$row, $relationship) {
+  $insert_index = variable_get('user_relationships_show_user_pictures', 0) + 2;
+  $row = array_merge(array_slice($row, 0, $insert_index), array($relationship->elaboration), array_slice($row, $insert_index));
+}
 
 /**
  * hook_form_alter() to catch the approval form
diff --git a/user_relationships.module b/user_relationships.module
index 07bd68a..14aa2ec 100644
--- a/user_relationships.module
+++ b/user_relationships.module
@@ -123,6 +123,10 @@ function _user_relationships_generate_query($param = array(), $options = array()
       $arguments[] = $value;
       break;
 
+    case 'exclude_rtids':
+      $query->condition('ur.rtid', $value, 'NOT IN');
+      break;
+
     case 'requester_id':
     case 'requestee_id':
       $twoway_reverse_clause = TRUE;//#479486 these columns automatically should exclude duplicates
@@ -187,6 +191,11 @@ function _user_relationships_generate_query($param = array(), $options = array()
       ->limit($paging);
     $query->setCountQuery($count);
   }
+  if (!empty($header)) {
+    $query = $query
+      ->extend('TableSort')
+      ->orderByHeader($header);
+  }
   return $query;
 }
 
@@ -346,10 +355,10 @@ function user_relationships_types_load($reset = FALSE) {
   if ($reset || !isset($relationship_types_list)) {
     // Clear the cached list, since some relationships may have disappeared.
     $relationship_types_list = db_query("SELECT * FROM {user_relationship_types}")->fetchAllAssoc('rtid');
-    if (empty($relationship_types_list)) {
+    //if (empty($relationship_types_list)) {
       // No relationships, avoid querying role permissions.
       return $relationship_types_list;
-    }
+    //}
     // Load role permissions for all types.
     $result = db_query('SELECT rtid, rid FROM {user_relationship_type_roles} WHERE rtid IN (:rtids) ORDER BY rtid, rid', array(':rtids' => array_keys($relationship_types_list)));
     foreach ($result as $mapping) {
diff --git a/user_relationships_ui/templates/user_relationships.tpl.php b/user_relationships_ui/templates/user_relationships.tpl.php
deleted file mode 100644
index aff72e4..0000000
--- a/user_relationships_ui/templates/user_relationships.tpl.php
+++ /dev/null
@@ -1,34 +0,0 @@
-<?php
-
-//$relationships array is loaded in template_preprocess_user_relationships()
-if ($relationships) {
-  foreach ($relationships as $relationship) {
-    $edit_access = ($user->uid == $account->uid && user_access('maintain own relationships')) || user_access('administer user relationships');
-
-    $this_user_str  = $account->uid == $relationship->requestee_id ? 'requester' : 'requestee';
-    $this_user      = $relationship->{$this_user_str};
-
-    $row = array(
-      theme('username', array('account' => $this_user)),
-      ur_tt("user_relationships:rtid:$relationship->rtid:name", $relationship->name) . ($relationship->is_oneway ? ($this_user_str == 'requestee' ? t(' (You to Them)') : t(' (Them to You)')) : NULL),
-      !empty($relationship->extra_for_display) ? $relationship->extra_for_display : '',
-      $edit_access ? theme('user_relationships_remove_link', array('uid' => $account->uid, 'rid' => $relationship->rid)) : '&nbsp;',
-    );
-    if (variable_get('user_relationships_show_user_pictures', 0)) {
-      array_unshift($row, theme('user_picture', array('account' => $this_user)));
-    }
-    $rows[] = $row;
-  }
-
-  print theme('table', array('rows' =>  $rows, 'attributes' => array('class' => array('user-relationships-listing-table'))));
-  print theme('pager');
-}
-else {
-  if (!empty($rtid)) {
-    print t('You do not have any %plural_name.', array('%plural_name' => ur_tt("user_relationships:rtid:$relationship_type->rtid:plural_name", $relationship_type->plural_name)));
-  }
-  else {
-    print t('You do not have any relationships with other users.');
-  }
-}
-?>
diff --git a/user_relationships_ui/templates/user_relationships_pending_requests.tpl.php b/user_relationships_ui/templates/user_relationships_pending_requests.tpl.php
deleted file mode 100644
index befbb7b..0000000
--- a/user_relationships_ui/templates/user_relationships_pending_requests.tpl.php
+++ /dev/null
@@ -1,53 +0,0 @@
-<?php
-/**
- * @file
- * Page to manage sent and received relationship requests
- */
-
-  $output = '';
-  $pager_id = 0;
-  $section_headings = array(
-    'sent_requests'     => t('Sent Requests'),
-    'received_requests' => t('Received Requests')
-  );
-
-  $edit_access = ($user->uid == $account->uid && user_access('maintain own relationships')) || user_access('administer user relationships');
-
-  $i = 0;
-  foreach ($sections as $column => $section) {
-    if (empty($$section)) { continue; }
-    $rows = array();
-
-    $header = array(array('data' => $section_headings[$section], 'colspan' => 2));
-
-    foreach ($$section as $relationship) {
-      $links = array();
-      if ($edit_access) {
-        if ($section == 'sent_requests') {
-          $links[] = theme('user_relationships_pending_request_cancel_link', array('uid' => $account->uid, 'rid' => $relationship->rid));
-        }
-        else {
-          $links[] = theme('user_relationships_pending_request_approve_link', array('uid' => $account->uid, 'rid' => $relationship->rid));
-          $links[] = theme('user_relationships_pending_request_disapprove_link', array('uid' => $account->uid, 'rid' => $relationship->rid));
-        }
-      }
-      $links = implode(' | ', $links);
-
-      if ($relationship->requester_id == $account->uid) {
-        $rows[]   = array(t('@rel_name to !requestee', array('@rel_name' => ur_tt("user_relationships:rtid:$relationship->rtid:name", $relationship->name), '!requestee' => theme('username', array('account' => $relationship->requestee)))), $links);
-      }
-      else {
-        $rows[]   = array(t('@rel_name from !requester', array('@rel_name' => ur_tt("user_relationships:rtid:$relationship->rtid:name", $relationship->name), '!requester' => theme('username', array('account' => $relationship->requester)))), $links);
-      }
-    }
-
-    $output .= theme('table', array('rows' => $rows, 'header' => $header, 'attributes' => array('class' => array('user-relationships-pending-listing-table'))));
-    $output .= theme('pager', array('element' => $i++));
-  }
-
-  if (empty($output)) {
-    $output = t('You do not have any relationship requests currently pending.');
-  }
-
-  print $output;
-?>
diff --git a/user_relationships_ui/user_relationships_ui.api.php b/user_relationships_ui/user_relationships_ui.api.php
new file mode 100644
index 0000000..e11d385
--- /dev/null
+++ b/user_relationships_ui/user_relationships_ui.api.php
@@ -0,0 +1,38 @@
+<?php
+
+/**
+ * @file
+ * Hook documentation for User Relationships UI.
+ */
+
+/**
+ * Alter the table header definition of relationship listings.
+ *
+ * @param $header
+ *   Array with the table header definition.
+ * @param $edit_access
+ *   TRUE if the current user has edit access.
+ *
+ * @see user_relationships_ui_get_table_header()
+ */
+function hook_user_relationships_ui_table_header_alter(&$header, $edit_access) {
+  $insert_index = array_search(t('Relationship'), $header) + 1;
+  $header = array_merge(array_slice($header, 0, $insert_index), array(t('Comments')), array_slice($header, $insert_index));
+}
+
+/**
+ * Alter the table rows of relationship listings.
+ *
+ * @param $row
+ *   Array with the table row content.
+ * @param $relationship
+ *   Relationship object.
+ * @param $account
+ *   User account for which the relationships are shown.
+ *
+ * @see user_relationships_ui_get_table_row()
+ */
+function user_relationship_elaborations_user_relationships_ui_table_row_alter(&$row, $relationship, $account) {
+  $insert_index = variable_get('user_relationships_show_user_pictures', 0) + 2;
+  $row = array_merge(array_slice($row, 0, $insert_index), array($relationship->elaboration), array_slice($row, $insert_index));
+}
\ No newline at end of file
diff --git a/user_relationships_ui/user_relationships_ui.install b/user_relationships_ui/user_relationships_ui.install
index 240ff4a..8acd47e 100644
--- a/user_relationships_ui/user_relationships_ui.install
+++ b/user_relationships_ui/user_relationships_ui.install
@@ -3,6 +3,31 @@
  * @file Installation functions for User Relationships UI module
  */
 
+function user_relationships_ui_schema() {
+  $schema['user_relationships_ui_settings'] = array(
+    'fields' => array(
+      'rtid' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'hide' => array(
+        'type' => 'int',
+        'size' => 'tiny',
+        'not null' => TRUE,
+      ),
+      'show_tab' => array(
+        'type' => 'int',
+        'size' => 'tiny',
+        'not null' => TRUE,
+      ),
+    ),
+    'unique keys' => array(
+      'rtid'  => array('rtid'),
+    ),
+  );
+  return $schema;
+}
+
 /**
  * Enable renamed user_relationships module and update changed variables.
  */
@@ -26,3 +51,31 @@ function user_relationships_ui_update_7001() {
   }
   variable_del('user_relationships_api_author_pane_rtids');
 }
+
+/**
+ * Install {user_relationships_ui_settings} table.
+ */
+function user_relationships_ui_update_7002() {
+  $schema['user_relationships_ui_settings'] = array(
+    'fields' => array(
+      'rtid' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'hide' => array(
+        'type' => 'int',
+        'size' => 'tiny',
+        'not null' => TRUE,
+      ),
+      'show_tab' => array(
+        'type' => 'int',
+        'size' => 'tiny',
+        'not null' => TRUE,
+      ),
+    ),
+    'unique keys' => array(
+      'rtid'  => array('rtid'),
+    ),
+  );
+  db_create_table('user_relationships_ui_settings', $schema['user_relationships_ui_settings']);
+}
diff --git a/user_relationships_ui/user_relationships_ui.module b/user_relationships_ui/user_relationships_ui.module
index a4392e9..f161bde 100644
--- a/user_relationships_ui/user_relationships_ui.module
+++ b/user_relationships_ui/user_relationships_ui.module
@@ -309,34 +309,55 @@ function user_relationships_ui_menu() {
     'menu_name' => 'user-menu',
   );
   $items['relationships/list'] = array(
-    'title' => 'All',
+    'title' => 'Current',
     'type' => MENU_DEFAULT_LOCAL_TASK,
     'weight' => -10,
     'access callback' => 'user_relationships_ui_check_access',
     'access arguments' => array('view'),
     'menu_name' => 'user-menu',
   );
-  $items['relationships/requests'] = array(
-    'title' => 'Pending',
+  $items['relationships/received'] = array(
+    'title' => 'Received requests',
+    'title callback' => 'user_relationships_ui_title_callback_pending',
+    'title arguments' => array('requestee_id'),
     'access callback' => 'user_relationships_ui_check_access',
     'access arguments' => array('edit'),
     'type' => MENU_LOCAL_TASK,
     'weight' => -9,
     'page callback' => 'user_relationships_pending_requests_page',
+    'page arguments' => array('requestee_id'),
     'file' => 'user_relationships_ui.pages.inc',
     'menu_name' => 'user-menu',
   );
+  $items['relationships/sent'] = array(
+    'title' => 'Sent requests',
+    'title callback' => 'user_relationships_ui_title_callback_pending',
+    'title arguments' => array('requester_id'),
+    'access callback' => 'user_relationships_ui_check_access',
+    'access arguments' => array('edit'),
+    'type' => MENU_LOCAL_TASK,
+    'weight' => -8,
+    'page callback' => 'user_relationships_pending_requests_page',
+    'page arguments' => array('requester_id'),
+    'file' => 'user_relationships_ui.pages.inc',
+    'menu_name' => 'user-menu',
+  );
+
+  $show_tabs = db_query('SELECT rtid, show_tab FROM {user_relationships_ui_settings} WHERE show_tab = 1')->fetchAllKeyed();
+
   foreach (user_relationships_types_load() as $rtid => $relationship) {
-    $items["relationships/{$rtid}"] = array(
-      'title' => $relationship->plural_name ? $relationship->plural_name : $relationship->name,
-      'type' => MENU_LOCAL_TASK,
-      'access callback' => 'user_relationships_ui_check_access',
-      'access arguments' => array('edit'),
-      'page callback' => 'user_relationships_page',
-      'page arguments' => array(NULL, 1),
-      'file' => 'user_relationships_ui.pages.inc',
-      'menu_name' => 'user-menu',
-    );
+    if (isset($show_tabs[$rtid])) {
+      $items["relationships/{$rtid}"] = array(
+        'title' => $relationship->plural_name ? $relationship->plural_name : $relationship->name,
+        'type' => MENU_LOCAL_TASK,
+        'access callback' => 'user_relationships_ui_check_access',
+        'access arguments' => array('edit'),
+        'page callback' => 'user_relationships_page',
+        'page arguments' => array(NULL, 1),
+        'file' => 'user_relationships_ui.pages.inc',
+        'menu_name' => 'user-menu',
+      );
+    }
   }
 
   $items['relationships/%user_relationships/remove'] = array(
@@ -370,23 +391,38 @@ function user_relationships_ui_menu() {
     'type' => MENU_LOCAL_TASK,
   );
   $items['user/%user/relationships/list'] = array(
-    'title' => 'All',
+    'title' => 'Current',
     'type' => MENU_DEFAULT_LOCAL_TASK,
     'weight' => -10,
     'access callback' => 'user_relationships_ui_check_access',
     'access arguments' => array(array('user'), 1),
   );
-  $items['user/%user/relationships/requests'] = array(
-    'title' => 'Pending',
+
+  $items['user/%user/relationships/received'] = array(
+    'title' => 'Received requests',
+    'title callback' => 'user_relationships_ui_title_callback_pending',
+    'title arguments' => array('requestee_id', 1),
+    'access callback' => 'user_relationships_ui_check_access',
+    'access arguments' => array(array('user'), 1),
     'type' => MENU_LOCAL_TASK,
     'weight' => -9,
+    'page callback' => 'user_relationships_pending_requests_page',
+    'page arguments' => array('requestee_id', 1),
+    'file' => 'user_relationships_ui.pages.inc',
+  );
+
+  $items['user/%user/relationships/sent'] = array(
+    'title' => 'Sent requests',
+    'title callback' => 'user_relationships_ui_title_callback_pending',
+    'title arguments' => array('requester_id', 1),
     'access callback' => 'user_relationships_ui_check_access',
     'access arguments' => array(array('user'), 1),
+    'type' => MENU_LOCAL_TASK,
+    'weight' => -8,
     'page callback' => 'user_relationships_pending_requests_page',
-    'page arguments' => array(1),
+    'page arguments' => array('requester_id', 1),
     'file' => 'user_relationships_ui.pages.inc',
   );
-
   foreach (user_relationships_types_load() as $rtid => $relationship) {
     $items["user/%user/relationships/{$rtid}"] = array(
       'title' => $relationship->plural_name ? $relationship->plural_name : $relationship->name,
@@ -399,17 +435,15 @@ function user_relationships_ui_menu() {
     );
   }
 
-  foreach (array('approve', 'disapprove', 'cancel') as $action) {
-    $items["user/%user/relationships/requested/%user_relationships/{$action}"] = array(
-      'title' => 'Approve Relationship',
-      'type' => MENU_CALLBACK,
-      'access callback' => 'user_relationships_ui_check_access',
-      'access arguments' => array(array('user'), 1),
-      'page callback' => 'user_relationships_ui_pending_requested_ajax',
-      'page arguments' => array($action, 1, 4),
-      'file' => 'user_relationships_ui.forms.inc',
-    );
-  }
+  $items["user/%user/relationships/requested/%user_relationships/%"] = array(
+    'title' => 'Approve Relationship',
+    'type' => MENU_CALLBACK,
+    'access callback' => 'user_relationships_ui_check_access',
+    'access arguments' => array(array('user'), 1),
+    'page callback' => 'user_relationships_ui_pending_requested_ajax',
+    'page arguments' => array(5, 1, 4),
+    'file' => 'user_relationships_ui.forms.inc',
+  );
 
   $items['user/%user/relationships/%user_relationships/remove'] = array(
     'title' => 'Remove relationship',
@@ -425,6 +459,37 @@ function user_relationships_ui_menu() {
 }
 
 /**
+ * Title callback to display the amount of pending requests.
+ * @param $column
+ *   Which column that should be checked: requester_id (sent) or requestee_id
+ *   (received).
+ * @param $account
+ *   For which account shall the title be displayed. Defaults to the current
+ *   user.
+ * @return
+ *   Title string for either the Sent or Received requests local tasks.
+ */
+function user_relationships_ui_title_callback_pending($column, $account = NULL) {
+  if (!$account || !is_object($account)) {
+    global $user;
+    $account = $user;
+  }
+
+  $count = user_relationships_load(array($column => $account->uid, 'approved' => FALSE), array('count' => TRUE));
+  if ($column == 'requester_id') {
+    if ($count > 0) {
+      return format_plural($count, 'Sent requests (1)', 'Sent requests (@count)');
+    }
+    return t('Sent requests');
+  }
+  else {
+    if ($count > 0) {
+      return format_plural($count, 'Received requests (1)', 'Received requests (@count)');
+    }
+    return t('Received requests');
+  }
+}
+/**
  * Implementation of hook_user_login().
  */
 function user_relationships_ui_user_login(&$edit, $account) {
@@ -603,69 +668,6 @@ function user_relationships_ui_theme() {
 }
 
 /**
- * Pre processor for user_relationships page
- */
-function template_preprocess_user_relationships(&$variables) {
-  global $user;
-
-  // readability
-  $account  = $variables['account'];
-  $rtid     = $variables['rtid'];
-
-  $args = array('user' => $account->uid, 'approved' => TRUE);
-  if (!empty($rtid)) {
-    $variables['relationship_type'] = user_relationships_type_load($rtid);
-    $args['rtid'] = $rtid;
-  }
-
-  // To Page or not to Page
-  $variables['relationships_per_page'] = variable_get('user_relationships_relationships_per_page', 16);
-  $options = array('include_user_info' => TRUE, 'paging' => $variables['relationships_per_page']);
-
-  $variables['relationships'] = user_relationships_load($args, $options);
-
-  if ($account->uid == $user->uid) {
-    $msg = isset($variables['relationship_type'])
-           ? t("My %relationships", array('%relationships' => $variables['relationship_type']->plural_name ? ur_tt("user_relationships:rtid:" . $variables['relationship_type']->rtid . ":plural_name", $variables['relationship_type']->plural_name) : ur_tt("user_relationships:rtid:" . $variables['relationship_type']->rtid . ":name", $variables['relationship_type']->name)))
-           : t("All my relationships");
-  }
-  else {
-    $msg = isset($variables['relationship_type'])
-           ? t("%username's %relationships", array('%username' => format_username($account), '%relationships' => $variables['relationship_type']->plural_name ? ur_tt("user_relationships:rtid:" . $variables['relationship_type']->rtid . ":plural_name", $variables['relationship_type']->plural_name) : ur_tt("user_relationships:rtid:" . $variables['relationship_type']->rtid . ":name", $variables['relationship_type']->name)))
-           : t("All %username's relationships", array('%username' => format_username($account)));
-  }
-
-  $variables['title'] = $msg;
-}
-
-/**
- * Pre processor page for user_relationships_pending_requests page
- */
-function template_preprocess_user_relationships_pending_requests(&$variables) {
-  global $user;
-
-  // readability
-  $account = $variables['account'];
-
-  $msg = $account->uid == $user->uid ? t('My pending relationships') : t("%username's pending relationships", array('%username' => format_username($account)));
-  $variables['title'] = $msg;
-
-  $variables['relationships_per_page'] = variable_get('user_relationships_relationships_per_page', 16);
-
-  $variables['sections'] = array(
-    'requester_id' => 'sent_requests',
-    'requestee_id' => 'received_requests'
-  );
-  $pager_id = 0;
-  foreach ($variables['sections'] as $column => $section) {
-    // To Page or not to Page
-    $options = array('include_user_info' => TRUE, 'paging' => $variables['relationships_per_page']);
-
-    $variables[$section] = user_relationships_load(array($column => $account->uid, 'approved' => FALSE), $options);
-  }
-}
-
-/**
  * Implements hook_field_extra_fields().
  */
 function user_relationships_ui_field_extra_fields() {
@@ -687,3 +689,55 @@ function user_relationships_ui_field_extra_fields() {
   );
   return $extra;
 }
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function user_relationships_ui_form_user_relationships_admin_type_edit_alter(&$form, &$form_state) {
+  $form['listings'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Listings'),
+    '#group' => 'tabs',
+  );
+
+  // Load settings.
+  $settings = db_query('SELECT * FROM {user_relationships_ui_settings} WHERE rtid = :rtid', array(':rtid' => (int)$form['rtid']['#value']))->fetchObject();
+
+  $form['listings']['ui_settings_hide'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Hide this relationship type from the relationships list'),
+    '#default_value' => $settings ? $settings->hide : FALSE,
+  );
+
+  $form['listings']['ui_settings_show_tab'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Create a separate tab (and path) for this relationship type'),
+    '#default_value' => $settings ? $settings->show_tab : FALSE,
+  );
+}
+
+/**
+ * Implements hook_user_relationships_type_insert().
+ */
+function user_relationships_ui_user_relationships_type_insert($relationship_type) {
+  db_merge('user_relationships_ui_settings')
+    ->key(array('rtid' => $relationship_type->rtid))
+    ->fields(array(
+      'hide' => $relationship_type->ui_settings_hide,
+      'show_tab' => $relationship_type->ui_settings_show_tab,
+    ))
+    ->execute();
+}
+
+/**
+ * Implements hook_user_relationships_type_update().
+ */
+function user_relationships_ui_user_relationships_type_update($relationship_type) {
+  db_merge('user_relationships_ui_settings')
+    ->key(array('rtid' => $relationship_type->rtid))
+    ->fields(array(
+      'hide' => $relationship_type->ui_settings_hide,
+      'show_tab' => $relationship_type->ui_settings_show_tab,
+    ))
+    ->execute();
+}
\ No newline at end of file
diff --git a/user_relationships_ui/user_relationships_ui.pages.inc b/user_relationships_ui/user_relationships_ui.pages.inc
index 6256b3d..c56d321 100644
--- a/user_relationships_ui/user_relationships_ui.pages.inc
+++ b/user_relationships_ui/user_relationships_ui.pages.inc
@@ -8,8 +8,8 @@
  * Main list of relationships for a specified user
  */
 function user_relationships_page($account = NULL, $rtid = NULL) {
+  global $user;
   if (!$account || !is_object($account) || !$account->uid) {
-    global $user;
     $account = $user;
   }
 
@@ -18,16 +18,82 @@ function user_relationships_page($account = NULL, $rtid = NULL) {
     return MENU_NOT_FOUND;
   }
 
-  return theme('user_relationships', array('account' => $account, 'rtid' =>  $rtid));
+  $args = array('user' => $account->uid, 'approved' => TRUE);
+  $relationship_type = NULL;
+  if (!empty($rtid)) {
+    $relationship_type = user_relationships_type_load($rtid);
+    $args['rtid'] = $rtid;
+  }
+  else {
+    // Get hidden relaionships.
+    $hidden = db_query('SELECT rtid FROM {user_relationships_ui_settings} WHERE hide = 1')->fetchCol();
+    if (!empty($hidden)) {
+      $args['exclude_rtids'] = $hidden;
+    }
+  }
+
+  $options = array(
+    'include_user_info' => TRUE,
+    'paging' => variable_get('user_relationships_relationships_per_page', 16),
+  );
+
+  $relationships = user_relationships_load($args, $options);
+
+  if ($account->uid == $user->uid) {
+    $msg = isset($relationship_type)
+           ? t("My %relationships", array('%relationships' => $relationship_type->plural_name ? ur_tt("user_relationships:rtid:" . $relationship_type->rtid . ":plural_name", $relationship_type->plural_name) : ur_tt("user_relationships:rtid:" . $relationship_type->rtid . ":name", $relationship_type->name)))
+           : t("My relationships");
+  }
+  else {
+    $msg = isset($relationship_type)
+           ? t("%username's %relationships", array('%username' => format_username($account), '%relationships' => $relationship_type->plural_name ? ur_tt("user_relationships:rtid:" . $relationship_type->rtid . ":plural_name", $relationship_type->plural_name) : ur_tt("user_relationships:rtid:" . $relationship_type->rtid . ":name", $relationship_type->name)))
+           : t("%username's relationships", array('%username' => format_username($account)));
+  }
+  drupal_set_title($msg, PASS_THROUGH);
+
+  $edit_access = ($user->uid == $account->uid && user_access('maintain own relationships')) || user_access('administer user relationships');
+  $rows = array();
+  foreach ($relationships as $relationship) {
+    $rows[$relationship->rid] = user_relationships_ui_get_table_row($relationship, $account, $edit_access);
+  }
+
+  $header = user_relationships_ui_get_table_header($edit_access);
+
+  if (!empty($rtid)) {
+    $empty = t('You do not have any %plural_name.', array('%plural_name' => ur_tt("user_relationships:rtid:$relationship_type->rtid:plural_name", $relationship_type->plural_name)));
+  }
+  else {
+    $empty = t('You do not have any relationships with other users.');
+  }
+
+  $output = array(
+    'list' => array(
+      '#theme' => 'table',
+      '#rows' => $rows,
+      '#header' => $header,
+      '#empty' => $empty,
+      '#attributes' => array('class' => array('user-relationships-listing-table')),
+    ),
+    // Theme pager so that it uses the correct pager query.
+    'pager' => array(
+      '#markup' => theme('pager'),
+    )
+  );
+
+  $context = array(
+    'account' => $account,
+    'relationship_type' => $relationship_type,
+  );
+
+  return $output;
 }
 
 /**
  * List of pending requests from other users
  */
-function user_relationships_pending_requests_page($account = NULL) {
-  // Check that the uid is valid, not the anonymous user, and the user exists
+function user_relationships_pending_requests_page($column, $account = NULL) {
+  global $user;
   if (!$account) {
-    global $user;
     $account = $user;
   }
 
@@ -35,5 +101,132 @@ function user_relationships_pending_requests_page($account = NULL) {
     return MENU_NOT_FOUND;
   }
 
-  return theme('user_relationships_pending_requests', array('account' => $account));
+  $edit_access = ($user->uid == $account->uid && user_access('maintain own relationships')) || user_access('administer user relationships');
+
+  if ($column == 'requester_id') {
+    if ($account->uid == $user->uid) {
+      $msg = t('My relationships (sent requests)');
+      $empty = t('You have not sent any relationship requests that are currently pending.');
+    }
+    else {
+      $msg = t("%username's relationships (pending sent requests)", array('%username' => format_username($account)));
+      $empty = t('%username has not sent any relationship requests that are currently pending.', array('%username' => format_username($account)));
+    }
+  }
+  else {
+    if ($account->uid == $user->uid) {
+      $msg = t('My relationships (received requests)');
+      $empty = t('You have not received any relationship requests that are currently pending.');
+    }
+    else {
+      $msg = t("%username's relationships (pending received requests)", array('%username' => format_username($account)));
+      $empty = t('%username has not received any relationship requests that are currently pending.', array('%username' => format_username($account)));
+    }
+  }
+  drupal_set_title($msg, PASS_THROUGH);
+
+  $options = array(
+    'include_user_info' => TRUE,
+    'paging' => variable_get('user_relationships_relationships_per_page', 16),
+  );
+
+  $relationships = user_relationships_load(array($column => $account->uid, 'approved' => FALSE), $options);
+  $rows = array();
+  foreach ($relationships as $relationship) {
+    $rows[] = user_relationships_ui_get_table_row($relationship, $account);
+  }
+
+
+  $output['list'] = array(
+    '#theme' => 'table',
+    '#rows' => $rows,
+    '#header' => user_relationships_ui_get_table_header($edit_access),
+    '#empty' => $empty,
+    'attributes' => array('class' => array('user-relationships-pending-listing-table')),
+  );
+  $output['pager'] = array(
+    '#markup' => theme('pager'),
+  );
+
+  return $output;
+}
+
+/**
+ * Builds a table row array from a relationship.
+ *
+ * @param $relationship
+ *   Relationship object.
+ * @param $account
+ *   User account object for which the relationship is being displayed.
+ *
+ * @return
+ *   Array with the table row content.
+ *
+ * @see hok_user_relationships_ui_table_row_alter()
+ */
+function user_relationships_ui_get_table_row($relationship, $account) {
+  global $user;
+
+  $this_user_str  = $account->uid == $relationship->requestee_id ? 'requester' : 'requestee';
+  $this_user      = $relationship->{$this_user_str};
+
+  $edit_access = ($user->uid == $account->uid && user_access('maintain own relationships')) || user_access('administer user relationships');
+
+  $row = array(
+    theme('username', array('account' => $this_user)),
+    ur_tt("user_relationships:rtid:$relationship->rtid:name", $relationship->name) . ($relationship->is_oneway ? ($this_user_str == 'requestee' ? t(' (You to Them)') : t(' (Them to You)')) : NULL),
+  );
+  if ($edit_access) {
+    $links = array();
+    if ($relationship->approved) {
+      $links[] = theme('user_relationships_remove_link', array('uid' => $account->uid, 'rid' => $relationship->rid));
+    }
+    else {
+      if ($this_user_str == 'requestee') {
+        // Sent requests, display cancel link.
+        $links[] = theme('user_relationships_pending_request_cancel_link', array('uid' => $account->uid, 'rid' => $relationship->rid));
+      }
+      else {
+        // Received requests, display approve and decline links.
+        $links[] = theme('user_relationships_pending_request_approve_link', array('uid' => $account->uid, 'rid' => $relationship->rid));
+        $links[] = theme('user_relationships_pending_request_disapprove_link', array('uid' => $account->uid, 'rid' => $relationship->rid));
+      }
+    }
+    $row[] = implode(' ', $links);
+  }
+  if (variable_get('user_relationships_show_user_pictures', 0)) {
+    array_unshift($row, theme('user_picture', array('account' => $this_user)));
+  }
+
+  drupal_alter('user_relationships_ui_table_row', $row, $relationship, $account);
+
+  return $row;
+}
+
+/**
+ * Return the table header for a relationship listing.
+ *
+ * @param $edit_access
+ *   If the user has edit access.
+ *
+ * @return
+ *   Array with the table header definition.
+ *
+ * @see hook_user_relationships_ui_table_header_alter().
+ */
+function user_relationships_ui_get_table_header($edit_access) {
+  $header = array(t('User'), t('Relationship'), t('Operations'));
+
+  if (!$edit_access) {
+    // Remove operations column.
+    array_pop($header);
+  }
+
+  if (variable_get('user_relationships_show_user_pictures', 0)) {
+    array_unshift($header, t('Picture'));
+  }
+
+  drupal_alter('user_relationships_ui_table_header', $header, $edit_access);
+
+  return $header;
 }
-- 
1.7.4.1

