module_blog_act_modify.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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
  38. tx, err := wrap.DB.Begin()
  39. if err != nil {
  40. wrap.MsgError(err.Error())
  41. return
  42. }
  43. // Insert row
  44. res, err := tx.Exec(
  45. `INSERT INTO blog_posts SET
  46. user = ?,
  47. name = ?,
  48. alias = ?,
  49. content = ?,
  50. datetime = ?,
  51. active = ?
  52. ;`,
  53. wrap.User.A_id,
  54. pf_name,
  55. pf_alias,
  56. pf_content,
  57. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  58. pf_active,
  59. )
  60. if err != nil {
  61. tx.Rollback()
  62. wrap.MsgError(err.Error())
  63. return
  64. }
  65. lastID, err := res.LastInsertId()
  66. if err != nil {
  67. tx.Rollback()
  68. wrap.MsgError(err.Error())
  69. return
  70. }
  71. // Insert post and categories relations
  72. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  73. if len(catids) > 0 {
  74. var catsCount int
  75. err = tx.QueryRow(`
  76. SELECT
  77. COUNT(*)
  78. FROM
  79. blog_cats
  80. WHERE
  81. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  82. ;`,
  83. ).Scan(
  84. &catsCount,
  85. )
  86. if err != nil {
  87. tx.Rollback()
  88. wrap.MsgError(err.Error())
  89. return
  90. }
  91. if len(catids) != catsCount {
  92. tx.Rollback()
  93. wrap.MsgError(`Inner system error`)
  94. return
  95. }
  96. var balkInsertArr []string
  97. for _, el := range catids {
  98. balkInsertArr = append(balkInsertArr, `(NULL,`+utils.Int64ToStr(lastID)+`,`+utils.IntToStr(el)+`)`)
  99. }
  100. if _, err = tx.Exec(
  101. `INSERT INTO blog_cat_post_rel (id,post_id,category_id) VALUES ` + strings.Join(balkInsertArr, ",") + `;`,
  102. ); err != nil {
  103. tx.Rollback()
  104. wrap.MsgError(err.Error())
  105. return
  106. }
  107. }
  108. // Commit all changes
  109. err = tx.Commit()
  110. if err != nil {
  111. wrap.MsgError(err.Error())
  112. return
  113. }
  114. wrap.Write(`window.location='/cp/blog/';`)
  115. } else {
  116. // Start transaction
  117. tx, err := wrap.DB.Begin()
  118. if err != nil {
  119. wrap.MsgError(err.Error())
  120. return
  121. }
  122. // Update row
  123. if _, err = tx.Exec(
  124. `UPDATE blog_posts SET
  125. name = ?,
  126. alias = ?,
  127. content = ?,
  128. active = ?
  129. WHERE
  130. id = ?
  131. ;`,
  132. pf_name,
  133. pf_alias,
  134. pf_content,
  135. pf_active,
  136. utils.StrToInt(pf_id),
  137. ); err != nil {
  138. tx.Rollback()
  139. wrap.MsgError(err.Error())
  140. return
  141. }
  142. // Delete post and categories relations
  143. if _, err = tx.Exec("DELETE FROM blog_cat_post_rel WHERE post_id = ?;", pf_id); err != nil {
  144. tx.Rollback()
  145. wrap.MsgError(err.Error())
  146. return
  147. }
  148. // Insert post and categories relations
  149. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  150. if len(catids) > 0 {
  151. var catsCount int
  152. err = tx.QueryRow(`
  153. SELECT
  154. COUNT(*)
  155. FROM
  156. blog_cats
  157. WHERE
  158. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  159. ;`,
  160. ).Scan(
  161. &catsCount,
  162. )
  163. if err != nil {
  164. tx.Rollback()
  165. wrap.MsgError(err.Error())
  166. return
  167. }
  168. if len(catids) != catsCount {
  169. tx.Rollback()
  170. wrap.MsgError(`Inner system error`)
  171. return
  172. }
  173. var balkInsertArr []string
  174. for _, el := range catids {
  175. balkInsertArr = append(balkInsertArr, `(NULL,`+pf_id+`,`+utils.IntToStr(el)+`)`)
  176. }
  177. if _, err = tx.Exec(
  178. `INSERT INTO blog_cat_post_rel (id,post_id,category_id) VALUES ` + strings.Join(balkInsertArr, ",") + `;`,
  179. ); err != nil {
  180. tx.Rollback()
  181. wrap.MsgError(err.Error())
  182. return
  183. }
  184. }
  185. // Commit all changes
  186. err = tx.Commit()
  187. if err != nil {
  188. wrap.MsgError(err.Error())
  189. return
  190. }
  191. wrap.Write(`window.location='/cp/blog/modify/` + pf_id + `/';`)
  192. }
  193. })
  194. }