diff options
author | Steve <nilslice@gmail.com> | 2016-12-19 11:19:53 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-19 11:19:53 -0800 |
commit | 3791fadda7b761ffba38c567da29e2e71acd1dfb (patch) | |
tree | 79d810f9aafa1868ee0760983937470d0eea3db8 /addons | |
parent | b20c5bdee38682edc851e646d815a34689c3c923 (diff) |
[addons] Creating foundation for plugin-like system "Addons" (#24)
* adding addons dir and sample addon which enables the use of a new input element in forms for referencing other content. "addons" is a conceptual plugin-like feature, similar to wordpress "plugins" dir, but not as sophisticated
Diffstat (limited to 'addons')
-rw-r--r-- | addons/reference/reference.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/addons/reference/reference.go b/addons/reference/reference.go new file mode 100644 index 0000000..78e46eb --- /dev/null +++ b/addons/reference/reference.go @@ -0,0 +1,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/bosssauce/ponzu/management/editor" + "github.com/bosssauce/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) +} |