2023-12-24 10:18:08 +00:00
|
|
|
// This file is part of the happyDomain (R) project.
|
|
|
|
// Copyright (c) 2020-2024 happyDomain
|
|
|
|
// Authors: Pierre-Olivier Mercier, et al.
|
2020-10-05 08:20:15 +00:00
|
|
|
//
|
2023-12-24 10:18:08 +00:00
|
|
|
// This program is offered under a commercial and under the AGPL license.
|
|
|
|
// For commercial licensing, contact us at <contact@happydomain.org>.
|
2020-10-05 08:20:15 +00:00
|
|
|
//
|
2023-12-24 10:18:08 +00:00
|
|
|
// For AGPL licensing:
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2020-10-05 08:20:15 +00:00
|
|
|
//
|
2023-12-24 10:18:08 +00:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
2020-10-05 08:20:15 +00:00
|
|
|
//
|
2023-12-24 10:18:08 +00:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-10-05 08:20:15 +00:00
|
|
|
|
2023-09-07 10:04:55 +00:00
|
|
|
package forms // import "git.happydns.org/happyDomain/forms"
|
2020-10-05 08:20:15 +00:00
|
|
|
|
2020-10-10 16:46:28 +00:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
)
|
2020-10-05 08:20:15 +00:00
|
|
|
|
|
|
|
// Field
|
|
|
|
type Field struct {
|
|
|
|
// Id is the field identifier.
|
|
|
|
Id string `json:"id"`
|
|
|
|
|
|
|
|
// Type is the string representation of the field's type.
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
|
|
|
// Label is the title given to the field, displayed as <label> tag on the interface.
|
|
|
|
Label string `json:"label,omitempty"`
|
|
|
|
|
|
|
|
// Placeholder is the placeholder attribute of the corresponding <input> tag.
|
|
|
|
Placeholder string `json:"placeholder,omitempty"`
|
|
|
|
|
|
|
|
// Default is the preselected value or the default value in case the field is not filled by the user.
|
2024-01-17 15:38:10 +00:00
|
|
|
Default interface{} `json:"default,omitempty"`
|
2020-10-05 08:20:15 +00:00
|
|
|
|
|
|
|
// Choices holds the differents choices shown to the user in <select> tag.
|
|
|
|
Choices []string `json:"choices,omitempty"`
|
|
|
|
|
|
|
|
// Required indicates whether the field has to be filled or not.
|
|
|
|
Required bool `json:"required,omitempty"`
|
|
|
|
|
|
|
|
// Secret indicates if the field contains sensitive information such as API key, in order to hide
|
|
|
|
// the field when not needed. When typing, it doesn't hide characters like in password input.
|
|
|
|
Secret bool `json:"secret,omitempty"`
|
|
|
|
|
2023-12-06 02:25:28 +00:00
|
|
|
// Hide indicates if the field should be hidden to the user.
|
|
|
|
Hide bool `json:"hide,omitempty"`
|
|
|
|
|
2024-02-06 11:35:47 +00:00
|
|
|
// Textarea indicates that a large field is expected.
|
|
|
|
Textarea bool `json:"textarea,omitempty"`
|
|
|
|
|
2020-10-05 08:20:15 +00:00
|
|
|
// Description stores an helpfull sentence describing the field.
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
}
|
2020-10-10 16:46:28 +00:00
|
|
|
|
2022-01-10 13:06:19 +00:00
|
|
|
// GenField generates a generic Field based on the happydomain tag.
|
2020-10-10 16:46:28 +00:00
|
|
|
func GenField(field reflect.StructField) (f *Field) {
|
|
|
|
jsonTag := field.Tag.Get("json")
|
|
|
|
jsonTuples := strings.Split(jsonTag, ",")
|
|
|
|
|
|
|
|
f = &Field{
|
|
|
|
Type: field.Type.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(jsonTuples) > 0 && len(jsonTuples[0]) > 0 {
|
|
|
|
f.Id = jsonTuples[0]
|
|
|
|
} else {
|
|
|
|
f.Id = field.Name
|
|
|
|
}
|
|
|
|
|
2022-01-10 13:06:19 +00:00
|
|
|
tag := field.Tag.Get("happydomain")
|
2020-10-10 16:46:28 +00:00
|
|
|
tuples := strings.Split(tag, ",")
|
|
|
|
|
|
|
|
for _, t := range tuples {
|
|
|
|
kv := strings.SplitN(t, "=", 2)
|
|
|
|
if len(kv) > 1 {
|
|
|
|
switch strings.ToLower(kv[0]) {
|
|
|
|
case "label":
|
|
|
|
f.Label = kv[1]
|
|
|
|
case "placeholder":
|
|
|
|
f.Placeholder = kv[1]
|
|
|
|
case "default":
|
|
|
|
f.Default = kv[1]
|
|
|
|
case "description":
|
|
|
|
f.Description = kv[1]
|
|
|
|
case "choices":
|
|
|
|
f.Choices = strings.Split(kv[1], ";")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch strings.ToLower(kv[0]) {
|
2023-12-06 02:25:28 +00:00
|
|
|
case "hidden":
|
|
|
|
f.Hide = true
|
2020-10-10 16:46:28 +00:00
|
|
|
case "required":
|
|
|
|
f.Required = true
|
|
|
|
case "secret":
|
|
|
|
f.Secret = true
|
2024-02-06 11:35:47 +00:00
|
|
|
case "textarea":
|
|
|
|
f.Textarea = true
|
2020-10-10 16:46:28 +00:00
|
|
|
default:
|
|
|
|
f.Label = kv[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenStructFields generates corresponding SourceFields of the given Source.
|
|
|
|
func GenStructFields(data interface{}) (fields []*Field) {
|
|
|
|
if data != nil {
|
|
|
|
dataMeta := reflect.Indirect(reflect.ValueOf(data)).Type()
|
|
|
|
|
|
|
|
for i := 0; i < dataMeta.NumField(); i += 1 {
|
|
|
|
fields = append(fields, GenField(dataMeta.Field(i)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenDefaultSettingsForm generates a generic CustomForm presenting all the fields in one page.
|
|
|
|
func GenDefaultSettingsForm(data interface{}) *CustomForm {
|
|
|
|
return &CustomForm{
|
|
|
|
Fields: GenStructFields(data),
|
2023-01-18 17:35:25 +00:00
|
|
|
NextButtonText: "common.create-thing",
|
|
|
|
NextEditButtonText: "common.update",
|
2020-10-10 16:46:28 +00:00
|
|
|
NextButtonState: 1,
|
2023-01-18 17:35:25 +00:00
|
|
|
PreviousButtonText: "provider.another",
|
|
|
|
PreviousEditButtonText: "common.cancel",
|
2020-10-10 16:46:28 +00:00
|
|
|
PreviousButtonState: -1,
|
|
|
|
}
|
|
|
|
}
|