module_index_act_modify.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package modules
  2. import (
  3. "context"
  4. "golang-fave/engine/wrapper"
  5. "golang-fave/utils"
  6. )
  7. func (this *Modules) RegisterAction_IndexModify() *Action {
  8. return this.newAction(AInfo{
  9. WantDB: true,
  10. Mount: "index-modify",
  11. WantAdmin: true,
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_id := utils.Trim(wrap.R.FormValue("id"))
  14. pf_name := utils.Trim(wrap.R.FormValue("name"))
  15. pf_alias := utils.Trim(wrap.R.FormValue("alias"))
  16. pf_content := utils.Trim(wrap.R.FormValue("content"))
  17. pf_meta_title := utils.Trim(wrap.R.FormValue("meta_title"))
  18. pf_meta_keywords := utils.Trim(wrap.R.FormValue("meta_keywords"))
  19. pf_meta_description := utils.Trim(wrap.R.FormValue("meta_description"))
  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 pf_name == "" {
  29. wrap.MsgError(`Please specify page name`)
  30. return
  31. }
  32. if pf_alias == "" {
  33. pf_alias = utils.GenerateAlias(pf_name)
  34. }
  35. if !utils.IsValidAlias(pf_alias) {
  36. wrap.MsgError(`Please specify correct page alias`)
  37. return
  38. }
  39. if pf_id == "0" {
  40. // Add new page
  41. var lastID int64 = 0
  42. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  43. res, err := tx.Exec(
  44. `INSERT INTO pages SET
  45. user = ?,
  46. name = ?,
  47. alias = ?,
  48. content = ?,
  49. meta_title = ?,
  50. meta_keywords = ?,
  51. meta_description = ?,
  52. datetime = ?,
  53. active = ?
  54. ;`,
  55. wrap.User.A_id,
  56. pf_name,
  57. pf_alias,
  58. pf_content,
  59. pf_meta_title,
  60. pf_meta_keywords,
  61. pf_meta_description,
  62. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  63. utils.StrToInt(pf_active),
  64. )
  65. if err != nil {
  66. return err
  67. }
  68. // Get inserted post id
  69. lastID, err = res.LastInsertId()
  70. if err != nil {
  71. return err
  72. }
  73. return nil
  74. }); err != nil {
  75. wrap.MsgError(err.Error())
  76. return
  77. }
  78. wrap.ResetCacheBlocks()
  79. wrap.Write(`window.location='/cp/index/modify/` + utils.Int64ToStr(lastID) + `/';`)
  80. } else {
  81. // Update page
  82. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  83. _, err := tx.Exec(
  84. `UPDATE pages SET
  85. name = ?,
  86. alias = ?,
  87. content = ?,
  88. meta_title = ?,
  89. meta_keywords = ?,
  90. meta_description = ?,
  91. active = ?
  92. WHERE
  93. id = ?
  94. ;`,
  95. pf_name,
  96. pf_alias,
  97. pf_content,
  98. pf_meta_title,
  99. pf_meta_keywords,
  100. pf_meta_description,
  101. utils.StrToInt(pf_active),
  102. utils.StrToInt(pf_id),
  103. )
  104. if err != nil {
  105. return err
  106. }
  107. return nil
  108. }); err != nil {
  109. wrap.MsgError(err.Error())
  110. return
  111. }
  112. wrap.ResetCacheBlocks()
  113. wrap.Write(`window.location='/cp/index/modify/` + pf_id + `/';`)
  114. }
  115. })
  116. }