module_blog_act_modify.go 4.0 KB

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