示例#1
0
文件: slug.go 项目: bdatchar/mongolar
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
}
示例#2
0
文件: slug.go 项目: bdatchar/mongolar
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
}
示例#3
0
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()

}
示例#4
0
// 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()
}