modules.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package modules
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "reflect"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "golang-fave/engine/wrapper"
  10. utils "golang-fave/engine/wrapper/utils"
  11. )
  12. type ModuleItem struct {
  13. Alias string
  14. Display bool
  15. Name string
  16. Order int
  17. }
  18. type Module struct {
  19. wrapper *wrapper.Wrapper
  20. db *sql.DB
  21. user *utils.MySql_user
  22. urls *[]string
  23. mmod string
  24. smod string
  25. imod int
  26. modlist []ModuleItem
  27. }
  28. func (this *Module) module_get_display(name string) bool {
  29. mname := "Module_" + name + "_display"
  30. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  31. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  32. return result[0].Bool()
  33. }
  34. return false
  35. }
  36. func (this *Module) module_get_name(name string) string {
  37. mname := "Module_" + name + "_name"
  38. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  39. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  40. return result[0].String()
  41. }
  42. return ""
  43. }
  44. func (this *Module) module_get_order(name string) int {
  45. mname := "Module_" + name + "_order"
  46. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  47. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  48. return int(result[0].Int())
  49. }
  50. return 0
  51. }
  52. func (this *Module) module_get_submenu(name string) string {
  53. mname := "Module_" + name + "_submenu"
  54. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  55. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  56. result_array := result[0].Interface().([]utils.ModuleSubMenu)
  57. result_html := ""
  58. for _, value := range result_array {
  59. class := ""
  60. if name == this.mmod && value.Alias == this.smod {
  61. class = " active"
  62. }
  63. result_html += `<li class="nav-item` + class + `"><a class="nav-link" href="/cp/` + name + `/` + value.Alias + `/">` + value.Name + `</a></li>`
  64. }
  65. if result_html != "" {
  66. result_html = `<ul class="nav flex-column">` + result_html + `</ul>`
  67. }
  68. return result_html
  69. }
  70. return ""
  71. }
  72. func (this *Module) module_get_list_of_modules() *[]ModuleItem {
  73. if len(this.modlist) <= 0 {
  74. t := reflect.TypeOf(this)
  75. for i := 0; i < t.NumMethod(); i++ {
  76. m := t.Method(i)
  77. if strings.HasPrefix(m.Name, "Module_") && strings.HasSuffix(m.Name, "_alias") {
  78. alias := m.Name[7:][:5]
  79. this.modlist = append(this.modlist, ModuleItem{
  80. alias,
  81. this.module_get_display(alias),
  82. this.module_get_name(alias),
  83. this.module_get_order(alias),
  84. })
  85. }
  86. }
  87. sort.Slice(this.modlist, func(i, j int) bool {
  88. return this.modlist[i].Order < this.modlist[j].Order
  89. })
  90. }
  91. return &this.modlist
  92. }
  93. func New(wrapper *wrapper.Wrapper, db *sql.DB, user *utils.MySql_user, url_args *[]string) *Module {
  94. mmod := "index"
  95. smod := "default"
  96. imod := 0
  97. if len(*url_args) >= 2 {
  98. mmod = (*url_args)[1]
  99. }
  100. if len(*url_args) >= 3 {
  101. smod = (*url_args)[2]
  102. }
  103. if len(*url_args) >= 4 {
  104. if val, err := strconv.Atoi((*url_args)[3]); err == nil {
  105. imod = val
  106. }
  107. }
  108. return &Module{wrapper, db, user, url_args, mmod, smod, imod, make([]ModuleItem, 0)}
  109. }
  110. func (this *Module) Run() bool {
  111. mname := "Module_" + this.mmod
  112. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  113. reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  114. return true
  115. }
  116. return false
  117. }
  118. func (this *Module) GetNavMenuModules() string {
  119. html := ""
  120. list := this.module_get_list_of_modules()
  121. for _, value := range *list {
  122. if value.Display {
  123. class := ""
  124. if value.Alias == this.mmod {
  125. class = " active"
  126. }
  127. html += `<a class="dropdown-item` + class + `" href="/cp/` + value.Alias + `/">` + value.Name + `</a>`
  128. }
  129. }
  130. return html
  131. }
  132. func (this *Module) GetSidebarLeft() string {
  133. sidebar := `<ul class="nav flex-column">`
  134. list := this.module_get_list_of_modules()
  135. for _, value := range *list {
  136. if value.Display {
  137. class := ""
  138. submenu := ""
  139. if value.Alias == this.mmod {
  140. class = " active"
  141. submenu = this.module_get_submenu(value.Alias)
  142. }
  143. sidebar += `<li class="nav-item` + class + `"><a class="nav-link" href="/cp/` + value.Alias + `/">` + value.Name + `</a>` + submenu + `</li>`
  144. }
  145. }
  146. sidebar += `</ul>`
  147. return sidebar
  148. }
  149. func (this *Module) GetContent() string {
  150. mname := "Module_" + this.mmod + "_content"
  151. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  152. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  153. return result[0].String()
  154. }
  155. return ""
  156. }
  157. func (this *Module) GetSidebarRight() string {
  158. mname := "Module_" + this.mmod + "_sidebar"
  159. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  160. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  161. return result[0].String()
  162. }
  163. return ""
  164. }