module_blog_categories.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package modules
  2. import (
  3. "html"
  4. "strings"
  5. "golang-fave/engine/utils"
  6. "golang-fave/engine/wrapper"
  7. )
  8. func (this *Modules) blog_GetCategorySelectOptions(wrap *wrapper.Wrapper, id int, parentId int, selids []int) string {
  9. result := ``
  10. rows, err := wrap.DB.Query(
  11. wrap.R.Context(),
  12. `SELECT
  13. node.id,
  14. node.user,
  15. node.name,
  16. node.alias,
  17. (COUNT(parent.id) - 1) AS depth
  18. FROM
  19. fave_blog_cats AS node,
  20. fave_blog_cats AS parent
  21. WHERE
  22. node.lft BETWEEN parent.lft AND parent.rgt AND
  23. node.id > 1
  24. GROUP BY
  25. node.id
  26. ORDER BY
  27. node.lft ASC
  28. ;`,
  29. )
  30. if err == nil {
  31. defer rows.Close()
  32. values := make([]string, 5)
  33. scan := make([]interface{}, len(values))
  34. for i := range values {
  35. scan[i] = &values[i]
  36. }
  37. idStr := utils.IntToStr(id)
  38. parentIdStr := utils.IntToStr(parentId)
  39. for rows.Next() {
  40. err = rows.Scan(scan...)
  41. if *wrap.LogCpError(&err) == nil {
  42. disabled := ""
  43. if string(values[0]) == idStr {
  44. disabled = " disabled"
  45. }
  46. selected := ""
  47. if string(values[0]) == parentIdStr {
  48. selected = " selected"
  49. }
  50. if len(selids) > 0 && utils.InArrayInt(selids, utils.StrToInt(string(values[0]))) {
  51. selected = " selected"
  52. }
  53. depth := utils.StrToInt(string(values[4])) - 1
  54. if depth < 0 {
  55. depth = 0
  56. }
  57. sub := strings.Repeat("&mdash; ", depth)
  58. result += `<option title="` + html.EscapeString(string(values[2])) + `" value="` + html.EscapeString(string(values[0])) + `"` + disabled + selected + `>` + sub + html.EscapeString(string(values[2])) + `</option>`
  59. }
  60. }
  61. }
  62. return result
  63. }
  64. func (this *Modules) blog_GetCategoryParentId(wrap *wrapper.Wrapper, id int) int {
  65. var parentId int
  66. err := wrap.DB.QueryRow(
  67. wrap.R.Context(),
  68. `SELECT
  69. parent.id
  70. FROM
  71. fave_blog_cats AS node,
  72. fave_blog_cats AS parent
  73. WHERE
  74. node.lft BETWEEN parent.lft AND parent.rgt AND
  75. node.id = ? AND
  76. parent.id <> ?
  77. ORDER BY
  78. parent.lft DESC
  79. LIMIT 1;`,
  80. id,
  81. id,
  82. ).Scan(
  83. &parentId,
  84. )
  85. if *wrap.LogCpError(&err) != nil {
  86. return 0
  87. }
  88. return parentId
  89. }