예제 #1
0
파일: content.go 프로젝트: blackmady/GoCMS
//单页面 添加内容
func (c Content) AddContent(article *models.Article) revel.Result {
	if c.Request.Method == "GET" {
		title := "内容--GoCMS添加内容"

		var cid string = c.Params.Get("cid")

		if len(cid) > 0 {

			Cid, err := strconv.ParseInt(cid, 10, 64)
			if err != nil {
				revel.WARN.Println(err)
			}

			//栏目信息
			category := new(models.Category)
			category_info := category.GetById(Cid)

			//内容
			article_info := article.GetByCid(Cid)

			c.Render(title, cid, category_info, article_info)
			return c.RenderTemplate("Content/Manage/AddContent.html")
		} else {
			c.Render(title, cid)
			return c.RenderTemplate("Content/Manage/AddContent.html")
		}
	} else {

		var cid string = c.Params.Get("cid")

		if len(cid) < 0 {
			c.Flash.Error("请选择栏目!")
			c.Flash.Out["url"] = "/Content/addContent/" + cid + "/"
			return c.Redirect("/Message/")
		}

		UserID := utils.GetSession("UserID", c.Session)

		if len(UserID) > 0 {
			UserID, err := strconv.ParseInt(UserID, 10, 64)
			if err != nil {
				revel.WARN.Println(err)
			}
			article.Aid = UserID
		}

		var title string = c.Params.Get("title")
		if len(title) > 0 {
			article.Title = title
		} else {
			c.Flash.Error("请输入标题!")
			c.Flash.Out["url"] = "/Content/addContent/" + cid + "/"
			return c.Redirect("/Message/")
		}

		var style_color string = c.Params.Get("style_color")
		article.Color = style_color
		var style_font_weight string = c.Params.Get("style_font_weight")
		article.Font = style_font_weight

		var keywords string = c.Params.Get("keywords")
		if len(keywords) > 0 {
			article.Keywords = keywords
		} else {
			article.Keywords = ""
		}

		var content string = c.Params.Get("content")
		if len(content) > 0 {
			article.Content = content
		} else {
			c.Flash.Error("请输入内容!")
			c.Flash.Out["url"] = "/Content/addContent/" + cid + "/"
			return c.Redirect("/Message/")
		}

		if len(cid) > 0 {

			Cid, err := strconv.ParseInt(cid, 10, 64)
			if err != nil {
				revel.WARN.Println(err)
			}

			article.Cid = Cid

			//内容
			article_info := article.GetByCid(Cid)

			if article_info.Id > 0 {
				if article.Edit(article_info.Id) {
					c.Flash.Success("编辑内容成功!")
					c.Flash.Out["url"] = "/Content/addContent/" + cid + "/"
					return c.Redirect("/Message/")
				} else {
					c.Flash.Success("编辑内容成功!")
					c.Flash.Out["url"] = "/Content/addContent/" + cid + "/"
					return c.Redirect("/Message/")
				}
			} else {
				if article.Save() {
					c.Flash.Success("添加内容成功!")
					c.Flash.Out["url"] = "/Content/addContent/" + cid + "/"
					return c.Redirect("/Message/")
				} else {
					c.Flash.Error("添加内容失败")
					c.Flash.Out["url"] = "/Content/addContent/" + cid + "/"
					return c.Redirect("/Message/")
				}
			}
		} else {
			c.Flash.Error("请选择栏目!")
			c.Flash.Out["url"] = "/Content/addContent/" + cid + "/"
			return c.Redirect("/Message/")
		}
	}
}