/
alert_filter_form.go
81 lines (76 loc) · 2.05 KB
/
alert_filter_form.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
package forms
import (
"github.com/APTrust/registry/common"
"github.com/APTrust/registry/constants"
"github.com/APTrust/registry/pgmodels"
)
// AlertFilterForm is the form that displays filtering options for
// the alert list page.
type AlertFilterForm struct {
Form
FilterCollection *pgmodels.FilterCollection
actingUserIsAdmin bool
instOptions []*ListOption
userOptions []*ListOption
}
func NewAlertFilterForm(fc *pgmodels.FilterCollection, actingUser *pgmodels.User) (FilterForm, error) {
f := &AlertFilterForm{
Form: NewForm(nil, "alerts/_filters.html", "/alerts"),
FilterCollection: fc,
}
var err error
if actingUser.IsAdmin() {
// SysAdmin can view alerts for all institutions and users.
f.instOptions, err = ListInstitutions(false)
if err != nil {
return nil, err
}
f.userOptions, err = ListUsers(0)
if err != nil {
return nil, err
}
}
f.init()
f.SetValues()
return f, nil
}
func (f *AlertFilterForm) init() {
f.Fields["created_at__gteq"] = &Field{
Name: "created_at__gteq",
Label: "Created On or After",
Placeholder: "Created On or After",
}
f.Fields["created_at__lteq"] = &Field{
Name: "created_at__lteq",
Label: "Created On or Before",
Placeholder: "Created On or Before",
}
f.Fields["institution_id"] = &Field{
Name: "institution_id",
Label: "Institution",
Placeholder: "Institution",
Options: f.instOptions,
}
f.Fields["type"] = &Field{
Name: "type",
Label: "Alert Type",
Placeholder: "Alert Type",
Options: Options(constants.AlertTypes),
}
f.Fields["user_id"] = &Field{
Name: "user_id",
Label: "Recipient",
Placeholder: "Recipient",
Options: f.userOptions,
}
}
// setValues sets the form values to match the Institution values.
func (f *AlertFilterForm) SetValues() {
for _, fieldName := range pgmodels.AlertFilters {
if f.Fields[fieldName] == nil {
common.ConsoleDebug("No filter for %s", fieldName)
continue
}
f.Fields[fieldName].Value = f.FilterCollection.ValueOf(fieldName)
}
}