From cc12ab39f01f367676e68de421c89e591412bce0 Mon Sep 17 00:00:00 2001 From: ptruessel Date: Fri, 13 Dec 2024 09:09:55 +0100 Subject: [PATCH] Generate SQL File --- www/backend/core/com/settings.cfc | 86 +++++++++++++++++++ .../core/handler/sysadmin/languages.cfm | 19 ---- www/backend/core/views/sysadmin/mappings.cfm | 33 ++++++- www/backend/core/views/sysadmin/sql_code.cfm | 24 ++++++ .../core/views/sysadmin/translations.cfm | 39 +++++++-- 5 files changed, 175 insertions(+), 26 deletions(-) create mode 100644 www/backend/core/views/sysadmin/sql_code.cfm diff --git a/www/backend/core/com/settings.cfc b/www/backend/core/com/settings.cfc index 457a20dc..62d9a184 100644 --- a/www/backend/core/com/settings.cfc +++ b/www/backend/core/com/settings.cfc @@ -102,4 +102,90 @@ component displayname="settings" output="false" { } + + // This code generates an SQL script that formats all entries from the given table + public string function generateSqlCode(required string sqlTable, required string primKey) { + + // Check if the table exists + local.checkSqlTable = queryExecute( + options = {datasource = application.datasource}, + params = { + table_name: {type: "string", value: arguments.sqlTable} + }, + sql = " + SELECT COUNT(*) as cnt + FROM information_schema.tables + WHERE table_name = :table_name + " + ) + + if (local.checkSqlTable.cnt gt 0) { + + local.getEntries = queryExecute( + options = {datasource = application.datasource}, + sql = " + SELECT * + FROM #arguments.sqlTable# + " + ) + + if (local.getEntries.recordCount) { + + // Get column list and remove the first entry + local.columns = local.getEntries.columnList; + local.columnsArray = listToArray(local.columns, ","); + arrayDeleteAt(local.columnsArray, 1); // Removes the first entry + + // Create the filtered list of columns + local.filteredColumns = arrayToList(local.columnsArray, ", "); + + // Generate the SQL statement + local.sqlOutput = "INSERT INTO #arguments.sqlTable# (" & local.filteredColumns & ") VALUES" & chr(10); + + // Collect values + local.values = []; + for (local.row in local.getEntries) { + local.valueRow = []; + for (local.column in local.columnsArray) { + local.value = "'" & replace(local.row[local.column], "'", "''", "all") & "'"; // Escaping + arrayAppend(local.valueRow, local.value); + } + arrayAppend(local.values, "(" & arrayToList(local.valueRow, ", ") & ")"); + } + local.sqlOutput &= arrayToList(local.values, "," & chr(10)) & chr(10); + + // ON DUPLICATE KEY UPDATE Logic + local.updateClauses = []; + for (local.column in local.columnsArray) { + if (local.column NEQ arguments.primKey) { // Exclude primary key + arrayAppend(local.updateClauses, local.column & " = VALUES(" & local.column & ")"); + } + } + local.sqlOutput &= "ON DUPLICATE KEY UPDATE " & chr(10) & arrayToList(local.updateClauses, "," & chr(10)) & ";"; + + // Return with textarea + return ' + '; + + } else { + + return "No entries in table " & arguments.sqlTable; + + } + + + + } else { + + return "No table found!" + + } + + + + + } + } \ No newline at end of file diff --git a/www/backend/core/handler/sysadmin/languages.cfm b/www/backend/core/handler/sysadmin/languages.cfm index 718cf27a..3ea4f578 100644 --- a/www/backend/core/handler/sysadmin/languages.cfm +++ b/www/backend/core/handler/sysadmin/languages.cfm @@ -16,25 +16,6 @@ if (structKeyExists(form, "modal_lng")) { table = form.table; key = form.key; - /* if(referer eq "/sysadmin/mappings##frontend" and field neq "strhtmlcodes"){ - loop list=form.fieldnames index="i" { - if (listFirst(i, "_") eq "text") { - text_value = evaluate(i); - text_value = application.objGlobal.cleanUpText(text_value); - form[i] = text_value; - } - } - } else if (referer eq "/sysadmin/mappings##frontend" and field eq "strhtmlcodes"){ - loop list=form.fieldnames index="i" { - if (listFirst(i, "_") eq "text") { - text_value = evaluate(i); - text_value = toString(binaryDecode(text_value, "base64")); - text_value = left(text_value, 3000); - form[i] = text_value; - } - } - } */ - try { loop list=form.fieldnames index="i" { diff --git a/www/backend/core/views/sysadmin/mappings.cfm b/www/backend/core/views/sysadmin/mappings.cfm index c3256694..d4504980 100644 --- a/www/backend/core/views/sysadmin/mappings.cfm +++ b/www/backend/core/views/sysadmin/mappings.cfm @@ -44,7 +44,22 @@
-
Custom mappings
+ +
+ +
Custom mappings
+ + + + + +
+ +

Here you can create your own mappings. These mappings are not affected by any system updates.

@@ -153,7 +168,21 @@
-
Frontend mappings
+ +
+ +
Frontend mappings
+ + + + + +
+

Here you can create your own Frontend mappings. These mappings are not affected by any system updates.

diff --git a/www/backend/core/views/sysadmin/sql_code.cfm b/www/backend/core/views/sysadmin/sql_code.cfm new file mode 100644 index 00000000..70c35875 --- /dev/null +++ b/www/backend/core/views/sysadmin/sql_code.cfm @@ -0,0 +1,24 @@ + + + +// This code generates an SQL script that formats all entries from the given table + +if (structKeyExists(session, "sysadmin") and session.sysadmin) { + + if (structKeyExists(url, "sql_table")) { + + param name="url.prim_key" default="strVariable"; + writeOutput(application.objSettings.generateSqlCode(url.sql_table, url.prim_key)); + + // For frontend mappings we also need the table frontend_mappings_trans + if (url.sql_table eq "frontend_mappings") { + + writeOutput(application.objSettings.generateSqlCode("frontend_mappings_trans", "strMapping")); + + } + + } + +} + + \ No newline at end of file diff --git a/www/backend/core/views/sysadmin/translations.cfm b/www/backend/core/views/sysadmin/translations.cfm index dc85f85e..b968cf57 100644 --- a/www/backend/core/views/sysadmin/translations.cfm +++ b/www/backend/core/views/sysadmin/translations.cfm @@ -112,8 +112,22 @@
active">
-
Custom translations
+
+ +
Custom translations
+ + + + + +
+

Here you can create your own translations (variables). These are used for system texts and are called with the function "getTrans()". These translations are not affected by any system updates.

+
@@ -273,7 +287,22 @@
active">
-
System translations
+ +
+ +
System translations
+ + + + + +
+ +

The system translations are used by the developers of the saaster.io project. Users of the tool should only perform translations and not change any variables. Co-developers can request changes via Github.

@@ -392,7 +421,7 @@ A Deepl API key is required for this. Please check whether the language you want to translate is supported.

- +