module_blog.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package modules
  2. import (
  3. "database/sql"
  4. "html"
  5. "strings"
  6. "golang-fave/assets"
  7. "golang-fave/consts"
  8. "golang-fave/engine/builder"
  9. "golang-fave/engine/wrapper"
  10. "golang-fave/utils"
  11. )
  12. func (this *Modules) RegisterModule_Blog() *Module {
  13. return this.newModule(MInfo{
  14. WantDB: true,
  15. Mount: "blog",
  16. Name: "Blog",
  17. Order: 1,
  18. System: false,
  19. Icon: assets.SysSvgIconList,
  20. Sub: &[]MISub{
  21. {Mount: "default", Name: "List of posts", Show: true, Icon: assets.SysSvgIconList},
  22. {Mount: "add", Name: "Add new post", Show: true, Icon: assets.SysSvgIconPlus},
  23. {Mount: "modify", Name: "Modify post", Show: false},
  24. {Sep: true, Show: true},
  25. {Mount: "categories", Name: "List of categories", Show: true, Icon: assets.SysSvgIconList},
  26. {Mount: "categories-add", Name: "Add new category", Show: true, Icon: assets.SysSvgIconPlus},
  27. {Mount: "categories-modify", Name: "Modify category", Show: false},
  28. },
  29. }, nil, func(wrap *wrapper.Wrapper) (string, string, string) {
  30. content := ""
  31. sidebar := ""
  32. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  33. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  34. {Name: "List of posts"},
  35. })
  36. //
  37. } else if wrap.CurrSubModule == "categories" {
  38. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  39. {Name: "List of categories"},
  40. })
  41. content += builder.DataTable(
  42. wrap,
  43. "blog_cats",
  44. "id",
  45. "ASC",
  46. &[]builder.DataTableRow{
  47. {
  48. DBField: "id",
  49. // NameInTable: "id",
  50. },
  51. {
  52. DBField: "user",
  53. // NameInTable: "user",
  54. },
  55. {
  56. DBField: "name",
  57. NameInTable: "Name",
  58. CallBack: func(values *[]string) string {
  59. sub := strings.Repeat("— ", utils.StrToInt((*values)[4]))
  60. name := `<a href="/cp/` + wrap.CurrModule + `/categories-modify/` + (*values)[0] + `/">` + sub + html.EscapeString((*values)[2]) + `</a>`
  61. // alias := html.EscapeString((*values)[3])
  62. // return `<div>` + name + `</div><div><small>` + alias + `</small></div>`
  63. return `<div>` + name + `</div>`
  64. },
  65. },
  66. {
  67. DBField: "alias",
  68. // NameInTable: "Alias",
  69. },
  70. {
  71. DBField: "depth",
  72. // NameInTable: "depth",
  73. },
  74. },
  75. func(values *[]string) string {
  76. return builder.DataTableAction(&[]builder.DataTableActionRow{
  77. {
  78. Icon: assets.SysSvgIconEdit,
  79. Href: "/cp/" + wrap.CurrModule + "/categories-modify/" + (*values)[0] + "/",
  80. Hint: "Edit",
  81. },
  82. {
  83. Icon: assets.SysSvgIconRemove,
  84. Href: "javascript:fave.ActionDataTableDelete(this,'blog-categories-delete','" +
  85. (*values)[0] + "','Are you sure want to delete category?');",
  86. Hint: "Delete",
  87. Classes: "delete",
  88. },
  89. })
  90. },
  91. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  92. func() (int, error) {
  93. var num int
  94. var err error
  95. err = wrap.DB.QueryRow("SELECT COUNT(*) FROM `blog_cats`;").Scan(&num)
  96. return num, err
  97. },
  98. func(limit_offset int, pear_page int) (*sql.Rows, error) {
  99. return wrap.DB.Query(
  100. `SELECT
  101. node.id,
  102. node.user,
  103. node.name,
  104. node.alias,
  105. (COUNT(parent.id) - 1) AS depth
  106. FROM
  107. blog_cats AS node,
  108. blog_cats AS parent
  109. WHERE
  110. node.lft BETWEEN parent.lft AND parent.rgt
  111. GROUP BY
  112. node.id
  113. ORDER BY
  114. node.lft ASC
  115. LIMIT ?, ?;`,
  116. limit_offset,
  117. pear_page,
  118. )
  119. },
  120. )
  121. } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
  122. if wrap.CurrSubModule == "add" {
  123. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  124. {Name: "Add new post"},
  125. })
  126. } else {
  127. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  128. {Name: "Modify post"},
  129. })
  130. }
  131. //
  132. } else if wrap.CurrSubModule == "categories-add" || wrap.CurrSubModule == "categories-modify" {
  133. if wrap.CurrSubModule == "categories-add" {
  134. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  135. {Name: "Add new category"},
  136. })
  137. } else {
  138. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  139. {Name: "Modify category"},
  140. })
  141. }
  142. //
  143. }
  144. return this.getSidebarModules(wrap), content, sidebar
  145. })
  146. }