module_blog.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 / URL",
  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>/blog/` + 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: `/blog/` + (*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. data := utils.MySql_blog_posts{
  194. A_id: 0,
  195. A_user: 0,
  196. A_name: "",
  197. A_alias: "",
  198. A_content: "",
  199. A_datetime: 0,
  200. A_active: 0,
  201. }
  202. if wrap.CurrSubModule == "modify" {
  203. if len(wrap.UrlArgs) != 3 {
  204. return "", "", ""
  205. }
  206. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  207. return "", "", ""
  208. }
  209. err := wrap.DB.QueryRow(`
  210. SELECT
  211. id,
  212. user,
  213. name,
  214. alias,
  215. content,
  216. active
  217. FROM
  218. blog_posts
  219. WHERE
  220. id = ?
  221. LIMIT 1;`,
  222. utils.StrToInt(wrap.UrlArgs[2]),
  223. ).Scan(
  224. &data.A_id,
  225. &data.A_user,
  226. &data.A_name,
  227. &data.A_alias,
  228. &data.A_content,
  229. &data.A_active,
  230. )
  231. if err != nil {
  232. return "", "", ""
  233. }
  234. }
  235. // All post current categories
  236. var selids []int
  237. if data.A_id > 0 {
  238. rows, err := wrap.DB.Query("SELECT category_id FROM blog_cat_post_rel WHERE post_id = ?;", data.A_id)
  239. if err == nil {
  240. defer rows.Close()
  241. values := make([]int, 1)
  242. scan := make([]interface{}, len(values))
  243. for i := range values {
  244. scan[i] = &values[i]
  245. }
  246. for rows.Next() {
  247. err = rows.Scan(scan...)
  248. if err == nil {
  249. selids = append(selids, int(values[0]))
  250. }
  251. }
  252. }
  253. }
  254. btn_caption := "Add"
  255. if wrap.CurrSubModule == "modify" {
  256. btn_caption = "Save"
  257. }
  258. content += builder.DataForm(wrap, []builder.DataFormField{
  259. {
  260. Kind: builder.DFKHidden,
  261. Name: "action",
  262. Value: "blog-modify",
  263. },
  264. {
  265. Kind: builder.DFKHidden,
  266. Name: "id",
  267. Value: utils.IntToStr(data.A_id),
  268. },
  269. {
  270. Kind: builder.DFKText,
  271. Caption: "Post name",
  272. Name: "name",
  273. Value: data.A_name,
  274. },
  275. {
  276. Kind: builder.DFKText,
  277. Caption: "Post alias",
  278. Name: "alias",
  279. Value: data.A_alias,
  280. Hint: "Example: our-news",
  281. },
  282. {
  283. Kind: builder.DFKText,
  284. Caption: "Categories",
  285. Name: "cats",
  286. Value: "0",
  287. CallBack: func(field *builder.DataFormField) string {
  288. return `<div class="form-group n4">
  289. <div class="row">
  290. <div class="col-md-3">
  291. <label for="lbl_parent">Categories</label>
  292. </div>
  293. <div class="col-md-9">
  294. <div>
  295. <select class="form-control" id="lbl_cats" name="cats[]" multiple>
  296. <!--<option value=""></option>-->
  297. ` + this.blog_GetCategorySelectOptions(wrap, 0, 0, selids) + `
  298. </select>
  299. </div>
  300. </div>
  301. </div>
  302. </div>`
  303. },
  304. },
  305. {
  306. Kind: builder.DFKTextArea,
  307. Caption: "Post content",
  308. Name: "content",
  309. Value: data.A_content,
  310. Classes: "autosize",
  311. },
  312. {
  313. Kind: builder.DFKCheckBox,
  314. Caption: "Active",
  315. Name: "active",
  316. Value: utils.IntToStr(data.A_active),
  317. },
  318. {
  319. Kind: builder.DFKMessage,
  320. },
  321. {
  322. Kind: builder.DFKSubmit,
  323. Value: btn_caption,
  324. Target: "add-edit-button",
  325. },
  326. })
  327. if wrap.CurrSubModule == "add" {
  328. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  329. } else {
  330. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  331. }
  332. } else if wrap.CurrSubModule == "categories-add" || wrap.CurrSubModule == "categories-modify" {
  333. if wrap.CurrSubModule == "categories-add" {
  334. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  335. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  336. {Name: "Add new category"},
  337. })
  338. } else {
  339. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  340. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  341. {Name: "Modify category"},
  342. })
  343. }
  344. data := utils.MySql_blog_category{
  345. A_id: 0,
  346. A_user: 0,
  347. A_name: "",
  348. A_alias: "",
  349. A_lft: 0,
  350. A_rgt: 0,
  351. }
  352. if wrap.CurrSubModule == "categories-modify" {
  353. if len(wrap.UrlArgs) != 3 {
  354. return "", "", ""
  355. }
  356. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  357. return "", "", ""
  358. }
  359. err := wrap.DB.QueryRow(`
  360. SELECT
  361. id,
  362. user,
  363. name,
  364. alias,
  365. lft,
  366. rgt
  367. FROM
  368. blog_cats
  369. WHERE
  370. id = ?
  371. LIMIT 1;`,
  372. utils.StrToInt(wrap.UrlArgs[2]),
  373. ).Scan(
  374. &data.A_id,
  375. &data.A_user,
  376. &data.A_name,
  377. &data.A_alias,
  378. &data.A_lft,
  379. &data.A_rgt,
  380. )
  381. if err != nil {
  382. return "", "", ""
  383. }
  384. }
  385. btn_caption := "Add"
  386. if wrap.CurrSubModule == "categories-modify" {
  387. btn_caption = "Save"
  388. }
  389. parentId := 0
  390. if wrap.CurrSubModule == "categories-modify" {
  391. parentId = this.blog_GetCategoryParentId(wrap, data.A_id)
  392. }
  393. content += builder.DataForm(wrap, []builder.DataFormField{
  394. {
  395. Kind: builder.DFKHidden,
  396. Name: "action",
  397. Value: "blog-categories-modify",
  398. },
  399. {
  400. Kind: builder.DFKHidden,
  401. Name: "id",
  402. Value: utils.IntToStr(data.A_id),
  403. },
  404. {
  405. Kind: builder.DFKText,
  406. Caption: "Parent",
  407. Name: "parent",
  408. Value: "0",
  409. CallBack: func(field *builder.DataFormField) string {
  410. return `<div class="form-group n2">
  411. <div class="row">
  412. <div class="col-md-3">
  413. <label for="lbl_parent">Parent</label>
  414. </div>
  415. <div class="col-md-9">
  416. <div>
  417. <select class="form-control" id="lbl_parent" name="parent">
  418. <option value="0">&mdash;</option>
  419. ` + this.blog_GetCategorySelectOptions(wrap, data.A_id, parentId, []int{}) + `
  420. </select>
  421. </div>
  422. </div>
  423. </div>
  424. </div>`
  425. },
  426. },
  427. {
  428. Kind: builder.DFKText,
  429. Caption: "Name",
  430. Name: "name",
  431. Value: data.A_name,
  432. },
  433. {
  434. Kind: builder.DFKText,
  435. Caption: "Alias",
  436. Name: "alias",
  437. Value: data.A_alias,
  438. Hint: "Example: popular-posts",
  439. },
  440. {
  441. Kind: builder.DFKMessage,
  442. },
  443. {
  444. Kind: builder.DFKSubmit,
  445. Value: btn_caption,
  446. Target: "add-edit-button",
  447. },
  448. })
  449. if wrap.CurrSubModule == "categories-add" {
  450. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  451. } else {
  452. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  453. }
  454. }
  455. return this.getSidebarModules(wrap), content, sidebar
  456. })
  457. }