// Controller to present path editor form func PathEditorForm(w *wrapper.Wrapper) { pathid := w.APIParams[0] f := form.NewForm() f.AddText("title", "text").AddLabel("Title").Required() f.AddText("path", "text").AddLabel("Path").Required() f.AddText("template", "text").AddLabel("Template").Required() f.AddCheckBox("wildcard").AddLabel("Wildcard") ops := []map[string]string{ map[string]string{"name": "published", "value": "published"}, map[string]string{"name": "unpublished", "value": "unpublished"}, } f.AddRadio("status", ops).AddLabel("Status").Required() f.AddText("id", "text").Hidden() var p paths.Path var err error if pathid != "new" { p, err = paths.LoadPath(pathid, w) if err != nil { errmessage := fmt.Sprintf("Could not retrieve path %s by %s: %s", w.APIParams[0], w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Error retrieving path information.", "Error", w) w.Serve() return } } else { p = paths.NewPath() } f.FormData = p f.Register(w) w.SetTemplate("admin/form.html") w.SetPayload("form", f) w.Serve() return }
// Controller for content editing form. func ContentEditorForm(w *wrapper.Wrapper) { elementid := w.APIParams[0] e, err := elements.LoadContentElement(elementid, w) if err != nil { errmessage := fmt.Sprintf("Element not found to edit for %s by %s", elementid, w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("This element was not found", "Error", w) w.Serve() return } if e.ContentValues.Type == "" { errmessage := fmt.Sprintf("No content type set for %s", elementid, w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("This element doesn't have a content type set. Set a content type to edit values.", "Error", w) w.Serve() return } var ct contenttypes.ContentType ct, err = contenttypes.LoadContentTypeT(e.ContentValues.Type, w) if err != nil { errmessage := fmt.Sprintf("Unable to find content type %s : %s", e.ContentValues.Type, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to find content type.", "Error", w) w.Serve() return } f := form.NewForm() f.Fields = ct.Form f.FormData = e.ContentValues.Content f.Register(w) w.SetTemplate("admin/form.html") w.SetPayload("form", f) w.Serve() return }
func SlugUrlEditorForm(w *wrapper.Wrapper) { slugid := w.APIParams[0] e, err := elements.LoadSlugElement(slugid, w) if err != nil { errmessage := fmt.Sprintf("Element not found to edit for %s by %s", slugid, w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to load slug parent", "Error", w) w.Serve() return } f := form.NewForm() data := make(map[string]string) for slug, id := range e.Slugs { data[id] = slug e := elements.NewElement() err = elements.GetById(id, &e, w) if err != nil { errmessage := fmt.Sprintf("Content not found %s : %s", id, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("There was a problem loading some slug elements.", "Error", w) w.Serve() return } f.AddText(id, "text").AddLabel(e.Title).Required() } f.FormData = data f.Register(w) w.SetTemplate("admin/form.html") w.SetPayload("form", f) w.Serve() return }
func EditContentTypeForm(w *wrapper.Wrapper) { f := form.NewForm() ct := new(ContentType) if w.APIParams[0] != "new" { c := w.DbSession.DB("").C("content_types") i := bson.M{"_id": bson.ObjectIdHex(w.APIParams[0])} err := c.Find(i).One(ct) if err != nil { errmessage := fmt.Sprintf("Content Type not found %s : %s", w.APIParams[0], err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Your content types was not found ", "Error", w) w.Serve() return } var elements []map[string]interface{} for _, field := range ct.Form { element := make(map[string]interface{}) element["type"] = field.Type element["key"] = field.Key element["label"] = field.TemplateOptions.Label element["placeholder"] = field.TemplateOptions.Placeholder element["rows"] = field.TemplateOptions.Rows element["cols"] = field.TemplateOptions.Cols element["options"] = "" for _, opt := range field.TemplateOptions.Options { element["options"] = fmt.Sprintf("%s%s|%s\n", element["options"], opt["name"], opt["value"]) } elements = append(elements, element) } data := make(map[string]interface{}) data["elements"] = elements data["content_type"] = ct.Type f.FormData = data } else { data := make(map[string]interface{}) fd := make([]map[string]string, 0) data["elements"] = fd data["content_type"] = "" f.FormData = data } f.AddText("content_type", "text").AddLabel("Content Type Name") f.AddRepeatSection("elements", "Add another field", FieldFormGroup()) f.Register(w) w.SetPayload("form", f) w.SetTemplate("admin/form.html") w.Serve() return }
func FieldFormGroup() []*form.Field { ft := []map[string]string{ map[string]string{"name": "Text Field", "value": "input"}, map[string]string{"name": "TextArea Field", "value": "textarea"}, map[string]string{"name": "Radio Buttons", "value": "radio"}, map[string]string{"name": "Checkbox", "value": "checkbox"}, } f := form.NewForm() f.AddRadio("type", ft).AddLabel("Field Type").Required() f.AddText("key", "text").AddLabel("Key").Required() f.AddText("label", "text").AddLabel("Label") f.AddText("placeholder", "text").AddLabel("Placeholder") f.AddTextArea("options").AddLabel("Options") f.AddText("cols", "text").AddLabel("Columns") f.AddText("rows", "text").AddLabel("Rows") return f.Fields }
// Controller to ddisplay form to change content type for conteent element func ContentTypeEditorForm(w *wrapper.Wrapper) { elementid := w.APIParams[0] e, err := elements.LoadContentElement(elementid, w) if err != nil { errmessage := fmt.Sprintf("Element not found to edit for %s by %s", elementid, w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("This element was not found", "Error", w) w.Serve() return } cts := make([]contenttypes.ContentType, 0) cts, err = contenttypes.AllContentTypes(w) if err != nil { errmessage := fmt.Sprintf("Unable to query all Content Types: %s", err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to retrieve content types.", "Error", w) w.Serve() return } opts := make([]map[string]string, 0) for _, ct := range cts { opt := map[string]string{ "name": ct.Type, "value": ct.Type, } opts = append(opts, opt) } f := form.NewForm() f.AddSelect("type", opts) data := map[string]string{"type": e.ContentValues.Type} f.FormData = data f.Register(w) w.SetTemplate("admin/form.html") w.SetPayload("form", f) w.Serve() return }
// Controller for adding existing element form. func AddExistingChildForm(w *wrapper.Wrapper) { elems, err := elements.ElementList(w) if err != nil { errmessage := fmt.Sprintf("Unable to retrieve a list of all elements: %s", err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("There was a problem retrieving the element list.", "Error", w) w.Serve() return } options := make([]map[string]string, 0) for _, element := range elems { option := map[string]string{"name": element.Title, "value": element.MongoId.Hex(), "group": element.Controller} options = append(options, option) } f := form.NewForm() f.AddSelect("element", options).AddLabel("Element").Required() f.Register(w) w.SetTemplate("admin/form.html") w.SetPayload("form", f) w.Serve() return }
// Controller to edit an elements properties // TODO: break this into smaller functions to make it easier to maintain func ElementEditor(w *wrapper.Wrapper) { var elementid string if len(w.APIParams) > 0 { elementid = w.APIParams[0] } else { http.Error(w.Writer, "Forbidden", 403) } if w.Request.Method != "POST" { f := form.NewForm() f.AddText("title", "text").AddLabel("Title") op := make([]map[string]string, 0) for _, ec := range w.SiteConfig.ElementControllers { uc := []rune(ec) uc[0] = unicode.ToUpper(uc[0]) name := string(uc) op = append(op, map[string]string{"name": name, "value": ec}) } f.AddSelect("controller", op).AddLabel("Controller") f.AddText("template", "text").AddLabel("Template") f.AddText("dyn", "text").AddLabel("Dynamic Id") f.AddText("classes", "text").AddLabel("Classes") f.AddText("id", "text").Hidden() if elementid != "new" { e := elements.NewElement() err := elements.GetById(elementid, &e, w) if err != nil { errmessage := fmt.Sprintf("Element not found to edit for %s by %s", elementid, w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("This element was not found", "Error", w) w.Serve() return } // Have to do this for namespacing stuff on the AngularJs side. data := map[string]string{ "controller": e.Controller, "template": e.Template, "dyn": e.DynamicId, "classes": e.Classes, "id": e.MongoId.Hex(), "title": e.Title, } f.FormData = data } f.Register(w) w.SetTemplate("admin/form.html") w.SetPayload("form", f) } else { post := make(map[string]string) err := form.GetValidFormData(w, &post) if err != nil { return } else { c := w.DbSession.DB("").C("elements") if post["mongolarid"] == "new" { p := elements.Element{ Controller: post["controller"], DynamicId: post["dyn"], Template: post["template"], Title: post["title"], Classes: post["classes"], } err := c.Insert(p) if err != nil { errmessage := fmt.Sprintf("Unable to save new element by %s : %s", w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("There was a problem saving your element.", "Error", w) } else { services.AddMessage("Your element was saved.", "Success", w) } } else { p := bson.M{ "$set": bson.M{ "template": post["template"], "title": post["title"], "dynamic_id": post["dyn"], "controller": post["controller"], "classes": post["classes"], }, } s := bson.M{"_id": bson.ObjectIdHex(post["mongolarid"])} err := c.Update(s, p) if err != nil { errmessage := fmt.Sprintf("Unable to save element %s by %s : %s", post["mongolarid"], w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("There was a problem saving your element.", "Error", w) } else { services.AddMessage("Your element was saved.", "Success", w) dynamic := services.Dynamic{ Target: post["mongolarid"], Id: post["mongolarid"], Controller: "admin/element", Template: "admin/element.html", } services.SetDynamic(dynamic, w) } } } } w.Serve() return }
func EditContentTypeSubmit(w *wrapper.Wrapper) { post := make(map[string]interface{}) err := form.GetValidFormData(w, &post) if err != nil { return } elements := reflect.ValueOf(post["elements"]) f := form.NewForm() for i := 0; i < elements.Len(); i++ { var field *form.Field element := elements.Index(i).Interface().(map[string]interface{}) switch element["type"].(string) { case "input": field = f.AddText(element["key"].(string), "text") case "textarea": field = f.AddTextArea(element["key"].(string)) case "radio": values := strings.Split(element["options"].(string), "\n") opt := make([]map[string]string, 0) for _, value := range values { namval := strings.Split(value, "|") if len(namval) < 2 { errmessage := fmt.Sprintf("Attempt to set incorrect form option %s by %s", value, w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Your options must be of the format Name|Value", "Error", w) w.Serve() return } newval := map[string]string{ "name": namval[0], "value": namval[1], } opt = append(opt, newval) } field = f.AddRadio(element["key"].(string), opt) case "checkbox": field = f.AddCheckBox(element["key"].(string)) default: //TODO messaging and logging return } if _, ok := element["label"]; ok { if element["label"].(string) != "" { field.AddLabel(element["label"].(string)) } } if _, ok := element["placeholder"]; ok { if element["placeholder"].(string) != "" { field.AddPlaceHolder(element["placeholder"].(string)) } } if _, ok := element["rows"]; ok { if _, ok := element["cols"]; ok { if element["rows"].(float64) != 0 && element["cols"] != 0 { field.AddRowsCols(int(element["rows"].(float64)), int(element["cols"].(float64))) } } } } var id bson.ObjectId if post["mongolarid"].(string) == "new" { id = bson.NewObjectId() } else { id = bson.ObjectIdHex(post["mongolarid"].(string)) } ct := ContentType{ Form: f.Fields, Type: post["content_type"].(string), MongoId: id, } s := bson.M{"_id": id} c := w.DbSession.DB("").C("content_types") _, err = c.Upsert(s, ct) if err != nil { errmessage := fmt.Sprintf("Cannnot save content type %s : %s", post["mongolarid"].(string), err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to save content type.", "Error", w) w.Serve() return } services.AddMessage("Content type saved.", "Success", w) dynamic := services.Dynamic{ Target: "contenttypelist", Controller: "admin/all_content_types", Template: "admin/content_type_list.html", } services.SetDynamic(dynamic, w) w.Serve() return }