--- /dev/null
+package zakegg
+
+import (
+ "fmt"
+ "strings"
+ "strconv"
+ "regexp"
+ "reflect"
+ "time"
+ "net/http"
+
+ "github.com/go-playground/validator/v10"
+ ut "github.com/go-playground/universal-translator"
+
+ "github.com/squeeze69/codicefiscale"
+)
+
+type FormElement struct {
+ Type string
+ Name string
+ DBName string
+ Label string
+ Value string
+ Help string
+ Options map[string]string
+ OptionsSort []string
+ Rows string
+ Disabled bool
+ Validation string
+ Filter string
+}
+
+func (f FormElement) GetValueInt() int {
+ v, _ := strconv.Atoi(f.Value)
+ return v
+}
+
+func ValidatorCodFisc(fl validator.FieldLevel) bool {
+ ok, _ := codicefiscale.CodiceFiscale(fmt.Sprintf("%s", fl.Field()))
+
+ return ok
+}
+
+func ValidatorNumeric(fl validator.FieldLevel) bool {
+ regThou := regexp.MustCompile("^[-+]?\\d{1,3}(.\\d{3})*(\\,\\d{1,2})?$")
+ regNoThou := regexp.MustCompile("^[-+]?\\d+(\\,\\d{1,2})?$")
+
+ return regThou.MatchString(fmt.Sprintf("%s", fl.Field())) || regNoThou.MatchString(fmt.Sprintf("%s", fl.Field()))
+}
+
+func ValidatorDate(fl validator.FieldLevel) bool {
+ fmt.Printf("DUMP top %+v\n", fl.Top())
+ fmt.Printf("DUMP parent %+v\n", fl.Parent())
+ fmt.Printf("DUMP field %+v\n", fl.Field())
+ fmt.Printf("DUMP field_name %+v\n", fl.FieldName())
+ fmt.Printf("DUMP struct_field_name %+v\n", fl.StructFieldName())
+ fmt.Printf("DUMP param %+v\n", fl.Param())
+ fmt.Printf("DUMP tag %+v\n", fl.GetTag())
+
+ v, _ := time.Parse("02/01/2006", fmt.Sprintf("%s", fl.Field()))
+
+ params := fl.Param()
+ p := strings.Split(params, "-")
+
+ var s time.Time
+ var e time.Time
+
+ if p[0] == "now" {
+ s = time.Now()
+ } else {
+ s, _ = time.Parse("02/01/2006", p[0])
+ }
+ if p[1] == "now" {
+ e = time.Now()
+ } else {
+ e, _ = time.Parse("02/01/2006", p[1])
+ }
+
+ if v.After(s) && v.Before(e) {
+ return true
+ } else {
+ return false
+ }
+}
+
+func ValidatorFormErrorsTranslations(validate *validator.Validate, trans ut.Translator) {
+ validate.RegisterTranslation("required", trans, func(ut ut.Translator) error {
+ return ut.Add("required", "Campo obbligatorio.", true)
+ }, func(ut ut.Translator, fe validator.FieldError) string {
+ t, _ := ut.T("required", fe.Field())
+
+ return t
+ })
+ validate.RegisterTranslation("codfisc", trans, func(ut ut.Translator) error {
+ return ut.Add("codfisc", "Codice fiscale non valido.", true)
+ }, func(ut ut.Translator, fe validator.FieldError) string {
+ t, _ := ut.T("codfisc", fe.Field())
+
+ return t
+ })
+ validate.RegisterTranslation("numeric", trans, func(ut ut.Translator) error {
+ return ut.Add("numeric", "Il campo deve contenere un numero.", true)
+ }, func(ut ut.Translator, fe validator.FieldError) string {
+ t, _ := ut.T("numeric", fe.Field())
+
+ return t
+ })
+ validate.RegisterTranslation("numeric_it", trans, func(ut ut.Translator) error {
+ return ut.Add("numeric_it", "Il campo deve contenere un numero (con o senza separatori delle migliaia).", true)
+ }, func(ut ut.Translator, fe validator.FieldError) string {
+ t, _ := ut.T("numeric_it", fe.Field())
+
+ return t
+ })
+}
+
+func Struct2FormElements(s interface{}, f map[string]*FormElement) {
+ value := reflect.ValueOf(s)
+ if value.Kind() != reflect.Struct {
+ return
+ }
+
+ numberOfFields := value.NumField()
+ for i := 0; i < numberOfFields; i++ {
+ fmt.Printf("%d. Name: %s Type: %T || Value: %#v || Kind: %#v\n",
+ (i + 1), value.Type().Field(i).Name, value.Field(i), value.Field(i), value.Field(i).Kind())
+
+ if e, ok := f[value.Type().Field(i).Name]; ok {
+ e.Value = fmt.Sprintf("%v", value.Field(i))
+ }
+ }
+}
+
+func Map2FormElements(s map[string]interface{}, f map[string]*FormElement) {
+ for k, v := range s {
+ for _, vg := range f {
+ if vg.DBName == k {
+ refv := reflect.ValueOf(v)
+ if refv.Kind() == reflect.Bool {
+ if v.(bool) {
+ vg.Value = "1"
+ } else {
+ vg.Value = "0"
+ }
+ } else if refv.Kind() == reflect.Float32 || refv.Kind() == reflect.Float64 {
+ vg.Value = fmt.Sprintf("%v", v)
+ vg.Value = strings.ReplaceAll(vg.Value, ".", ",")
+ } else {
+ vg.Value = fmt.Sprintf("%v", v)
+ }
+ break;
+ }
+ }
+ }
+}
+
+func CreaMapValidator(elems map[string]*FormElement, r *http.Request) (map[string]interface{}, map[string]interface{}) {
+ valori := make(map[string]interface{})
+ regole := make(map[string]interface{})
+ for _, elem := range elems {
+ /*if elem.Name == "IDAssociazioni" {
+ i, _ := strconv.Atoi(r.FormValue(elem.Name))
+ valori[elem.Name] = i
+ } else {*/
+ if elem.Filter != "" {
+ splitted := strings.Split(elem.Filter, ",")
+ for _, f := range splitted {
+ switch f {
+ case "uppercase":
+ elem.Value = strings.ToUpper(r.FormValue(elem.Name))
+ valori[elem.Name] = elem.Value
+ }
+ }
+ } else {
+ valori[elem.Name] = r.FormValue(elem.Name)
+ elem.Value = r.FormValue(elem.Name)
+ }
+ regole[elem.Name] = elem.Validation
+ }
+
+ fmt.Println(valori)
+ fmt.Println(regole)
+
+ return valori, regole
+}
+
+func CreaMapDb (elems map[string]*FormElement) (map[string]interface{}) {
+ campi := make(map[string]interface{})
+ for _, elem := range elems {
+ if strings.Contains(elem.Validation, "numeric_it") {
+ /* tolgo la formattazione */
+ campi[elem.DBName] = strings.ReplaceAll(elem.Value, ".", "")
+ campi[elem.DBName] = strings.ReplaceAll(campi[elem.DBName].(string), ",", ".")
+ } else {
+ campi[elem.DBName] = elem.Value
+ }
+ }
+
+ return campi
+}