2016-12-09 13:46:29 +00:00
|
|
|
package main
|
|
|
|
import "strconv"
|
2016-12-21 02:30:32 +00:00
|
|
|
import "strings"
|
|
|
|
|
|
|
|
var settingLabels map[string]string
|
|
|
|
|
2016-12-23 12:35:22 +00:00
|
|
|
type OptionLabel struct
|
2016-12-21 02:30:32 +00:00
|
|
|
{
|
|
|
|
Label string
|
|
|
|
Value int
|
|
|
|
Selected bool
|
|
|
|
}
|
2016-12-09 13:46:29 +00:00
|
|
|
|
|
|
|
type Setting struct
|
|
|
|
{
|
|
|
|
Name string
|
|
|
|
Content string
|
|
|
|
Type string
|
2016-12-21 02:30:32 +00:00
|
|
|
Constraint string
|
2016-12-09 13:46:29 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 02:30:32 +00:00
|
|
|
func init() {
|
|
|
|
settingLabels = make(map[string]string)
|
|
|
|
settingLabels["activation_type"] = "Activate All,Email Activation,Admin Approval"
|
|
|
|
}
|
|
|
|
|
2017-06-14 09:55:47 +00:00
|
|
|
func LoadSettings() error {
|
|
|
|
rows, err := get_full_settings_stmt.Query()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var sname, scontent, stype, sconstraints string
|
|
|
|
for rows.Next() {
|
|
|
|
err = rows.Scan(&sname, &scontent, &stype, &sconstraints)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
errmsg := parseSetting(sname, scontent, stype, sconstraints)
|
|
|
|
if errmsg != "" {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-21 02:30:32 +00:00
|
|
|
func parseSetting(sname string, scontent string, stype string, constraint string) string {
|
2016-12-09 13:46:29 +00:00
|
|
|
var err error
|
|
|
|
if stype == "bool" {
|
2017-05-07 08:31:41 +00:00
|
|
|
settings[sname] = (scontent == "1")
|
2016-12-09 13:46:29 +00:00
|
|
|
} else if stype == "int" {
|
|
|
|
settings[sname], err = strconv.Atoi(scontent)
|
|
|
|
if err != nil {
|
|
|
|
return "You were supposed to enter an integer x.x\nType mismatch in " + sname
|
|
|
|
}
|
|
|
|
} else if stype == "int64" {
|
|
|
|
settings[sname], err = strconv.ParseInt(scontent, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return "You were supposed to enter an integer x.x\nType mismatch in " + sname
|
|
|
|
}
|
2016-12-21 02:30:32 +00:00
|
|
|
} else if stype == "list" {
|
|
|
|
cons := strings.Split(constraint,"-")
|
|
|
|
if len(cons) < 2 {
|
|
|
|
return "Invalid constraint! The second field wasn't set!"
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2016-12-21 02:30:32 +00:00
|
|
|
con1, err := strconv.Atoi(cons[0])
|
|
|
|
if err != nil {
|
|
|
|
return "Invalid contraint! The constraint field wasn't an integer!"
|
|
|
|
}
|
|
|
|
con2, err := strconv.Atoi(cons[1])
|
|
|
|
if err != nil {
|
|
|
|
return "Invalid contraint! The constraint field wasn't an integer!"
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2016-12-21 02:30:32 +00:00
|
|
|
value, err := strconv.Atoi(scontent)
|
|
|
|
if err != nil {
|
|
|
|
return "Only integers are allowed in this setting x.x\nType mismatch in " + sname
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2016-12-21 02:30:32 +00:00
|
|
|
if value < con1 || value > con2 {
|
|
|
|
return "Only integers between a certain range are allowed in this setting"
|
|
|
|
}
|
|
|
|
settings[sname] = value
|
2016-12-09 13:46:29 +00:00
|
|
|
} else {
|
|
|
|
settings[sname] = scontent
|
|
|
|
}
|
|
|
|
return ""
|
2017-06-10 07:58:15 +00:00
|
|
|
}
|