module_index_act_modify.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package modules
  2. import (
  3. "context"
  4. "golang-fave/engine/utils"
  5. "golang-fave/engine/wrapper"
  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. ctx,
  45. `INSERT INTO fave_pages SET
  46. user = ?,
  47. name = ?,
  48. alias = ?,
  49. content = ?,
  50. meta_title = ?,
  51. meta_keywords = ?,
  52. meta_description = ?,
  53. datetime = ?,
  54. active = ?
  55. ;`,
  56. wrap.User.A_id,
  57. pf_name,
  58. pf_alias,
  59. pf_content,
  60. pf_meta_title,
  61. pf_meta_keywords,
  62. pf_meta_description,
  63. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  64. utils.StrToInt(pf_active),
  65. )
  66. if err != nil {
  67. return err
  68. }
  69. // Get inserted post id
  70. lastID, err = res.LastInsertId()
  71. if err != nil {
  72. return err
  73. }
  74. return nil
  75. }); err != nil {
  76. wrap.MsgError(err.Error())
  77. return
  78. }
  79. wrap.ResetCacheBlocks()
  80. wrap.Write(`window.location='/cp/index/modify/` + utils.Int64ToStr(lastID) + `/';`)
  81. } else {
  82. // Update page
  83. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  84. _, err := tx.Exec(
  85. ctx,
  86. `UPDATE fave_pages SET
  87. name = ?,
  88. alias = ?,
  89. content = ?,
  90. meta_title = ?,
  91. meta_keywords = ?,
  92. meta_description = ?,
  93. active = ?
  94. WHERE
  95. id = ?
  96. ;`,
  97. pf_name,
  98. pf_alias,
  99. pf_content,
  100. pf_meta_title,
  101. pf_meta_keywords,
  102. pf_meta_description,
  103. utils.StrToInt(pf_active),
  104. utils.StrToInt(pf_id),
  105. )
  106. if err != nil {
  107. return err
  108. }
  109. return nil
  110. }); err != nil {
  111. wrap.MsgError(err.Error())
  112. return
  113. }
  114. wrap.ResetCacheBlocks()
  115. wrap.Write(`window.location='/cp/index/modify/` + pf_id + `/';`)
  116. }
  117. })
  118. }