module_blog.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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) blog_GetCategorySelectOptions(wrap *wrapper.Wrapper, id int, parentId int) string {
  13. result := ``
  14. rows, err := wrap.DB.Query(
  15. `SELECT
  16. node.id,
  17. node.user,
  18. node.name,
  19. node.alias,
  20. (COUNT(parent.id) - 1) AS depth
  21. FROM
  22. blog_cats AS node,
  23. blog_cats AS parent
  24. WHERE
  25. node.lft BETWEEN parent.lft AND parent.rgt
  26. GROUP BY
  27. node.id
  28. ORDER BY
  29. node.lft ASC
  30. ;`,
  31. )
  32. if err == nil {
  33. values := make([]string, 5)
  34. scan := make([]interface{}, len(values))
  35. for i := range values {
  36. scan[i] = &values[i]
  37. }
  38. idStr := utils.IntToStr(id)
  39. parentIdStr := utils.IntToStr(parentId)
  40. for rows.Next() {
  41. err = rows.Scan(scan...)
  42. if err == nil {
  43. disabled := ""
  44. if string(values[0]) == idStr {
  45. disabled = " disabled"
  46. }
  47. selected := ""
  48. if string(values[0]) == parentIdStr {
  49. selected = " selected"
  50. }
  51. sub := strings.Repeat("— ", utils.StrToInt(string(values[4])))
  52. result += `<option value="` + html.EscapeString(string(values[0])) + `"` + disabled + selected + `>` + sub + html.EscapeString(string(values[2])) + `</option>`
  53. }
  54. }
  55. }
  56. return result
  57. }
  58. func (this *Modules) blog_GetCategoryParentId(wrap *wrapper.Wrapper, id int) int {
  59. var parentId int
  60. _ = wrap.DB.QueryRow(`
  61. SELECT
  62. parent.id
  63. FROM
  64. blog_cats AS node,
  65. blog_cats AS parent
  66. WHERE
  67. node.lft BETWEEN parent.lft AND parent.rgt AND
  68. node.id = ? AND
  69. parent.id <> ?
  70. ORDER BY
  71. parent.lft DESC
  72. LIMIT 1;`,
  73. id,
  74. id,
  75. ).Scan(
  76. &parentId,
  77. )
  78. return parentId
  79. }
  80. func (this *Modules) RegisterModule_Blog() *Module {
  81. return this.newModule(MInfo{
  82. WantDB: true,
  83. Mount: "blog",
  84. Name: "Blog",
  85. Order: 1,
  86. System: false,
  87. Icon: assets.SysSvgIconList,
  88. Sub: &[]MISub{
  89. {Mount: "default", Name: "List of posts", Show: true, Icon: assets.SysSvgIconList},
  90. {Mount: "add", Name: "Add new post", Show: true, Icon: assets.SysSvgIconPlus},
  91. {Mount: "modify", Name: "Modify post", Show: false},
  92. {Sep: true, Show: true},
  93. {Mount: "categories", Name: "List of categories", Show: true, Icon: assets.SysSvgIconList},
  94. {Mount: "categories-add", Name: "Add new category", Show: true, Icon: assets.SysSvgIconPlus},
  95. {Mount: "categories-modify", Name: "Modify category", Show: false},
  96. },
  97. }, nil, func(wrap *wrapper.Wrapper) (string, string, string) {
  98. content := ""
  99. sidebar := ""
  100. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  101. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  102. {Name: "List of posts"},
  103. })
  104. } else if wrap.CurrSubModule == "categories" {
  105. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  106. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  107. {Name: "List of categories"},
  108. })
  109. content += builder.DataTable(
  110. wrap,
  111. "blog_cats",
  112. "id",
  113. "ASC",
  114. &[]builder.DataTableRow{
  115. {
  116. DBField: "id",
  117. },
  118. {
  119. DBField: "user",
  120. },
  121. {
  122. DBField: "name",
  123. NameInTable: "Category",
  124. CallBack: func(values *[]string) string {
  125. sub := strings.Repeat("&mdash; ", utils.StrToInt((*values)[4]))
  126. name := `<a href="/cp/` + wrap.CurrModule + `/categories-modify/` + (*values)[0] + `/">` + sub + html.EscapeString((*values)[2]) + `</a>`
  127. return `<div>` + name + `</div>`
  128. },
  129. },
  130. {
  131. DBField: "alias",
  132. },
  133. {
  134. DBField: "depth",
  135. },
  136. },
  137. func(values *[]string) string {
  138. return builder.DataTableAction(&[]builder.DataTableActionRow{
  139. {
  140. Icon: assets.SysSvgIconEdit,
  141. Href: "/cp/" + wrap.CurrModule + "/categories-modify/" + (*values)[0] + "/",
  142. Hint: "Edit",
  143. },
  144. {
  145. Icon: assets.SysSvgIconRemove,
  146. Href: "javascript:fave.ActionDataTableDelete(this,'blog-categories-delete','" +
  147. (*values)[0] + "','Are you sure want to delete category?');",
  148. Hint: "Delete",
  149. Classes: "delete",
  150. },
  151. })
  152. },
  153. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  154. func() (int, error) {
  155. var num int
  156. var err error
  157. err = wrap.DB.QueryRow("SELECT COUNT(*) FROM `blog_cats`;").Scan(&num)
  158. return num, err
  159. },
  160. func(limit_offset int, pear_page int) (*sql.Rows, error) {
  161. return wrap.DB.Query(
  162. `SELECT
  163. node.id,
  164. node.user,
  165. node.name,
  166. node.alias,
  167. (COUNT(parent.id) - 1) AS depth
  168. FROM
  169. blog_cats AS node,
  170. blog_cats AS parent
  171. WHERE
  172. node.lft BETWEEN parent.lft AND parent.rgt
  173. GROUP BY
  174. node.id
  175. ORDER BY
  176. node.lft ASC
  177. LIMIT ?, ?;`,
  178. limit_offset,
  179. pear_page,
  180. )
  181. },
  182. )
  183. } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
  184. if wrap.CurrSubModule == "add" {
  185. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  186. {Name: "Add new post"},
  187. })
  188. } else {
  189. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  190. {Name: "Modify post"},
  191. })
  192. }
  193. } else if wrap.CurrSubModule == "categories-add" || wrap.CurrSubModule == "categories-modify" {
  194. if wrap.CurrSubModule == "categories-add" {
  195. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  196. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  197. {Name: "Add new category"},
  198. })
  199. } else {
  200. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  201. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  202. {Name: "Modify category"},
  203. })
  204. }
  205. data := utils.MySql_blog_category{
  206. A_id: 0,
  207. A_user: 0,
  208. A_name: "",
  209. A_alias: "",
  210. A_lft: 0,
  211. A_rgt: 0,
  212. }
  213. if wrap.CurrSubModule == "categories-modify" {
  214. if len(wrap.UrlArgs) != 3 {
  215. return "", "", ""
  216. }
  217. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  218. return "", "", ""
  219. }
  220. err := wrap.DB.QueryRow(`
  221. SELECT
  222. id,
  223. user,
  224. name,
  225. alias,
  226. lft,
  227. rgt
  228. FROM
  229. blog_cats
  230. WHERE
  231. id = ?
  232. LIMIT 1;`,
  233. utils.StrToInt(wrap.UrlArgs[2]),
  234. ).Scan(
  235. &data.A_id,
  236. &data.A_user,
  237. &data.A_name,
  238. &data.A_alias,
  239. &data.A_lft,
  240. &data.A_rgt,
  241. )
  242. if err != nil {
  243. return "", "", ""
  244. }
  245. }
  246. btn_caption := "Add"
  247. if wrap.CurrSubModule == "categories-modify" {
  248. btn_caption = "Save"
  249. }
  250. parentId := 0
  251. if wrap.CurrSubModule == "categories-modify" {
  252. parentId = this.blog_GetCategoryParentId(wrap, data.A_id)
  253. }
  254. content += builder.DataForm(wrap, []builder.DataFormField{
  255. {
  256. Kind: builder.DFKHidden,
  257. Name: "action",
  258. Value: "blog-categories-modify",
  259. },
  260. {
  261. Kind: builder.DFKHidden,
  262. Name: "id",
  263. Value: utils.IntToStr(data.A_id),
  264. },
  265. {
  266. Kind: builder.DFKText,
  267. Caption: "Name",
  268. Name: "name",
  269. Value: data.A_name,
  270. },
  271. {
  272. Kind: builder.DFKText,
  273. Caption: "Alias",
  274. Name: "alias",
  275. Value: data.A_alias,
  276. Hint: "Example: popular-posts",
  277. },
  278. {
  279. Kind: builder.DFKText,
  280. Caption: "Parent",
  281. Name: "parent",
  282. Value: "0",
  283. CallBack: func(field *builder.DataFormField) string {
  284. return `<div class="form-group last">
  285. <div class="row">
  286. <div class="col-md-3">
  287. <label for="lbl_parent">Parent</label>
  288. </div>
  289. <div class="col-md-9">
  290. <div>
  291. <select class="form-control" id="lbl_parent" name="parent">
  292. <option value="0">&mdash;</option>
  293. ` + this.blog_GetCategorySelectOptions(wrap, data.A_id, parentId) + `
  294. </select>
  295. </div>
  296. </div>
  297. </div>
  298. </div>`
  299. },
  300. },
  301. {
  302. Kind: builder.DFKMessage,
  303. },
  304. {
  305. Kind: builder.DFKSubmit,
  306. Value: btn_caption,
  307. Target: "add-edit-button",
  308. },
  309. })
  310. if wrap.CurrSubModule == "categories-add" {
  311. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  312. } else {
  313. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  314. }
  315. }
  316. return this.getSidebarModules(wrap), content, sidebar
  317. })
  318. }