module_blog.go 6.7 KB

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