From efbc295ba2c4e5e30f358ac28afccaf243bc6298 Mon Sep 17 00:00:00 2001 From: Tornike Razmadze Date: Mon, 26 Jun 2017 22:02:08 +0400 Subject: [PATCH 1/2] fixed problem with saving multiple repeaters closes #59 closes #167 --- system/admin/handlers.go | 5 +++-- system/api/create.go | 5 +++-- system/api/update.go | 5 +++-- system/db/config.go | 5 +++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/system/admin/handlers.go b/system/admin/handlers.go index 3933d3d4..0700003f 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -1851,7 +1851,6 @@ func editHandler(res http.ResponseWriter, req *http.Request) { // and correctly format for db storage. Essentially, we need // fieldX.0: value1, fieldX.1: value2 => fieldX: []string{value1, value2} fieldOrderValue := make(map[string]map[string][]string) - ordVal := make(map[string][]string) for k, v := range req.PostForm { if strings.Contains(k, ".") { fo := strings.Split(k, ".") @@ -1859,7 +1858,9 @@ func editHandler(res http.ResponseWriter, req *http.Request) { // put the order and the field value into map field := string(fo[0]) order := string(fo[1]) - fieldOrderValue[field] = ordVal + if len(fieldOrderValue[field]) == 0 { + fieldOrderValue[field] = make(map[string][]string) + } // orderValue is 0:[?type=Thing&id=1] orderValue := fieldOrderValue[field] diff --git a/system/api/create.go b/system/api/create.go index 3b748cce..8af415b4 100644 --- a/system/api/create.go +++ b/system/api/create.go @@ -83,7 +83,6 @@ func createContentHandler(res http.ResponseWriter, req *http.Request) { // and correctly format for db storage. Essentially, we need // fieldX.0: value1, fieldX.1: value2 => fieldX: []string{value1, value2} fieldOrderValue := make(map[string]map[string][]string) - ordVal := make(map[string][]string) for k, v := range req.PostForm { if strings.Contains(k, ".") { fo := strings.Split(k, ".") @@ -91,7 +90,9 @@ func createContentHandler(res http.ResponseWriter, req *http.Request) { // put the order and the field value into map field := string(fo[0]) order := string(fo[1]) - fieldOrderValue[field] = ordVal + if len(fieldOrderValue[field]) == 0 { + fieldOrderValue[field] = make(map[string][]string) + } // orderValue is 0:[?type=Thing&id=1] orderValue := fieldOrderValue[field] diff --git a/system/api/update.go b/system/api/update.go index 36ffaeb2..31f29c11 100644 --- a/system/api/update.go +++ b/system/api/update.go @@ -84,7 +84,6 @@ func updateContentHandler(res http.ResponseWriter, req *http.Request) { // and correctly format for db storage. Essentially, we need // fieldX.0: value1, fieldX.1: value2 => fieldX: []string{value1, value2} fieldOrderValue := make(map[string]map[string][]string) - ordVal := make(map[string][]string) for k, v := range req.PostForm { if strings.Contains(k, ".") { fo := strings.Split(k, ".") @@ -92,7 +91,9 @@ func updateContentHandler(res http.ResponseWriter, req *http.Request) { // put the order and the field value into map field := string(fo[0]) order := string(fo[1]) - fieldOrderValue[field] = ordVal + if len(fieldOrderValue[field]) == 0 { + fieldOrderValue[field] = make(map[string][]string) + } // orderValue is 0:[?type=Thing&id=1] orderValue := fieldOrderValue[field] diff --git a/system/db/config.go b/system/db/config.go index 5bb010aa..c67fbe3e 100644 --- a/system/db/config.go +++ b/system/db/config.go @@ -36,7 +36,6 @@ func SetConfig(data url.Values) error { // and correctly format for db storage. Essentially, we need // fieldX.0: value1, fieldX.1: value2 => fieldX: []string{value1, value2} fieldOrderValue := make(map[string]map[string][]string) - ordVal := make(map[string][]string) for k, v := range data { if strings.Contains(k, ".") { fo := strings.Split(k, ".") @@ -44,7 +43,9 @@ func SetConfig(data url.Values) error { // put the order and the field value into map field := string(fo[0]) order := string(fo[1]) - fieldOrderValue[field] = ordVal + if len(fieldOrderValue[field]) == 0 { + fieldOrderValue[field] = make(map[string][]string) + } // orderValue is 0:[?type=Thing&id=1] orderValue := fieldOrderValue[field] From abd0e593338e1a3b128a0b49a2da650a67e54230 Mon Sep 17 00:00:00 2001 From: Tornike Razmadze Date: Tue, 27 Jun 2017 00:18:55 +0400 Subject: [PATCH 2/2] fixed approve handler losing data for string slices --- system/db/content.go | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/system/db/content.go b/system/db/content.go index 940c57ba..e73d8032 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -711,6 +711,53 @@ func postToJSON(ns string, data url.Values) ([]byte, error) { } post := t() + // check for any multi-value fields (ex. checkbox fields) + // and correctly format for db storage. Essentially, we need + // fieldX.0: value1, fieldX.1: value2 => fieldX: []string{value1, value2} + fieldOrderValue := make(map[string]map[string][]string) + for k, v := range data { + if strings.Contains(k, ".") { + fo := strings.Split(k, ".") + + // put the order and the field value into map + field := string(fo[0]) + order := string(fo[1]) + if len(fieldOrderValue[field]) == 0 { + fieldOrderValue[field] = make(map[string][]string) + } + + // orderValue is 0:[?type=Thing&id=1] + orderValue := fieldOrderValue[field] + orderValue[order] = v + fieldOrderValue[field] = orderValue + + // discard the post form value with name.N + data.Del(k) + } + } + + // add/set the key & value to the post form in order + for f, ov := range fieldOrderValue { + for i := 0; i < len(ov); i++ { + position := fmt.Sprintf("%d", i) + fieldValue := ov[position] + + if data.Get(f) == "" { + for i, fv := range fieldValue { + if i == 0 { + data.Set(f, fv) + } else { + data.Add(f, fv) + } + } + } else { + for _, fv := range fieldValue { + data.Add(f, fv) + } + } + } + } + dec := schema.NewDecoder() dec.SetAliasTag("json") // allows simpler struct tagging when creating a content type dec.IgnoreUnknownKeys(true) // will skip over form values submitted, but not in struct