From 9889881c11970d7ff4acb928dcbca889ca0be7a6 Mon Sep 17 00:00:00 2001 From: Ubaldo Porcheddu Date: Fri, 12 Jan 2024 15:32:27 +0000 Subject: [PATCH] module import --- api/plugins.go | 24 ++++++++++++++++++++++++ db/assets/ejaExport.json | 2 +- db/assets/ejaImport.json | 34 ++++++++++++++++++++++++++-------- db/export.go | 4 ++-- db/import.go | 19 +++++++++++-------- 5 files changed, 64 insertions(+), 19 deletions(-) diff --git a/api/plugins.go b/api/plugins.go index a5fb743..336482b 100644 --- a/api/plugins.go +++ b/api/plugins.go @@ -30,6 +30,30 @@ var Plugins = TypePlugins{ } return eja }, + "ejaImport": func(eja TypeApi) TypeApi { + if eja.Action == "run" { + moduleName := eja.Values["moduleName"] + moduleData := eja.Values["import"] + dataImport := db.Number(eja.Values["dataImport"]) > 0 + if moduleData != "" { + var module db.TypeModule + if err := json.Unmarshal([]byte(moduleData), &module); err != nil { + alert(&eja.Alert, db.Translate("ejaImportJsonError", eja.Owner)) + } else { + if !dataImport { + module.Data = nil + } + if err := db.ModuleImport(module, moduleName); err != nil { + alert(&eja.Alert, db.Translate("ejaImportError", eja.Owner)) + } else { + eja.Values["import"] = "" + info(&eja.Info, db.Translate("ejaImportOk", eja.Owner)) + } + } + } + } + return eja + }, "ejaExport": func(eja TypeApi) TypeApi { if eja.Action == "run" { moduleId := db.Number(eja.Values["ejaModuleId"]) diff --git a/db/assets/ejaExport.json b/db/assets/ejaExport.json index a203479..622e38d 100644 --- a/db/assets/ejaExport.json +++ b/db/assets/ejaExport.json @@ -1,7 +1,7 @@ { "module": { "parentName": "ejaSystem", - "power": 10, + "power": 11, "searchLimit": 0, "sqlCreated": 1 }, diff --git a/db/assets/ejaImport.json b/db/assets/ejaImport.json index 5b8269f..bd3ccba 100644 --- a/db/assets/ejaImport.json +++ b/db/assets/ejaImport.json @@ -1,7 +1,7 @@ { "module": { "parentName": "ejaSystem", - "power": 11, + "power": 10, "searchLimit": 0, "sqlCreated": 1 }, @@ -17,7 +17,7 @@ "type": "text", "translate": 0, "powerSearch": 1, - "name": "ejaModuleName" + "name": "moduleName" }, { "value": "", @@ -32,10 +32,10 @@ "value": "", "powerEdit": 0, "powerList": 0, - "type": "file", + "type": "textArea", "translate": 0, "powerSearch": 3, - "name": "dataFile" + "name": "import" } ], "translation": [ @@ -47,20 +47,38 @@ { "ejaLanguage": "en", "ejaModuleName": "ejaImport", - "word": "ejaModuleName", + "word": "moduleName", "translation": "Module Name" }, + { + "ejaLanguage": "en", + "ejaModuleName": "ejaImport", + "word": "import", + "translation": "Import" + }, { "ejaLanguage": "en", "ejaModuleName": "ejaImport", "word": "dataImport", - "translation": "Data Import" + "translation": "Import Data" + }, + { + "ejaLanguage": "en", + "ejaModuleName": "ejaImport", + "word": "ejaImportJsonError", + "translation": "Json structure is not compatible" + }, + { + "ejaLanguage": "en", + "ejaModuleName": "ejaImport", + "word": "ejaImportError", + "translation": "Cannot import this module" }, { "ejaLanguage": "en", "ejaModuleName": "ejaImport", - "word": "dataFile", - "translation": "File" + "word": "ejaImportOk", + "translation": "Module imported" }, { "ejaLanguage": "en", diff --git a/db/export.go b/db/export.go index 6535bda..44363b7 100644 --- a/db/export.go +++ b/db/export.go @@ -39,14 +39,14 @@ func ModuleExport(moduleId int64, data bool) (module TypeModule, err error) { rows, err = Rows(` SELECT ejaLanguage, word, translation, (SELECT ejaModules.name FROM ejaModules WHERE ejaModules.ejaId=ejaModuleId) AS ejaModuleName FROM ejaTranslations - WHERE ejaModuleId=? OR word='?' + WHERE ejaModuleId=? OR word=? `, moduleId, moduleName) if err != nil { return } for _, row := range rows { module.Translation = append(module.Translation, TypeModuleTranslation{ - EjaLanguage: row["ejaLangauge"], + EjaLanguage: row["ejaLanguage"], EjaModuleName: row["ejaModuleName"], Word: row["word"], Translation: row["translation"], diff --git a/db/import.go b/db/import.go index 4491fb8..3b4ed4e 100644 --- a/db/import.go +++ b/db/import.go @@ -11,12 +11,11 @@ import ( // ModuleImport imports a module into the database based on the provided TypeModule and module name. func ModuleImport(module TypeModule, moduleName string) error { const owner = 1 - name := moduleName if moduleName == "" { - name = String(module.Name) + moduleName = String(module.Name) } - moduleId := ModuleGetIdByName(name) + moduleId := ModuleGetIdByName(moduleName) if moduleId < 1 { moduleIdRun, err := Run(` @@ -24,7 +23,7 @@ func ModuleImport(module TypeModule, moduleName string) error { (ejaId, ejaOwner, ejaLog, name, power, searchLimit, sqlCreated, sortList, parentId) VALUES (NULL,?,?,?,?,?,?,?,?) - `, owner, Now(), name, + `, owner, Now(), moduleName, module.Module.Power, module.Module.SearchLimit, module.Module.SqlCreated, @@ -35,6 +34,10 @@ func ModuleImport(module TypeModule, moduleName string) error { return err } moduleId = moduleIdRun.LastId + if err := TableAdd(moduleName); err != nil { + return err + } + } if moduleId > 0 { @@ -45,11 +48,11 @@ func ModuleImport(module TypeModule, moduleName string) error { if module.Module.SqlCreated > 0 { for _, field := range module.Field { - if check, err := FieldExists(module.Name, field.Name); !check { + if check, err := FieldExists(moduleName, field.Name); !check { if err != nil { return err } - if err := FieldAdd(module.Name, field.Name, field.Type); err != nil { + if err := FieldAdd(moduleName, field.Name, field.Type); err != nil { return err } } @@ -114,14 +117,14 @@ func ModuleImport(module TypeModule, moduleName string) error { return err } - _, err = Run(`DELETE FROM ejaTranslations WHERE word=? AND ejaModuleId < 1`, name) + _, err = Run(`DELETE FROM ejaTranslations WHERE word=? AND ejaModuleId < 1`, moduleName) if err != nil { return err } for _, field := range module.Translation { moduleTmpId := moduleId - if field.EjaModuleName != name { + if field.EjaModuleName != moduleName { moduleTmpId = 0 }