modules.go 4.2 KB

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