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 }
// The controller function for Values found directly in the controller values of the element func WrapperValues(w *wrapper.Wrapper) { var wrapid string if len(w.APIParams) > 0 { wrapid = w.APIParams[0] } else { http.Error(w.Writer, "Forbidden", 403) w.Serve() return } e, err := elements.LoadWrapperElement(wrapid, w) if err != nil { errmessage := fmt.Sprintf("Content not found %s : %s", wrapid, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("There was a problem loading some content on your page.", "Error", w) w.Serve() return } var v []elements.Element for _, id := range e.Elements { 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) } else { v = append(v, e) } } w.SetDynamicId(e.DynamicId) w.SetContent(v) w.Serve() }
func AddSlugChild(w *wrapper.Wrapper) { parentid := w.APIParams[0] e := elements.NewElement() e.Title = "New Element" e.Controller = "content" err := e.Save(w) if err != nil { errmessage := fmt.Sprintf("Unable to create new element by %s : %s", w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Could not create a new element.", "Error", w) w.Serve() return } var parent elements.SlugElement parent, err = elements.LoadSlugElement(parentid, w) if err != nil { errmessage := fmt.Sprintf("Unable to loap parent element by %s : %s", w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Could not load parent element.", "Error", w) w.Serve() return } if parent.Slugs == nil { slug := map[string]string{e.MongoId.Hex(): e.MongoId.Hex()} parent.Slugs = slug } else { parent.Slugs[e.MongoId.Hex()] = e.MongoId.Hex() } err = parent.Save(w) if err != nil { errmessage := fmt.Sprintf("Unable to save parent element by %s : %s", w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Could not save parent element.", "Error", w) w.Serve() return } dynamic := services.Dynamic{ Target: parentid, Controller: "admin/element", Template: "admin/element.html", Id: parentid, } services.SetDynamic(dynamic, w) services.AddMessage("You have added a new element.", "Success", w) w.Serve() }
// The controller function to retrieve elements ids from the path func PathValues(w *wrapper.Wrapper) { // Request is never url based so we can Never cache this request. // TODO: move no caching into a reusable function. w.Writer.Header().Add("Cache-Control", "no-cache, no-store, must-revalidate") w.Writer.Header().Add("Pragma", "no-cache") w.Writer.Header().Add("Expires", "0") p := paths.NewPath() u := w.Request.Header.Get("CurrentPath") if u == "" { http.Error(w.Writer, "Forbidden", 403) w.Serve() return } qp, err := p.PathMatch(u, "published", w) if err != nil { if err.Error() == "not found" { if "/"+w.SiteConfig.FourOFour != u { services.Redirect("/"+w.SiteConfig.FourOFour, w) w.Serve() return } else { services.AddMessage("There was a problem with the system.", "Error", w) w.Serve() return } } } var v []elements.Element for _, eid := range p.Elements { e := elements.NewElement() err = elements.GetById(eid, &e, w) if err != nil { errmessage := fmt.Sprintf("Content not found %s : %s", eid, err.Error()) w.SiteConfig.Logger.Error(errmessage) } else { v = append(v, e) } } w.SetPayload("mongolar_slug", qp) w.SetContent(v) w.SetTemplate(p.Template) w.Serve() return }
// Controller for adding children to wrapper element. func AddWrapperChild(w *wrapper.Wrapper) { parentid := w.APIParams[0] e := elements.NewElement() e.Title = "New Element" err := e.Save(w) if err != nil { errmessage := fmt.Sprintf("Unable to create new element by %s : %s", w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Could not create a new element.", "Error", w) w.Serve() return } var parent elements.WrapperElement parent, err = elements.LoadWrapperElement(parentid, w) if err != nil { errmessage := fmt.Sprintf("Unable to loap parent element by %s : %s", w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Could not load parent element.", "Error", w) w.Serve() return } parent.Elements = append(parent.Elements, e.MongoId.Hex()) err = parent.Save(w) if err != nil { errmessage := fmt.Sprintf("Unable to loap parent element by %s : %s", w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Could not load parent element.", "Error", w) w.Serve() return } dynamic := services.Dynamic{ Target: parentid, Controller: "admin/element", Template: "admin/element.html", Id: parentid, } services.SetDynamic(dynamic, w) services.AddMessage("You have added a new element.", "Success", w) w.Serve() return }
// Controller to render an element in content editor func Element(w *wrapper.Wrapper) { e := elements.NewElement() var id string if len(w.APIParams) == 0 { http.Error(w.Writer, "Forbidden", 403) return } else { id = w.APIParams[0] } err := elements.GetById(id, &e, w) if err != nil { errmessage := fmt.Sprintf("Element not found to edit for %s by %s.", id, w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("This element was not found", "Error", w) } else { w.SetPayload("mongolarid", e.MongoId.Hex()) w.SetPayload("title", e.Title) w.SetPayload("mongolartype", e.Controller) w.SetDynamicId(e.MongoId.Hex()) if e.Controller == "wrapper" { we, err := elements.LoadWrapperElement(id, w) if err != nil { errmessage := fmt.Sprintf("Element not found to edit for %s by %s.", id, w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("This element was not found", "Error", w) } w.SetPayload("elements", we.Elements) } if e.Controller == "slug" { we, err := elements.LoadSlugElement(id, w) if err != nil { errmessage := fmt.Sprintf("Element not found to edit for %s by %s.", id, w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("This element was not found", "Error", w) } w.SetPayload("elements", we.Slugs) } } w.Serve() }
// 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 }