modules.go 9.2 KB

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