module_index.go 7.1 KB

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