From 06c284f6cc6256b22f645d2d35f966c0bc98f4df Mon Sep 17 00:00:00 2001
From: Arthur Schiwon <blizzz@owncloud.com>
Date: Wed, 23 Jan 2013 23:41:35 +0100
Subject: [PATCH] LDAP settings: read configuration when another server config
 is chosen

---
 apps/user_ldap/ajax/getConfiguration.php | 31 ++++++++++++++++++++
 apps/user_ldap/js/settings.js            | 37 ++++++++++++++++++++++--
 2 files changed, 65 insertions(+), 3 deletions(-)
 create mode 100644 apps/user_ldap/ajax/getConfiguration.php

diff --git a/apps/user_ldap/ajax/getConfiguration.php b/apps/user_ldap/ajax/getConfiguration.php
new file mode 100644
index 0000000000..dfae68d2dc
--- /dev/null
+++ b/apps/user_ldap/ajax/getConfiguration.php
@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * ownCloud - user_ldap
+ *
+ * @author Arthur Schiwon
+ * @copyright 2013 Arthur Schiwon blizzz@owncloud.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+// Check user and app status
+OCP\JSON::checkAdminUser();
+OCP\JSON::checkAppEnabled('user_ldap');
+OCP\JSON::callCheck();
+
+$prefix = $_POST['ldap_serverconfig_chooser'];
+$connection = new \OCA\user_ldap\lib\Connection($prefix);
+OCP\JSON::success(array('configuration' => $connection->getConfiguration()));
\ No newline at end of file
diff --git a/apps/user_ldap/js/settings.js b/apps/user_ldap/js/settings.js
index a07d140cf8..66876cadaf 100644
--- a/apps/user_ldap/js/settings.js
+++ b/apps/user_ldap/js/settings.js
@@ -49,6 +49,9 @@ $(document).ready(function() {
 							function(keep) {
 								if(!keep) {
 									$('#ldap').find('input[type=text], input[type=number], input[type=password], textarea, select').each(function() {
+										if($(this).attr('id') == 'ldap_serverconfig_chooser') {
+											return;
+										}
 										$(this).val($(this).attr('data-default'));
 									});
 									$('#ldap').find('input[type=checkbox]').each(function() {
@@ -61,8 +64,8 @@ $(document).ready(function() {
 								}
 							}
 						);
-						$('#ldap_serverconfig_chooser option:selected:first').removeAttr('selected');
-						var html = '<option value="'+result.configPrefix+'" selected>'+$('#ldap_serverconfig_chooser option').length+'. Server</option>';
+						$('#ldap_serverconfig_chooser option:selected').removeAttr('selected');
+						var html = '<option value="'+result.configPrefix+'" selected="selected">'+$('#ldap_serverconfig_chooser option').length+'. Server</option>';
 						$('#ldap_serverconfig_chooser option:last').before(html);
 					} else {
 						OC.dialogs.alert(
@@ -73,7 +76,35 @@ $(document).ready(function() {
 				}
 			);
 		} else {
-			alert(value);
+			$.post(
+				OC.filePath('user_ldap','ajax','getConfiguration.php'),
+				$('#ldap_serverconfig_chooser').serialize(),
+				function (result) {
+					if(result.status == 'success') {
+						$.each(result.configuration, function(configkey, configvalue) {
+							elementID = '#'+configkey;
+							
+							//deal with Checkboxes
+							if($(elementID).is('input[type=checkbox]')) {
+								if(configvalue == 1) {
+									$(elementID).attr('checked', 'checked');
+								} else {
+									$(elementID).removeAttr('checked');
+								}
+								return;
+							}
+							
+							//On Textareas, Multi-Line Settings come as array
+							if($(elementID).is('textarea') && $.isArray(configvalue)) {
+								configvalue = configvalue.join("\n");
+							}
+
+							// assign the value
+							$('#'+configkey).val(configvalue);
+						});
+					}
+				}
+			);
 		}
 	});
 });
\ No newline at end of file
-- 
GitLab