modules.go 7.7 KB

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