module_blog.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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: `/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. btn_caption := "Add"
  236. if wrap.CurrSubModule == "modify" {
  237. btn_caption = "Save"
  238. }
  239. content += builder.DataForm(wrap, []builder.DataFormField{
  240. {
  241. Kind: builder.DFKHidden,
  242. Name: "action",
  243. Value: "blog-modify",
  244. },
  245. {
  246. Kind: builder.DFKHidden,
  247. Name: "id",
  248. Value: utils.IntToStr(data.A_id),
  249. },
  250. {
  251. Kind: builder.DFKText,
  252. Caption: "Post name",
  253. Name: "name",
  254. Value: data.A_name,
  255. },
  256. {
  257. Kind: builder.DFKText,
  258. Caption: "Post alias",
  259. Name: "alias",
  260. Value: data.A_alias,
  261. Hint: "Example: our-news",
  262. },
  263. {
  264. Kind: builder.DFKTextArea,
  265. Caption: "Post content",
  266. Name: "content",
  267. Value: data.A_content,
  268. Classes: "autosize",
  269. },
  270. {
  271. Kind: builder.DFKCheckBox,
  272. Caption: "Active",
  273. Name: "active",
  274. Value: utils.IntToStr(data.A_active),
  275. },
  276. {
  277. Kind: builder.DFKMessage,
  278. },
  279. {
  280. Kind: builder.DFKSubmit,
  281. Value: btn_caption,
  282. Target: "add-edit-button",
  283. },
  284. })
  285. if wrap.CurrSubModule == "add" {
  286. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  287. } else {
  288. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  289. }
  290. } else if wrap.CurrSubModule == "categories-add" || wrap.CurrSubModule == "categories-modify" {
  291. if wrap.CurrSubModule == "categories-add" {
  292. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  293. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  294. {Name: "Add new category"},
  295. })
  296. } else {
  297. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  298. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  299. {Name: "Modify category"},
  300. })
  301. }
  302. data := utils.MySql_blog_category{
  303. A_id: 0,
  304. A_user: 0,
  305. A_name: "",
  306. A_alias: "",
  307. A_lft: 0,
  308. A_rgt: 0,
  309. }
  310. if wrap.CurrSubModule == "categories-modify" {
  311. if len(wrap.UrlArgs) != 3 {
  312. return "", "", ""
  313. }
  314. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  315. return "", "", ""
  316. }
  317. err := wrap.DB.QueryRow(`
  318. SELECT
  319. id,
  320. user,
  321. name,
  322. alias,
  323. lft,
  324. rgt
  325. FROM
  326. blog_cats
  327. WHERE
  328. id = ?
  329. LIMIT 1;`,
  330. utils.StrToInt(wrap.UrlArgs[2]),
  331. ).Scan(
  332. &data.A_id,
  333. &data.A_user,
  334. &data.A_name,
  335. &data.A_alias,
  336. &data.A_lft,
  337. &data.A_rgt,
  338. )
  339. if err != nil {
  340. return "", "", ""
  341. }
  342. }
  343. btn_caption := "Add"
  344. if wrap.CurrSubModule == "categories-modify" {
  345. btn_caption = "Save"
  346. }
  347. parentId := 0
  348. if wrap.CurrSubModule == "categories-modify" {
  349. parentId = this.blog_GetCategoryParentId(wrap, data.A_id)
  350. }
  351. content += builder.DataForm(wrap, []builder.DataFormField{
  352. {
  353. Kind: builder.DFKHidden,
  354. Name: "action",
  355. Value: "blog-categories-modify",
  356. },
  357. {
  358. Kind: builder.DFKHidden,
  359. Name: "id",
  360. Value: utils.IntToStr(data.A_id),
  361. },
  362. {
  363. Kind: builder.DFKText,
  364. Caption: "Parent",
  365. Name: "parent",
  366. Value: "0",
  367. CallBack: func(field *builder.DataFormField) string {
  368. return `<div class="form-group n2">
  369. <div class="row">
  370. <div class="col-md-3">
  371. <label for="lbl_parent">Parent</label>
  372. </div>
  373. <div class="col-md-9">
  374. <div>
  375. <select class="form-control" id="lbl_parent" name="parent">
  376. <option value="0">&mdash;</option>
  377. ` + this.blog_GetCategorySelectOptions(wrap, data.A_id, parentId) + `
  378. </select>
  379. </div>
  380. </div>
  381. </div>
  382. </div>`
  383. },
  384. },
  385. {
  386. Kind: builder.DFKText,
  387. Caption: "Name",
  388. Name: "name",
  389. Value: data.A_name,
  390. },
  391. {
  392. Kind: builder.DFKText,
  393. Caption: "Alias",
  394. Name: "alias",
  395. Value: data.A_alias,
  396. Hint: "Example: popular-posts",
  397. },
  398. {
  399. Kind: builder.DFKMessage,
  400. },
  401. {
  402. Kind: builder.DFKSubmit,
  403. Value: btn_caption,
  404. Target: "add-edit-button",
  405. },
  406. })
  407. if wrap.CurrSubModule == "categories-add" {
  408. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  409. } else {
  410. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  411. }
  412. }
  413. return this.getSidebarModules(wrap), content, sidebar
  414. })
  415. }
  416. func (this *Modules) RegisterAction_BlogModify() *Action {
  417. return this.newAction(AInfo{
  418. WantDB: true,
  419. Mount: "blog-modify",
  420. WantAdmin: true,
  421. }, func(wrap *wrapper.Wrapper) {
  422. pf_id := wrap.R.FormValue("id")
  423. pf_name := wrap.R.FormValue("name")
  424. pf_alias := wrap.R.FormValue("alias")
  425. pf_content := wrap.R.FormValue("content")
  426. pf_active := wrap.R.FormValue("active")
  427. if pf_active == "" {
  428. pf_active = "0"
  429. }
  430. if !utils.IsNumeric(pf_id) {
  431. wrap.MsgError(`Inner system error`)
  432. return
  433. }
  434. if pf_name == "" {
  435. wrap.MsgError(`Please specify page name`)
  436. return
  437. }
  438. if pf_alias == "" {
  439. pf_alias = utils.GenerateSingleAlias(pf_name)
  440. }
  441. if !utils.IsValidSingleAlias(pf_alias) {
  442. wrap.MsgError(`Please specify correct post alias`)
  443. return
  444. }
  445. if pf_id == "0" {
  446. // Add new post
  447. _, err := wrap.DB.Exec(
  448. `INSERT INTO blog_posts SET
  449. user = ?,
  450. name = ?,
  451. alias = ?,
  452. content = ?,
  453. datetime = ?,
  454. active = ?
  455. ;`,
  456. wrap.User.A_id,
  457. pf_name,
  458. pf_alias,
  459. pf_content,
  460. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  461. pf_active,
  462. )
  463. if err != nil {
  464. wrap.MsgError(err.Error())
  465. return
  466. }
  467. wrap.Write(`window.location='/cp/blog/';`)
  468. } else {
  469. // Update post
  470. _, err := wrap.DB.Exec(
  471. `UPDATE blog_posts SET
  472. name = ?,
  473. alias = ?,
  474. content = ?,
  475. active = ?
  476. WHERE
  477. id = ?
  478. ;`,
  479. pf_name,
  480. pf_alias,
  481. pf_content,
  482. pf_active,
  483. utils.StrToInt(pf_id),
  484. )
  485. if err != nil {
  486. wrap.MsgError(err.Error())
  487. return
  488. }
  489. wrap.Write(`window.location='/cp/blog/modify/` + pf_id + `/';`)
  490. }
  491. })
  492. }
  493. func (this *Modules) RegisterAction_BlogDelete() *Action {
  494. return this.newAction(AInfo{
  495. WantDB: true,
  496. Mount: "blog-delete",
  497. WantAdmin: true,
  498. }, func(wrap *wrapper.Wrapper) {
  499. pf_id := wrap.R.FormValue("id")
  500. if !utils.IsNumeric(pf_id) {
  501. wrap.MsgError(`Inner system error`)
  502. return
  503. }
  504. // Start transaction with table lock
  505. _, err := wrap.DB.Exec("LOCK TABLES blog_posts WRITE, blog_cat_post_rel WRITE;")
  506. if err != nil {
  507. wrap.MsgError(err.Error())
  508. return
  509. }
  510. tx, err := wrap.DB.Begin()
  511. if err != nil {
  512. wrap.MsgError(err.Error())
  513. return
  514. }
  515. // Delete target post with category connection data
  516. if _, err = tx.Exec("DELETE FROM blog_cat_post_rel WHERE post_id = ?;", pf_id); err != nil {
  517. tx.Rollback()
  518. wrap.MsgError(err.Error())
  519. return
  520. }
  521. if _, err = tx.Exec("DELETE FROM blog_posts WHERE id = ?;", pf_id); err != nil {
  522. tx.Rollback()
  523. wrap.MsgError(err.Error())
  524. return
  525. }
  526. // Commit all changes and unlock table
  527. err = tx.Commit()
  528. if err != nil {
  529. wrap.MsgError(err.Error())
  530. return
  531. }
  532. _, err = wrap.DB.Exec("UNLOCK TABLES;")
  533. if err != nil {
  534. wrap.MsgError(err.Error())
  535. return
  536. }
  537. // Reload current page
  538. wrap.Write(`window.location.reload(false);`)
  539. })
  540. }