module_index_act_modify.go 2.7 KB

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