// Controller to delete an element and all references to the element func DeleteElement(w *wrapper.Wrapper) { id := w.APIParams[0] err := elements.Delete(id, w) if err != nil { errmessage := fmt.Sprintf("Unable to delete %s : %s", id, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to delete element.", "Error", w) } err = elements.WrapperDeleteAllChild(id, w) if err != nil { errmessage := fmt.Sprintf("Unable to delete reference to element %s : %s", id, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to delete all references to your element.", "Error", w) } err = elements.SlugDeleteAllChild(id, w) if err != nil { errmessage := fmt.Sprintf("Unable to delete reference to element %s : %s", id, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to delete all references to your element.", "Error", w) } err = paths.DeleteAllChild(id, w) if err != nil { errmessage := fmt.Sprintf("Unable to delete reference to %s : %s", id, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to delete all references to your element.", "Error", w) } dynamic := services.Dynamic{ Target: id, Template: "default.html", } services.SetDynamic(dynamic, w) services.AddMessage("Successfully deleted element", "Success", w) w.Serve() return }
func SlugUrlEditorSubmit(w *wrapper.Wrapper) { slugid := w.APIParams[0] post := make(map[string]string) err := form.GetValidFormData(w, &post) if err != nil { return } vals := make(map[string]string) for id, slug := range post { if bson.IsObjectIdHex(id) { vals[slug] = id } } var e elements.SlugElement 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 parent slug", "Error", w) w.Serve() return } e.Slugs = vals err = e.Save(w) if err != nil { errmessage := fmt.Sprintf("Slugs not saved %s by %s", w.APIParams[0], w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to save slug values.", "Error", w) w.Serve() return } services.AddMessage("Slug values updated.", "Success", w) 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 }
// Controller to handle content editor submission. func ContentEditorSubmit(w *wrapper.Wrapper) { elementid := w.APIParams[0] e, err := elements.LoadContentElement(elementid, w) post := make(map[string]interface{}) err = form.GetValidFormData(w, &post) if err != nil { return } e.ContentValues.Content = post delete(e.ContentValues.Content, "mongolartype") delete(e.ContentValues.Content, "mongolarid") delete(e.ContentValues.Content, "form_id") err = e.Save(w) if err != nil { errmessage := fmt.Sprintf("Element not saved %s by %s", w.APIParams[0], w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to save element.", "Error", w) w.Serve() return } services.AddMessage("Element content saved.", "Success", w) dynamic := services.Dynamic{ Target: elementid, Id: elementid, Controller: "admin/element", Template: "admin/element.html", } services.SetDynamic(dynamic, w) w.Serve() return }
// Controller to handle submission for content type change form. func ContentTypeEditorSubmit(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 } type Post struct { Type string `json:"type"` } var post Post err = form.GetValidFormData(w, &post) if err != nil { return } e.ContentValues.Type = post.Type err = e.Save(w) if err != nil { errmessage := fmt.Sprintf("Element not saved %s by %s", w.APIParams[0], w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to save element.", "Error", w) } else { services.AddMessage("Element content type saved.", "Success", w) } w.Serve() return }
// Controller for adding existing element to path form submission. func AddExistingPathSubmit(w *wrapper.Wrapper) { parentid := w.APIParams[0] var post map[string]string err := form.GetValidFormData(w, &post) if err != nil { return } var parent paths.Path parent, err = paths.LoadPath(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: "centereditor", Controller: "admin/path_elements", Template: "admin/path_elements.html", Id: parentid, } services.SetDynamic(dynamic, w) services.AddMessage("You have added an existing element.", "Success", w) w.Serve() }
// Controller for path sort form. func SortPathForm(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 } p, err := paths.LoadPath(parentid, w) if err != nil { errmessage := fmt.Sprintf("Path 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 } else { if len(p.Elements) > 0 { w.SetPayload("elements", p.Elements) } else { services.AddMessage("This has no elements assigned yet.", "Error", w) } w.SetTemplate("admin/element_sorter.html") w.Serve() return } }
func GetValidFormData(w *wrapper.Wrapper, post interface{}) error { p := make([]byte, w.Request.ContentLength) _, err := w.Request.Body.Read(p) if err != nil { errmessage := fmt.Sprintf("Error processing post values %s: %s", w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("There was an issue processing your form.", "Error", w) w.Serve() return errors.New("Could not marshall Post values") } data := make(map[string]interface{}) err = json.Unmarshal(p, &data) if err != nil { errmessage := fmt.Sprintf("Error processing post values %s: %s", w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("There was an issue processing your form.", "Error", w) w.Serve() return errors.New("Could not marshall Post values") } register, reg_err := GetFormRegister(data["form_id"].(string), w) if reg_err != nil { errmessage := fmt.Sprintf("Invalid or expired form %s: %s", w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Your form was expired, please try again.", "Error", w) w.Serve() return errors.New("Inalid or expired form") } err = json.Unmarshal(p, post) if err != nil { errmessage := fmt.Sprintf("Error processing post values %s: %s", w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("There was an issue processing your form.", "Error", w) w.Serve() return errors.New("Could not marshall Post values") } missing := register.ValidateRequired(data) if len(missing) > 0 { for _, label := range missing { message := fmt.Sprintf("%s is required.", label) services.AddMessage(message, "Error", w) } w.Serve() return errors.New("missing fields") } return nil }
// 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 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 }
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() }
// 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 path sort submission. func SortPathSubmit(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 } pes := paths.NewPathElements() err := json.NewDecoder(w.Request.Body).Decode(&pes) 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 } pe, err := paths.LoadPath(parentid, w) if err != nil { errmessage := fmt.Sprintf("Path not found to sort for %s by %s.", parentid, w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("This path was not found", "Error", w) w.Serve() return } pe.PathElements = pes err = pe.Save(w) if err != nil { errmessage := fmt.Sprintf("Unable to save path %s by %s : %s", parentid, w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Could not save path.", "Error", w) w.Serve() return } dynamic := services.Dynamic{ Target: "centereditor", Controller: "admin/path_elements", Template: "admin/path_elements.html", Id: w.APIParams[1], } 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 list all paths func AdminPaths(w *wrapper.Wrapper) { pl, err := paths.PathList(w) if err != nil { services.AddMessage("There was an error retrieving your site paths", "Error", w) errmessage := fmt.Sprintf("Error getting path list: %s", err.Error()) w.SiteConfig.Logger.Error(errmessage) } else { w.SetContent(pl) } w.Serve() }
// Controller to delete a path func DeletePath(w *wrapper.Wrapper) { id := w.APIParams[0] err := paths.Delete(id, w) if err != nil { errmessage := fmt.Sprintf("Unable to delete path %s : %s", id, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to delete.", "Error", w) w.Serve() return } dynamic := services.Dynamic{ Target: "pathbar", Controller: "admin/paths", Template: "admin/path_list.html", } services.SetDynamic(dynamic, w) services.AddMessage("Successfully deleted path", "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 handle path editor form submissions. func PathEditorSubmit(w *wrapper.Wrapper) { pathid := w.APIParams[0] var path paths.Path var err error if pathid != "new" { path, err = paths.LoadPath(pathid, w) if err != nil { errmessage := fmt.Sprintf("Unable to save path %s by %s: %s", pathid, w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("There was a problem saving your path.", "Error", w) w.Serve() return } } err = form.GetValidFormData(w, &path) if err != nil { return } err = path.Save(w) if err != nil { errmessage := fmt.Sprintf("Unable to save path %s by %s: %s", pathid, w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("There was a problem saving your path.", "Error", w) w.Serve() return } services.AddMessage("Your path was saved.", "Success", w) dynamic := services.Dynamic{ Target: "pathbar", Controller: "admin/paths", Template: "admin/path_list.html", } services.SetDynamic(dynamic, w) w.Serve() return }
// Retrieve a list of elements in a path for content editor. func PathElements(w *wrapper.Wrapper) { if len(w.APIParams) == 0 { http.Error(w.Writer, "Forbidden", 403) w.Serve() return } pathid := w.APIParams[0] p, err := paths.LoadPath(pathid, w) if err != nil { errmessage := fmt.Sprintf("Path not found to edit for %s by %s ", pathid, w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("This path was not found", "Error", w) w.Serve() } else { w.SetPayload("path", p) if len(p.Elements) == 0 { services.AddMessage("This path has no elements.", "Info", w) } w.Serve() } }
// 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 }
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 GetAllContentTypes(w *wrapper.Wrapper) { c := w.DbSession.DB("").C("content_types") var cts []ContentType err := c.Find(nil).Limit(50).Iter().All(&cts) if err != nil { errmessage := fmt.Sprintf("Unable to retrieve a list of content types.") w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to retrieve a list of elements.", "Error", w) w.Serve() return } w.SetPayload("content_types", cts) w.Serve() return }
// Controller to list all elements func AllElements(w *wrapper.Wrapper) { c := w.DbSession.DB("").C("elements") var es []elements.Element err := c.Find(nil).Limit(50).Iter().All(&es) 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 } w.SetPayload("elements", es) w.Serve() return }
func GetContentType(w *wrapper.Wrapper) { c := w.DbSession.DB("").C("content_types") i := bson.M{"_id": bson.ObjectIdHex(w.APIParams[0])} var ct ContentType 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 } w.SetPayload("content_type", ct) w.Serve() return }
// 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 }
// The controller function for Values found directly in the controller values of the element func ContentValues(w *wrapper.Wrapper) { var contentid string if len(w.APIParams) > 0 { contentid = w.APIParams[0] } else { http.Error(w.Writer, "Forbidden", 403) w.Serve() return } e, err := elements.LoadContentElement(contentid, w) if err != nil { errmessage := fmt.Sprintf("Content not found %s : %s", contentid, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("There was a problem loading some content on your page.", "Error", w) w.Serve() return } w.SetContent(e.ContentValues.Content) 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 }
// Controller to retrive a list of all orphaned elements. func OrphanElements(w *wrapper.Wrapper) { assigned := make([]bson.ObjectId, 0) paths, err := paths.PathList(w) if err != nil { errmessage := fmt.Sprintf("Could not retrieve path elements for orphan list: %s", err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Could not retrieve path elements.", "Error", w) w.Serve() } for _, path := range paths { for _, element := range path.Elements { id := bson.ObjectIdHex(element) assigned = append(assigned, id) } } wrappers := make([]elements.WrapperElement, 0) c := w.DbSession.DB("").C("elements") s := bson.M{"controller": "wrapper"} i := c.Find(s).Limit(50).Iter() err = i.All(&wrappers) if err != nil { errmessage := fmt.Sprintf("Could not retrieve wrapper elements for orphan list: %s", err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Could not retrieve wrapper elements.", "Error", w) w.Serve() } for _, wrapper := range wrappers { for _, eid := range wrapper.Elements { bsonid := bson.ObjectIdHex(eid) assigned = append(assigned, bsonid) } } slugs := make([]elements.SlugElement, 0) s = bson.M{"controller": "slug"} i = c.Find(s).Limit(50).Iter() err = i.All(&slugs) if err != nil { errmessage := fmt.Sprintf("Could not retrieve slug elements for orphan list: %s", err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Could not retrieve slug elements.", "Error", w) w.Serve() } for _, slug := range slugs { for _, eid := range slug.Slugs { bsonid := bson.ObjectIdHex(eid) assigned = append(assigned, bsonid) } } unassigned := new([]elements.Element) s = bson.M{"_id": bson.M{"$nin": assigned}} i = c.Find(s).Limit(50).Iter() err = i.All(unassigned) if err != nil { errmessage := fmt.Sprintf("Could not retrieve unassigned elements: %s", err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Could not retrieve unassigned elements.", "Error", w) w.Serve() } w.SetTemplate("admin/orphan_path_elements.html") w.SetPayload("elements", unassigned) w.Serve() return }