/
publish.go
66 lines (55 loc) · 1.92 KB
/
publish.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
package publish
import (
"fmt"
"net/http"
"github.com/jinzhu/gorm"
"github.com/kisrobot/admin"
"github.com/kisrobot/l10n"
"github.com/kisrobot/publish"
"github.com/kisrobot/qor"
)
type availableLocalesInterface interface {
AvailableLocales() []string
}
type publishableLocalesInterface interface {
PublishableLocales() []string
}
type editableLocalesInterface interface {
EditableLocales() []string
}
func getPublishableLocales(req *http.Request, currentUser interface{}) []string {
if user, ok := currentUser.(publishableLocalesInterface); ok {
return user.PublishableLocales()
}
if user, ok := currentUser.(editableLocalesInterface); ok {
return user.EditableLocales()
}
if user, ok := currentUser.(availableLocalesInterface); ok {
return user.AvailableLocales()
}
return []string{l10n.Global}
}
// RegisterL10nForPublish register l10n language switcher for publish
func RegisterL10nForPublish(Publish *publish.Publish, Admin *admin.Admin) {
searchHandler := Publish.SearchHandler
Publish.SearchHandler = func(db *gorm.DB, context *qor.Context) *gorm.DB {
if context != nil {
if context.Request != nil && context.Request.URL.Query().Get("locale") == "" {
publishableLocales := getPublishableLocales(context.Request, context.CurrentUser)
return searchHandler(db, context).Set("l10n:mode", "unscoped").Scopes(func(db *gorm.DB) *gorm.DB {
scope := db.NewScope(db.Value)
if l10n.IsLocalizable(scope) {
return db.Where(fmt.Sprintf("%v.language_code IN (?)", scope.QuotedTableName()), publishableLocales)
}
return db
})
}
return searchHandler(db, context).Set("l10n:mode", "locale")
}
return searchHandler(db, context).Set("l10n:mode", "unscoped")
}
Admin.RegisterViewPath("github.com/kisrobot/l10n/publish/views")
Admin.RegisterFuncMap("publishable_locales", func(context admin.Context) []string {
return getPublishableLocales(context.Request, context.CurrentUser)
})
}