Esempio n. 1
0
// Post implemented creating new post
func (this *PostController) NewPost() {
	var (
		valid validation.Validation
		form  models.PostForm
		post  models.Node
		err   error
	)

	if err = this.ParseForm(&form); err == nil {
		if ok, e := valid.Valid(form); ok && e == nil {
			post.Title = form.Title
			post.Content = form.Content
			post.ContentType = form.ContentType
			post.CreateTime = time.Now()
			post.Status = models.GetStatus(form.Status)
			post.IUser = models.User{Id: this.User().Id}
			post.INodeType = models.NodeType{Id: models.GetNodeType("post")}

			tag := strings.TrimSpace(form.Tags)
			tags := make([]string, 0)
			tagarr := strings.Split(tag, ",")
			for _, v := range tagarr {
				if t := strings.TrimSpace(v); t != "" {
					tags = append(tags, t)
				}
			}

			if _, err := models.Engine.Insert(&post); err == nil {
				models.AddTags(this.User().Id, post.Id, tags)
				this.FlashNotice("Post created successfully.")
				this.SaveFlash()
				this.Redirect("/post/view/"+fmt.Sprintf("%d", post.Id), 302)
				return
			}
		} else {
			for _, m := range valid.Errors {
				this.FlashError(m.Key + " : " + m.Message)
			}
		}
	}

	this.SaveFlash()
	this.Data["Form"] = form
	this.Data["TYPE_"+form.ContentType] = true
	this.Data["STATUS_"+form.Status] = true
	this.Data["Title"] = "New post"
	this.TplNames = "post/new.html"
}