blob: 35b7d3d0f6570d047550ed7e07dc1ebf6779bada (
plain)
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
|
// Package reference is a Ponzu addon to enable content editors to create
// references to other content types which are stored as query strings within
// the referencer's content DB
package reference
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"log"
"github.com/ponzu-cms/ponzu/management/editor"
"github.com/ponzu-cms/ponzu/system/addon"
)
// Select returns the []byte of a <select> HTML element plus internal <options> with a label.
// IMPORTANT:
// The `fieldName` argument will cause a panic if it is not exactly the string
// form of the struct field that this editor input is representing
func Select(fieldName string, p interface{}, attrs map[string]string, contentType, tmplString string) []byte {
// decode all content type from db into options map
// map["?type=<contentType>&id=<id>"]t.String()
options := make(map[string]string)
var all map[string]interface{}
j := addon.ContentAll(contentType)
err := json.Unmarshal(j, &all)
if err != nil {
return nil
}
// make template for option html display
tmpl := template.Must(template.New(contentType).Parse(tmplString))
// make data something usable to iterate over and assign options
data := all["data"].([]interface{})
for i := range data {
item := data[i].(map[string]interface{})
k := fmt.Sprintf("?type=%s&id=%.0f", contentType, item["id"].(float64))
v := &bytes.Buffer{}
err := tmpl.Execute(v, item)
if err != nil {
log.Println("Error executing template for reference of:", contentType)
return nil
}
options[k] = v.String()
}
return editor.Select(fieldName, p, attrs, options)
}
|