module_blog.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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: "Categories", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  40. {Name: "List of categories"},
  41. })
  42. content += builder.DataTable(
  43. wrap,
  44. "blog_cats",
  45. "id",
  46. "ASC",
  47. &[]builder.DataTableRow{
  48. {
  49. DBField: "id",
  50. // NameInTable: "id",
  51. },
  52. {
  53. DBField: "user",
  54. // NameInTable: "user",
  55. },
  56. {
  57. DBField: "name",
  58. NameInTable: "Category",
  59. CallBack: func(values *[]string) string {
  60. sub := strings.Repeat("— ", utils.StrToInt((*values)[4]))
  61. name := `<a href="/cp/` + wrap.CurrModule + `/categories-modify/` + (*values)[0] + `/">` + sub + html.EscapeString((*values)[2]) + `</a>`
  62. // alias := html.EscapeString((*values)[3])
  63. // return `<div>` + name + `</div><div><small>` + alias + `</small></div>`
  64. return `<div>` + name + `</div>`
  65. },
  66. },
  67. {
  68. DBField: "alias",
  69. // NameInTable: "Alias",
  70. },
  71. {
  72. DBField: "depth",
  73. // NameInTable: "depth",
  74. },
  75. },
  76. func(values *[]string) string {
  77. return builder.DataTableAction(&[]builder.DataTableActionRow{
  78. {
  79. Icon: assets.SysSvgIconEdit,
  80. Href: "/cp/" + wrap.CurrModule + "/categories-modify/" + (*values)[0] + "/",
  81. Hint: "Edit",
  82. },
  83. {
  84. Icon: assets.SysSvgIconRemove,
  85. Href: "javascript:fave.ActionDataTableDelete(this,'blog-categories-delete','" +
  86. (*values)[0] + "','Are you sure want to delete category?');",
  87. Hint: "Delete",
  88. Classes: "delete",
  89. },
  90. })
  91. },
  92. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  93. func() (int, error) {
  94. var num int
  95. var err error
  96. err = wrap.DB.QueryRow("SELECT COUNT(*) FROM `blog_cats`;").Scan(&num)
  97. return num, err
  98. },
  99. func(limit_offset int, pear_page int) (*sql.Rows, error) {
  100. return wrap.DB.Query(
  101. `SELECT
  102. node.id,
  103. node.user,
  104. node.name,
  105. node.alias,
  106. (COUNT(parent.id) - 1) AS depth
  107. FROM
  108. blog_cats AS node,
  109. blog_cats AS parent
  110. WHERE
  111. node.lft BETWEEN parent.lft AND parent.rgt
  112. GROUP BY
  113. node.id
  114. ORDER BY
  115. node.lft ASC
  116. LIMIT ?, ?;`,
  117. limit_offset,
  118. pear_page,
  119. )
  120. },
  121. )
  122. } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
  123. if wrap.CurrSubModule == "add" {
  124. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  125. {Name: "Add new post"},
  126. })
  127. } else {
  128. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  129. {Name: "Modify post"},
  130. })
  131. }
  132. //
  133. } else if wrap.CurrSubModule == "categories-add" || wrap.CurrSubModule == "categories-modify" {
  134. if wrap.CurrSubModule == "categories-add" {
  135. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  136. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  137. {Name: "Add new category"},
  138. })
  139. } else {
  140. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  141. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  142. {Name: "Modify category"},
  143. })
  144. }
  145. // ---
  146. btn_caption := "Add"
  147. if wrap.CurrSubModule == "categories-modify" {
  148. btn_caption = "Save"
  149. }
  150. // ---
  151. select_parent_options := ""
  152. rows, err := wrap.DB.Query(
  153. `SELECT
  154. node.id,
  155. node.user,
  156. node.name,
  157. node.alias,
  158. (COUNT(parent.id) - 1) AS depth
  159. FROM
  160. blog_cats AS node,
  161. blog_cats AS parent
  162. WHERE
  163. node.lft BETWEEN parent.lft AND parent.rgt
  164. GROUP BY
  165. node.id
  166. ORDER BY
  167. node.lft ASC
  168. ;`,
  169. )
  170. if err == nil {
  171. values := make([]string, 5)
  172. scan := make([]interface{}, len(values))
  173. for i := range values {
  174. scan[i] = &values[i]
  175. }
  176. for rows.Next() {
  177. err = rows.Scan(scan...)
  178. if err == nil {
  179. sub := strings.Repeat("&mdash; ", utils.StrToInt(string(values[4])))
  180. select_parent_options += `<option value="` + html.EscapeString(string(values[0])) + `">` + sub + html.EscapeString(string(values[2])) + `</option>`
  181. }
  182. }
  183. }
  184. // ---
  185. content += builder.DataForm(wrap, []builder.DataFormField{
  186. {
  187. Kind: builder.DFKHidden,
  188. Name: "action",
  189. Value: "blog-categories-modify",
  190. },
  191. {
  192. Kind: builder.DFKHidden,
  193. Name: "id",
  194. Value: "0",
  195. },
  196. {
  197. Kind: builder.DFKText,
  198. Caption: "Name",
  199. Name: "name",
  200. Value: "",
  201. },
  202. {
  203. Kind: builder.DFKText,
  204. Caption: "Alias",
  205. Name: "alias",
  206. Value: "",
  207. Hint: "Example: popular-posts",
  208. },
  209. {
  210. Kind: builder.DFKText,
  211. Caption: "Parent",
  212. Name: "parent",
  213. Value: "0",
  214. CallBack: func(field *builder.DataFormField) string {
  215. return `<div class="form-group last">
  216. <div class="row">
  217. <div class="col-md-3">
  218. <label for="lbl_parent">Parent</label>
  219. </div>
  220. <div class="col-md-9">
  221. <div>
  222. <select class="form-control" id="lbl_parent" name="parent">
  223. <option value="0">&mdash;</option>
  224. ` + select_parent_options + `
  225. </select>
  226. </div>
  227. </div>
  228. </div>
  229. </div>`
  230. },
  231. },
  232. {
  233. Kind: builder.DFKMessage,
  234. },
  235. {
  236. Kind: builder.DFKSubmit,
  237. Value: btn_caption,
  238. Target: "add-edit-button",
  239. },
  240. })
  241. if wrap.CurrSubModule == "categories-add" {
  242. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  243. } else {
  244. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  245. }
  246. // ---
  247. }
  248. return this.getSidebarModules(wrap), content, sidebar
  249. })
  250. }