modules.go 5.0 KB

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