module_blog_act_modify.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package modules
  2. import (
  3. "strings"
  4. "golang-fave/engine/wrapper"
  5. "golang-fave/utils"
  6. )
  7. func (this *Modules) RegisterAction_BlogModify() *Action {
  8. return this.newAction(AInfo{
  9. WantDB: true,
  10. Mount: "blog-modify",
  11. WantAdmin: true,
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_id := wrap.R.FormValue("id")
  14. pf_name := wrap.R.FormValue("name")
  15. pf_alias := wrap.R.FormValue("alias")
  16. pf_content := wrap.R.FormValue("content")
  17. pf_active := wrap.R.FormValue("active")
  18. if pf_active == "" {
  19. pf_active = "0"
  20. }
  21. if !utils.IsNumeric(pf_id) {
  22. wrap.MsgError(`Inner system error`)
  23. return
  24. }
  25. if pf_name == "" {
  26. wrap.MsgError(`Please specify page name`)
  27. return
  28. }
  29. if pf_alias == "" {
  30. pf_alias = utils.GenerateSingleAlias(pf_name)
  31. }
  32. if !utils.IsValidSingleAlias(pf_alias) {
  33. wrap.MsgError(`Please specify correct post alias`)
  34. return
  35. }
  36. if pf_id == "0" {
  37. // Start transaction with table lock
  38. _, err := wrap.DB.Exec("LOCK TABLE blog_cats WRITE, blog_cat_post_rel WRITE;")
  39. if err != nil {
  40. wrap.MsgError(err.Error())
  41. return
  42. }
  43. tx, err := wrap.DB.Begin()
  44. if err != nil {
  45. wrap.MsgError(err.Error())
  46. return
  47. }
  48. // Insert row
  49. res, err := tx.Exec(
  50. `INSERT INTO blog_posts SET
  51. user = ?,
  52. name = ?,
  53. alias = ?,
  54. content = ?,
  55. datetime = ?,
  56. active = ?
  57. ;`,
  58. wrap.User.A_id,
  59. pf_name,
  60. pf_alias,
  61. pf_content,
  62. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  63. pf_active,
  64. )
  65. if err != nil {
  66. tx.Rollback()
  67. wrap.MsgError(err.Error())
  68. return
  69. }
  70. lastID, err := res.LastInsertId()
  71. if err != nil {
  72. tx.Rollback()
  73. wrap.MsgError(err.Error())
  74. return
  75. }
  76. // Insert post and categories relations
  77. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  78. if len(catids) > 0 {
  79. var catsCount int
  80. err = tx.QueryRow(`
  81. SELECT
  82. COUNT(*)
  83. FROM
  84. blog_cats
  85. WHERE
  86. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  87. ;`,
  88. ).Scan(
  89. &catsCount,
  90. )
  91. if err != nil {
  92. tx.Rollback()
  93. wrap.MsgError(err.Error())
  94. return
  95. }
  96. if len(catids) != catsCount {
  97. tx.Rollback()
  98. wrap.MsgError(`Inner system error`)
  99. return
  100. }
  101. var balkInsertArr []string
  102. for _, el := range catids {
  103. balkInsertArr = append(balkInsertArr, `(NULL,`+utils.Int64ToStr(lastID)+`,`+utils.IntToStr(el)+`)`)
  104. }
  105. if _, err = tx.Exec(
  106. `INSERT INTO blog_cat_post_rel (id,post_id,category_id) VALUES ` + strings.Join(balkInsertArr, ",") + `;`,
  107. ); err != nil {
  108. tx.Rollback()
  109. wrap.MsgError(err.Error())
  110. return
  111. }
  112. }
  113. // Commit all changes and unlock table
  114. err = tx.Commit()
  115. if err != nil {
  116. wrap.MsgError(err.Error())
  117. return
  118. }
  119. _, err = wrap.DB.Exec("UNLOCK TABLES;")
  120. if err != nil {
  121. wrap.MsgError(err.Error())
  122. return
  123. }
  124. wrap.Write(`window.location='/cp/blog/';`)
  125. } else {
  126. // Start transaction with table lock
  127. _, err := wrap.DB.Exec("LOCK TABLE blog_cats WRITE, blog_cat_post_rel WRITE;")
  128. if err != nil {
  129. wrap.MsgError(err.Error())
  130. return
  131. }
  132. tx, err := wrap.DB.Begin()
  133. if err != nil {
  134. wrap.MsgError(err.Error())
  135. return
  136. }
  137. // Update row
  138. if _, err = tx.Exec(
  139. `UPDATE blog_posts SET
  140. name = ?,
  141. alias = ?,
  142. content = ?,
  143. active = ?
  144. WHERE
  145. id = ?
  146. ;`,
  147. pf_name,
  148. pf_alias,
  149. pf_content,
  150. pf_active,
  151. utils.StrToInt(pf_id),
  152. ); err != nil {
  153. tx.Rollback()
  154. wrap.MsgError(err.Error())
  155. return
  156. }
  157. // Delete post and categories relations
  158. if _, err = tx.Exec("DELETE FROM blog_cat_post_rel WHERE post_id = ?;", pf_id); err != nil {
  159. tx.Rollback()
  160. wrap.MsgError(err.Error())
  161. return
  162. }
  163. // Insert post and categories relations
  164. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  165. if len(catids) > 0 {
  166. var catsCount int
  167. err = tx.QueryRow(`
  168. SELECT
  169. COUNT(*)
  170. FROM
  171. blog_cats
  172. WHERE
  173. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  174. ;`,
  175. ).Scan(
  176. &catsCount,
  177. )
  178. if err != nil {
  179. tx.Rollback()
  180. wrap.MsgError(err.Error())
  181. return
  182. }
  183. if len(catids) != catsCount {
  184. tx.Rollback()
  185. wrap.MsgError(`Inner system error`)
  186. return
  187. }
  188. var balkInsertArr []string
  189. for _, el := range catids {
  190. balkInsertArr = append(balkInsertArr, `(NULL,`+pf_id+`,`+utils.IntToStr(el)+`)`)
  191. }
  192. if _, err = tx.Exec(
  193. `INSERT INTO blog_cat_post_rel (id,post_id,category_id) VALUES ` + strings.Join(balkInsertArr, ",") + `;`,
  194. ); err != nil {
  195. tx.Rollback()
  196. wrap.MsgError(err.Error())
  197. return
  198. }
  199. }
  200. // Commit all changes and unlock table
  201. err = tx.Commit()
  202. if err != nil {
  203. wrap.MsgError(err.Error())
  204. return
  205. }
  206. _, err = wrap.DB.Exec("UNLOCK TABLES;")
  207. if err != nil {
  208. wrap.MsgError(err.Error())
  209. return
  210. }
  211. wrap.Write(`window.location='/cp/blog/modify/` + pf_id + `/';`)
  212. }
  213. })
  214. }