module_blog_act_modify.go 5.1 KB

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