Skip to content

Commit

Permalink
module import
Browse files Browse the repository at this point in the history
  • Loading branch information
ubaldus committed Jan 12, 2024
1 parent 8a2a3b2 commit 9889881
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 19 deletions.
24 changes: 24 additions & 0 deletions api/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
2 changes: 1 addition & 1 deletion db/assets/ejaExport.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"module": {
"parentName": "ejaSystem",
"power": 10,
"power": 11,
"searchLimit": 0,
"sqlCreated": 1
},
Expand Down
34 changes: 26 additions & 8 deletions db/assets/ejaImport.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"module": {
"parentName": "ejaSystem",
"power": 11,
"power": 10,
"searchLimit": 0,
"sqlCreated": 1
},
Expand All @@ -17,7 +17,7 @@
"type": "text",
"translate": 0,
"powerSearch": 1,
"name": "ejaModuleName"
"name": "moduleName"
},
{
"value": "",
Expand All @@ -32,10 +32,10 @@
"value": "",
"powerEdit": 0,
"powerList": 0,
"type": "file",
"type": "textArea",
"translate": 0,
"powerSearch": 3,
"name": "dataFile"
"name": "import"
}
],
"translation": [
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions db/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
19 changes: 11 additions & 8 deletions db/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@ 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(`
INSERT INTO ejaModules
(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,
Expand All @@ -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 {
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 9889881

Please sign in to comment.