-
Notifications
You must be signed in to change notification settings - Fork 10
/
controller.go
100 lines (88 loc) · 2.99 KB
/
controller.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
package seo
import (
"net/http"
"net/url"
"path"
"strings"
"github.com/qor/admin"
"github.com/qor/responder"
)
type seoController struct {
Collection *Collection
}
func (sc seoController) Index(context *admin.Context) {
context = context.NewResourceContext(sc.Collection.SettingResource)
context.Execute("index", struct {
Collection *Collection
SettingResource *admin.Resource
}{
Collection: sc.Collection,
SettingResource: sc.Collection.SettingResource,
})
}
func (sc seoController) InlineEdit(context *admin.Context) {
settingContext := context.NewResourceContext(sc.Collection.SettingResource)
result := sc.Collection.SettingResource.NewStruct()
name, err := url.QueryUnescape(context.Request.Form.Get("name"))
if err != nil {
settingContext.AddError(err)
}
context.DB.Where("name = ?", name).First(result)
if seoSetting, ok := result.(QorSEOSettingInterface); ok {
seoSetting.SetCollection(sc.Collection)
}
responder.With("html", func() {
settingContext.Execute("edit", struct {
Setting interface{}
EditURL string
Metas []*admin.Section
}{
Setting: result,
EditURL: sc.Collection.SEOSettingURL(name),
Metas: seoSettingMetas(sc.Collection),
})
}).With("json", func() {
settingContext.JSON("edit", result)
}).Respond(context.Request)
}
func (sc seoController) Update(context *admin.Context) {
settingResource := sc.Collection.SettingResource
settingContext := context.NewResourceContext(settingResource)
result := settingResource.NewStruct()
name, err := url.QueryUnescape(context.Request.Form.Get("name"))
if err != nil {
settingContext.AddError(err)
}
context.DB.Where("name = ?", name).First(result)
if context.DB.NewRecord(result) {
context.Request.Form["QorResource.Name"] = []string{name}
context.Request.Form["QorResource.Setting.Type"] = []string{name}
}
seoSettingInterface := result.(QorSEOSettingInterface)
if seoSettingInterface.GetIsGlobalSEO() {
globalSetting := make(map[string]string)
for fieldWithPrefix := range context.Request.Form {
if strings.HasPrefix(fieldWithPrefix, "QorResource") {
field := strings.Replace(fieldWithPrefix, "QorResource.", "", -1)
globalSetting[field] = context.Request.Form.Get(fieldWithPrefix)
}
}
seoSettingInterface.SetGlobalSetting(globalSetting)
}
res := settingContext.Resource
if !settingContext.HasError() {
if settingContext.AddError(res.Decode(settingContext.Context, result)); !settingContext.HasError() {
settingContext.AddError(res.CallSave(result, settingContext.Context))
}
}
responder.With("html", func() {
http.Redirect(context.Writer, context.Request, path.Join(settingResource.GetAdmin().GetRouter().Prefix, context.Resource.ToParam()), http.StatusFound)
}).With("json", func() {
if settingContext.HasError() {
context.Writer.WriteHeader(admin.HTTPUnprocessableEntity)
settingContext.JSON("edit", map[string]interface{}{"errors": settingContext.GetErrors()})
} else {
settingContext.JSON("show", result)
}
}).Respond(context.Request)
}