// 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 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 }
// 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() }
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 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 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 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 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 }
// 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 }
// Controller to edit a menu element // TODO: break this into smaller functions func MenuEditor(w *wrapper.Wrapper) { if len(w.APIParams) == 0 { http.Error(w.Writer, "Forbidden", 403) return } menuid := w.APIParams[0] if w.Request.Method != "POST" { e, err := elements.LoadMenuElement(menuid, w) if err != nil { errmessage := fmt.Sprintf("Element not found to edit for %s by %s.", menuid, w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("This element was not found", "Error", w) w.Serve() return } if e.MenuItems == nil { items := make(map[string][]map[string]string) items["menu_items"] = make([]map[string]string, 0) w.SetPayload("menu", items) } else { w.SetPayload("menu", e) } w.SetPayload("title", e.Title) w.SetTemplate("admin/menu_editor.html") w.Serve() return } else { e, err := elements.LoadMenuElement(menuid, w) if err != nil { errmessage := fmt.Sprintf("Element not found to edit for %s by %s.", menuid, w.Request.Host) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("This element was not found", "Error", w) w.Serve() return } err = json.NewDecoder(w.Request.Body).Decode(&e) if err != nil { errmessage := fmt.Sprintf("Unable to update marshall menu elements by %s: %s", w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to save menu element.", "Error", w) w.Serve() return } err = e.Save(w) if err != nil { errmessage := fmt.Sprintf("Unable to update menu element %s by %s: %s", menuid, w.Request.Host, err.Error()) w.SiteConfig.Logger.Error(errmessage) services.AddMessage("Unable to save menu element.", "Error", w) w.Serve() return } dynamic := services.Dynamic{ Target: "modaleditor", Controller: "", Template: "", Id: "", } services.SetDynamic(dynamic, w) services.AddMessage("You menu element have been updated.", "Success", w) w.Serve() } }