forked from admpub/nging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings_helper.go
163 lines (155 loc) · 3.83 KB
/
settings_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package manager
import (
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/admpub/nging/application/dbschema"
"github.com/admpub/nging/application/model"
"github.com/admpub/nging/application/registry/settings"
)
func configPost(c echo.Context, groups ...string) error {
m := model.NewConfig(c)
formValues := c.Forms()
mapx := echo.NewMapx(formValues)
var configList map[string]map[string]*dbschema.NgingConfig
if len(groups) > 0 {
configList = map[string]map[string]*dbschema.NgingConfig{}
defaults := settings.ConfigDefaults()
for _, group := range groups {
conf, ok := defaults[group]
if !ok {
continue
}
configList[group] = conf
}
} else {
configList = settings.ConfigDefaults()
}
for group, configs := range configList {
_, err := m.ListByGroup(group)
if err != nil {
if err == db.ErrNoMoreRows {
for _, cfg := range configs {
_, err := cfg.Add()
if err != nil {
return err
}
}
continue
}
return err
}
encoder := settings.GetEncoder(group)
gm := mapx.Get(group)
if gm == nil {
continue
}
added := map[string]int{}
for key, conf := range m.Objects() {
conf.CPAFrom(m.NgingConfig)
added[conf.Key] = key
setting := gm.Get(conf.Key)
if setting == nil {
continue
}
disabled := setting.Value(`disabled`)
_v := setting.Get(`value`)
if _v == nil {
continue
}
value, err := settings.EncodeConfigValue(_v, conf, encoder)
if err != nil {
return err
}
var n int64
condition := db.And(
db.Cond{`key`: conf.Key},
db.Cond{`group`: group},
)
n, err = m.Count(nil, condition)
if err != nil {
return err
}
if n < 1 {
err = settings.InsertBy(c, configs, conf.Key, value, disabled)
if err != nil {
return err
}
}
set := echo.H{}
if value != conf.Value {
set[`value`] = value
}
if _v.IsMap() {
if conf.Type != `json` {
set[`type`] = `json`
}
} else if _v.IsSlice() {
if conf.Type != `list` {
set[`type`] = `list`
}
} else {
cfg, ok := configs[conf.Key]
if ok && cfg != nil && conf.Type != cfg.Type {
set[`type`] = cfg.Type
}
//set[`type`] = `text`
}
if len(disabled) > 0 && conf.Disabled != disabled {
set[`disabled`] = disabled
}
if len(set) > 0 {
err = m.SetFields(nil, set, condition)
if err != nil {
return err
}
}
}
err = settings.InsertMissing(c, gm, added, configs, encoder)
if err != nil {
return err
}
}
return nil
}
func configGet(c echo.Context, groups ...string) error {
m := model.NewConfig(c)
if len(groups) > 0 {
for _, group := range groups {
cfg, err := m.ListMapByGroup(group)
if err != nil {
return err
}
c.Set(group, cfg) //Stored.base.siteName
}
return nil
}
_, err := m.ListByOffset(nil, func(r db.Result) db.Result {
return r.Group(`group`)
}, 0, -1)
if err != nil {
return err
}
for _, setting := range m.Objects() {
group := setting.Group
cfg, err := m.ListMapByGroup(group)
if err != nil {
return err
}
c.Set(group, cfg) //Stored.base.siteName
}
return nil
}