modules.go 9.9 KB

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