// 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() }
// Controller for wrapper element sort form. func SortWrapperForm(w *wrapper.Wrapper) { var parentid string if len(w.APIParams) > 0 { parentid = w.APIParams[0] } else { http.Error(w.Writer, "Forbidden", 403) w.Serve() return } e, err := elements.LoadWrapperElement(parentid, w) if err != nil { errmessage := fmt.Sprintf("Element not found to sort for %s by %s.", w.APIParams[1], w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("This element was not found", "Error", w) w.Serve() return } if len(e.Elements) > 0 { w.SetPayload("elements", e.Elements) } else { services.AddMessage("This has no elements assigned yet.", "Error", w) } w.SetTemplate("admin/element_sorter.html") w.Serve() return }
// Controller for adding existing element to wrapper element form submission. func AddExistingWrapperSubmit(w *wrapper.Wrapper) { parentid := w.APIParams[0] var post map[string]string err := form.GetValidFormData(w, &post) if err != nil { 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, post["element"]) 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 an existing element.", "Success", w) w.Serve() }
// Controller for wrapper sort submission. func SortWrapperSubmit(w *wrapper.Wrapper) { var parentid string if len(w.APIParams) > 0 { parentid = w.APIParams[0] } else { http.Error(w.Writer, "Forbidden", 403) w.Serve() return } wes := elements.NewWrapperElements() err := json.NewDecoder(w.Request.Body).Decode(&wes) if err != nil { errmessage := fmt.Sprintf("Unable to marshall elements %s by %s: %s", parentid, w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to save elements.", "Error", w) w.Serve() return } we, err := elements.LoadWrapperElement(parentid, w) if err != nil { errmessage := fmt.Sprintf("Element not found to sort for %s by %s.", parentid, w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("This element was not found", "Error", w) w.Serve() return } we.WrapperElements = wes we.Save(w) if err != nil { errmessage := fmt.Sprintf("Unable to save wrapper element %s by %s : %s", parentid, 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 elements have been updated.", "Success", w) 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() }