From c00b66fe5bb37403e4dec1ede9d509947b795df0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Schie=C3=9Fle?= <schiessle@owncloud.com>
Date: Mon, 28 Jan 2013 15:47:57 +0100
Subject: [PATCH] implement  DisplayNamesInGroup for database back-end

---
 lib/group.php          |  2 +-
 lib/group/backend.php  |  2 +-
 lib/group/database.php | 28 ++++++++++++++++++++++++++++
 lib/user.php           |  2 +-
 lib/user/database.php  | 10 ++++++----
 5 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/lib/group.php b/lib/group.php
index 8ebb698692..5afef76936 100644
--- a/lib/group.php
+++ b/lib/group.php
@@ -289,7 +289,7 @@ class OC_Group {
 	
 	/**
 	 * @brief get a list of all display names in a group
-	 * @returns array with display names (key) and user ids(value)
+	 * @returns array with display names (value) and user ids(key)
 	 */
 	public static function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
 		$displayNames=array();
diff --git a/lib/group/backend.php b/lib/group/backend.php
index ceb2d9242d..4f6570c3be 100644
--- a/lib/group/backend.php
+++ b/lib/group/backend.php
@@ -140,7 +140,7 @@ abstract class OC_Group_Backend implements OC_Group_Interface {
 	 * @param string $search
 	 * @param int $limit
 	 * @param int $offset
-	 * @return array with display names (key) and user ids (value)
+	 * @return array with display names (value) and user ids (key)
 	 */
 	public function DisplayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
 		$displayNames = '';
diff --git a/lib/group/database.php b/lib/group/database.php
index 6eca98ba01..c5dd402b21 100644
--- a/lib/group/database.php
+++ b/lib/group/database.php
@@ -208,4 +208,32 @@ class OC_Group_Database extends OC_Group_Backend {
 		}
 		return $users;
 	}
+	
+	/**
+	 * @brief get a list of all display names in a group
+	 * @param string $gid
+	 * @param string $search
+	 * @param int $limit
+	 * @param int $offset
+	 * @return array with display names (value) and user ids (key)
+	 */
+	public function DisplayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
+		$displayNames = '';
+		/*
+		
+		SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
+		FROM Persons
+		INNER JOIN Orders
+		ON Persons.P_Id=Orders.P_Id
+		ORDER BY Persons.LastName
+		*/
+		$stmt = OC_DB::prepare('SELECT `*PREFIX*users`.`uid`, `*PREFIX*users`.`displayname` FROM `*PREFIX*users` INNER JOIN `*PREFIX*group_user` ON `*PREFIX*group_user`.`uid` = `*PREFIX*users`.`uid`  WHERE `gid` = ? AND `*PREFIX*group_user.uid` LIKE ?', $limit, $offset);
+		$result = $stmt->execute(array($gid, $search.'%'));
+		$users = array();
+		while ($row = $result->fetchRow()) {
+			$displayName = trim($row['displayname'], ' ');
+			$displayNames[$row['uid']] = empty($displayName) ? $row['uid'] : $displayName;
+		}
+		return $displayNames;
+	}
 }
diff --git a/lib/user.php b/lib/user.php
index b91abd71fe..399a3240c8 100644
--- a/lib/user.php
+++ b/lib/user.php
@@ -477,7 +477,7 @@ class OC_User {
 	
 	/**
 	 * @brief Get a list of all users display name
-	 * @returns associative array with all display names (key) and corresponding uids (value)
+	 * @returns associative array with all display names (value) and corresponding uids (key)
 	 *
 	 * Get a list of all display names and user ids.
 	 */
diff --git a/lib/user/database.php b/lib/user/database.php
index 52f11a5e29..bed97f25fd 100644
--- a/lib/user/database.php
+++ b/lib/user/database.php
@@ -138,8 +138,9 @@ class OC_User_Database extends OC_User_Backend {
 		if( $this->userExists($uid) ) {
 			$query = OC_DB::prepare( 'SELECT displayname FROM `*PREFIX*users` WHERE `uid` = ?' );
 			$result = $query->execute( array( $uid ))->fetchAll();
-			if (!empty($result[0]['displayname'])) {
-				return $result[0]['displayname'];
+			$displayName = trim($result[0]['displayname'], ' ');
+			if ( !empty($displayName) ) {
+				return $displayName;
 			} else {
 				return $uid;
 			}
@@ -157,8 +158,9 @@ class OC_User_Database extends OC_User_Backend {
 		$query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` WHERE LOWER(`uid`) LIKE LOWER(?)', $limit, $offset);
 		$result = $query->execute(array($search.'%'));
 		$users = array();
-		while ($row = $result->fetchRow()) {
-			$displayNames[$row['uid']] = $row['displayname'];
+		while ($row = $result->fetchRow()) {
+			$displayName =  trim($row['displayname'], ' ');
+			$displayNames[$row['uid']] = empty($displayName) ? $row['uid'] : $displayName;
 		}
 		return $displayNames;
 	}
-- 
GitLab