From 127abd6fb8bab4e46e1a13d65ac2d767f52545fc Mon Sep 17 00:00:00 2001
From: guillermo <guillermo@verdnatura.es>
Date: Mon, 2 Jan 2023 12:50:13 +0100
Subject: [PATCH] refs #4974 Added updateODBC function and more

---
 src/main.js       | 159 +++++++++++++++++++++++++---------------------
 src/style.css     |  22 +++----
 src/vn-access.hta |   6 +-
 3 files changed, 99 insertions(+), 88 deletions(-)

diff --git a/src/main.js b/src/main.js
index 844c1ec..87e5d2c 100755
--- a/src/main.js
+++ b/src/main.js
@@ -1,11 +1,11 @@
 
+var odbcPath = 'HKCU\\Software\\ODBC\\ODBC.INI\\';
 var Conf = {
 	appName: 'Verdnatura',
 	dsName: 'verdnatura',
-	dsPath: 'HKCU\\SOFTWARE\\ODBC\\ODBC.INI\\verdnatura',
+	dsPath: odbcPath + 'verdnatura',
 	regPath: 'HKCU\\SOFTWARE\\Verdnatura\\vn-access',
 	remoteUrl: 'https://cdn.verdnatura.es/vn-access',
-	dbHost: 'db.verdnatura.es',
 	defaultModule: 'vn',
 	defaultLocale: 'es',
 	defaultBranch: 'master',
@@ -16,7 +16,6 @@ var Conf = {
 	defaultDatasource: 'production',
 	version: 4,
     cdnURL: 'https://cdn.verdnatura.es/vn-access',
-    cdnURL: 'https://salix.verdnatura.es',
     datasources: {
         production: {
             DESCRIPTION: 'production',
@@ -47,7 +46,7 @@ var Conf = {
 
 var Locale = {
 	es: {
-		"Enter a user name":
+		"Enter a username":
 			"Introduce un nombre de usuario",
 		"Enter a password":
 			"Introduce una contraseña",
@@ -79,7 +78,7 @@ var App = {
 	init: function() {
 		// Specify the size of window
 		var width = 420;
-		var height = 560;
+		var height = 550;
 		window.resizeTo(width, height);
 		window.moveTo((screen.width - width) / 2, (screen.height - height) / 2);
 	},
@@ -109,42 +108,24 @@ var App = {
 		var myVersion = this.regRead(Conf.regPath, 'version');
 		
 		if (!myVersion || myVersion != Conf.version) {
-			var path;
+			var path = 'HKCU\\Software\\Microsoft\\Office\\11.0\\Access\\';
 			
 			// Creates the Access configuration entries
-			path = 'HKCU\\Software\\Microsoft\\Office\\11.0\\Access\\Settings';
-			this.regWrites(path, {
-				'Confirm Document Deletions' : false,
-				'Confirm Action Queries'     : false,
-				'Confirm Record Changes'     : false
+			this.regWrites(path + 'Settings', {
+				'Confirm Document Deletions': false,
+				'Confirm Action Queries': false,
+				'Confirm Record Changes': false
 			});
-			
-			path = 'HKCU\\Software\\Microsoft\\Office\\11.0\\Access\\Security';
-			this.regWrite(path, 'Level', 1);
+			this.regWrite(path + 'Security', 'Level', 1);
 			
 			// Creates the MySQL ODBC connection
-			var driverPath = this.getEnv('ProgramFiles') + Conf.driverPath;
-			
-			var params = {
-				Driver                  : driverPath,
-				DESCRIPTION             : Conf.appName,
-				SERVER                  : Conf.dbHost,
-				DATABASE                : Conf.dbName,
-				SSLCA                   : this.certFile,
-				SSLMODE                 : 'VERIFY_IDENTITY',
-				SSLCIPHER               : 'AES256-SHA',
-				AUTO_RECONNECT          : '1',
-				NO_PROMPT               : '1',
-				ENABLE_CLEARTEXT_PLUGIN : '1'
-			};
-
-			this.createOdbc(
+			this.createODBC(
+				odbcPath,
 				Conf.dsName,
-				Conf.odbcDriver,
-				params
+				Conf.odbcDriver
 			);
 
-			//Datasources creations
+			//Datasources creation
 			this.createDatasources();
 			
 			// Upload the string value to version installed
@@ -186,7 +167,7 @@ var App = {
 
 		// Datasource options
 		this.$('datasource').value = this.getDatasource();
-		this.onChangeDatasource();
+		this.onChangeDatasource(true);
 	},
 
 	resetForm: function(clearPassword) {
@@ -197,10 +178,42 @@ var App = {
 		this.$('user').select();
 	},
 
-	createOdbc: function(dsName, driverName, params) {
-		var odbcPath = 'HKCU\\Software\\ODBC\\ODBC.INI\\';		
-		this.regWrites(odbcPath + dsName, params);
-		this.regWrite(odbcPath + 'ODBC Data Sources', dsName, driverName);	
+	createODBC: function(path, dsName, driverName) {
+		var driverPath = this.getEnv('ProgramFiles') + Conf.driverPath;
+			
+		var params = {
+			Driver: driverPath,
+			DESCRIPTION: Conf.appName,
+			SERVER: Conf.datasources.production.SERVER,
+			DATABASE: Conf.dbName,
+			SSLCA: this.certFile,
+			SSLMODE: 'VERIFY_IDENTITY',
+			SSLCIPHER: 'AES256-SHA',
+			AUTO_RECONNECT: 1,
+			NO_PROMPT: 1,
+			ENABLE_CLEARTEXT_PLUGIN: 1
+		};
+
+		this.regWrite(path + 'ODBC Data Sources', dsName, driverName);
+		this.regWrites(path + dsName, params);
+	},
+
+	updateODBC: function(path, dsName, datasource) {
+		var params;
+
+		if (datasource == 'production')
+			params = Conf.datasources.production
+
+		if (datasource == 'test')
+			params = Conf.datasources.test
+
+		if (datasource == 'dev')
+			params = Conf.datasources.dev
+
+		if (datasource == 'localhost')
+			params = Conf.datasources.localhost
+
+		this.regWrites(path + dsName, params);
 	},
 	
 	disableUi: function(disabled, loadMessage) {
@@ -256,16 +269,23 @@ var App = {
 		this.$('user').focus();
 	},
 
-	onChangeDatasource: function() {
-		if (this.$("datasource").value == "production"||"test"||"dev") {
-			this.$("datasourceButton").className = this.$("datasource").value;
-			this.$("datasource").className = this.$("datasource").value;
+	onChangeDatasource: function(isOnLoad) {
+		var myDatasource = this.$("datasource").value;
+		if (myDatasource == "production"||"test"||"dev") {
+			this.$("datasourceButton").className = myDatasource;
+			this.$("datasource").className = myDatasource;
 		} else {
 			this.$("datasourceButton").className = null;
 			this.$("datasource").className = null;
 		}
-		this.updateDatasource(this.$("datasource").value);
-		this.$('user').focus();
+		if (!isOnLoad) {
+			this.updateDatasource(myDatasource);
+			this.updateODBC(
+				odbcPath, 
+				Conf.dsName,
+				myDatasource
+			);
+		}
 
 		var curDatasource = this.regRead(Conf.regPath, 'currentDatasource');
 		var datasourcePath = Conf.regPath + '\\datasources\\' + curDatasource;
@@ -276,6 +296,7 @@ var App = {
 			this.$('user').value = myUID;
 			this.$('password').value = myPWD;
 		}
+		this.$('user').focus();
 	},
 
 	cleanCache: function() {
@@ -324,18 +345,12 @@ var App = {
 	login: function() {
 		try {
 			var user = this.$('user').value;
+			if (!user)
+				throw new Error(_('Enter a username'));
+
 			var password = this.$('password').value;
-			var branch = this.$('branch').value;
-			var datasource = this.$('datasource').value;
-	
-			if (!user || user === '')
-				throw new Error(_('Enter a user name'));
-			if (!password || password === '')
+			if (!password)
 				throw new Error(_('Enter a password'));
-			if (!branch || branch === '')
-				throw new Error(_('Select a branch'));
-			if (!datasource || datasource === '')
-				throw new Error(_('Select a datasource'));	
 		
 			this.regWrite(Conf.dsPath, 'UID', user);
 			this.regWrite(Conf.dsPath, 'PWD', password);
@@ -372,15 +387,15 @@ var App = {
 		var mysqlConn = new ActiveXObject('ADODB.Connection');;
 
 		try {
-			mysqlConn.open(this.getOdbcString({
-				Driver                  : '{'+ Conf.odbcDriver +'}',
-				Server                  : Conf.dbHost,
-				Database                : Conf.dbName,
-				Uid                     : this.$('user').value,
-				Pwd                     : this.$('password').value,
-				Sslca                   : this.certFile,
-				SslMode                 : 'VERIFY_IDENTITY',
-				SslCipher               : 'AES256-SHA',
+			mysqlConn.open(this.getODBCString({
+				Driver: '{'+ Conf.odbcDriver +'}',
+				Server: Conf.datasources.production.SERVER,
+				Database: Conf.dbName,
+				Uid: this.$('user').value,
+				Pwd: this.$('password').value,
+				Sslca: this.certFile,
+				SslMode: 'VERIFY_IDENTITY',
+				SslCipher: 'AES256-SHA',
 				ENABLE_CLEARTEXT_PLUGIN : 1
 			}));
 		} catch (err) {
@@ -442,7 +457,8 @@ var App = {
 			'SELECT Version FROM tblVariables',
 			'Version', parseInt
 		);
-		if (!localVersion) localVersion = false;
+		if (!localVersion)
+			localVersion = false;
 
 		// Determines if should download
 		return !localVersion || version === false || localVersion != version
@@ -455,7 +471,7 @@ var App = {
 		try {
 			if (this.fso.fileExists(this.mdbFile)) {
 				var mdbConn = new ActiveXObject('ADODB.Connection');
-				mdbConn.open(this.getOdbcString({
+				mdbConn.open(this.getODBCString({
 					'Provider': 'Microsoft.Jet.OLEDB.4.0',
 					'Data Source': this.mdbFile
 				}));
@@ -473,7 +489,7 @@ var App = {
 
 		return value;
 	},
-	getOdbcString: function(options) {
+	getODBCString: function(options) {
 		var optionsArray = [];
 		for (var option in options)
 			optionsArray.push(option +'='+ options[option]);
@@ -485,7 +501,7 @@ var App = {
 		
 		try {
 			if (request.status !== 200)
-				throw new Error('HTTP: '+ request.statusText);
+				throw new Error('HTTP: '+ request.statusText + ' ' + request.status);
 
 			if (this.fso.fileExists(this.compressFile))
 				this.fso.deleteFile(this.compressFile);
@@ -652,20 +668,19 @@ var App = {
 		this.regWrite(Conf.regPath, 'currentDatasource', datasource);
 	},
 	createDatasources: function() {
-		this.regWrite(Conf.regPath + '\\datasources');
-
 		var params = {
 			'UID': null,
 			'PWD': null
 		}
 		for (var datasource in Conf.datasources) {
+			var myPath = Conf.regPath + '\\datasources\\' + datasource
 			for (var stringValues in Conf.datasources[datasource]) {
-				this.regWrite(Conf.regPath + '\\datasources\\' + datasource,
+				this.regWrite(myPath,
 					stringValues,
 					Conf.datasources[datasource][stringValues]
 				);
 			}
-			this.regWrites(Conf.regPath + '\\datasources\\' + datasource, params);
+			this.regWrites(myPath, params);
 		};
 
 	},
@@ -676,4 +691,4 @@ App.init();
 function _(string) {
 	var translation = Locale[Conf.defaultLocale][string];
 	return translation ? translation : string;
-}
+}
\ No newline at end of file
diff --git a/src/style.css b/src/style.css
index 33cb571..19f6458 100755
--- a/src/style.css
+++ b/src/style.css
@@ -41,12 +41,8 @@ option {
 #inputs > div > * {
 	display: block;
 }
-label {
-	font-weight: normal;
-	font-size: .9em;
-}
 #inputs label {
-	color: #666;
+	color: rgb(155, 155, 155);
 	font-size: .8em;
 }
 input[type='text'],
@@ -58,7 +54,7 @@ input[type='password'] {
 	color: #333;
 	padding-top: .5em;
 	padding-bottom: .5em;
-	padding-left: .2em;
+	padding-left: .3em;
 	max-width: auto;
 }
 input[type='text']:focus,
@@ -67,7 +63,7 @@ input[type='password']:focus {
 }
 #notSignOut {
 	margin-top: 1em;
-	font-size: .9em;
+	font-size: .8em;
 	margin-bottom: 2.5em;
 }
 #submit {
@@ -86,7 +82,6 @@ input[type='password']:focus {
 	margin-top: 10px;
 }
 #clean:hover {
-
 	text-decoration: underline;
 	text-decoration-color: #44f;
 }
@@ -124,6 +119,7 @@ button:hover {
 #branchSelector {
 	float: right;
 	margin-top: .7em;
+	width: 107px;
 	display: none;
 	padding-left: .3em;
 	padding-right: .8em;
@@ -132,7 +128,7 @@ button:hover {
 	float: left;
 	border-radius: 25px;
 	color: black;
-	box-shadow: 0px 0px 5px 0px black;
+	box-shadow: 0 0 .25em rgba(1,1,1,.6);
 	background-color: #8d8d8d;
 }
 #branchButton:hover {
@@ -186,14 +182,14 @@ select.dev {
 	float: right;
 	display: none;
 	padding-right: .8em;
-	margin-right: 0.5em;
+	margin-right: .5em;
 	margin-top: -1.91em;
 }
 #datasourceButton {
 	float: right;
 	border-radius: 25px;
 	color: black;
-	box-shadow: 0px 0px 5px 0px #000000;
+	box-shadow: 0 0 .25em rgba(1,1,1,.6);
 	background-color: #8d8d8d;
 }
 #datasourceButton:hover {
@@ -235,7 +231,7 @@ select.dev {
 	font-size: .8em;
 	display: none;
 	border-radius: 5px;
-	box-shadow: 0 0 0.4em rgba(1,1,1,0.6);
+	box-shadow: 0 0 .4em rgba(1,1,1,.6);
 }
 #message.error {
 	border-color: black;
@@ -257,7 +253,7 @@ select.dev {
 	width: 100%;
 	height: 100%;
 	background-color: black;
-	opacity: 0.7;
+	opacity: .7;
 	filter: alpha(opacity=50);
 	top: 0;
 	left: 0;
diff --git a/src/vn-access.hta b/src/vn-access.hta
index 3151c38..26d5356 100755
--- a/src/vn-access.hta
+++ b/src/vn-access.hta
@@ -47,10 +47,10 @@
 		</div>
 	</div>
 	<div id="submit">
-		<button id="enter" onclick="App.onEnterClick()"> Iniciar sesión</button>
+		<button id="enter" onclick="App.onEnterClick()">Iniciar sesión</button>
 	</div>
 	<div id="cleanDiv">
-		<a id="clean" href="#" onclick="App.onCleanCacheClick()"> Limpiar caché </a>
+		<a id="clean" href="#" onclick="App.onCleanCacheClick()">Limpiar caché </a>
 	</div>
 	<div id="branchOptions">
 		<div id="branchButton">
@@ -78,7 +78,7 @@
 					App.onShowBranchOptionsClick()"
 				alt="Change datasource"/>
 			<div id="datasourceSelector">
-				<select id="datasource" onchange="App.onChangeDatasource()">
+				<select id="datasource" onchange="App.onChangeDatasource(false)">
 					<option value="production">production</option>
 					<option value="test">test</option>
 					<option value="dev">dev</option>