summaryrefslogtreecommitdiff
path: root/management/reference/reference.go
blob: e6d7eddcaa41e3ef57d4d8a0eaab6552ec588023 (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
package reference

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/bosssauce/ponzu/management/editor"
	"github.com/bosssauce/ponzu/system/db"
)

// 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, display string) []byte {
	// decode all content type from db into options map
	// map["?type=<contentType>&id=<id>"]t.String()
	options := make(map[string]string)
	jj := db.ContentAll(contentType + "__sorted")

	data := make(map[string]interface{})
	for i := range jj {
		err := json.Unmarshal(jj[i], data)
		if err != nil {
			log.Println("Error decoding into reference handle:", contentType, err)
		}

		k := fmt.Sprintf("?type=%s&id=%d", contentType, data["id"].(int))
		v := data[display].(string)
		options[k] = v
	}

	return editor.Select(fieldName, p, attrs, options)
}