Skip to content

Commit

Permalink
ejaModuleLinks import/export support
Browse files Browse the repository at this point in the history
  • Loading branch information
ubaldus committed Sep 26, 2024
1 parent 3700978 commit f2ac795
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 51 deletions.
37 changes: 30 additions & 7 deletions db/assets/ejaGroups.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"type": "module",
"module": {
"parentName": "ejaAdministration",
"power": 2,
Expand All @@ -12,11 +11,11 @@
"edit",
"previous",
"next",
"search",
"save",
"copy",
"list",
"delete",
"save",
"search",
"list",
"link",
"unlink"
],
Expand All @@ -28,7 +27,10 @@
"type": "text",
"translate": 0,
"powerSearch": 1,
"name": "name"
"name": "name",
"sizeSearch": 0,
"sizeList": 0,
"sizeEdit": 0
},
{
"value": "",
Expand All @@ -37,7 +39,27 @@
"type": "textArea",
"translate": 0,
"powerSearch": 0,
"name": "note"
"name": "note",
"sizeSearch": 0,
"sizeList": 0,
"sizeEdit": 0
}
],
"link": [
{
"srcModule": "ejaPermissions",
"dstModule": "ejaGroups",
"power": 2
},
{
"srcModule": "ejaModules",
"dstModule": "ejaGroups",
"power": 1
},
{
"srcModule": "ejaGroups",
"dstModule": "ejaUsers",
"power": 1
}
],
"translation": [
Expand All @@ -59,5 +81,6 @@
"translation": "Note"
}
],
"name": "ejaGroups"
"name": "ejaGroups",
"type": "module"
}
54 changes: 42 additions & 12 deletions db/assets/ejaUsers.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"type": "module",
"module": {
"parentName": "ejaAdministration",
"power": 1,
Expand All @@ -12,11 +11,11 @@
"edit",
"previous",
"next",
"search",
"save",
"copy",
"list",
"delete"
"delete",
"save",
"search",
"list"
],
"field": [
{
Expand All @@ -26,7 +25,10 @@
"type": "text",
"translate": 0,
"powerSearch": 1,
"name": "username"
"name": "username",
"sizeSearch": 0,
"sizeList": 0,
"sizeEdit": 0
},
{
"value": "",
Expand All @@ -35,7 +37,10 @@
"type": "password",
"translate": 0,
"powerSearch": 0,
"name": "password"
"name": "password",
"sizeSearch": 0,
"sizeList": 0,
"sizeEdit": 0
},
{
"value": "SELECT ejaId,name FROM ejaModules ORDER BY name",
Expand All @@ -44,7 +49,10 @@
"type": "sqlMatrix",
"translate": 0,
"powerSearch": 0,
"name": "defaultModuleId"
"name": "defaultModuleId",
"sizeSearch": 0,
"sizeList": 0,
"sizeEdit": 0
},
{
"value": "SELECT ejaId,username FROM ejaUsers WHERE ejaId IN (SELECT value FROM ejaSession WHERE ejaSession.name='ejaOwners') ORDER BY username",
Expand All @@ -53,7 +61,10 @@
"type": "sqlMatrix",
"translate": 0,
"powerSearch": 3,
"name": "ejaOwner"
"name": "ejaOwner",
"sizeSearch": 0,
"sizeList": 0,
"sizeEdit": 0
},
{
"value": "SELECT name,nameFull FROM ejaLanguages ORDER BY nameFull",
Expand All @@ -62,7 +73,10 @@
"type": "sqlMatrix",
"translate": 0,
"powerSearch": 0,
"name": "ejaLanguage"
"name": "ejaLanguage",
"sizeSearch": 0,
"sizeList": 0,
"sizeEdit": 0
},
{
"value": "",
Expand All @@ -71,7 +85,22 @@
"type": "text",
"translate": 0,
"powerSearch": 0,
"name": "ejaSession"
"name": "ejaSession",
"sizeSearch": 0,
"sizeList": 0,
"sizeEdit": 0
}
],
"link": [
{
"srcModule": "ejaGroups",
"dstModule": "ejaUsers",
"power": 1
},
{
"srcModule": "ejaPermissions",
"dstModule": "ejaUsers",
"power": 2
}
],
"translation": [
Expand Down Expand Up @@ -117,5 +146,6 @@
"translation": "Language"
}
],
"name": "ejaUsers"
"name": "ejaUsers",
"type": "module"
}
23 changes: 23 additions & 0 deletions db/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,29 @@ func (session *TypeSession) ModuleExport(moduleId int64, data bool) (module Type
})
}

rows, err = session.Rows(`
SELECT
power,
srcFieldName,
(SELECT a.name FROM ejaModules AS a WHERE a.ejaId=srcModuleId LIMIT 1) AS srcModuleName,
(SELECT b.name FROM ejaModules AS b WHERE b.ejaId=dstModuleId LIMIT 1) AS dstModuleName
FROM
ejaModuleLinks
WHERE
srcModuleId=? OR dstModuleId=?
`, moduleId, moduleId)
if err != nil {
return
}
for _, row := range rows {
module.Link = append(module.Link, TypeModuleLink{
SrcModule: row["srcModuleName"],
SrcField: row["srcFieldName"],
DstModule: row["dstModuleName"],
Power: session.Number(row["power"]),
})
}

module.Command = []string{}
rows, err = session.Rows("SELECT name from ejaCommands WHERE ejaId IN (SELECT ejaCommandId FROM ejaPermissions WHERE ejaModuleId=?)", moduleId)
if err != nil {
Expand Down
22 changes: 21 additions & 1 deletion db/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ func (session *TypeSession) ModuleImport(module TypeModule, moduleName string) e
if field.EjaModuleName != moduleName {
moduleTmpId = 0
}

_, err := session.Run(`
INSERT INTO ejaTranslations
(ejaId, ejaOwner, ejaLog, ejaModuleId, ejaLanguage, word, translation)
Expand All @@ -248,6 +247,27 @@ func (session *TypeSession) ModuleImport(module TypeModule, moduleName string) e
}
}

for _, field := range module.Link {
srcModuleId := session.ModuleGetIdByName(field.SrcModule)
dstModuleId := session.ModuleGetIdByName(field.DstModule)
if srcModuleId > 0 && dstModuleId > 0 {
alreadyExists, err := session.Value(`SELECT COUNT(*) FROM ejaModuleLinks WHERE srcModuleId=? AND dstModuleId=?`, srcModuleId, dstModuleId)
if err != nil {
return err
}
if session.Number(alreadyExists) == 0 {
if _, err := session.Run(`
INSERT INTO ejaModuleLinks
(ejaOwner, ejaLog, srcModuleId, srcFieldName, dstModuleId, power)
VALUES
(?,?,?,?,?,?);
`, owner, session.Now(), srcModuleId, field.SrcField, dstModuleId, field.Power); err != nil {
return err
}
}
}
}

for _, data := range module.Data {
var keys = []string{"ejaLog"}
var values = []string{"?"}
Expand Down
15 changes: 12 additions & 3 deletions db/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

package db

// TypeModule represents a modular structure containing information about modules, fields, translations, and data.
// TypeModule represents a modular structure containing information about modules, fields, translations, links and data.
type TypeModule struct {
Module TypeModuleModule `json:"module"`
Command []string `json:"command"`
Field []TypeModuleField `json:"field"`
Translation []TypeModuleTranslation `json:"translation"`
Link []TypeModuleLink `json:"link,omitempty"`
Translation []TypeModuleTranslation `json:"translation,omitempty"`
Name string `json:"name"`
Data []map[string]interface{} `json:"data"`
Data []map[string]interface{} `json:"data,omitempty"`
Type string `json:"type"`
}

Expand Down Expand Up @@ -44,6 +45,14 @@ type TypeModuleTranslation struct {
Translation string `json:"translation"`
}

// TypeModuleModule represents module links withing modules in TypeModule.
type TypeModuleLink struct {
SrcField string `json:"srcField,omitempty"`
SrcModule string `json:"srcModule"`
DstModule string `json:"dstModule"`
Power int64 `json:"power,omitempty"`
}

// ModuleGetIdByName retrieves the module ID based on the given module name.
// If an error occurs during the database operation or table name is not valid, it returns 0.
func (session *TypeSession) ModuleGetIdByName(name string) int64 {
Expand Down
10 changes: 8 additions & 2 deletions db/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ func (session *TypeSession) mysqlRun(query string, args ...interface{}) (TypeRun

// mysqlValue executes a SQL query on the MySQL database and returns a single result as a string.
func (session *TypeSession) mysqlValue(query string, args ...interface{}) (result string, err error) {
row := session.Handler.QueryRow(query, args...)
err = row.Scan(&result)
var nullResult sql.NullString
err = session.Handler.QueryRow(query, args...).Scan(&nullResult)
if err != nil {
return
}
if nullResult.Valid {
result = nullResult.String
} else {
result = ""
}

return
}

Expand Down
25 changes: 0 additions & 25 deletions db/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,31 +138,6 @@ func (session *TypeSession) Setup(setupPath string) error {
}
}

// add module links
if moduleIdMap["ejaGroups"] > 0 {
if moduleIdMap["ejaPermissions"] > 0 {
if _, err := session.Run("INSERT INTO ejaModuleLinks (ejaOwner,ejaLog,dstModuleId,srcModuleId,power) VALUES (1,?,?,?,?)", session.Now(), moduleIdMap["ejaGroups"], moduleIdMap["ejaPermissions"], 2); err != nil {
return err
}
}
if moduleIdMap["ejaModules"] > 0 {
if _, err := session.Run("INSERT INTO ejaModuleLinks (ejaOwner,ejaLog,dstModuleId,srcModuleId,power) VALUES (1,?,?,?,?)", session.Now(), moduleIdMap["ejaGroups"], moduleIdMap["ejaModules"], 1); err != nil {
return err
}
}
}
if moduleIdMap["ejaUsers"] > 0 {
if moduleIdMap["ejaGroups"] > 0 {
if _, err := session.Run("INSERT INTO ejaModuleLinks (ejaOwner,ejaLog,dstModuleId,srcModuleId,power) VALUES (1,?,?,?,?)", session.Now(), moduleIdMap["ejaUsers"], moduleIdMap["ejaGroups"], 1); err != nil {
return err
}
}
if moduleIdMap["ejaPermissions"] > 0 {
if _, err := session.Run("INSERT INTO ejaModuleLinks (ejaOwner,ejaLog,dstModuleId,srcModuleId,power) VALUES (1,?,?,?,?)", session.Now(), moduleIdMap["ejaUsers"], moduleIdMap["ejaPermissions"], 2); err != nil {
return err
}
}
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion sys/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package sys

const Name = "Tibula"
const Version = "17.9.14"
const Version = "17.9.26"

var Options TypeConfig
var Commands TypeCommand

0 comments on commit f2ac795

Please sign in to comment.