From 2535e04a5d9bb583477877f302f09b224ba48916 Mon Sep 17 00:00:00 2001
From: Marco Villegas <marvil07@gmail.com>
Date: Tue, 9 Nov 2010 18:52:32 -0500
Subject: [PATCH] task #890216 follow-up by marvil07, sdboyer: Fix account status functionality

1. Fix the last username/vcs_username nomenclature problem

   - patch by sam at #890216-15
   - Just another s/username/vcs_username at
     versioncontrol_account_edit_form_submit().

2. Handle special two-PK entinty controller for account

   - Deprecate returning FALSE on versioncontrol_user_accounts_load().
   - Manually get the right repo object at account load.
   - Use only one approach for specially keyed array returned at
     VersioncontrolAccountController::executeQuery(): nested array.
   - Process the nested array mentioned at
     VersioncontrolAccountController::buildEntities() to get one-level
     array that can be processed by VersioncontrolEntityController
     caching mechanism.
   - Some minor refactor at versioncontrol_user_accounts_title_callback()
     and versioncontrol_account_page().
---
 includes/VersioncontrolAccount.php    |    4 +-
 includes/VersioncontrolRepository.php |    2 +-
 includes/controllers.inc              |   17 +++++++------
 versioncontrol.module                 |   25 ++++++++------------
 versioncontrol.pages.inc              |   39 +++++++++++++-------------------
 5 files changed, 38 insertions(+), 49 deletions(-)

diff --git includes/VersioncontrolAccount.php includes/VersioncontrolAccount.php
index e345348..e86ec85 100644
--- includes/VersioncontrolAccount.php
+++ includes/VersioncontrolAccount.php
@@ -93,7 +93,7 @@ abstract class VersioncontrolAccount extends VersioncontrolEntity {
     if ($username_changed) {
       $this->vcs_username = $vcs_username;
       db_query("UPDATE {versioncontrol_accounts}
-                SET username = '%s'
+                SET vcs_username = '%s'
                 WHERE uid = %d AND repo_id = %d",
                 $this->vcs_username, $this->uid, $repo_id
       );
@@ -143,7 +143,7 @@ abstract class VersioncontrolAccount extends VersioncontrolEntity {
    */
   public final function insert($additional_data = array()) {
     db_query(
-      "INSERT INTO {versioncontrol_accounts} (uid, repo_id, username)
+      "INSERT INTO {versioncontrol_accounts} (uid, repo_id, vcs_username)
        VALUES (%d, %d, '%s')", $this->uid, $this->repository->repo_id, $this->vcs_username
     );
 
diff --git includes/VersioncontrolRepository.php includes/VersioncontrolRepository.php
index f2a523f..6d8f1e5 100644
--- includes/VersioncontrolRepository.php
+++ includes/VersioncontrolRepository.php
@@ -478,7 +478,7 @@ abstract class VersioncontrolRepository implements VersioncontrolEntityInterface
   public function getAccountUidForUsername($username, $include_unauthorized = FALSE) {
     $result = db_query("SELECT uid, repo_id
       FROM {versioncontrol_accounts}
-      WHERE username = '%s' AND repo_id = %d",
+      WHERE vcs_username = '%s' AND repo_id = %d",
       $username, $this->repo_id);
 
     while ($account = db_fetch_object($result)) {
diff --git includes/controllers.inc includes/controllers.inc
index c9234ce..c6b1bd3 100644
--- includes/controllers.inc
+++ includes/controllers.inc
@@ -427,7 +427,6 @@ class VersioncontrolAccountController extends VersioncontrolEntityController {
     else { // No repository specified, create a specially-keyed array
       $return = array();
       foreach ($result as $row) {
-        $return[$row->repo_id . '-' . $row->uid] = $row;
         if (!isset($return[$row->uid])) {
           $return[$row->uid] = array();
         }
@@ -443,14 +442,16 @@ class VersioncontrolAccountController extends VersioncontrolEntityController {
       return parent::buildEntities($queried_entities);
     }
     // Otherwise we need special handling for our two-level array.
-    $this->idKey = 'repo_id';
-    $built = array();
-    foreach ($queried_entities as $uid => $repo_array) {
-      // Trick the parent into doing each of the inner arrays for us.
-      $built[$uid] = parent::buildEntities($repo_array);
+    $almost_built = array();
+    foreach ($queried_entities as $uid => $raw_accounts_per_repo) {
+      foreach ($raw_accounts_per_repo as $repo_id => $raw_account) {
+        $id = "$uid-$repo_id";
+        // load the repository object
+        $raw_account->repository = reset($this->backends[$raw_account->vcs]->loadEntities('repo', array($repo_id)));
+        $almost_built[$id] = $raw_account;
+      }
     }
-    $this->idKey = 'uid';
-    return $built;
+    return parent::buildEntities($almost_built);
   }
 
   protected function modifyReturn($entities) {
diff --git versioncontrol.module versioncontrol.module
index 741ab0c..33e8600 100644
--- versioncontrol.module
+++ versioncontrol.module
@@ -240,11 +240,10 @@ function versioncontrol_user($type, &$edit, &$user, $category = NULL) {
       return $categories;
 
     case 'delete':
-      if ($accounts = versioncontrol_user_accounts_load($user->uid, TRUE)) {
-        foreach ($accounts as $uid => $usernames_by_repository) {
-          foreach ($usernames_by_repository as $repo_id => $account) {
-            $account->delete();
-          }
+      $accounts = versioncontrol_user_accounts_load($user->uid, TRUE);
+      foreach ($accounts as $uid => $usernames_by_repository) {
+        foreach ($usernames_by_repository as $repo_id => $account) {
+          $account->delete();
         }
       }
       break;
@@ -503,13 +502,11 @@ function versioncontrol_private_account_access($vcs_accounts, $account = NULL) {
  * Title callback for the "user/%versioncontrol_user_accounts/edit/versioncontrol" tab.
  */
 function versioncontrol_user_accounts_title_callback($accounts) {
-  $usernames = array();
-  foreach ($accounts as $uid => $user_accounts) {
-    foreach ($user_accounts as $repo_id => $account) {
-      $usernames[] = $account->vcs_username;
-    }
+  $repo_ids = array();
+  foreach ($accounts as $account) {
+    $repo_ids[] = $account->repository->repo_id;
   }
-  $repositories = versioncontrol_repository_load_multiple(array_keys(reset($accounts)));
+  $repositories = versioncontrol_repository_load_multiple($repo_ids);
   $vcses = array();
   foreach ($repositories as $repository) {
     $vcses[$repository['vcs']] = TRUE;
@@ -564,8 +561,7 @@ function versioncontrol_repository_load_multiple($ids = array(), $conditions = a
 
 /**
  * Menu wildcard loader for '%versioncontrol_user_accounts':
- * Load and return all VCS accounts of a given user, or FALSE if no accounts
- * exist for that user.
+ * Load and return all VCS accounts of a given user.
  *
  * @param $uid
  *   Drupal user id of the user whose VCS accounts should be loaded.
@@ -574,8 +570,7 @@ function versioncontrol_repository_load_multiple($ids = array(), $conditions = a
  *   API documentation of that function.
  */
 function versioncontrol_user_accounts_load($uid, $include_unauthorized = FALSE) {
-  $accounts = versioncontrol_user_accounts_load_multiple(array($uid), array(), array('include unauthorized' => $include_unauthorized));
-  return empty($accounts) ? FALSE : $accounts;
+  return versioncontrol_user_accounts_load_multiple(array($uid), array(), array('include unauthorized' => $include_unauthorized));
 }
 
 function versioncontrol_user_accounts_load_multiple($ids = array(), $conditions = array(), $options = array()) {
diff --git versioncontrol.pages.inc versioncontrol.pages.inc
index 43f6552..febc969 100644
--- versioncontrol.pages.inc
+++ versioncontrol.pages.inc
@@ -231,29 +231,22 @@ function versioncontrol_account_register_form_submit($form, &$form_state) {
  * array from versioncontrol_account_edit_form() if only a single account
  * exists for that user (or if the account has been given in the URL).
  */
-function versioncontrol_account_page($accounts, $url_repo_id = NULL, $url_username = NULL) {
+function versioncontrol_account_page($vcs_accounts, $url_repo_id = NULL, $url_username = NULL) {
   $selected_usernames = array();
 
-  foreach ($accounts as $only_uid => $vcs_accounts_by_repository) {
+  foreach ($vcs_accounts as $key => $vcs_account) {
     // The caller (menu system) ensures that there is only one uid.
-    $uid = $only_uid;
-
-    foreach ($vcs_accounts_by_repository as $repo_id => $vcs_account) {
-      if (isset($url_repo_id) && $repo_id != $url_repo_id) {
-        unset($accounts[$uid][$repo_id]);
-        continue; // disregard repositories that don't match the URL constraints
-      }
-      $vcs_accounts = array($vcs_account);
-
-      foreach ($vcs_accounts as $key => $vcs_account) {
-        if (isset($url_username) && $vcs_account->vcs_username != $url_username) {
-          unset($vcs_accounts[$uid][$repo_id]);
-          continue; // disregard usernames that don't match the URL constraints
-        }
-        $any_repo_id = $repo_id;
-        $selected_usernames[] = $vcs_account->vcs_username;
-      }
+    $uid = $vcs_account->uid;
+    if (isset($url_repo_id) && $vcs_account->repository->repo_id != $url_repo_id) {
+      unset($vcs_accounts[$key]);
+      continue; // disregard repositories that don't match the URL constraints
+    }
+    if (isset($url_username) && $vcs_account->vcs_username != $url_username) {
+      unset($vcs_accounts[$key]);
+      continue; // disregard usernames that don't match the URL constraints
     }
+    $any_repo_id = $vcs_account->repository->repo_id;
+    $selected_usernames[] = $vcs_account->vcs_username;
   }
 
   if (empty($selected_usernames)) {
@@ -267,7 +260,7 @@ function versioncontrol_account_page($accounts, $url_repo_id = NULL, $url_userna
     );
   }
   else {
-    return drupal_get_form('versioncontrol_account_list_form', $accounts);
+    return drupal_get_form('versioncontrol_account_list_form', $vcs_accounts);
   }
 }
 
@@ -545,11 +538,11 @@ function versioncontrol_account_edit_form_submit($form, &$form_state) {
     //      uid and repo_id are the PK on the table getAccounts query
     $conditions = array(
       'repo_id' => array($repository->repo_id),
-      'username' => array($form['#original_username'])
+      'vcs_username' => array($form['#original_username'])
     );
-    $vcs_accounts = versioncontrol_user_account_load_multiple(array($uid), $conditions, array('include unauthorized' => TRUE));
+    $vcs_accounts = versioncontrol_user_accounts_load_multiple(array($uid), $conditions, array('include unauthorized' => TRUE));
     // if we got an original_username we can trust on getting a value
-    $vcs_account = array_shift(array_shift($vcs_accounts));
+    $vcs_account = array_shift($vcs_accounts);
 
     $vcs_account->update($username, $additional_data);
 
-- 
1.7.2.3

