modules.go 12 KB

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