modules.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. return ""
  83. }
  84. func (this *Module) GetSidebarLeft() string {
  85. sidebar := `<ul class="nav flex-column">`
  86. // Make module list
  87. aType := reflect.TypeOf(this)
  88. for i := 0; i < aType.NumMethod(); i++ {
  89. aMethod := aType.Method(i)
  90. if strings.HasPrefix(aMethod.Name, "Module_") && strings.HasSuffix(aMethod.Name, "_alias") {
  91. // Extract module alias
  92. alias := aMethod.Name[7:][:5]
  93. if this.module_get_display(alias) {
  94. // Item active class
  95. class := ""
  96. if alias == this.mmod {
  97. class = " active"
  98. }
  99. // Active item sub menu
  100. submenu := ""
  101. if alias == this.mmod {
  102. submenu = this.module_get_submenu(alias)
  103. }
  104. // Add module to list
  105. sidebar += `<li class="nav-item` + class + `"><a class="nav-link" href="/cp/` + alias + `/">` + this.module_get_name(alias) + `</a>` + submenu + `</li>`
  106. }
  107. }
  108. }
  109. sidebar += `</ul>`
  110. return sidebar
  111. }
  112. func (this *Module) GetContent() string {
  113. mname := "Module_" + this.mmod + "_content"
  114. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  115. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  116. return result[0].String()
  117. }
  118. return ""
  119. }
  120. func (this *Module) GetSidebarRight() string {
  121. mname := "Module_" + this.mmod + "_sidebar"
  122. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  123. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  124. return result[0].String()
  125. }
  126. return ""
  127. }