Skip to content

Commit

Permalink
Merge pull request #171 from ponzu-cms/ponzu-dev
Browse files Browse the repository at this point in the history
[core] Fixes for multi-slice content type field conflicts
  • Loading branch information
nilslice authored Jun 26, 2017
2 parents 5af38b7 + cd6be5e commit 9105d78
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
5 changes: 3 additions & 2 deletions system/admin/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1851,15 +1851,16 @@ 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, ".")

// 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]
Expand Down
5 changes: 3 additions & 2 deletions system/api/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,16 @@ 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, ".")

// 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]
Expand Down
5 changes: 3 additions & 2 deletions system/api/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,16 @@ 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, ".")

// 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]
Expand Down
5 changes: 3 additions & 2 deletions system/db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ 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, ".")

// 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]
Expand Down
47 changes: 47 additions & 0 deletions system/db/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9105d78

Please sign in to comment.