module_blog_categories.go 2.0 KB

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