modules.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package modules
  2. import (
  3. "html"
  4. "html/template"
  5. "net/http"
  6. "reflect"
  7. "sort"
  8. "strings"
  9. "golang-fave/assets"
  10. "golang-fave/consts"
  11. "golang-fave/engine/wrapper"
  12. "golang-fave/utils"
  13. )
  14. type MISub struct {
  15. Mount string
  16. Name string
  17. Icon string
  18. }
  19. type MInfo struct {
  20. Id string
  21. WantDB bool
  22. Mount string
  23. Name string
  24. Order int
  25. System bool
  26. Icon string
  27. Sub *[]MISub
  28. }
  29. type Module struct {
  30. Info MInfo
  31. Front func(wrap *wrapper.Wrapper)
  32. Back func(wrap *wrapper.Wrapper) (string, string, string)
  33. }
  34. type AInfo struct {
  35. Id string
  36. WantDB bool
  37. Mount string
  38. WantUser bool
  39. }
  40. type Action struct {
  41. Info AInfo
  42. Act func(wrap *wrapper.Wrapper)
  43. }
  44. type Modules struct {
  45. mods map[string]*Module
  46. acts map[string]*Action
  47. }
  48. func (this *Modules) load() {
  49. t := reflect.TypeOf(this)
  50. for i := 0; i < t.NumMethod(); i++ {
  51. m := t.Method(i)
  52. if strings.HasPrefix(m.Name, "XXX") {
  53. continue
  54. }
  55. if strings.HasPrefix(m.Name, "RegisterModule_") {
  56. id := m.Name[15:]
  57. if _, ok := reflect.TypeOf(this).MethodByName("RegisterModule_" + id); ok {
  58. result := reflect.ValueOf(this).MethodByName("RegisterModule_" + id).Call([]reflect.Value{})
  59. if len(result) >= 1 {
  60. mod := result[0].Interface().(*Module)
  61. mod.Info.Id = id
  62. this.mods[mod.Info.Mount] = mod
  63. }
  64. }
  65. }
  66. if strings.HasPrefix(m.Name, "RegisterAction_") {
  67. id := m.Name[15:]
  68. if _, ok := reflect.TypeOf(this).MethodByName("RegisterAction_" + id); ok {
  69. result := reflect.ValueOf(this).MethodByName("RegisterAction_" + id).Call([]reflect.Value{})
  70. if len(result) >= 1 {
  71. act := result[0].Interface().(*Action)
  72. act.Info.Id = id
  73. this.acts[act.Info.Mount] = act
  74. }
  75. }
  76. }
  77. }
  78. }
  79. func (this *Modules) newModule(info MInfo, ff func(wrap *wrapper.Wrapper), bf func(wrap *wrapper.Wrapper) (string, string, string)) *Module {
  80. return &Module{Info: info, Front: ff, Back: bf}
  81. }
  82. func (this *Modules) newAction(info AInfo, af func(wrap *wrapper.Wrapper)) *Action {
  83. return &Action{Info: info, Act: af}
  84. }
  85. func (this *Modules) getCurrentModule(wrap *wrapper.Wrapper, backend bool) (*Module, string) {
  86. var mod *Module = nil
  87. var modCurr string = ""
  88. // Some module
  89. if len(wrap.UrlArgs) >= 1 {
  90. if m, ok := this.mods[wrap.UrlArgs[0]]; ok {
  91. if (!backend && m.Front != nil) || (backend && m.Back != nil) {
  92. mod = m
  93. modCurr = wrap.UrlArgs[0]
  94. }
  95. }
  96. }
  97. // Default module
  98. if mod == nil {
  99. if m, ok := this.mods["index"]; ok {
  100. mod = m
  101. modCurr = "index"
  102. }
  103. }
  104. return mod, modCurr
  105. }
  106. func (this *Modules) getModulesList(wrap *wrapper.Wrapper, sys bool, all bool) []*MInfo {
  107. list := make([]*MInfo, 0)
  108. for _, mod := range this.mods {
  109. if mod.Back != nil {
  110. if mod.Info.System == sys || all {
  111. list = append(list, &mod.Info)
  112. }
  113. }
  114. }
  115. sort.Slice(list, func(i, j int) bool {
  116. return list[i].Order < list[j].Order
  117. })
  118. return list
  119. }
  120. func (this *Modules) getSidebarModuleSubMenu(wrap *wrapper.Wrapper, mod *MInfo) string {
  121. html := ``
  122. if mod.Sub != nil {
  123. for _, item := range *mod.Sub {
  124. class := ""
  125. if (item.Mount == "default" && len(wrap.UrlArgs) <= 1) || (len(wrap.UrlArgs) >= 2 && item.Mount == wrap.UrlArgs[1]) {
  126. class = " active"
  127. }
  128. icon := item.Icon
  129. if icon == "" {
  130. icon = assets.SysSvgIconGear
  131. }
  132. href := "/cp/" + mod.Mount + "/" + item.Mount + "/"
  133. if mod.Mount == "index" && item.Mount == "default" {
  134. href = "/cp/"
  135. } else if item.Mount == "default" {
  136. href = "/cp/" + mod.Mount + "/"
  137. }
  138. html += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + item.Name + `</a></li>`
  139. }
  140. if html != "" {
  141. html = `<ul class="nav flex-column">` + html + `</ul>`
  142. }
  143. }
  144. return html
  145. }
  146. func (this *Modules) getNavMenuModules(wrap *wrapper.Wrapper, sys bool) string {
  147. html := ``
  148. list := this.getModulesList(wrap, sys, false)
  149. for _, mod := range list {
  150. class := ""
  151. if mod.Mount == wrap.CurrModule {
  152. class = " active"
  153. }
  154. html += `<a class="dropdown-item` + class + `" href="/cp/` + mod.Mount + `/">` + mod.Name + `</a>`
  155. }
  156. return html
  157. }
  158. func (this *Modules) getSidebarModules(wrap *wrapper.Wrapper) string {
  159. html_def := ""
  160. html_sys := ""
  161. list := this.getModulesList(wrap, false, true)
  162. for _, mod := range list {
  163. class := ""
  164. submenu := ""
  165. if mod.Mount == wrap.CurrModule {
  166. class = " active"
  167. submenu = this.getSidebarModuleSubMenu(wrap, mod)
  168. }
  169. icon := mod.Icon
  170. if icon == "" {
  171. icon = assets.SysSvgIconGear
  172. }
  173. href := "/cp/" + mod.Mount + "/"
  174. if mod.Mount == "index" {
  175. href = "/cp/"
  176. }
  177. if !mod.System {
  178. html_def += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
  179. } else {
  180. html_sys += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
  181. }
  182. }
  183. if html_def != "" {
  184. html_def = `<ul class="nav flex-column">` + html_def + `</ul>`
  185. }
  186. if html_sys != "" {
  187. html_sys = `<ul class="nav flex-column">` + html_sys + `</ul>`
  188. }
  189. if html_def != "" && html_sys != "" {
  190. html_sys = `<div class="dropdown-divider"></div>` + html_sys
  191. }
  192. return html_def + html_sys
  193. }
  194. func (this *Modules) getBreadCrumbs(wrap *wrapper.Wrapper, data *[]consts.BreadCrumb) string {
  195. res := `<nav aria-label="breadcrumb">`
  196. res += `<ol class="breadcrumb">`
  197. res += `<li class="breadcrumb-item"><a href="/cp/` + this.mods[wrap.CurrModule].Info.Mount + `/">` + html.EscapeString(this.mods[wrap.CurrModule].Info.Name) + `</a></li>`
  198. for _, item := range *data {
  199. if item.Link == "" {
  200. res += `<li class="breadcrumb-item active" aria-current="page">` + html.EscapeString(item.Name) + `</li>`
  201. } else {
  202. res += `<li class="breadcrumb-item"><a href="` + item.Link + `">` + html.EscapeString(item.Name) + `</a></li>`
  203. }
  204. }
  205. res += `</ol>`
  206. res += `</nav>`
  207. return res
  208. }
  209. func New() *Modules {
  210. m := Modules{
  211. mods: map[string]*Module{},
  212. acts: map[string]*Action{},
  213. }
  214. m.load()
  215. return &m
  216. }
  217. func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
  218. if wrap.R.Method == "POST" {
  219. if err := wrap.R.ParseForm(); err == nil {
  220. name := wrap.R.FormValue("action")
  221. if name != "" {
  222. wrap.W.WriteHeader(http.StatusOK)
  223. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  224. wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  225. if act, ok := this.acts[name]; ok {
  226. if act.Info.WantDB {
  227. err := wrap.UseDatabase()
  228. if err != nil {
  229. wrap.MsgError(err.Error())
  230. return true
  231. }
  232. defer wrap.DB.Close()
  233. }
  234. if act.Info.WantUser {
  235. if !wrap.LoadSessionUser() {
  236. wrap.MsgError(`You must be loginned to run this action`)
  237. return true
  238. }
  239. }
  240. act.Act(wrap)
  241. return true
  242. } else {
  243. wrap.MsgError(`This action is not implemented`)
  244. return true
  245. }
  246. }
  247. }
  248. }
  249. return false
  250. }
  251. func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
  252. mod, cm := this.getCurrentModule(wrap, false)
  253. if mod != nil {
  254. wrap.CurrModule = cm
  255. if mod.Front != nil {
  256. if mod.Info.WantDB {
  257. err := wrap.UseDatabase()
  258. if err != nil {
  259. utils.SystemErrorPageEngine(wrap.W, err)
  260. return true
  261. }
  262. defer wrap.DB.Close()
  263. }
  264. mod.Front(wrap)
  265. return true
  266. }
  267. }
  268. return false
  269. }
  270. func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
  271. mod, cm := this.getCurrentModule(wrap, true)
  272. if mod != nil {
  273. wrap.CurrModule = cm
  274. if len(wrap.UrlArgs) >= 2 && wrap.UrlArgs[1] != "" {
  275. wrap.CurrSubModule = wrap.UrlArgs[1]
  276. }
  277. if mod.Back != nil {
  278. sidebar_left, content, sidebar_right := mod.Back(wrap)
  279. body_class := "cp"
  280. if sidebar_left != "" {
  281. body_class = body_class + " cp-sidebar-left"
  282. }
  283. if content == "" {
  284. body_class = body_class + " cp-404"
  285. content = "Panel 404"
  286. }
  287. if sidebar_right != "" {
  288. body_class = body_class + " cp-sidebar-right"
  289. }
  290. wrap.RenderBackEnd(assets.TmplCpBase, consts.TmplDataCpBase{
  291. Title: "Fave " + consts.ServerVersion,
  292. BodyClasses: body_class,
  293. UserId: wrap.User.A_id,
  294. UserFirstName: wrap.User.A_first_name,
  295. UserLastName: wrap.User.A_last_name,
  296. UserEmail: wrap.User.A_email,
  297. UserPassword: "",
  298. UserAvatarLink: "https://s.gravatar.com/avatar/" + utils.GetMd5(wrap.User.A_email) + "?s=80&r=g",
  299. NavBarModules: template.HTML(this.getNavMenuModules(wrap, false)),
  300. NavBarModulesSys: template.HTML(this.getNavMenuModules(wrap, true)),
  301. ModuleCurrentAlias: wrap.CurrModule,
  302. SidebarLeft: template.HTML(sidebar_left),
  303. Content: template.HTML(content),
  304. SidebarRight: template.HTML(sidebar_right),
  305. })
  306. return true
  307. }
  308. }
  309. return false
  310. }