modules.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 !backend || (backend && len(wrap.UrlArgs) <= 0) {
  99. if mod == nil {
  100. if m, ok := this.mods["index"]; ok {
  101. mod = m
  102. modCurr = "index"
  103. }
  104. }
  105. }
  106. return mod, modCurr
  107. }
  108. func (this *Modules) getModulesList(wrap *wrapper.Wrapper, sys bool, all bool) []*MInfo {
  109. list := make([]*MInfo, 0)
  110. for _, mod := range this.mods {
  111. if mod.Back != nil {
  112. if mod.Info.System == sys || all {
  113. list = append(list, &mod.Info)
  114. }
  115. }
  116. }
  117. sort.Slice(list, func(i, j int) bool {
  118. return list[i].Order < list[j].Order
  119. })
  120. return list
  121. }
  122. func (this *Modules) getSidebarModuleSubMenu(wrap *wrapper.Wrapper, mod *MInfo) string {
  123. html := ``
  124. if mod.Sub != nil {
  125. for _, item := range *mod.Sub {
  126. class := ""
  127. if (item.Mount == "default" && len(wrap.UrlArgs) <= 1) || (len(wrap.UrlArgs) >= 2 && item.Mount == wrap.UrlArgs[1]) {
  128. class = " active"
  129. }
  130. icon := item.Icon
  131. if icon == "" {
  132. icon = assets.SysSvgIconGear
  133. }
  134. href := "/cp/" + mod.Mount + "/" + item.Mount + "/"
  135. if mod.Mount == "index" && item.Mount == "default" {
  136. href = "/cp/"
  137. } else if item.Mount == "default" {
  138. href = "/cp/" + mod.Mount + "/"
  139. }
  140. html += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + item.Name + `</a></li>`
  141. }
  142. if html != "" {
  143. html = `<ul class="nav flex-column">` + html + `</ul>`
  144. }
  145. }
  146. return html
  147. }
  148. func (this *Modules) getNavMenuModules(wrap *wrapper.Wrapper, sys bool) string {
  149. html := ``
  150. list := this.getModulesList(wrap, sys, false)
  151. for _, mod := range list {
  152. class := ""
  153. if mod.Mount == wrap.CurrModule {
  154. class = " active"
  155. }
  156. html += `<a class="dropdown-item` + class + `" href="/cp/` + mod.Mount + `/">` + mod.Name + `</a>`
  157. }
  158. return html
  159. }
  160. func (this *Modules) getSidebarModules(wrap *wrapper.Wrapper) string {
  161. html_def := ""
  162. html_sys := ""
  163. list := this.getModulesList(wrap, false, true)
  164. for _, mod := range list {
  165. class := ""
  166. submenu := ""
  167. if mod.Mount == wrap.CurrModule {
  168. class = " active"
  169. submenu = this.getSidebarModuleSubMenu(wrap, mod)
  170. }
  171. icon := mod.Icon
  172. if icon == "" {
  173. icon = assets.SysSvgIconGear
  174. }
  175. href := "/cp/" + mod.Mount + "/"
  176. if mod.Mount == "index" {
  177. href = "/cp/"
  178. }
  179. if !mod.System {
  180. html_def += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
  181. } else {
  182. html_sys += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
  183. }
  184. }
  185. if html_def != "" {
  186. html_def = `<ul class="nav flex-column">` + html_def + `</ul>`
  187. }
  188. if html_sys != "" {
  189. html_sys = `<ul class="nav flex-column">` + html_sys + `</ul>`
  190. }
  191. if html_def != "" && html_sys != "" {
  192. html_sys = `<div class="dropdown-divider"></div>` + html_sys
  193. }
  194. return html_def + html_sys
  195. }
  196. func (this *Modules) getBreadCrumbs(wrap *wrapper.Wrapper, data *[]consts.BreadCrumb) string {
  197. res := `<nav aria-label="breadcrumb">`
  198. res += `<ol class="breadcrumb">`
  199. res += `<li class="breadcrumb-item"><a href="/cp/` + this.mods[wrap.CurrModule].Info.Mount + `/">` + html.EscapeString(this.mods[wrap.CurrModule].Info.Name) + `</a></li>`
  200. for _, item := range *data {
  201. if item.Link == "" {
  202. res += `<li class="breadcrumb-item active" aria-current="page">` + html.EscapeString(item.Name) + `</li>`
  203. } else {
  204. res += `<li class="breadcrumb-item"><a href="` + item.Link + `">` + html.EscapeString(item.Name) + `</a></li>`
  205. }
  206. }
  207. res += `</ol>`
  208. res += `</nav>`
  209. return res
  210. }
  211. func New() *Modules {
  212. m := Modules{
  213. mods: map[string]*Module{},
  214. acts: map[string]*Action{},
  215. }
  216. m.load()
  217. return &m
  218. }
  219. func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
  220. if wrap.R.Method == "POST" {
  221. if err := wrap.R.ParseForm(); err == nil {
  222. name := wrap.R.FormValue("action")
  223. if name != "" {
  224. wrap.W.WriteHeader(http.StatusOK)
  225. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  226. wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  227. if act, ok := this.acts[name]; ok {
  228. if act.Info.WantDB {
  229. err := wrap.UseDatabase()
  230. if err != nil {
  231. wrap.MsgError(err.Error())
  232. return true
  233. }
  234. defer wrap.DB.Close()
  235. }
  236. if act.Info.WantUser {
  237. if !wrap.LoadSessionUser() {
  238. wrap.MsgError(`You must be loginned to run this action`)
  239. return true
  240. }
  241. }
  242. act.Act(wrap)
  243. return true
  244. } else {
  245. wrap.MsgError(`This action is not implemented`)
  246. return true
  247. }
  248. }
  249. }
  250. }
  251. return false
  252. }
  253. func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
  254. mod, cm := this.getCurrentModule(wrap, false)
  255. if mod != nil {
  256. wrap.CurrModule = cm
  257. if mod.Front != nil {
  258. if mod.Info.WantDB {
  259. err := wrap.UseDatabase()
  260. if err != nil {
  261. utils.SystemErrorPageEngine(wrap.W, err)
  262. return true
  263. }
  264. defer wrap.DB.Close()
  265. }
  266. mod.Front(wrap)
  267. return true
  268. }
  269. }
  270. return false
  271. }
  272. func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
  273. mod, cm := this.getCurrentModule(wrap, true)
  274. if mod != nil {
  275. wrap.CurrModule = cm
  276. if len(wrap.UrlArgs) >= 2 && wrap.UrlArgs[1] != "" {
  277. wrap.CurrSubModule = wrap.UrlArgs[1]
  278. }
  279. // Search for sub module mount
  280. found := false
  281. submount := "default"
  282. if wrap.CurrSubModule != "" {
  283. submount = wrap.CurrSubModule
  284. }
  285. for _, item := range *mod.Info.Sub {
  286. if item.Mount == submount {
  287. found = true
  288. break
  289. }
  290. }
  291. // Display standart 404 error page
  292. if !found {
  293. return found
  294. }
  295. // Call module function
  296. if mod.Back != nil {
  297. sidebar_left, content, sidebar_right := mod.Back(wrap)
  298. // Prepare CP page
  299. body_class := "cp"
  300. if sidebar_left != "" {
  301. body_class = body_class + " cp-sidebar-left"
  302. }
  303. if content == "" {
  304. body_class = body_class + " cp-404"
  305. }
  306. if sidebar_right != "" {
  307. body_class = body_class + " cp-sidebar-right"
  308. }
  309. wrap.RenderBackEnd(assets.TmplCpBase, consts.TmplDataCpBase{
  310. Title: "Fave " + consts.ServerVersion,
  311. BodyClasses: body_class,
  312. UserId: wrap.User.A_id,
  313. UserFirstName: wrap.User.A_first_name,
  314. UserLastName: wrap.User.A_last_name,
  315. UserEmail: wrap.User.A_email,
  316. UserPassword: "",
  317. UserAvatarLink: "https://s.gravatar.com/avatar/" + utils.GetMd5(wrap.User.A_email) + "?s=80&r=g",
  318. NavBarModules: template.HTML(this.getNavMenuModules(wrap, false)),
  319. NavBarModulesSys: template.HTML(this.getNavMenuModules(wrap, true)),
  320. ModuleCurrentAlias: wrap.CurrModule,
  321. SidebarLeft: template.HTML(sidebar_left),
  322. Content: template.HTML(content),
  323. SidebarRight: template.HTML(sidebar_right),
  324. })
  325. return true
  326. }
  327. }
  328. return false
  329. }