module_index_act_modify.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. Mount: "index-modify",
  10. WantAdmin: true,
  11. }, func(wrap *wrapper.Wrapper) {
  12. pf_id := utils.Trim(wrap.R.FormValue("id"))
  13. pf_name := utils.Trim(wrap.R.FormValue("name"))
  14. pf_alias := utils.Trim(wrap.R.FormValue("alias"))
  15. pf_content := utils.Trim(wrap.R.FormValue("content"))
  16. pf_meta_title := utils.Trim(wrap.R.FormValue("meta_title"))
  17. pf_meta_keywords := utils.Trim(wrap.R.FormValue("meta_keywords"))
  18. pf_meta_description := utils.Trim(wrap.R.FormValue("meta_description"))
  19. pf_active := utils.Trim(wrap.R.FormValue("active"))
  20. if pf_active == "" {
  21. pf_active = "0"
  22. }
  23. if !utils.IsNumeric(pf_id) {
  24. wrap.MsgError(`Inner system error`)
  25. return
  26. }
  27. if pf_name == "" {
  28. wrap.MsgError(`Please specify page name`)
  29. return
  30. }
  31. if pf_alias == "" {
  32. pf_alias = utils.GenerateAlias(pf_name)
  33. }
  34. if !utils.IsValidAlias(pf_alias) {
  35. wrap.MsgError(`Please specify correct page alias`)
  36. return
  37. }
  38. if pf_id == "0" {
  39. // Add new page
  40. var lastID int64 = 0
  41. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  42. res, err := tx.Exec(
  43. ctx,
  44. `INSERT INTO fave_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. ctx,
  85. `UPDATE fave_pages SET
  86. name = ?,
  87. alias = ?,
  88. content = ?,
  89. meta_title = ?,
  90. meta_keywords = ?,
  91. meta_description = ?,
  92. active = ?
  93. WHERE
  94. id = ?
  95. ;`,
  96. pf_name,
  97. pf_alias,
  98. pf_content,
  99. pf_meta_title,
  100. pf_meta_keywords,
  101. pf_meta_description,
  102. utils.StrToInt(pf_active),
  103. utils.StrToInt(pf_id),
  104. )
  105. if err != nil {
  106. return err
  107. }
  108. return nil
  109. }); err != nil {
  110. wrap.MsgError(err.Error())
  111. return
  112. }
  113. wrap.ResetCacheBlocks()
  114. wrap.Write(`window.location='/cp/index/modify/` + pf_id + `/';`)
  115. }
  116. })
  117. }