module_blog.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. //
  105. } else if wrap.CurrSubModule == "categories" {
  106. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  107. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  108. {Name: "List of categories"},
  109. })
  110. content += builder.DataTable(
  111. wrap,
  112. "blog_cats",
  113. "id",
  114. "ASC",
  115. &[]builder.DataTableRow{
  116. {
  117. DBField: "id",
  118. // NameInTable: "id",
  119. },
  120. {
  121. DBField: "user",
  122. // NameInTable: "user",
  123. },
  124. {
  125. DBField: "name",
  126. NameInTable: "Category",
  127. CallBack: func(values *[]string) string {
  128. sub := strings.Repeat("&mdash; ", utils.StrToInt((*values)[4]))
  129. name := `<a href="/cp/` + wrap.CurrModule + `/categories-modify/` + (*values)[0] + `/">` + sub + html.EscapeString((*values)[2]) + `</a>`
  130. // alias := html.EscapeString((*values)[3])
  131. // return `<div>` + name + `</div><div><small>` + alias + `</small></div>`
  132. return `<div>` + name + `</div>`
  133. },
  134. },
  135. {
  136. DBField: "alias",
  137. // NameInTable: "Alias",
  138. },
  139. {
  140. DBField: "depth",
  141. // NameInTable: "depth",
  142. },
  143. },
  144. func(values *[]string) string {
  145. return builder.DataTableAction(&[]builder.DataTableActionRow{
  146. {
  147. Icon: assets.SysSvgIconEdit,
  148. Href: "/cp/" + wrap.CurrModule + "/categories-modify/" + (*values)[0] + "/",
  149. Hint: "Edit",
  150. },
  151. {
  152. Icon: assets.SysSvgIconRemove,
  153. Href: "javascript:fave.ActionDataTableDelete(this,'blog-categories-delete','" +
  154. (*values)[0] + "','Are you sure want to delete category?');",
  155. Hint: "Delete",
  156. Classes: "delete",
  157. },
  158. })
  159. },
  160. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  161. func() (int, error) {
  162. var num int
  163. var err error
  164. err = wrap.DB.QueryRow("SELECT COUNT(*) FROM `blog_cats`;").Scan(&num)
  165. return num, err
  166. },
  167. func(limit_offset int, pear_page int) (*sql.Rows, error) {
  168. return wrap.DB.Query(
  169. `SELECT
  170. node.id,
  171. node.user,
  172. node.name,
  173. node.alias,
  174. (COUNT(parent.id) - 1) AS depth
  175. FROM
  176. blog_cats AS node,
  177. blog_cats AS parent
  178. WHERE
  179. node.lft BETWEEN parent.lft AND parent.rgt
  180. GROUP BY
  181. node.id
  182. ORDER BY
  183. node.lft ASC
  184. LIMIT ?, ?;`,
  185. limit_offset,
  186. pear_page,
  187. )
  188. },
  189. )
  190. } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
  191. if wrap.CurrSubModule == "add" {
  192. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  193. {Name: "Add new post"},
  194. })
  195. } else {
  196. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  197. {Name: "Modify post"},
  198. })
  199. }
  200. //
  201. } else if wrap.CurrSubModule == "categories-add" || wrap.CurrSubModule == "categories-modify" {
  202. if wrap.CurrSubModule == "categories-add" {
  203. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  204. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  205. {Name: "Add new category"},
  206. })
  207. } else {
  208. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  209. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  210. {Name: "Modify category"},
  211. })
  212. }
  213. data := utils.MySql_blog_category{
  214. A_id: 0,
  215. A_user: 0,
  216. A_name: "",
  217. A_alias: "",
  218. A_lft: 0,
  219. A_rgt: 0,
  220. }
  221. if wrap.CurrSubModule == "categories-modify" {
  222. if len(wrap.UrlArgs) != 3 {
  223. return "", "", ""
  224. }
  225. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  226. return "", "", ""
  227. }
  228. err := wrap.DB.QueryRow(`
  229. SELECT
  230. id,
  231. user,
  232. name,
  233. alias,
  234. lft,
  235. rgt
  236. FROM
  237. blog_cats
  238. WHERE
  239. id = ?
  240. LIMIT 1;`,
  241. utils.StrToInt(wrap.UrlArgs[2]),
  242. ).Scan(
  243. &data.A_id,
  244. &data.A_user,
  245. &data.A_name,
  246. &data.A_alias,
  247. &data.A_lft,
  248. &data.A_rgt,
  249. )
  250. if err != nil {
  251. return "", "", ""
  252. }
  253. }
  254. // ---
  255. btn_caption := "Add"
  256. if wrap.CurrSubModule == "categories-modify" {
  257. btn_caption = "Save"
  258. }
  259. // ---
  260. // select_parent_options := this.blog_GetCategorySelectOptions(wrap, "")
  261. // ---
  262. parentId := 0
  263. if wrap.CurrSubModule == "categories-modify" {
  264. parentId = this.blog_GetCategoryParentId(wrap, data.A_id)
  265. }
  266. content += builder.DataForm(wrap, []builder.DataFormField{
  267. {
  268. Kind: builder.DFKHidden,
  269. Name: "action",
  270. Value: "blog-categories-modify",
  271. },
  272. {
  273. Kind: builder.DFKHidden,
  274. Name: "id",
  275. Value: utils.IntToStr(data.A_id),
  276. },
  277. {
  278. Kind: builder.DFKText,
  279. Caption: "Name",
  280. Name: "name",
  281. Value: data.A_name,
  282. },
  283. {
  284. Kind: builder.DFKText,
  285. Caption: "Alias",
  286. Name: "alias",
  287. Value: data.A_alias,
  288. Hint: "Example: popular-posts",
  289. },
  290. {
  291. Kind: builder.DFKText,
  292. Caption: "Parent",
  293. Name: "parent",
  294. Value: "0",
  295. CallBack: func(field *builder.DataFormField) string {
  296. return `<div class="form-group last">
  297. <div class="row">
  298. <div class="col-md-3">
  299. <label for="lbl_parent">Parent</label>
  300. </div>
  301. <div class="col-md-9">
  302. <div>
  303. <select class="form-control" id="lbl_parent" name="parent">
  304. <option value="0">&mdash;</option>
  305. ` + this.blog_GetCategorySelectOptions(wrap, data.A_id, parentId) + `
  306. </select>
  307. </div>
  308. </div>
  309. </div>
  310. </div>`
  311. },
  312. },
  313. {
  314. Kind: builder.DFKMessage,
  315. },
  316. {
  317. Kind: builder.DFKSubmit,
  318. Value: btn_caption,
  319. Target: "add-edit-button",
  320. },
  321. })
  322. if wrap.CurrSubModule == "categories-add" {
  323. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  324. } else {
  325. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  326. }
  327. // ---
  328. }
  329. return this.getSidebarModules(wrap), content, sidebar
  330. })
  331. }