2017-11-10 03:33:11 +00:00
package common
2017-09-03 04:50:31 +00:00
2017-11-11 23:34:27 +00:00
import (
"database/sql"
"strconv"
"strings"
"sync/atomic"
"../query_gen/lib"
)
2016-12-21 02:30:32 +00:00
2017-11-10 03:33:11 +00:00
// SettingMap is a map type specifically for holding the various settings admins set to toggle features on and off or to otherwise alter Gosora's behaviour from the Control Panel
type SettingMap map [ string ] interface { }
2017-09-03 04:50:31 +00:00
2017-11-10 03:33:11 +00:00
var SettingBox atomic . Value // An atomic value pointing to a SettingBox
2016-12-21 02:30:32 +00:00
2017-09-03 04:50:31 +00:00
type OptionLabel struct {
Label string
Value int
2016-12-21 02:30:32 +00:00
Selected bool
}
2016-12-09 13:46:29 +00:00
2017-09-03 04:50:31 +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
}
2017-11-11 23:34:27 +00:00
type SettingStmts struct {
getFull * sql . Stmt
}
var settingStmts SettingStmts
2016-12-21 02:30:32 +00:00
func init ( ) {
2017-11-10 03:33:11 +00:00
SettingBox . Store ( SettingMap ( make ( map [ string ] interface { } ) ) )
2017-11-12 03:29:05 +00:00
DbInits . Add ( func ( acc * qgen . Accumulator ) error {
2017-11-11 23:34:27 +00:00
settingStmts = SettingStmts {
getFull : acc . Select ( "settings" ) . Columns ( "name, content, type, constraints" ) . Prepare ( ) ,
}
return acc . FirstError ( )
} )
2016-12-21 02:30:32 +00:00
}
2017-06-14 09:55:47 +00:00
func LoadSettings ( ) error {
2017-11-11 23:34:27 +00:00
rows , err := settingStmts . getFull . Query ( )
2017-06-14 09:55:47 +00:00
if err != nil {
return err
}
defer rows . Close ( )
2017-11-10 03:33:11 +00:00
var sBox = SettingMap ( make ( map [ string ] interface { } ) )
2017-06-14 09:55:47 +00:00
var sname , scontent , stype , sconstraints string
for rows . Next ( ) {
err = rows . Scan ( & sname , & scontent , & stype , & sconstraints )
if err != nil {
return err
}
2017-08-20 09:39:02 +00:00
errmsg := sBox . ParseSetting ( sname , scontent , stype , sconstraints )
2017-06-14 09:55:47 +00:00
if errmsg != "" {
return err
}
}
err = rows . Err ( )
if err != nil {
return err
}
2017-08-20 09:39:02 +00:00
2017-11-10 03:33:11 +00:00
SettingBox . Store ( sBox )
2017-06-14 09:55:47 +00:00
return nil
}
2017-09-10 16:57:22 +00:00
// TODO: Add better support for HTML attributes (html-attribute). E.g. Meta descriptions.
2017-11-10 03:33:11 +00:00
func ( sBox SettingMap ) ParseSetting ( sname string , scontent string , stype string , constraint string ) string {
2016-12-09 13:46:29 +00:00
var err error
2017-09-10 16:57:22 +00:00
var ssBox = map [ string ] interface { } ( sBox )
2016-12-09 13:46:29 +00:00
if stype == "bool" {
2017-08-20 09:39:02 +00:00
ssBox [ sname ] = ( scontent == "1" )
2016-12-09 13:46:29 +00:00
} else if stype == "int" {
2017-08-20 09:39:02 +00:00
ssBox [ sname ] , err = strconv . Atoi ( scontent )
2016-12-09 13:46:29 +00:00
if err != nil {
return "You were supposed to enter an integer x.x\nType mismatch in " + sname
}
} else if stype == "int64" {
2017-08-20 09:39:02 +00:00
ssBox [ sname ] , err = strconv . ParseInt ( scontent , 10 , 64 )
2016-12-09 13:46:29 +00:00
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" {
2017-09-03 04:50:31 +00:00
cons := strings . Split ( constraint , "-" )
2016-12-21 02:30:32 +00:00
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 ] )
2017-11-08 07:28:33 +00:00
con2 , err2 := strconv . Atoi ( cons [ 1 ] )
if err != nil || err2 != nil {
2016-12-21 02:30:32 +00:00
return "Invalid contraint! The constraint field wasn't an integer!"
}
2017-06-10 07:58:15 +00:00
2017-09-03 04:50:31 +00:00
value , err := strconv . Atoi ( scontent )
2016-12-21 02:30:32 +00:00
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"
}
2017-08-20 09:39:02 +00:00
ssBox [ sname ] = value
2016-12-09 13:46:29 +00:00
} else {
2017-08-20 09:39:02 +00:00
ssBox [ sname ] = scontent
2016-12-09 13:46:29 +00:00
}
return ""
2017-06-10 07:58:15 +00:00
}