modules.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 := `<ul class="nav flex-column">`
  157. list := this.getModulesList(wrap, false, true)
  158. for _, mod := range list {
  159. class := ""
  160. submenu := ""
  161. if mod.Mount == wrap.CurrModule {
  162. class = " active"
  163. submenu = this.getSidebarModuleSubMenu(wrap, mod)
  164. }
  165. icon := mod.Icon
  166. if icon == "" {
  167. icon = assets.SysSvgIconGear
  168. }
  169. href := "/cp/" + mod.Mount + "/"
  170. if mod.Mount == "index" {
  171. href = "/cp/"
  172. }
  173. html += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
  174. }
  175. html += `</ul>`
  176. return html
  177. }
  178. func New() *Modules {
  179. m := Modules{
  180. mods: map[string]*Module{},
  181. acts: map[string]*Action{},
  182. }
  183. m.load()
  184. return &m
  185. }
  186. func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
  187. if wrap.R.Method == "POST" {
  188. if err := wrap.R.ParseForm(); err == nil {
  189. name := wrap.R.FormValue("action")
  190. if name != "" {
  191. wrap.W.WriteHeader(http.StatusOK)
  192. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  193. wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  194. if act, ok := this.acts[name]; ok {
  195. if act.Info.WantDB {
  196. err := wrap.UseDatabase()
  197. if err != nil {
  198. wrap.MsgError(err.Error())
  199. return true
  200. }
  201. defer wrap.DB.Close()
  202. }
  203. if act.Info.WantUser {
  204. if !wrap.LoadSessionUser() {
  205. wrap.MsgError(`You must be loginned to run this action`)
  206. return true
  207. }
  208. }
  209. act.Act(wrap)
  210. return true
  211. } else {
  212. wrap.MsgError(`This action is not implemented`)
  213. return true
  214. }
  215. }
  216. }
  217. }
  218. return false
  219. }
  220. func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
  221. mod, cm := this.getCurrentModule(wrap, false)
  222. if mod != nil {
  223. wrap.CurrModule = cm
  224. if mod.Front != nil {
  225. if mod.Info.WantDB {
  226. err := wrap.UseDatabase()
  227. if err != nil {
  228. utils.SystemErrorPageEngine(wrap.W, err)
  229. return true
  230. }
  231. defer wrap.DB.Close()
  232. }
  233. mod.Front(wrap)
  234. return true
  235. }
  236. }
  237. return false
  238. }
  239. func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
  240. mod, cm := this.getCurrentModule(wrap, true)
  241. if mod != nil {
  242. wrap.CurrModule = cm
  243. if mod.Back != nil {
  244. sidebar_left, content, sidebar_right := mod.Back(wrap)
  245. body_class := "cp"
  246. if sidebar_left != "" {
  247. body_class = body_class + " cp-sidebar-left"
  248. }
  249. if content == "" {
  250. body_class = body_class + " cp-404"
  251. content = "Panel 404"
  252. }
  253. if sidebar_right != "" {
  254. body_class = body_class + " cp-sidebar-right"
  255. }
  256. wrap.RenderBackEnd(assets.TmplCpBase, consts.TmplDataCpBase{
  257. Title: "Fave " + consts.ServerVersion,
  258. BodyClasses: body_class,
  259. UserId: wrap.User.A_id,
  260. UserFirstName: wrap.User.A_first_name,
  261. UserLastName: wrap.User.A_last_name,
  262. UserEmail: wrap.User.A_email,
  263. UserPassword: "",
  264. UserAvatarLink: "https://s.gravatar.com/avatar/" + utils.GetMd5(wrap.User.A_email) + "?s=80&r=g",
  265. NavBarModules: template.HTML(this.getNavMenuModules(wrap, false)),
  266. NavBarModulesSys: template.HTML(this.getNavMenuModules(wrap, true)),
  267. ModuleCurrentAlias: wrap.CurrModule,
  268. SidebarLeft: template.HTML(sidebar_left),
  269. Content: template.HTML(content),
  270. SidebarRight: template.HTML(sidebar_right),
  271. })
  272. return true
  273. }
  274. }
  275. return false
  276. }