From ff350ba9cffbbfafc31306c96e4e01d7597ccdc4 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          |   37 ++++-
 user_relationships.module                          |    5 +
 .../templates/user_relationships.tpl.php           |   34 ----
 .../user_relationships_pending_requests.tpl.php    |   53 -------
 user_relationships_ui/user_relationships_ui.module |   63 --------
 .../user_relationships_ui.pages.inc                |  164 +++++++++++++++++++-
 6 files changed, 194 insertions(+), 162 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

diff --git a/user_relationship_elaborations/user_relationship_elaborations.module b/user_relationship_elaborations/user_relationship_elaborations.module
index e1c60b4..a6348b2 100644
--- a/user_relationship_elaborations/user_relationship_elaborations.module
+++ b/user_relationship_elaborations/user_relationship_elaborations.module
@@ -98,18 +98,41 @@ function _user_relationship_elaborations_walk_recursive($action, &$relationships
   }
 }
 
-
 /**
- * Add elaborations to relationships page through MODULE_preprocess_HOOK
+ * Implements hook_user_relationships_list_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_list_alter(&$output, $relationships, $context) {
+  $header = $output['list']['#header'];
+  $insert_index = array_search(t('Relationship'), $header) + 1;
+  $output['list']['#header'] = array_merge(array_slice($header, 0, $insert_index), array(t('Comments')), array_slice($header, $insert_index));
+
+  if (empty($relationships)) {
+    return;
+  }
+  $elaborations = db_query('SELECT rid, elaboration FROM {user_relationship_elaborations} WHERE rid IN (:rids)', array(':rids' => array_keys($relationships)))->fetchAllKeyed();
+  foreach ($relationships as $rid => $relationship) {
+    $row = &$output['list']['#rows'][$rid];
+    $row = array_merge(array_slice($row, 0, $insert_index), array(isset($elaborations[$rid]) ? $elaborations[$rid] : ''), array_slice($row, $insert_index));
   }
 }
 
+/**
+ * Implements hook_user_relationships_list_alter().
+ */
+function user_relationship_elaborations_user_relationships_list_pending_alter(&$output, $relationships, $context) {
+  $header = $output['list']['#header'];
+  $insert_index = 1;
+  $output['list']['#header'] = array_merge(array_slice($header, 0, $insert_index), array(t('Comments')), array_slice($header, $insert_index));
+
+  if (empty($relationships)) {
+    return;
+  }
+  $elaborations = db_query('SELECT rid, elaboration FROM {user_relationship_elaborations} WHERE rid IN (:rids)', array(':rids' => array_keys($relationships)))->fetchAllKeyed();
+  foreach ($relationships as $rid => $relationship) {
+    $row = &$output['list']['#rows'][$rid];
+    $row = array_merge(array_slice($row, 0, $insert_index), array(isset($elaborations[$rid]) ? $elaborations[$rid] : ''), 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.module b/user_relationships_ui/user_relationships_ui.module
index a4392e9..c49096c 100644
--- a/user_relationships_ui/user_relationships_ui.module
+++ b/user_relationships_ui/user_relationships_ui.module
@@ -603,69 +603,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..226eb71 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,98 @@ 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($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)));
+  }
+  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) {
+    $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),
+    );
+    if ($edit_access) {
+      $row[] = theme('user_relationships_remove_link', array('uid' => $account->uid, 'rid' => $relationship->rid));
+    }
+    if (variable_get('user_relationships_show_user_pictures', 0)) {
+      array_unshift($row, theme('user_picture', array('account' => $this_user)));
+    }
+    $rows[$relationship->rid] = $row;
+  }
+
+  $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'));
+  }
+
+  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,
+  );
+  drupal_alter('user_relationships_list', $output, $relationships, $context);
+
+  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
+  global $user;
   if (!$account) {
-    global $user;
     $account = $user;
   }
 
@@ -35,5 +117,77 @@ function user_relationships_pending_requests_page($account = NULL) {
     return MENU_NOT_FOUND;
   }
 
-  return theme('user_relationships_pending_requests', array('account' => $account));
+  $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);
+
+  $edit_access = ($user->uid == $account->uid && user_access('maintain own relationships')) || user_access('administer user relationships');
+
+  $section_columns = array(
+    'requester_id' => 'sent_requests',
+    'requestee_id' => 'received_requests'
+  );
+  $section_headings = array(
+    'sent_requests'     => t('Sent Requests'),
+    'received_requests' => t('Received Requests')
+  );
+
+  $output = array();
+  foreach ($section_columns as $column => $section) {
+    $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);
+
+    if (empty($relationships)) {
+      continue;
+    }
+
+    $rows = array();
+    $header = array($section_headings[$section], t('Operations'));
+    foreach ($relationships 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[$relationship->rid]   = 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[$relationship->rid]   = 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[$section]['list'] = array(
+      '#theme' => 'table',
+      '#rows' => $rows,
+      '#header' => $header,
+      'attributes' => array('class' => array('user-relationships-pending-listing-table')),
+    );
+    $output[$section]['pager'] = array(
+      '#markup' => theme('pager'),
+    );
+
+    $context = array(
+      'account' => $account,
+      'section' => $section,
+    );
+    drupal_alter('user_relationships_list_pending', $output[$section], $relationships, $context);
+  }
+  if (empty($output)) {
+    $output['empty'] = t('You do not have any relationship requests currently pending.');
+  }
+
+
+
+  return $output;
 }
-- 
1.7.4.1

