module_blog_act_modify.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 := wrap.R.FormValue("id")
  15. pf_name := wrap.R.FormValue("name")
  16. pf_alias := wrap.R.FormValue("alias")
  17. pf_content := wrap.R.FormValue("content")
  18. pf_active := wrap.R.FormValue("active")
  19. if pf_active == "" {
  20. pf_active = "0"
  21. }
  22. if !utils.IsNumeric(pf_id) {
  23. wrap.MsgError(`Inner system error`)
  24. return
  25. }
  26. if pf_name == "" {
  27. wrap.MsgError(`Please specify page name`)
  28. return
  29. }
  30. if pf_alias == "" {
  31. pf_alias = utils.GenerateSingleAlias(pf_name)
  32. }
  33. if !utils.IsValidSingleAlias(pf_alias) {
  34. wrap.MsgError(`Please specify correct post alias`)
  35. return
  36. }
  37. if pf_id == "0" {
  38. if err := wrap.DBTrans(func(tx *wrapper.Tx) error {
  39. // Insert row
  40. res, err := tx.Exec(
  41. `INSERT INTO blog_posts SET
  42. user = ?,
  43. name = ?,
  44. alias = ?,
  45. content = ?,
  46. datetime = ?,
  47. active = ?
  48. ;`,
  49. wrap.User.A_id,
  50. pf_name,
  51. pf_alias,
  52. pf_content,
  53. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  54. pf_active,
  55. )
  56. if err != nil {
  57. return err
  58. }
  59. // Get inserted post id
  60. lastID, err := res.LastInsertId()
  61. if err != nil {
  62. return err
  63. }
  64. // Insert post and categories relations
  65. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  66. if len(catids) > 0 {
  67. var catsCount int
  68. err = tx.QueryRow(`
  69. SELECT
  70. COUNT(*)
  71. FROM
  72. blog_cats
  73. WHERE
  74. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  75. ;`,
  76. ).Scan(
  77. &catsCount,
  78. )
  79. if err != nil {
  80. return err
  81. }
  82. if len(catids) != catsCount {
  83. return errors.New("Inner system error")
  84. }
  85. var balkInsertArr []string
  86. for _, el := range catids {
  87. balkInsertArr = append(balkInsertArr, `(NULL,`+utils.Int64ToStr(lastID)+`,`+utils.IntToStr(el)+`)`)
  88. }
  89. if _, err = tx.Exec(
  90. `INSERT INTO blog_cat_post_rel (id,post_id,category_id) VALUES ` + strings.Join(balkInsertArr, ",") + `;`,
  91. ); err != nil {
  92. return err
  93. }
  94. }
  95. return nil
  96. }); err != nil {
  97. wrap.MsgError(err.Error())
  98. return
  99. }
  100. wrap.Write(`window.location='/cp/blog/';`)
  101. } else {
  102. if err := wrap.DBTrans(func(tx *wrapper.Tx) error {
  103. // Update row
  104. if _, err := tx.Exec(
  105. `UPDATE blog_posts SET
  106. name = ?,
  107. alias = ?,
  108. content = ?,
  109. active = ?
  110. WHERE
  111. id = ?
  112. ;`,
  113. pf_name,
  114. pf_alias,
  115. pf_content,
  116. pf_active,
  117. utils.StrToInt(pf_id),
  118. ); err != nil {
  119. return err
  120. }
  121. // Delete post and categories relations
  122. if _, err := tx.Exec("DELETE FROM blog_cat_post_rel WHERE post_id = ?;", pf_id); err != nil {
  123. return err
  124. }
  125. // Insert post and categories relations
  126. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  127. if len(catids) > 0 {
  128. var catsCount int
  129. err := tx.QueryRow(`
  130. SELECT
  131. COUNT(*)
  132. FROM
  133. blog_cats
  134. WHERE
  135. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  136. ;`,
  137. ).Scan(
  138. &catsCount,
  139. )
  140. if err != nil {
  141. return err
  142. }
  143. if len(catids) != catsCount {
  144. return errors.New("Inner system error")
  145. }
  146. var balkInsertArr []string
  147. for _, el := range catids {
  148. balkInsertArr = append(balkInsertArr, `(NULL,`+pf_id+`,`+utils.IntToStr(el)+`)`)
  149. }
  150. if _, err := tx.Exec(
  151. `INSERT INTO blog_cat_post_rel (id,post_id,category_id) VALUES ` + strings.Join(balkInsertArr, ",") + `;`,
  152. ); err != nil {
  153. return err
  154. }
  155. }
  156. return nil
  157. }); err != nil {
  158. wrap.MsgError(err.Error())
  159. return
  160. }
  161. wrap.Write(`window.location='/cp/blog/modify/` + pf_id + `/';`)
  162. }
  163. })
  164. }