module_users.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package modules
  2. import (
  3. "html"
  4. "golang-fave/engine/assets"
  5. "golang-fave/engine/builder"
  6. "golang-fave/engine/consts"
  7. "golang-fave/engine/utils"
  8. "golang-fave/engine/wrapper"
  9. )
  10. func (this *Modules) RegisterModule_Users() *Module {
  11. return this.newModule(MInfo{
  12. Mount: "users",
  13. Name: "Users",
  14. Order: 800,
  15. System: true,
  16. Icon: assets.SysSvgIconUser,
  17. Sub: &[]MISub{
  18. {Mount: "default", Name: "List of users", Show: true, Icon: assets.SysSvgIconList},
  19. {Mount: "add", Name: "Add new user", Show: true, Icon: assets.SysSvgIconPlus},
  20. {Mount: "modify", Name: "Modify user", Show: false},
  21. },
  22. }, nil, func(wrap *wrapper.Wrapper) (string, string, string) {
  23. content := ""
  24. sidebar := ""
  25. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  26. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  27. {Name: "List of users"},
  28. })
  29. content += builder.DataTable(
  30. wrap,
  31. "fave_users",
  32. "id",
  33. "DESC",
  34. &[]builder.DataTableRow{
  35. {
  36. DBField: "id",
  37. },
  38. {
  39. DBField: "email",
  40. NameInTable: "Email / Name",
  41. CallBack: func(values *[]string) string {
  42. email := `<a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
  43. name := html.EscapeString((*values)[2])
  44. if name != "" && (*values)[3] != "" {
  45. name += ` ` + (*values)[3]
  46. }
  47. if name != "" {
  48. name = `<div><small>` + name + `</small></div>`
  49. }
  50. return `<div>` + email + `</div>` + name
  51. },
  52. },
  53. {
  54. DBField: "first_name",
  55. },
  56. {
  57. DBField: "last_name",
  58. },
  59. {
  60. DBField: "active",
  61. NameInTable: "Active",
  62. Classes: "d-none d-sm-table-cell",
  63. CallBack: func(values *[]string) string {
  64. return builder.CheckBox(utils.StrToInt((*values)[4]))
  65. },
  66. },
  67. {
  68. DBField: "admin",
  69. NameInTable: "Admin",
  70. Classes: "d-none d-md-table-cell",
  71. CallBack: func(values *[]string) string {
  72. return builder.CheckBox(utils.StrToInt((*values)[5]))
  73. },
  74. },
  75. },
  76. func(values *[]string) string {
  77. return builder.DataTableAction(&[]builder.DataTableActionRow{
  78. {
  79. Icon: assets.SysSvgIconEdit,
  80. Href: "/cp/" + wrap.CurrModule + "/modify/" + (*values)[0] + "/",
  81. Hint: "Edit",
  82. },
  83. {
  84. Icon: assets.SysSvgIconRemove,
  85. Href: "javascript:fave.ActionDataTableDelete(this,'users-delete','" +
  86. (*values)[0] + "','Are you sure want to delete user?');",
  87. Hint: "Delete",
  88. Classes: "delete",
  89. },
  90. })
  91. },
  92. "/cp/"+wrap.CurrModule+"/",
  93. nil,
  94. nil,
  95. true,
  96. )
  97. } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
  98. if wrap.CurrSubModule == "add" {
  99. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  100. {Name: "Add new user"},
  101. })
  102. } else {
  103. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  104. {Name: "Modify user"},
  105. })
  106. }
  107. data := utils.MySql_user{
  108. A_id: 0,
  109. A_first_name: "",
  110. A_last_name: "",
  111. A_email: "",
  112. A_admin: 0,
  113. A_active: 0,
  114. }
  115. if wrap.CurrSubModule == "modify" {
  116. if len(wrap.UrlArgs) != 3 {
  117. return "", "", ""
  118. }
  119. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  120. return "", "", ""
  121. }
  122. err := wrap.DB.QueryRow(
  123. wrap.R.Context(),
  124. `SELECT
  125. id,
  126. first_name,
  127. last_name,
  128. email,
  129. admin,
  130. active
  131. FROM
  132. fave_users
  133. WHERE
  134. id = ?
  135. LIMIT 1;`,
  136. utils.StrToInt(wrap.UrlArgs[2]),
  137. ).Scan(
  138. &data.A_id,
  139. &data.A_first_name,
  140. &data.A_last_name,
  141. &data.A_email,
  142. &data.A_admin,
  143. &data.A_active,
  144. )
  145. if *wrap.LogCpError(&err) != nil {
  146. return "", "", ""
  147. }
  148. }
  149. pass_req := true
  150. pass_hint := ""
  151. if wrap.CurrSubModule == "modify" {
  152. pass_req = false
  153. pass_hint = "Leave the field blank to not change the password"
  154. }
  155. btn_caption := "Add"
  156. if wrap.CurrSubModule == "modify" {
  157. btn_caption = "Save"
  158. }
  159. content += builder.DataForm(wrap, []builder.DataFormField{
  160. {
  161. Kind: builder.DFKHidden,
  162. Name: "action",
  163. Value: "users-modify",
  164. },
  165. {
  166. Kind: builder.DFKHidden,
  167. Name: "id",
  168. Value: utils.IntToStr(data.A_id),
  169. },
  170. {
  171. Kind: builder.DFKText,
  172. Caption: "First name",
  173. Name: "first_name",
  174. Value: data.A_first_name,
  175. },
  176. {
  177. Kind: builder.DFKText,
  178. Caption: "Last name",
  179. Name: "last_name",
  180. Value: data.A_last_name,
  181. },
  182. {
  183. Kind: builder.DFKEmail,
  184. Caption: "Email",
  185. Name: "email",
  186. Value: data.A_email,
  187. Required: true,
  188. },
  189. {
  190. Kind: builder.DFKPassword,
  191. Caption: "Password",
  192. Name: "password",
  193. Required: pass_req,
  194. Hint: pass_hint,
  195. },
  196. {
  197. Kind: builder.DFKCheckBox,
  198. Caption: "Active",
  199. Name: "active",
  200. Value: utils.IntToStr(data.A_active),
  201. },
  202. {
  203. Kind: builder.DFKCheckBox,
  204. Caption: "Admin",
  205. Name: "admin",
  206. Value: utils.IntToStr(data.A_admin),
  207. },
  208. {
  209. Kind: builder.DFKSubmit,
  210. Value: btn_caption,
  211. Target: "add-edit-button",
  212. },
  213. })
  214. if wrap.CurrSubModule == "add" {
  215. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  216. } else {
  217. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  218. }
  219. }
  220. return this.getSidebarModules(wrap), content, sidebar
  221. })
  222. }