module_blog.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package modules
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "html"
  6. "strings"
  7. "golang-fave/assets"
  8. "golang-fave/consts"
  9. "golang-fave/engine/builder"
  10. "golang-fave/engine/wrapper"
  11. "golang-fave/utils"
  12. )
  13. func (this *Modules) RegisterModule_Blog() *Module {
  14. return this.newModule(MInfo{
  15. WantDB: true,
  16. Mount: "blog",
  17. Name: "Blog",
  18. Order: 1,
  19. System: false,
  20. Icon: assets.SysSvgIconList,
  21. Sub: &[]MISub{
  22. {Mount: "default", Name: "List of posts", Show: true, Icon: assets.SysSvgIconList},
  23. {Mount: "add", Name: "Add new post", Show: true, Icon: assets.SysSvgIconPlus},
  24. {Mount: "modify", Name: "Modify post", Show: false},
  25. {Sep: true, Show: true},
  26. {Mount: "categories", Name: "List of categories", Show: true, Icon: assets.SysSvgIconList},
  27. {Mount: "categories-add", Name: "Add new category", Show: true, Icon: assets.SysSvgIconPlus},
  28. {Mount: "categories-modify", Name: "Modify category", Show: false},
  29. },
  30. }, nil, func(wrap *wrapper.Wrapper) (string, string, string) {
  31. content := ""
  32. sidebar := ""
  33. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  34. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  35. {Name: "List of posts"},
  36. })
  37. content += builder.DataTable(
  38. wrap,
  39. "blog_posts",
  40. "id",
  41. "DESC",
  42. &[]builder.DataTableRow{
  43. {
  44. DBField: "id",
  45. },
  46. {
  47. DBField: "name",
  48. NameInTable: "Post / Alias",
  49. CallBack: func(values *[]string) string {
  50. name := `<a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
  51. alias := html.EscapeString((*values)[2])
  52. return `<div>` + name + `</div><div><small>` + alias + `</small></div>`
  53. },
  54. },
  55. {
  56. DBField: "alias",
  57. },
  58. {
  59. DBField: "datetime",
  60. DBExp: "UNIX_TIMESTAMP(`datetime`)",
  61. NameInTable: "Date / Time",
  62. Classes: "d-none d-md-table-cell",
  63. CallBack: func(values *[]string) string {
  64. t := int64(utils.StrToInt((*values)[3]))
  65. return `<div>` + utils.UnixTimestampToFormat(t, "02.01.2006") + `</div>` +
  66. `<div><small>` + utils.UnixTimestampToFormat(t, "15:04:05") + `</small></div>`
  67. },
  68. },
  69. {
  70. DBField: "active",
  71. NameInTable: "Active",
  72. Classes: "d-none d-sm-table-cell",
  73. CallBack: func(values *[]string) string {
  74. return builder.CheckBox(utils.StrToInt((*values)[4]))
  75. },
  76. },
  77. },
  78. func(values *[]string) string {
  79. return builder.DataTableAction(&[]builder.DataTableActionRow{
  80. {
  81. Icon: assets.SysSvgIconView,
  82. Href: (*values)[2],
  83. Hint: "View",
  84. Target: "_blank",
  85. },
  86. {
  87. Icon: assets.SysSvgIconEdit,
  88. Href: "/cp/" + wrap.CurrModule + "/modify/" + (*values)[0] + "/",
  89. Hint: "Edit",
  90. },
  91. {
  92. Icon: assets.SysSvgIconRemove,
  93. Href: "javascript:fave.ActionDataTableDelete(this,'blog-delete','" +
  94. (*values)[0] + "','Are you sure want to delete post?');",
  95. Hint: "Delete",
  96. Classes: "delete",
  97. },
  98. })
  99. },
  100. "/cp/"+wrap.CurrModule+"/",
  101. nil,
  102. nil,
  103. true,
  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. },
  119. {
  120. DBField: "user",
  121. },
  122. {
  123. DBField: "name",
  124. NameInTable: "Category",
  125. CallBack: func(values *[]string) string {
  126. depth := utils.StrToInt((*values)[4]) - 1
  127. if depth < 0 {
  128. depth = 0
  129. }
  130. sub := strings.Repeat("&mdash; ", depth)
  131. name := `<a href="/cp/` + wrap.CurrModule + `/categories-modify/` + (*values)[0] + `/">` + sub + html.EscapeString((*values)[2]) + `</a>`
  132. return `<div>` + name + `</div>`
  133. },
  134. },
  135. {
  136. DBField: "alias",
  137. },
  138. {
  139. DBField: "depth",
  140. },
  141. },
  142. func(values *[]string) string {
  143. return builder.DataTableAction(&[]builder.DataTableActionRow{
  144. {
  145. Icon: assets.SysSvgIconEdit,
  146. Href: "/cp/" + wrap.CurrModule + "/categories-modify/" + (*values)[0] + "/",
  147. Hint: "Edit",
  148. },
  149. {
  150. Icon: assets.SysSvgIconRemove,
  151. Href: "javascript:fave.ActionDataTableDelete(this,'blog-categories-delete','" +
  152. (*values)[0] + "','Are you sure want to delete category?');",
  153. Hint: "Delete",
  154. Classes: "delete",
  155. },
  156. })
  157. },
  158. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  159. nil,
  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 AND
  173. node.id > 1
  174. GROUP BY
  175. node.id
  176. ORDER BY
  177. node.lft ASC
  178. ;`,
  179. )
  180. },
  181. false,
  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: "Parent",
  268. Name: "parent",
  269. Value: "0",
  270. CallBack: func(field *builder.DataFormField) string {
  271. return `<div class="form-group n2">
  272. <div class="row">
  273. <div class="col-md-3">
  274. <label for="lbl_parent">Parent</label>
  275. </div>
  276. <div class="col-md-9">
  277. <div>
  278. <select class="form-control" id="lbl_parent" name="parent">
  279. <option value="0">&mdash;</option>
  280. ` + this.blog_GetCategorySelectOptions(wrap, data.A_id, parentId) + `
  281. </select>
  282. </div>
  283. </div>
  284. </div>
  285. </div>`
  286. },
  287. },
  288. {
  289. Kind: builder.DFKText,
  290. Caption: "Name",
  291. Name: "name",
  292. Value: data.A_name,
  293. },
  294. {
  295. Kind: builder.DFKText,
  296. Caption: "Alias",
  297. Name: "alias",
  298. Value: data.A_alias,
  299. Hint: "Example: popular-posts",
  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. }