From e0f2ed2757b698448d49fa3e73340ce9ced25e7a 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 14:09:11 +0100
Subject: [PATCH] interface and API to change display names

---
 lib/user.php                 | 19 ++++++++++++++++--
 lib/user/backend.php         |  2 ++
 settings/js/users.js         | 37 ++++++++++++++++++++++++++++++++++++
 settings/routes.php          |  2 ++
 settings/templates/users.php |  2 +-
 5 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/lib/user.php b/lib/user.php
index 65f899aa27..b91abd71fe 100644
--- a/lib/user.php
+++ b/lib/user.php
@@ -269,10 +269,25 @@ class OC_User {
 	/**
 	 * @brief Sets user display name for session
 	 */
-	private static function setDisplayName($uid) {
-		$_SESSION['display_name'] = self::determineDisplayName($uid);
+	public static function setDisplayName($uid, $displayName = null) {
+		$result = false;
+		if ($displayName ) {
+			foreach(self::$_usedBackends as $backend) {
+				if($backend->implementsActions(OC_USER_BACKEND_SET_DISPLAYNAME)) {
+					if($backend->userExists($uid)) {
+						$success |= $backend->setDisplayName($uid, $displayName);
+					}
+				}
+			}
+		} else {
+			$displayName = self::determineDisplayName($uid);
+			$result = true;
+		}
+		$_SESSION['display_name'] = $displayName;
+		return result;
 	}
 	
+	
 	/**
 	 * @brief get display name
 	 * @param $uid The username
diff --git a/lib/user/backend.php b/lib/user/backend.php
index fe37a64cc0..efea622e31 100644
--- a/lib/user/backend.php
+++ b/lib/user/backend.php
@@ -36,6 +36,7 @@ define('OC_USER_BACKEND_SET_PASSWORD',      0x000010);
 define('OC_USER_BACKEND_CHECK_PASSWORD',    0x000100);
 define('OC_USER_BACKEND_GET_HOME',			0x001000);
 define('OC_USER_BACKEND_GET_DISPLAYNAME',	0x010000);
+define('OC_USER_BACKEND_SET_DISPLAYNAME',	0x010000);
 
 
 /**
@@ -52,6 +53,7 @@ abstract class OC_User_Backend implements OC_User_Interface {
 		OC_USER_BACKEND_CHECK_PASSWORD => 'checkPassword',
 		OC_USER_BACKEND_GET_HOME => 'getHome',
 		OC_USER_BACKEND_GET_DISPLAYNAME => 'getDisplayName',
+		OC_USER_BACKEND_SET_DISPLAYNAME => 'setDisplayName',
 	);
 
 	/**
diff --git a/settings/js/users.js b/settings/js/users.js
index fa6f058d92..835f46f6ed 100644
--- a/settings/js/users.js
+++ b/settings/js/users.js
@@ -300,6 +300,43 @@ $(document).ready(function () {
     $('td.password').live('click', function (event) {
         $(this).children('img').click();
     });
+    
+    $('td.displayName>img').live('click', function (event) {
+        event.stopPropagation();
+        var img = $(this);
+        var uid = img.parent().parent().attr('data-uid');
+        var input = $('<input type="text">');
+        img.css('display', 'none');
+        img.parent().children('span').replaceWith(input);
+        input.focus();
+        input.keypress(function (event) {
+        	console.log("event!");
+            if (event.keyCode == 13) {
+            	console.log("13");
+                if ($(this).val().length > 0) {
+                	console.log("post");
+                    $.post(
+                        OC.filePath('settings', 'ajax', 'changedisplayname.php'),
+                        {username:uid, displayName:$(this).val()},
+                        function (result) {
+                        	console.log("come back!");
+                        }
+                    );
+                    input.blur();
+                } else {
+                    input.blur();
+                }
+            }
+        });
+        input.blur(function () {
+            $(this).replaceWith($(this).val());
+            img.css('display', '');
+        });
+    });
+    $('td.displayName').live('click', function (event) {
+        $(this).children('img').click();
+    });
+    
 
     $('select.quota, select.quota-user').live('change', function () {
         var select = $(this);
diff --git a/settings/routes.php b/settings/routes.php
index 9b5bf80923..c9156f9a11 100644
--- a/settings/routes.php
+++ b/settings/routes.php
@@ -39,6 +39,8 @@ $this->create('settings_ajax_removegroup', '/settings/ajax/removegroup.php')
 	->actionInclude('settings/ajax/removegroup.php');
 $this->create('settings_ajax_changepassword', '/settings/ajax/changepassword.php')
 	->actionInclude('settings/ajax/changepassword.php');
+$this->create('settings_ajax_changedisplayname', '/settings/ajax/changedisplayname.php')
+->actionInclude('settings/ajax/changedisplayname.php');
 // personel
 $this->create('settings_ajax_lostpassword', '/settings/ajax/lostpassword.php')
 	->actionInclude('settings/ajax/lostpassword.php');
diff --git a/settings/templates/users.php b/settings/templates/users.php
index 64dce38ba3..2ba57fb4a7 100644
--- a/settings/templates/users.php
+++ b/settings/templates/users.php
@@ -93,7 +93,7 @@ var isadmin = <?php echo $_['isadmin']?'true':'false'; ?>;
 		<?php foreach($_["users"] as $user): ?>
 		<tr data-uid="<?php echo $user["name"] ?>">
 			<td class="name"><?php echo $user["name"]; ?></td>
-			<td class="displayName"><?php echo $user["displayName"]; ?> <img class="svg action"
+			<td class="displayName"><span><?php echo $user["displayName"]; ?></span> <img class="svg action"
 				src="<?php echo image_path('core', 'actions/rename.svg')?>"
 				alt="change display name" title="change display name"/>
 			</td>
-- 
GitLab