forked from qor/publish2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
83 lines (68 loc) · 2.56 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
package publish2
import (
"fmt"
"html/template"
"github.com/qor/admin"
"github.com/qor/qor"
"github.com/qor/roles"
)
type controller struct {
Resource *admin.Resource
}
type visiblePublishResourceInterface interface {
VisiblePublishResource(*qor.Context) bool
}
func (ctr controller) Dashboard(context *admin.Context) {
type resourceResult struct {
Resource *admin.Resource
ComingOnlineResults interface{}
GoingOfflineResults interface{}
}
var results = []resourceResult{}
for _, res := range context.Admin.GetResources() {
if IsSchedulableModel(res.Value) {
if visibleInterface, ok := res.Value.(visiblePublishResourceInterface); ok {
if !visibleInterface.VisiblePublishResource(context.Context) {
continue
}
} else if res.Config.Invisible {
continue
}
db := context.GetDB()
result := resourceResult{Resource: res}
comingOnlineData := res.NewSlice()
if db.Set(VisibleMode, "on").Set(ScheduleMode, ComingOnlineMode).Set(VersionMode, VersionMultipleMode).Find(comingOnlineData).RowsAffected > 0 {
result.ComingOnlineResults = comingOnlineData
}
goingOfflineData := res.NewSlice()
if db.Set(VisibleMode, "on").Set(ScheduleMode, GoingOfflineMode).Set(VersionMode, VersionMultipleMode).Find(goingOfflineData).RowsAffected > 0 {
result.GoingOfflineResults = goingOfflineData
}
if result.ComingOnlineResults != nil || result.GoingOfflineResults != nil {
results = append(results, result)
}
}
}
context.Action = "index"
context.Execute("publish2/dashboard", results)
}
func (ctr controller) Versions(context *admin.Context) {
records := context.Resource.NewSlice()
record := context.Resource.NewStruct()
primaryQuerySQL, primaryParams := ctr.Resource.ToPrimaryQueryParams(context.ResourceID, context.Context)
tx := context.GetDB().Set(admin.DisableCompositePrimaryKeyMode, "on").Set(VersionMode, VersionMultipleMode).Set(ScheduleMode, ModeOff).Set(VisibleMode, ModeOff)
tx.Where(primaryQuerySQL, primaryParams...).First(record)
scope := tx.NewScope(record)
tx.Find(records, fmt.Sprintf("%v = ?", scope.PrimaryKey()), scope.PrimaryKeyValue())
result := context.Funcs(template.FuncMap{
"version_metas": func() (metas []*admin.Meta) {
for _, name := range []string{"VersionName", "ScheduledStartAt", "ScheduledEndAt", "PublishReady", "PublishLiveNow"} {
if meta := ctr.Resource.GetMeta(name); meta != nil && meta.HasPermission(roles.Read, context.Context) {
metas = append(metas, meta)
}
}
return
},
}).Render("publish2/versions", records)
context.Writer.Write([]byte(result))
}