Browse Source

Post modify/delete action, fix view post url

Vova Tkach 6 years ago
parent
commit
09887d89c6
1 changed files with 113 additions and 1 deletions
  1. 113 1
      modules/module_blog.go

+ 113 - 1
modules/module_blog.go

@@ -83,7 +83,7 @@ func (this *Modules) RegisterModule_Blog() *Module {
 					return builder.DataTableAction(&[]builder.DataTableActionRow{
 						{
 							Icon:   assets.SysSvgIconView,
-							Href:   (*values)[2],
+							Href:   `/blog/` + (*values)[2] + `/`,
 							Hint:   "View",
 							Target: "_blank",
 						},
@@ -428,3 +428,115 @@ func (this *Modules) RegisterModule_Blog() *Module {
 		return this.getSidebarModules(wrap), content, sidebar
 	})
 }
+
+func (this *Modules) RegisterAction_BlogModify() *Action {
+	return this.newAction(AInfo{
+		WantDB:    true,
+		Mount:     "blog-modify",
+		WantAdmin: true,
+	}, func(wrap *wrapper.Wrapper) {
+		pf_id := wrap.R.FormValue("id")
+		pf_name := wrap.R.FormValue("name")
+		pf_alias := wrap.R.FormValue("alias")
+		pf_content := wrap.R.FormValue("content")
+		pf_active := wrap.R.FormValue("active")
+
+		if pf_active == "" {
+			pf_active = "0"
+		}
+
+		if !utils.IsNumeric(pf_id) {
+			wrap.MsgError(`Inner system error`)
+			return
+		}
+
+		if pf_name == "" {
+			wrap.MsgError(`Please specify page name`)
+			return
+		}
+
+		if pf_alias == "" {
+			pf_alias = utils.GenerateSingleAlias(pf_name)
+		}
+
+		if !utils.IsValidSingleAlias(pf_alias) {
+			wrap.MsgError(`Please specify correct post alias`)
+			return
+		}
+
+		if pf_id == "0" {
+			// Add new post
+			_, err := wrap.DB.Exec(
+				`INSERT INTO blog_posts SET
+					user = ?,
+					name = ?,
+					alias = ?,
+					content = ?,
+					datetime = ?,
+					active = ?
+				;`,
+				wrap.User.A_id,
+				pf_name,
+				pf_alias,
+				pf_content,
+				utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
+				pf_active,
+			)
+			if err != nil {
+				wrap.MsgError(err.Error())
+				return
+			}
+			wrap.Write(`window.location='/cp/blog/';`)
+		} else {
+			// Update post
+			_, err := wrap.DB.Exec(
+				`UPDATE blog_posts SET
+					name = ?,
+					alias = ?,
+					content = ?,
+					active = ?
+				WHERE
+					id = ?
+				;`,
+				pf_name,
+				pf_alias,
+				pf_content,
+				pf_active,
+				utils.StrToInt(pf_id),
+			)
+			if err != nil {
+				wrap.MsgError(err.Error())
+				return
+			}
+			wrap.Write(`window.location='/cp/blog/modify/` + pf_id + `/';`)
+		}
+	})
+}
+
+func (this *Modules) RegisterAction_BlogDelete() *Action {
+	return this.newAction(AInfo{
+		WantDB:    true,
+		Mount:     "blog-delete",
+		WantAdmin: true,
+	}, func(wrap *wrapper.Wrapper) {
+		pf_id := wrap.R.FormValue("id")
+
+		if !utils.IsNumeric(pf_id) {
+			wrap.MsgError(`Inner system error`)
+			return
+		}
+
+		// Delete post
+		_, err := wrap.DB.Exec(
+			`DELETE FROM blog_posts WHERE id = ?;`,
+			utils.StrToInt(pf_id),
+		)
+		if err != nil {
+			wrap.MsgError(err.Error())
+			return
+		}
+
+		// Reload current page
+		wrap.Write(`window.location.reload(false);`)
+	})
+}