From 26ba513969313d81d9383ab89cd9b52b6b468027 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 using renderable arrays and alter hooks instead of templates.

---
 .../user_relationship_elaborations.module          |   23 ++-
 user_relationships.module                          |    5 +
 .../templates/user_relationships.tpl.php           |   34 ----
 .../user_relationships_pending_requests.tpl.php    |   53 ------
 .../user_relationships_ui.api.php                  |   38 +++++
 user_relationships_ui/user_relationships_ui.module |   79 ++-------
 .../user_relationships_ui.pages.inc                |  176 +++++++++++++++++++-
 7 files changed, 240 insertions(+), 168 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 e1c60b4..b3fb22f 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 3996731..2f4f4e2 100644
--- a/user_relationships.module
+++ b/user_relationships.module
@@ -187,6 +187,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;
 }
 
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.module b/user_relationships_ui/user_relationships_ui.module
index a4392e9..1d09de0 100644
--- a/user_relationships_ui/user_relationships_ui.module
+++ b/user_relationships_ui/user_relationships_ui.module
@@ -316,13 +316,25 @@ function user_relationships_ui_menu() {
     'access arguments' => array('view'),
     'menu_name' => 'user-menu',
   );
-  $items['relationships/requests'] = array(
-    'title' => 'Pending',
+  $items['relationships/sent'] = array(
+    'title' => 'Sent requests',
+    '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('requester_id'),
+    'file' => 'user_relationships_ui.pages.inc',
+    'menu_name' => 'user-menu',
+  );
+  $items['relationships/received'] = array(
+    'title' => 'Received requests',
     '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',
   );
@@ -603,69 +615,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() {
diff --git a/user_relationships_ui/user_relationships_ui.pages.inc b/user_relationships_ui/user_relationships_ui.pages.inc
index 6256b3d..cfdf894 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,75 @@ 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;
+  }
+
+  $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 +94,110 @@ 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');
+
+  $msg = $account->uid == $user->uid ? t('My pending relationships') : t("%username's pending relationships", 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' => t('You do not have any relationship requests currently pending.'),
+    '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 == 'requester') {
+        $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));
+      }
+    }
+    $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

