-
Notifications
You must be signed in to change notification settings - Fork 189
/
filter.go
119 lines (102 loc) · 2.78 KB
/
filter.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
package admin
import (
"strings"
"github.com/jinzhu/gorm"
"github.com/qor/qor"
"github.com/qor/qor/resource"
"github.com/qor/qor/utils"
)
// Filter filter definiation
type Filter struct {
Name string
Label string
Type string
Operations []string // eq, cont, gt, gteq, lt, lteq
Resource *Resource
Visible func(context *Context) bool
Handler func(*gorm.DB, *FilterArgument) *gorm.DB
Config FilterConfigInterface
}
// SavedFilter saved filter settings
type SavedFilter struct {
Name string
URL string
}
// FilterConfigInterface filter config interface
type FilterConfigInterface interface {
ConfigureQORAdminFilter(*Filter)
}
// FilterArgument filter argument that used in handler
type FilterArgument struct {
Value *resource.MetaValues
Resource *Resource
Context *qor.Context
}
// Filter register filter for qor resource
func (res *Resource) Filter(filter *Filter) {
filter.Resource = res
if filter.Label == "" {
filter.Label = utils.HumanizeString(filter.Name)
}
if meta := res.GetMeta(filter.Name); meta != nil {
if filter.Type == "" {
filter.Type = meta.Type
}
if filter.Config == nil {
if filterConfig, ok := meta.Config.(FilterConfigInterface); ok {
filter.Config = filterConfig
}
}
}
if filter.Config != nil {
filter.Config.ConfigureQORAdminFilter(filter)
}
if filter.Handler == nil {
// generate default handler
filter.Handler = func(db *gorm.DB, filterArgument *FilterArgument) *gorm.DB {
if metaValue := filterArgument.Value.Get("Value"); metaValue != nil {
keyword := utils.ToString(metaValue.Value)
if _, ok := filter.Config.(*SelectManyConfig); ok {
if arr, ok := metaValue.Value.([]string); ok {
keyword = strings.Join(arr, ",")
}
}
if keyword != "" {
field := filterField{FieldName: filter.Name}
if operationMeta := filterArgument.Value.Get("Operation"); operationMeta != nil {
if operation := utils.ToString(operationMeta.Value); operation != "" {
field.Operation = operation
}
}
if field.Operation == "" {
if len(filter.Operations) > 0 {
field.Operation = filter.Operations[0]
} else {
field.Operation = "contains"
}
}
return filterResourceByFields(res, []filterField{field}, keyword, db, filterArgument.Context)
}
}
return db
}
}
if filter.Type != "" {
res.filters = append(res.filters, filter)
} else {
utils.ExitWithMsg("Invalid filter definition %v for resource %v", filter.Name, res.Name)
}
}
// GetFilters get registered filters
func (res *Resource) GetFilters() []*Filter {
return res.filters
}
// GetFilter get defined action
func (res *Resource) GetFilter(name string) *Filter {
for _, action := range res.filters {
if action.Name == name {
return action
}
}
return nil
}