modules.go 10 KB

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