modules.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package modules
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "fmt"
  6. "math"
  7. "reflect"
  8. "sort"
  9. "strconv"
  10. "strings"
  11. "golang-fave/engine/wrapper"
  12. utils "golang-fave/engine/wrapper/utils"
  13. )
  14. type dataTableDisplay func(values *[]string) string
  15. type dataTableAction func(values *[]string) string
  16. type dataTableRow struct {
  17. dbField string
  18. nameInTable string
  19. display dataTableDisplay
  20. }
  21. type dataBreadcrumb struct {
  22. name string
  23. link string
  24. }
  25. type ModuleItem struct {
  26. Alias string
  27. Display bool
  28. Name string
  29. Icon string
  30. Order int
  31. }
  32. type Module struct {
  33. wrapper *wrapper.Wrapper
  34. db *sql.DB
  35. user *utils.MySql_user
  36. urls *[]string
  37. mmod string
  38. smod string
  39. imod int
  40. modlist []ModuleItem
  41. }
  42. func (this *Module) module_get_display(name string) bool {
  43. mname := "Module_" + name + "_display"
  44. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  45. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  46. return result[0].Bool()
  47. }
  48. return false
  49. }
  50. func (this *Module) module_get_name(name string) string {
  51. mname := "Module_" + name + "_name"
  52. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  53. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  54. return result[0].String()
  55. }
  56. return ""
  57. }
  58. func (this *Module) module_get_icon(name string) string {
  59. mname := "Module_" + name + "_icon"
  60. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  61. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  62. return result[0].String()
  63. }
  64. return ""
  65. }
  66. func (this *Module) module_get_order(name string) int {
  67. mname := "Module_" + name + "_order"
  68. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  69. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  70. return int(result[0].Int())
  71. }
  72. return 0
  73. }
  74. func (this *Module) module_get_submenu(name string) string {
  75. mname := "Module_" + name + "_submenu"
  76. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  77. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  78. result_array := result[0].Interface().([]utils.ModuleSubMenu)
  79. result_html := ""
  80. for _, value := range result_array {
  81. class := ""
  82. if name == this.mmod && value.Alias == this.smod {
  83. class = " active"
  84. }
  85. result_html += `<li class="nav-item` + class + `"><a class="nav-link" href="/cp/` + name + `/` + value.Alias + `/">` + value.Icon + value.Name + `</a></li>`
  86. }
  87. if result_html != "" {
  88. result_html = `<ul class="nav flex-column">` + result_html + `</ul>`
  89. }
  90. return result_html
  91. }
  92. return ""
  93. }
  94. func (this *Module) module_get_list_of_modules() *[]ModuleItem {
  95. if len(this.modlist) <= 0 {
  96. t := reflect.TypeOf(this)
  97. for i := 0; i < t.NumMethod(); i++ {
  98. m := t.Method(i)
  99. if strings.HasPrefix(m.Name, "Module_") && strings.HasSuffix(m.Name, "_alias") {
  100. alias := m.Name[7:]
  101. alias = alias[0 : len(alias)-6]
  102. this.modlist = append(this.modlist, ModuleItem{
  103. alias,
  104. this.module_get_display(alias),
  105. this.module_get_name(alias),
  106. this.module_get_icon(alias),
  107. this.module_get_order(alias),
  108. })
  109. }
  110. }
  111. sort.Slice(this.modlist, func(i, j int) bool {
  112. return this.modlist[i].Order < this.modlist[j].Order
  113. })
  114. }
  115. return &this.modlist
  116. }
  117. func (this *Module) breadcrumb(data []dataBreadcrumb) string {
  118. result := ``
  119. result += `<nav aria-label="breadcrumb">`
  120. result += `<ol class="breadcrumb">`
  121. result += `<li class="breadcrumb-item"><a href="/cp/` + this.mmod + `/">` + this.module_get_name(this.mmod) + `</a></li>`
  122. for _, item := range data {
  123. if item.link == "" {
  124. result += `<li class="breadcrumb-item active" aria-current="page">` + item.name + `</li>`
  125. } else {
  126. result += `<li class="breadcrumb-item"><a href="` + item.link + `">` + item.name + `</a></li>`
  127. }
  128. }
  129. result += `</ol>`
  130. result += `</nav>`
  131. return result
  132. }
  133. func (this *Module) data_table(table string, order_by string, order_way string, data []dataTableRow, action dataTableAction, pagination_url string) string {
  134. var num int
  135. err := this.db.QueryRow("SELECT COUNT(*) FROM `" + table + "`;").Scan(&num)
  136. if err != nil {
  137. return ""
  138. }
  139. pear_page := 10
  140. max_pages := int(math.Ceil(float64(num) / float64(pear_page)))
  141. curr_page := 1
  142. p := this.wrapper.R.URL.Query().Get("p")
  143. if p != "" {
  144. pi, err := strconv.Atoi(p)
  145. if err != nil {
  146. curr_page = 1
  147. } else {
  148. if pi < 1 {
  149. curr_page = 1
  150. } else if pi > max_pages {
  151. curr_page = max_pages
  152. } else {
  153. curr_page = pi
  154. }
  155. }
  156. }
  157. limit_offset := curr_page*pear_page - pear_page
  158. result := `<table class="table table-striped table-bordered table_` + table + `">`
  159. result += `<thead>`
  160. result += `<tr>`
  161. sql := "SELECT"
  162. for i, column := range data {
  163. if column.nameInTable != "" {
  164. result += `<th scope="col" class="col_` + column.dbField + `">` + column.nameInTable + `</th>`
  165. }
  166. sql += " `" + column.dbField + "`"
  167. if i+1 < len(data) {
  168. sql += ","
  169. }
  170. }
  171. sql += " FROM `" + table + "` ORDER BY `" + order_by + "` " + order_way + " LIMIT ?, ?;"
  172. if action != nil {
  173. result += `<th scope="col" class="col_action">Action</th>`
  174. }
  175. result += `</tr>`
  176. result += `</thead>`
  177. result += `<tbody>`
  178. rows, err := this.db.Query(sql, limit_offset, pear_page)
  179. if err == nil {
  180. values := make([]string, len(data))
  181. scan := make([]interface{}, len(values))
  182. for i := range values {
  183. scan[i] = &values[i]
  184. }
  185. for rows.Next() {
  186. err = rows.Scan(scan...)
  187. if err == nil {
  188. result += `<tr>`
  189. for i, val := range values {
  190. if data[i].nameInTable != "" {
  191. if data[i].display == nil {
  192. result += `<td class="col_` + data[i].dbField + `">` + string(val) + `</td>`
  193. } else {
  194. result += `<td class="col_` + data[i].dbField + `">` + data[i].display(&values) + `</td>`
  195. }
  196. }
  197. }
  198. if action != nil {
  199. result += `<td class="col_action">` + action(&values) + `</td>`
  200. }
  201. result += `</tr>`
  202. }
  203. }
  204. }
  205. result += `</tbody></table>`
  206. result += `<nav>`
  207. result += `<ul class="pagination" style="margin-bottom:0px;">`
  208. class := ""
  209. if curr_page <= 1 {
  210. class = " disabled"
  211. }
  212. result += `<li class="page-item` + class + `">`
  213. result += `<a class="page-link" href="` + pagination_url + `?p=` + fmt.Sprintf("%d", curr_page-1) + `" aria-label="Previous">`
  214. result += `<span aria-hidden="true">&laquo;</span>`
  215. result += `<span class="sr-only">Previous</span>`
  216. result += `</a>`
  217. result += `</li>`
  218. for i := 1; i <= max_pages; i++ {
  219. class = ""
  220. if i == curr_page {
  221. class = " active"
  222. }
  223. result += `<li class="page-item` + class + `">`
  224. result += `<a class="page-link" href="` + pagination_url + `?p=` + fmt.Sprintf("%d", i) + `">` + fmt.Sprintf("%d", i) + `</a>`
  225. result += `</li>`
  226. }
  227. class = ""
  228. if curr_page >= max_pages {
  229. class = " disabled"
  230. }
  231. result += `<li class="page-item` + class + `">`
  232. result += `<a class="page-link" href="` + pagination_url + `?p=` + fmt.Sprintf("%d", curr_page+1) + `" aria-label="Next">`
  233. result += `<span aria-hidden="true">&raquo;</span>`
  234. result += `<span class="sr-only">Next</span>`
  235. result += `</a>`
  236. result += `</li>`
  237. result += `</ul>`
  238. result += `</nav>`
  239. return result
  240. }
  241. func New(wrapper *wrapper.Wrapper, db *sql.DB, user *utils.MySql_user, url_args *[]string) *Module {
  242. mmod := "index"
  243. smod := "default"
  244. imod := 0
  245. if len(*url_args) >= 2 {
  246. mmod = (*url_args)[1]
  247. }
  248. if len(*url_args) >= 3 {
  249. smod = (*url_args)[2]
  250. }
  251. if len(*url_args) >= 4 {
  252. if val, err := strconv.Atoi((*url_args)[3]); err == nil {
  253. imod = val
  254. }
  255. }
  256. return &Module{wrapper, db, user, url_args, mmod, smod, imod, make([]ModuleItem, 0)}
  257. }
  258. func (this *Module) Run() bool {
  259. mname := "Module_" + this.mmod
  260. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  261. reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  262. return true
  263. }
  264. return false
  265. }
  266. func (this *Module) GetNavMenuModules() string {
  267. html := ""
  268. list := this.module_get_list_of_modules()
  269. for _, value := range *list {
  270. if value.Display {
  271. class := ""
  272. if value.Alias == this.mmod {
  273. class = " active"
  274. }
  275. html += `<a class="dropdown-item` + class + `" href="/cp/` + value.Alias + `/">` + value.Name + `</a>`
  276. }
  277. }
  278. return html
  279. }
  280. func (this *Module) GetNavMenuModulesSys() string {
  281. html := ""
  282. list := this.module_get_list_of_modules()
  283. for _, value := range *list {
  284. if !value.Display {
  285. class := ""
  286. if value.Alias == this.mmod {
  287. class = " active"
  288. }
  289. html += `<a class="dropdown-item` + class + `" href="/cp/` + value.Alias + `/">` + value.Name + `</a>`
  290. }
  291. }
  292. return html
  293. }
  294. func (this *Module) GetSidebarLeft() string {
  295. list := this.module_get_list_of_modules()
  296. modules_all := `<ul class="nav flex-column">`
  297. for _, value := range *list {
  298. if value.Display {
  299. class := ""
  300. submenu := ""
  301. if value.Alias == this.mmod {
  302. class = " active"
  303. submenu = this.module_get_submenu(value.Alias)
  304. }
  305. modules_all += `<li class="nav-item` + class + `"><a class="nav-link" href="/cp/` + value.Alias + `/">` + value.Icon + value.Name + `</a>` + submenu + `</li>`
  306. }
  307. }
  308. modules_all += `</ul>`
  309. modules_sys := `<ul class="nav flex-column">`
  310. for _, value := range *list {
  311. if !value.Display {
  312. class := ""
  313. submenu := ""
  314. if value.Alias == this.mmod {
  315. class = " active"
  316. submenu = this.module_get_submenu(value.Alias)
  317. }
  318. modules_sys += `<li class="nav-item` + class + `"><a class="nav-link" href="/cp/` + value.Alias + `/">` + value.Icon + value.Name + `</a>` + submenu + `</li>`
  319. }
  320. }
  321. modules_sys += `</ul>`
  322. return modules_all + `<div class="dropdown-divider"></div>` + modules_sys
  323. }
  324. func (this *Module) GetContent() string {
  325. mname := "Module_" + this.mmod + "_content"
  326. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  327. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  328. return result[0].String()
  329. }
  330. return ""
  331. }
  332. func (this *Module) GetSidebarRight() string {
  333. mname := "Module_" + this.mmod + "_sidebar"
  334. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  335. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  336. return result[0].String()
  337. }
  338. return ""
  339. }