module_blog_categories.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package modules
  2. import (
  3. "html"
  4. "strings"
  5. "golang-fave/engine/wrapper"
  6. "golang-fave/utils"
  7. )
  8. func (this *Modules) blog_GetCategorySelectOptions(wrap *wrapper.Wrapper, id int, parentId int) string {
  9. result := ``
  10. rows, err := wrap.DB.Query(
  11. `SELECT
  12. node.id,
  13. node.user,
  14. node.name,
  15. node.alias,
  16. (COUNT(parent.id) - 1) AS depth
  17. FROM
  18. blog_cats AS node,
  19. blog_cats AS parent
  20. WHERE
  21. node.lft BETWEEN parent.lft AND parent.rgt
  22. GROUP BY
  23. node.id
  24. ORDER BY
  25. node.lft ASC
  26. ;`,
  27. )
  28. if err == nil {
  29. values := make([]string, 5)
  30. scan := make([]interface{}, len(values))
  31. for i := range values {
  32. scan[i] = &values[i]
  33. }
  34. idStr := utils.IntToStr(id)
  35. parentIdStr := utils.IntToStr(parentId)
  36. for rows.Next() {
  37. err = rows.Scan(scan...)
  38. if err == nil {
  39. disabled := ""
  40. if string(values[0]) == idStr {
  41. disabled = " disabled"
  42. }
  43. selected := ""
  44. if string(values[0]) == parentIdStr {
  45. selected = " selected"
  46. }
  47. sub := strings.Repeat("— ", utils.StrToInt(string(values[4])))
  48. result += `<option value="` + html.EscapeString(string(values[0])) + `"` + disabled + selected + `>` + sub + html.EscapeString(string(values[2])) + `</option>`
  49. }
  50. }
  51. }
  52. return result
  53. }
  54. func (this *Modules) blog_GetCategoryParentId(wrap *wrapper.Wrapper, id int) int {
  55. var parentId int
  56. _ = wrap.DB.QueryRow(`
  57. SELECT
  58. parent.id
  59. FROM
  60. blog_cats AS node,
  61. blog_cats AS parent
  62. WHERE
  63. node.lft BETWEEN parent.lft AND parent.rgt AND
  64. node.id = ? AND
  65. parent.id <> ?
  66. ORDER BY
  67. parent.lft DESC
  68. LIMIT 1;`,
  69. id,
  70. id,
  71. ).Scan(
  72. &parentId,
  73. )
  74. return parentId
  75. }
  76. func (this *Modules) blog_ActionCategoryAdd(wrap *wrapper.Wrapper, pf_id, pf_name, pf_alias, pf_parent string) error {
  77. //
  78. return nil
  79. }
  80. func (this *Modules) blog_ActionCategoryUpdate(wrap *wrapper.Wrapper, pf_id, pf_name, pf_alias, pf_parent string) error {
  81. //
  82. return nil
  83. }
  84. func (this *Modules) RegisterAction_BlogCategoriesModify() *Action {
  85. return this.newAction(AInfo{
  86. WantDB: true,
  87. Mount: "blog-categories-modify",
  88. WantAdmin: true,
  89. }, func(wrap *wrapper.Wrapper) {
  90. pf_id := wrap.R.FormValue("id")
  91. pf_name := wrap.R.FormValue("name")
  92. pf_alias := wrap.R.FormValue("alias")
  93. pf_parent := wrap.R.FormValue("parent")
  94. if !utils.IsNumeric(pf_id) || !utils.IsNumeric(pf_parent) {
  95. wrap.MsgError(`Inner system error`)
  96. return
  97. }
  98. if pf_name == "" {
  99. wrap.MsgError(`Please specify category name`)
  100. return
  101. }
  102. if pf_alias == "" {
  103. pf_alias = utils.GenerateSingleAlias(pf_name)
  104. }
  105. if !utils.IsValidSingleAlias(pf_alias) {
  106. wrap.MsgError(`Please specify correct category alias`)
  107. return
  108. }
  109. if pf_id == "0" {
  110. if err := this.blog_ActionCategoryAdd(wrap, pf_id, pf_name, pf_alias, pf_parent); err != nil {
  111. wrap.MsgError(err.Error())
  112. return
  113. }
  114. wrap.Write(`window.location='/cp/blog/categories/';`)
  115. } else {
  116. if err := this.blog_ActionCategoryUpdate(wrap, pf_id, pf_name, pf_alias, pf_parent); err != nil {
  117. wrap.MsgError(err.Error())
  118. return
  119. }
  120. wrap.Write(`window.location='/cp/blog/categories-modify/` + pf_id + `/';`)
  121. }
  122. })
  123. }