module_index_act_modify.go 3.1 KB

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