-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller_utils.go
94 lines (84 loc) · 2.35 KB
/
controller_utils.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
package admin
import (
"net/http"
"strings"
"github.com/ecletus/responder"
"github.com/ecletus/roles"
)
// Show render show page
func (this *Controller) showOrEdit(context *Context, onRecorde func(record interface{}) bool) {
context.DefaulLayout()
if context.Type.Has(SHOW) {
if !context.Resource.ReadOnly && !context.Resource.Sections.Default.Screen.Show.IsSetI() {
context.Type = context.Type.Clear(SHOW).Set(EDIT)
context.PermissionMode = roles.Update
}
}
recorde, _ := this.LoadShowData(context)
if recorde == nil && !context.HasError() {
context.Writer.WriteHeader(http.StatusNotFound)
responder.
With("html", func() {
context.NotFound = true
context.Execute("record_not_found", recorde)
}).
Respond(context.Request)
return
}
if context.HasError() {
context.Execute("shared/errors", nil)
return
}
if context.Resource.AdminHasRecordPermission(context.PermissionMode, context, recorde).Deny() {
if strings.HasSuffix(context.OriginalURL.Path, "/edit") {
http.Redirect(context.Writer, context.Request, strings.TrimSuffix(context.OriginalURL.Path, "/edit"), http.StatusFound)
} else {
context.Writer.WriteHeader(http.StatusForbidden)
}
return
}
if context.Resource.IsSoftDeleted(recorde) {
context.Type |= DELETED
}
responder.
With("html", func() {
if context.LoadDisplayOrError() {
if !context.HasError() {
if recorde == nil {
context.NotFound = true
http.NotFound(context.Writer, context.Request)
return
}
// if !context.Type.Has(DELETED) && context.Resource.IsSoftDeleted(recorde) {
// http.Redirect(context.Writer, context.Request, context.Resource.GetContextIndexURI(context.Context), http.StatusSeeOther)
// return
// }
if onRecorde(recorde) {
context.Execute("", recorde)
}
} else {
context.Execute("shared/errors", recorde)
}
}
}).
With([]string{"json", "xml"}, func() {
if context.ValidateLayoutOrError() {
if !context.HasError() {
if recorde == nil {
context.NotFound = true
http.NotFound(context.Writer, context.Request)
return
} else {
if onRecorde(recorde) {
context.Encode(recorde)
}
}
} else {
context.Writer.WriteHeader(http.StatusBadGateway)
context.Writer.Write([]byte(context.Error()))
}
}
}).
XAccept().
Respond(context.Request)
}