module_blog.go 6.8 KB

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