module_index.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. package modules
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "html"
  6. "net/http"
  7. "golang-fave/assets"
  8. "golang-fave/consts"
  9. "golang-fave/engine/builder"
  10. "golang-fave/engine/fetdata"
  11. "golang-fave/engine/wrapper"
  12. "golang-fave/utils"
  13. )
  14. func (this *Modules) RegisterModule_Index() *Module {
  15. return this.newModule(MInfo{
  16. WantDB: true,
  17. Mount: "index",
  18. Name: "Pages",
  19. Order: 0,
  20. Icon: assets.SysSvgIconPage,
  21. Sub: &[]MISub{
  22. {Mount: "default", Name: "List of pages", Show: true, Icon: assets.SysSvgIconList},
  23. {Mount: "add", Name: "Add new page", Show: true, Icon: assets.SysSvgIconPlus},
  24. {Mount: "modify", Name: "Modify page", Show: false},
  25. },
  26. }, func(wrap *wrapper.Wrapper) {
  27. // Front-end
  28. row := &utils.MySql_page{}
  29. err := wrap.DB.QueryRow(`
  30. SELECT
  31. id,
  32. user,
  33. name,
  34. alias,
  35. content,
  36. meta_title,
  37. meta_keywords,
  38. meta_description,
  39. UNIX_TIMESTAMP(datetime) as datetime,
  40. active
  41. FROM
  42. pages
  43. WHERE
  44. active = 1 and
  45. alias = ?
  46. LIMIT 1;`,
  47. wrap.R.URL.Path,
  48. ).Scan(
  49. &row.A_id,
  50. &row.A_user,
  51. &row.A_name,
  52. &row.A_alias,
  53. &row.A_content,
  54. &row.A_meta_title,
  55. &row.A_meta_keywords,
  56. &row.A_meta_description,
  57. &row.A_datetime,
  58. &row.A_active,
  59. )
  60. if err != nil && err != sql.ErrNoRows {
  61. // System error 500
  62. utils.SystemErrorPageEngine(wrap.W, err)
  63. return
  64. } else if err == sql.ErrNoRows {
  65. // User error 404 page
  66. wrap.W.WriteHeader(http.StatusNotFound)
  67. wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true))
  68. return
  69. }
  70. // Replace title with page name
  71. if row.A_meta_title == "" {
  72. row.A_meta_title = row.A_name
  73. }
  74. // Which template
  75. tmpl_name := "index"
  76. if wrap.R.URL.Path != "/" {
  77. tmpl_name = "page"
  78. }
  79. // Render template
  80. wrap.RenderFrontEnd(tmpl_name, fetdata.New(wrap, row, false))
  81. }, func(wrap *wrapper.Wrapper) (string, string, string) {
  82. content := ""
  83. sidebar := ""
  84. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  85. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  86. {Name: "List of pages"},
  87. })
  88. content += builder.DataTable(
  89. wrap,
  90. "pages",
  91. "id",
  92. "DESC",
  93. &[]builder.DataTableRow{
  94. {
  95. DBField: "id",
  96. },
  97. {
  98. DBField: "name",
  99. NameInTable: "Page / Alias",
  100. CallBack: func(values *[]string) string {
  101. name := `<a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
  102. alias := html.EscapeString((*values)[2])
  103. return `<div>` + name + `</div><div><small>` + alias + `</small></div>`
  104. },
  105. },
  106. {
  107. DBField: "alias",
  108. },
  109. {
  110. DBField: "datetime",
  111. DBExp: "UNIX_TIMESTAMP(`datetime`)",
  112. NameInTable: "Date / Time",
  113. Classes: "d-none d-md-table-cell",
  114. CallBack: func(values *[]string) string {
  115. t := int64(utils.StrToInt((*values)[3]))
  116. return `<div>` + utils.UnixTimestampToFormat(t, "02.01.2006") + `</div>` +
  117. `<div><small>` + utils.UnixTimestampToFormat(t, "15:04:05") + `</small></div>`
  118. },
  119. },
  120. {
  121. DBField: "active",
  122. NameInTable: "Active",
  123. Classes: "d-none d-sm-table-cell",
  124. CallBack: func(values *[]string) string {
  125. return builder.CheckBox(utils.StrToInt((*values)[4]))
  126. },
  127. },
  128. },
  129. func(values *[]string) string {
  130. return builder.DataTableAction(&[]builder.DataTableActionRow{
  131. {
  132. Icon: assets.SysSvgIconView,
  133. Href: (*values)[2],
  134. Hint: "View",
  135. Target: "_blank",
  136. },
  137. {
  138. Icon: assets.SysSvgIconEdit,
  139. Href: "/cp/" + wrap.CurrModule + "/modify/" + (*values)[0] + "/",
  140. Hint: "Edit",
  141. },
  142. {
  143. Icon: assets.SysSvgIconRemove,
  144. Href: "javascript:fave.ActionDataTableDelete(this,'index-delete','" +
  145. (*values)[0] + "','Are you sure want to delete page?');",
  146. Hint: "Delete",
  147. Classes: "delete",
  148. },
  149. })
  150. },
  151. "/cp/"+wrap.CurrModule+"/",
  152. nil,
  153. nil,
  154. true,
  155. )
  156. } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
  157. if wrap.CurrSubModule == "add" {
  158. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  159. {Name: "Add new page"},
  160. })
  161. } else {
  162. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  163. {Name: "Modify page"},
  164. })
  165. }
  166. data := utils.MySql_page{
  167. A_id: 0,
  168. A_user: 0,
  169. A_name: "",
  170. A_alias: "",
  171. A_content: "",
  172. A_meta_title: "",
  173. A_meta_keywords: "",
  174. A_meta_description: "",
  175. A_datetime: 0,
  176. A_active: 0,
  177. }
  178. if wrap.CurrSubModule == "modify" {
  179. if len(wrap.UrlArgs) != 3 {
  180. return "", "", ""
  181. }
  182. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  183. return "", "", ""
  184. }
  185. err := wrap.DB.QueryRow(`
  186. SELECT
  187. id,
  188. user,
  189. name,
  190. alias,
  191. content,
  192. meta_title,
  193. meta_keywords,
  194. meta_description,
  195. active
  196. FROM
  197. pages
  198. WHERE
  199. id = ?
  200. LIMIT 1;`,
  201. utils.StrToInt(wrap.UrlArgs[2]),
  202. ).Scan(
  203. &data.A_id,
  204. &data.A_user,
  205. &data.A_name,
  206. &data.A_alias,
  207. &data.A_content,
  208. &data.A_meta_title,
  209. &data.A_meta_keywords,
  210. &data.A_meta_description,
  211. &data.A_active,
  212. )
  213. if err != nil {
  214. return "", "", ""
  215. }
  216. }
  217. btn_caption := "Add"
  218. if wrap.CurrSubModule == "modify" {
  219. btn_caption = "Save"
  220. }
  221. content += builder.DataForm(wrap, []builder.DataFormField{
  222. {
  223. Kind: builder.DFKHidden,
  224. Name: "action",
  225. Value: "index-modify",
  226. },
  227. {
  228. Kind: builder.DFKHidden,
  229. Name: "id",
  230. Value: utils.IntToStr(data.A_id),
  231. },
  232. {
  233. Kind: builder.DFKText,
  234. Caption: "Page name",
  235. Name: "name",
  236. Value: data.A_name,
  237. },
  238. {
  239. Kind: builder.DFKText,
  240. Caption: "Page alias",
  241. Name: "alias",
  242. Value: data.A_alias,
  243. Hint: "Example: /about-us/ or /about-us.html",
  244. },
  245. {
  246. Kind: builder.DFKTextArea,
  247. Caption: "Page content",
  248. Name: "content",
  249. Value: data.A_content,
  250. Classes: "autosize",
  251. },
  252. {
  253. Kind: builder.DFKText,
  254. Caption: "Meta title",
  255. Name: "meta_title",
  256. Value: data.A_meta_title,
  257. },
  258. {
  259. Kind: builder.DFKText,
  260. Caption: "Meta keywords",
  261. Name: "meta_keywords",
  262. Value: data.A_meta_keywords,
  263. },
  264. {
  265. Kind: builder.DFKTextArea,
  266. Caption: "Meta description",
  267. Name: "meta_description",
  268. Value: data.A_meta_description,
  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. }
  291. return this.getSidebarModules(wrap), content, sidebar
  292. })
  293. }
  294. func (this *Modules) RegisterAction_IndexModify() *Action {
  295. return this.newAction(AInfo{
  296. WantDB: true,
  297. Mount: "index-modify",
  298. WantAdmin: true,
  299. }, func(wrap *wrapper.Wrapper) {
  300. pf_id := wrap.R.FormValue("id")
  301. pf_name := wrap.R.FormValue("name")
  302. pf_alias := wrap.R.FormValue("alias")
  303. pf_content := wrap.R.FormValue("content")
  304. pf_meta_title := wrap.R.FormValue("meta_title")
  305. pf_meta_keywords := wrap.R.FormValue("meta_keywords")
  306. pf_meta_description := wrap.R.FormValue("meta_description")
  307. pf_active := wrap.R.FormValue("active")
  308. if pf_active == "" {
  309. pf_active = "0"
  310. }
  311. if !utils.IsNumeric(pf_id) {
  312. wrap.MsgError(`Inner system error`)
  313. return
  314. }
  315. if pf_name == "" {
  316. wrap.MsgError(`Please specify page name`)
  317. return
  318. }
  319. if pf_alias == "" {
  320. pf_alias = utils.GenerateAlias(pf_name)
  321. }
  322. if !utils.IsValidAlias(pf_alias) {
  323. wrap.MsgError(`Please specify correct page alias`)
  324. return
  325. }
  326. if pf_id == "0" {
  327. // Add new page
  328. _, err := wrap.DB.Exec(
  329. `INSERT INTO pages SET
  330. user = ?,
  331. name = ?,
  332. alias = ?,
  333. content = ?,
  334. meta_title = ?,
  335. meta_keywords = ?,
  336. meta_description = ?,
  337. datetime = ?,
  338. active = ?
  339. ;`,
  340. wrap.User.A_id,
  341. pf_name,
  342. pf_alias,
  343. pf_content,
  344. pf_meta_title,
  345. pf_meta_keywords,
  346. pf_meta_description,
  347. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  348. pf_active,
  349. )
  350. if err != nil {
  351. wrap.MsgError(err.Error())
  352. return
  353. }
  354. wrap.Write(`window.location='/cp/';`)
  355. } else {
  356. // Update page
  357. _, err := wrap.DB.Exec(
  358. `UPDATE pages SET
  359. name = ?,
  360. alias = ?,
  361. content = ?,
  362. meta_title = ?,
  363. meta_keywords = ?,
  364. meta_description = ?,
  365. active = ?
  366. WHERE
  367. id = ?
  368. ;`,
  369. pf_name,
  370. pf_alias,
  371. pf_content,
  372. pf_meta_title,
  373. pf_meta_keywords,
  374. pf_meta_description,
  375. pf_active,
  376. utils.StrToInt(pf_id),
  377. )
  378. if err != nil {
  379. wrap.MsgError(err.Error())
  380. return
  381. }
  382. wrap.Write(`window.location='/cp/index/modify/` + pf_id + `/';`)
  383. }
  384. })
  385. }
  386. func (this *Modules) RegisterAction_IndexDelete() *Action {
  387. return this.newAction(AInfo{
  388. WantDB: true,
  389. Mount: "index-delete",
  390. WantAdmin: true,
  391. }, func(wrap *wrapper.Wrapper) {
  392. pf_id := wrap.R.FormValue("id")
  393. if !utils.IsNumeric(pf_id) {
  394. wrap.MsgError(`Inner system error`)
  395. return
  396. }
  397. // Delete page
  398. _, err := wrap.DB.Exec(
  399. `DELETE FROM pages WHERE id = ?;`,
  400. utils.StrToInt(pf_id),
  401. )
  402. if err != nil {
  403. wrap.MsgError(err.Error())
  404. return
  405. }
  406. // Reload current page
  407. wrap.Write(`window.location.reload(false);`)
  408. })
  409. }