modules.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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:]
  88. alias = alias[0 : len(alias)-6]
  89. this.modlist = append(this.modlist, ModuleItem{
  90. alias,
  91. this.module_get_display(alias),
  92. this.module_get_name(alias),
  93. this.module_get_icon(alias),
  94. this.module_get_order(alias),
  95. })
  96. }
  97. }
  98. sort.Slice(this.modlist, func(i, j int) bool {
  99. return this.modlist[i].Order < this.modlist[j].Order
  100. })
  101. }
  102. return &this.modlist
  103. }
  104. func New(wrapper *wrapper.Wrapper, db *sql.DB, user *utils.MySql_user, url_args *[]string) *Module {
  105. mmod := "index"
  106. smod := "default"
  107. imod := 0
  108. if len(*url_args) >= 2 {
  109. mmod = (*url_args)[1]
  110. }
  111. if len(*url_args) >= 3 {
  112. smod = (*url_args)[2]
  113. }
  114. if len(*url_args) >= 4 {
  115. if val, err := strconv.Atoi((*url_args)[3]); err == nil {
  116. imod = val
  117. }
  118. }
  119. return &Module{wrapper, db, user, url_args, mmod, smod, imod, make([]ModuleItem, 0)}
  120. }
  121. func (this *Module) Run() bool {
  122. mname := "Module_" + this.mmod
  123. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  124. reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  125. return true
  126. }
  127. return false
  128. }
  129. func (this *Module) GetNavMenuModules() string {
  130. html := ""
  131. list := this.module_get_list_of_modules()
  132. for _, value := range *list {
  133. if value.Display {
  134. class := ""
  135. if value.Alias == this.mmod {
  136. class = " active"
  137. }
  138. html += `<a class="dropdown-item` + class + `" href="/cp/` + value.Alias + `/">` + value.Name + `</a>`
  139. }
  140. }
  141. return html
  142. }
  143. func (this *Module) GetSidebarLeft() string {
  144. list := this.module_get_list_of_modules()
  145. modules_all := `<ul class="nav flex-column">`
  146. for _, value := range *list {
  147. if value.Display {
  148. class := ""
  149. submenu := ""
  150. if value.Alias == this.mmod {
  151. class = " active"
  152. submenu = this.module_get_submenu(value.Alias)
  153. }
  154. modules_all += `<li class="nav-item` + class + `"><a class="nav-link" href="/cp/` + value.Alias + `/">` + value.Icon + value.Name + `</a>` + submenu + `</li>`
  155. }
  156. }
  157. modules_all += `</ul>`
  158. modules_sys := `<ul class="nav flex-column">`
  159. for _, value := range *list {
  160. if !value.Display {
  161. class := ""
  162. submenu := ""
  163. if value.Alias == this.mmod {
  164. class = " active"
  165. submenu = this.module_get_submenu(value.Alias)
  166. }
  167. modules_sys += `<li class="nav-item` + class + `"><a class="nav-link" href="/cp/` + value.Alias + `/">` + value.Icon + value.Name + `</a>` + submenu + `</li>`
  168. }
  169. }
  170. modules_sys += `</ul>`
  171. return modules_all + `<div class="dropdown-divider" style="border-color:#d6d6d6;margin:0px;"></div>` + modules_sys
  172. }
  173. func (this *Module) GetContent() string {
  174. mname := "Module_" + this.mmod + "_content"
  175. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  176. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  177. return result[0].String()
  178. }
  179. return ""
  180. }
  181. func (this *Module) GetSidebarRight() string {
  182. mname := "Module_" + this.mmod + "_sidebar"
  183. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  184. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  185. return result[0].String()
  186. }
  187. return ""
  188. }