-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcontroller.go
150 lines (125 loc) · 4.83 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
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
package publish
import (
"fmt"
"net/http"
"strings"
"github.com/qor/admin"
"github.com/qor/qor"
"github.com/qor/qor/resource"
"github.com/qor/qor/utils"
"github.com/qor/roles"
"github.com/qor/worker"
)
const (
// PublishPermission publish permission
PublishPermission roles.PermissionMode = "publish"
)
type publishController struct {
*Publish
}
type visiblePublishResourceInterface interface {
VisiblePublishResource(*qor.Context) bool
}
func (pc *publishController) Preview(context *admin.Context) {
type resource struct {
*admin.Resource
Value interface{}
}
var drafts = []resource{}
draftDB := context.GetDB().Set(publishDraftMode, true).Unscoped()
for _, res := range context.Admin.GetResources() {
if visibleInterface, ok := res.Value.(visiblePublishResourceInterface); ok {
if !visibleInterface.VisiblePublishResource(context.Context) {
continue
}
} else if res.Config.Invisible {
continue
}
if res.HasPermission(PublishPermission, context.Context) {
results := res.NewSlice()
if IsPublishableModel(res.Value) || IsPublishEvent(res.Value) {
if pc.SearchHandler(draftDB.Where("publish_status = ?", DIRTY), context.Context).Find(results).RowsAffected > 0 {
drafts = append(drafts, resource{
Resource: res,
Value: results,
})
}
}
}
}
context.Execute("publish_drafts", drafts)
}
func (pc *publishController) Diff(context *admin.Context) {
var (
resourceID = context.Request.URL.Query().Get(":publish_unique_key")
params = strings.Split(resourceID, "__") // name__primary_keys
res = context.Admin.GetResource(params[0])
)
draft := res.NewStruct()
pc.search(context.GetDB().Set(publishDraftMode, true), res, [][]string{params[1:]}).First(draft)
production := res.NewStruct()
pc.search(context.GetDB().Set(publishDraftMode, false), res, [][]string{params[1:]}).First(production)
results := map[string]interface{}{"Production": production, "Draft": draft, "Resource": res}
fmt.Fprintf(context.Writer, string(context.Render("publish_diff", results)))
}
func (pc *publishController) PublishOrDiscard(context *admin.Context) {
var request = context.Request
var ids = request.Form["checked_ids[]"]
if scheduler := pc.Publish.WorkerScheduler; scheduler != nil {
jobResource := scheduler.JobResource
result := jobResource.NewStruct().(worker.QorJobInterface)
if request.Form.Get("publish_type") == "discard" {
result.SetJob(scheduler.GetRegisteredJob("Discard"))
} else {
result.SetJob(scheduler.GetRegisteredJob("Publish"))
}
workerArgument := &QorWorkerArgument{IDs: ids}
if t, err := utils.ParseTime(request.Form.Get("scheduled_time"), context.Context); err == nil {
workerArgument.ScheduleTime = &t
}
result.SetSerializableArgumentValue(workerArgument)
jobResource.CallSave(result, context.Context)
scheduler.AddJob(result)
http.Redirect(context.Writer, context.Request, context.URLFor(jobResource), http.StatusFound)
} else {
records := pc.searchWithPublishIDs(context.GetDB().Set(publishDraftMode, true), context.Admin, ids)
if request.Form.Get("publish_type") == "publish" {
pc.Publish.Publish(records...)
} else if request.Form.Get("publish_type") == "discard" {
pc.Publish.Discard(records...)
}
http.Redirect(context.Writer, context.Request, context.Request.RequestURI, http.StatusFound)
}
}
// ConfigureQorResourceBeforeInitialize configure qor resource when initialize qor admin
func (publish *Publish) ConfigureQorResourceBeforeInitialize(res resource.Resourcer) {
if res, ok := res.(*admin.Resource); ok {
res.GetAdmin().RegisterViewPath("github.com/qor/publish/views")
res.UseTheme("publish")
if event := res.GetAdmin().GetResource("PublishEvent"); event == nil {
eventResource := res.GetAdmin().AddResource(&PublishEvent{}, &admin.Config{Invisible: true})
eventResource.IndexAttrs("Name", "Description", "CreatedAt")
}
}
}
// ConfigureQorResource configure qor resource for qor admin
func (publish *Publish) ConfigureQorResource(res resource.Resourcer) {
if res, ok := res.(*admin.Resource); ok {
controller := publishController{publish}
router := res.GetAdmin().GetRouter()
router.Get(fmt.Sprintf("/%v/diff/:publish_unique_key", res.ToParam()), controller.Diff)
router.Get(res.ToParam(), controller.Preview)
router.Post(res.ToParam(), controller.PublishOrDiscard)
res.GetAdmin().RegisterFuncMap("publish_unique_key", func(res *admin.Resource, record interface{}, context *admin.Context) string {
var publishKeys = []string{res.ToParam()}
var scope = publish.DB.NewScope(record)
for _, primaryField := range scope.PrimaryFields() {
publishKeys = append(publishKeys, fmt.Sprint(primaryField.Field.Interface()))
}
return strings.Join(publishKeys, "__")
})
res.GetAdmin().RegisterFuncMap("is_publish_event_resource", func(res *admin.Resource) bool {
return IsPublishEvent(res.Value)
})
}
}