module_index.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package modules
  2. import (
  3. "html"
  4. "net/http"
  5. "golang-fave/assets"
  6. "golang-fave/consts"
  7. "golang-fave/engine/builder"
  8. "golang-fave/engine/fetdata"
  9. "golang-fave/engine/wrapper"
  10. "golang-fave/utils"
  11. )
  12. func (this *Modules) RegisterModule_Index() *Module {
  13. return this.newModule(MInfo{
  14. WantDB: true,
  15. Mount: "index",
  16. Name: "Pages",
  17. Order: 0,
  18. Icon: assets.SysSvgIconPage,
  19. Sub: &[]MISub{
  20. {Mount: "default", Name: "List of pages", Show: true, Icon: assets.SysSvgIconList},
  21. {Mount: "add", Name: "Add new page", Show: true, Icon: assets.SysSvgIconPlus},
  22. {Mount: "modify", Name: "Modify page", Show: false},
  23. },
  24. }, func(wrap *wrapper.Wrapper) {
  25. // Front-end
  26. row := &utils.MySql_page{}
  27. rou := &utils.MySql_user{}
  28. err := wrap.DB.QueryRow(`
  29. SELECT
  30. pages.id,
  31. pages.user,
  32. pages.name,
  33. pages.alias,
  34. pages.content,
  35. pages.meta_title,
  36. pages.meta_keywords,
  37. pages.meta_description,
  38. UNIX_TIMESTAMP(pages.datetime) as datetime,
  39. pages.active,
  40. users.id,
  41. users.first_name,
  42. users.last_name,
  43. users.email,
  44. users.admin,
  45. users.active
  46. FROM
  47. pages
  48. LEFT JOIN users ON users.id = pages.user
  49. WHERE
  50. pages.active = 1 and
  51. pages.alias = ?
  52. LIMIT 1;`,
  53. wrap.R.URL.Path,
  54. ).Scan(
  55. &row.A_id,
  56. &row.A_user,
  57. &row.A_name,
  58. &row.A_alias,
  59. &row.A_content,
  60. &row.A_meta_title,
  61. &row.A_meta_keywords,
  62. &row.A_meta_description,
  63. &row.A_datetime,
  64. &row.A_active,
  65. &rou.A_id,
  66. &rou.A_first_name,
  67. &rou.A_last_name,
  68. &rou.A_email,
  69. &rou.A_admin,
  70. &rou.A_active,
  71. )
  72. if err != nil && err != wrapper.ErrNoRows {
  73. // System error 500
  74. wrap.LogCpError(&err)
  75. utils.SystemErrorPageEngine(wrap.W, err)
  76. return
  77. } else if err == wrapper.ErrNoRows {
  78. // User error 404 page
  79. wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
  80. return
  81. }
  82. // Which template
  83. tname := "index"
  84. if wrap.R.URL.Path != "/" {
  85. tname = "page"
  86. }
  87. // Render template
  88. wrap.RenderFrontEnd(tname, fetdata.New(wrap, false, row, rou), http.StatusOK)
  89. }, func(wrap *wrapper.Wrapper) (string, string, string) {
  90. content := ""
  91. sidebar := ""
  92. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  93. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  94. {Name: "List of pages"},
  95. })
  96. content += builder.DataTable(
  97. wrap,
  98. "pages",
  99. "id",
  100. "DESC",
  101. &[]builder.DataTableRow{
  102. {
  103. DBField: "id",
  104. },
  105. {
  106. DBField: "name",
  107. NameInTable: "Page / URL",
  108. CallBack: func(values *[]string) string {
  109. name := `<a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
  110. alias := html.EscapeString((*values)[2])
  111. return `<div>` + name + `</div><div><small>` + alias + `</small></div>`
  112. },
  113. },
  114. {
  115. DBField: "alias",
  116. },
  117. {
  118. DBField: "datetime",
  119. DBExp: "UNIX_TIMESTAMP(`datetime`)",
  120. NameInTable: "Date / Time",
  121. Classes: "d-none d-md-table-cell",
  122. CallBack: func(values *[]string) string {
  123. t := int64(utils.StrToInt((*values)[3]))
  124. return `<div>` + utils.UnixTimestampToFormat(t, "02.01.2006") + `</div>` +
  125. `<div><small>` + utils.UnixTimestampToFormat(t, "15:04:05") + `</small></div>`
  126. },
  127. },
  128. {
  129. DBField: "active",
  130. NameInTable: "Active",
  131. Classes: "d-none d-sm-table-cell",
  132. CallBack: func(values *[]string) string {
  133. return builder.CheckBox(utils.StrToInt((*values)[4]))
  134. },
  135. },
  136. },
  137. func(values *[]string) string {
  138. return builder.DataTableAction(&[]builder.DataTableActionRow{
  139. {
  140. Icon: assets.SysSvgIconView,
  141. Href: (*values)[2],
  142. Hint: "View",
  143. Target: "_blank",
  144. },
  145. {
  146. Icon: assets.SysSvgIconEdit,
  147. Href: "/cp/" + wrap.CurrModule + "/modify/" + (*values)[0] + "/",
  148. Hint: "Edit",
  149. },
  150. {
  151. Icon: assets.SysSvgIconRemove,
  152. Href: "javascript:fave.ActionDataTableDelete(this,'index-delete','" +
  153. (*values)[0] + "','Are you sure want to delete page?');",
  154. Hint: "Delete",
  155. Classes: "delete",
  156. },
  157. })
  158. },
  159. "/cp/"+wrap.CurrModule+"/",
  160. nil,
  161. nil,
  162. true,
  163. )
  164. } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
  165. if wrap.CurrSubModule == "add" {
  166. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  167. {Name: "Add new page"},
  168. })
  169. } else {
  170. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  171. {Name: "Modify page"},
  172. })
  173. }
  174. data := utils.MySql_page{
  175. A_id: 0,
  176. A_user: 0,
  177. A_name: "",
  178. A_alias: "",
  179. A_content: "",
  180. A_meta_title: "",
  181. A_meta_keywords: "",
  182. A_meta_description: "",
  183. A_datetime: 0,
  184. A_active: 0,
  185. }
  186. if wrap.CurrSubModule == "modify" {
  187. if len(wrap.UrlArgs) != 3 {
  188. return "", "", ""
  189. }
  190. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  191. return "", "", ""
  192. }
  193. err := wrap.DB.QueryRow(`
  194. SELECT
  195. id,
  196. user,
  197. name,
  198. alias,
  199. content,
  200. meta_title,
  201. meta_keywords,
  202. meta_description,
  203. active
  204. FROM
  205. pages
  206. WHERE
  207. id = ?
  208. LIMIT 1;`,
  209. utils.StrToInt(wrap.UrlArgs[2]),
  210. ).Scan(
  211. &data.A_id,
  212. &data.A_user,
  213. &data.A_name,
  214. &data.A_alias,
  215. &data.A_content,
  216. &data.A_meta_title,
  217. &data.A_meta_keywords,
  218. &data.A_meta_description,
  219. &data.A_active,
  220. )
  221. if *wrap.LogCpError(&err) != nil {
  222. return "", "", ""
  223. }
  224. }
  225. btn_caption := "Add"
  226. if wrap.CurrSubModule == "modify" {
  227. btn_caption = "Save"
  228. }
  229. content += builder.DataForm(wrap, []builder.DataFormField{
  230. {
  231. Kind: builder.DFKHidden,
  232. Name: "action",
  233. Value: "index-modify",
  234. },
  235. {
  236. Kind: builder.DFKHidden,
  237. Name: "id",
  238. Value: utils.IntToStr(data.A_id),
  239. },
  240. {
  241. Kind: builder.DFKText,
  242. Caption: "Page name",
  243. Name: "name",
  244. Value: data.A_name,
  245. Required: true,
  246. Min: "1",
  247. Max: "255",
  248. },
  249. {
  250. Kind: builder.DFKText,
  251. Caption: "Page alias",
  252. Name: "alias",
  253. Value: data.A_alias,
  254. Hint: "Example: /about-us/ or /about-us.html",
  255. Max: "255",
  256. },
  257. {
  258. Kind: builder.DFKTextArea,
  259. Caption: "Page content",
  260. Name: "content",
  261. Value: data.A_content,
  262. Classes: "wysiwyg",
  263. },
  264. {
  265. Kind: builder.DFKText,
  266. Caption: "Meta title",
  267. Name: "meta_title",
  268. Value: data.A_meta_title,
  269. Max: "255",
  270. },
  271. {
  272. Kind: builder.DFKText,
  273. Caption: "Meta keywords",
  274. Name: "meta_keywords",
  275. Value: data.A_meta_keywords,
  276. Max: "255",
  277. },
  278. {
  279. Kind: builder.DFKTextArea,
  280. Caption: "Meta description",
  281. Name: "meta_description",
  282. Value: data.A_meta_description,
  283. Max: "510",
  284. },
  285. {
  286. Kind: builder.DFKCheckBox,
  287. Caption: "Active",
  288. Name: "active",
  289. Value: utils.IntToStr(data.A_active),
  290. },
  291. {
  292. Kind: builder.DFKSubmit,
  293. Value: btn_caption,
  294. Target: "add-edit-button",
  295. },
  296. })
  297. if wrap.CurrSubModule == "add" {
  298. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  299. } else {
  300. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  301. }
  302. }
  303. return this.getSidebarModules(wrap), content, sidebar
  304. })
  305. }