forked from qor/publish2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
52 lines (43 loc) · 1.38 KB
/
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
package publish2
import (
"fmt"
"net/http"
"github.com/jinzhu/gorm"
"github.com/qor/qor"
"github.com/qor/qor/utils"
)
func getPublishScheduleTime(context *qor.Context) string {
if values, ok := context.Request.URL.Query()["publish_scheduled_time"]; ok {
if len(values) > 0 && values[0] != "" {
return values[0]
}
} else if cookie, err := context.Request.Cookie("publish2_publish_scheduled_time"); err == nil {
return cookie.Value
}
return ""
}
func requestingPublishDraftContent(context *qor.Context) bool {
if values, ok := context.Request.URL.Query()["publish_draft_content"]; ok {
if len(values) > 0 && values[0] != "" {
return true
}
} else if cookie, err := context.Request.Cookie("pubilsh2_publish_draft_content"); err == nil && cookie.Value == "true" {
return true
}
return false
}
func PreviewByDB(tx *gorm.DB, context *qor.Context) *gorm.DB {
scheduledTime := getPublishScheduleTime(context)
draftContent := requestingPublishDraftContent(context)
utils.SetCookie(http.Cookie{Name: "publish2_publish_scheduled_time", Value: scheduledTime}, context)
utils.SetCookie(http.Cookie{Name: "pubilsh2_publish_draft_content", Value: fmt.Sprint(draftContent)}, context)
if scheduledTime != "" {
if t, err := utils.ParseTime(scheduledTime, context); err == nil {
tx = tx.Set(ScheduledTime, t)
}
}
if draftContent {
tx = tx.Set(VisibleMode, ModeOff)
}
return tx
}