From 891b87984ffe6dc4a8bb48bd42e2746fb35ee9f1 Mon Sep 17 00:00:00 2001
From: Niklas Fiekas <niklas.fiekas@googlemail.com>
Date: Wed, 28 Sep 2011 18:08:52 +0200
Subject: [PATCH]  #1293720 by Niklas Fiekas: Allow backend specific
 repository controllers

---
 includes/controllers.inc |   24 ++++++++++++++++++++++++
 versioncontrol.module    |   26 ++++++++++++--------------
 2 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/includes/controllers.inc b/includes/controllers.inc
index b5cdfea..a1dc0cf 100644
--- a/includes/controllers.inc
+++ b/includes/controllers.inc
@@ -418,6 +418,30 @@ class VersioncontrolRepositoryController extends VersioncontrolEntityController
    * @param array $queried_entities
    */
   protected function extendData(&$queried_entities) {}
+
+  /**
+   * Adds a condition on the vcs column, so that only repositories are loaded,
+   * that are supported by the current controller.
+   */
+  protected function buildQueryConditions(&$query, $ids, $conditions) {
+    parent::buildQueryConditions(&$query, $ids, $conditions);
+
+    // We have a backend specific controller. Load only the supported
+    // repositories.
+    if (isset($this->backend->type)) {
+      $this->attachCondition($query, 'vcs', $this->backend->type);
+    }
+    // backend->type is not set, that means we use the default controller. We
+    // must load all repositories, except those that have an entity specific
+    // controller.
+    else {
+      $this->attachCondition($query, 'vcs', array(
+        'values' => array_keys(versioncontrol_get_backends()),
+        'operator' => 'NOT IN',
+      ));
+    }
+    return $query;
+  }
 }
 
 class VersioncontrolBranchController extends VersioncontrolEntityController {
diff --git a/versioncontrol.module b/versioncontrol.module
index 2b7afe7..f335e67 100644
--- a/versioncontrol.module
+++ b/versioncontrol.module
@@ -353,21 +353,19 @@ function versioncontrol_repository_load($repo_id, $conditions = array(), $option
  *
  */
 function versioncontrol_repository_load_multiple($ids = array(), $conditions = array(), $options = array()) {
-  $controller = &ctools_static(__FUNCTION__);
-  if (!isset($controller)) {
-    if (variable_get('versioncontrol_single_backend_mode', FALSE)) {
-      $backends = versioncontrol_get_backends();
-      $backend = reset($backends);
-      // need to ensure the controller is initialized, and this is the only way
-      $result = $backend->loadEntities('repo', $ids, $conditions, $options);
-      $controller = $backend->controllers['repo'];
-      return $result;
-    }
-    else {
-      $controller = new VersioncontrolRepositoryController();
-    }
+  $entities = array();
+
+  // Let all backends load the repositories they support.
+  foreach (versioncontrol_get_backends() as $type => $backend) {
+    $entities += $backend->loadEntities('repo', $ids, $conditions, $options);
   }
-  return $controller->load($ids, $conditions, $options);
+
+  // For all repositories that don't have a backend specific controller, use
+  // the default one.
+  $default_controller = new VersioncontrolRepositoryController();
+  $entities += $default_controller->load($ids, $conditions, $options);
+
+  return $entities;
 }
 
 /**
-- 
1.7.4.4

