module_blog.go 6.7 KB

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