modules.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. const (
  27. dfkHidden = iota
  28. dfkText
  29. dfkEmail
  30. dfkPassword
  31. dfkSubmit
  32. )
  33. type dataFormFieldOption struct {
  34. caption string
  35. value string
  36. }
  37. type dataFormFieldHook func(field *dataFormField) string
  38. type dataFormField struct {
  39. caption string
  40. kind int
  41. name string
  42. value string
  43. placeholder string
  44. hint string
  45. target string
  46. options []dataFormFieldOption
  47. hook dataFormFieldHook
  48. }
  49. type ModuleItem struct {
  50. Alias string
  51. Display bool
  52. Name string
  53. Icon string
  54. Order int
  55. }
  56. type Module struct {
  57. wrapper *wrapper.Wrapper
  58. db *sql.DB
  59. user *utils.MySql_user
  60. urls *[]string
  61. mmod string
  62. smod string
  63. imod int
  64. modlist []ModuleItem
  65. }
  66. func (this *Module) module_get_display(name string) bool {
  67. mname := "Module_" + name + "_display"
  68. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  69. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  70. return result[0].Bool()
  71. }
  72. return false
  73. }
  74. func (this *Module) module_get_name(name string) string {
  75. mname := "Module_" + name + "_name"
  76. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  77. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  78. return result[0].String()
  79. }
  80. return ""
  81. }
  82. func (this *Module) module_get_icon(name string) string {
  83. mname := "Module_" + name + "_icon"
  84. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  85. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  86. return result[0].String()
  87. }
  88. return ""
  89. }
  90. func (this *Module) module_get_order(name string) int {
  91. mname := "Module_" + name + "_order"
  92. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  93. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  94. return int(result[0].Int())
  95. }
  96. return 0
  97. }
  98. func (this *Module) module_get_submenu(name string) string {
  99. mname := "Module_" + name + "_submenu"
  100. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  101. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  102. result_array := result[0].Interface().([]utils.ModuleSubMenu)
  103. result_html := ""
  104. for _, value := range result_array {
  105. class := ""
  106. if name == this.mmod && value.Alias == this.smod && this.imod == 0 {
  107. class = " active"
  108. }
  109. result_html += `<li class="nav-item` + class + `"><a class="nav-link" href="/cp/` + name + `/` + value.Alias + `/">` + value.Icon + value.Name + `</a></li>`
  110. }
  111. if result_html != "" {
  112. result_html = `<ul class="nav flex-column">` + result_html + `</ul>`
  113. }
  114. return result_html
  115. }
  116. return ""
  117. }
  118. func (this *Module) module_get_list_of_modules() *[]ModuleItem {
  119. if len(this.modlist) <= 0 {
  120. t := reflect.TypeOf(this)
  121. for i := 0; i < t.NumMethod(); i++ {
  122. m := t.Method(i)
  123. if strings.HasPrefix(m.Name, "Module_") && strings.HasSuffix(m.Name, "_alias") {
  124. alias := m.Name[7:]
  125. alias = alias[0 : len(alias)-6]
  126. this.modlist = append(this.modlist, ModuleItem{
  127. alias,
  128. this.module_get_display(alias),
  129. this.module_get_name(alias),
  130. this.module_get_icon(alias),
  131. this.module_get_order(alias),
  132. })
  133. }
  134. }
  135. sort.Slice(this.modlist, func(i, j int) bool {
  136. return this.modlist[i].Order < this.modlist[j].Order
  137. })
  138. }
  139. return &this.modlist
  140. }
  141. func (this *Module) breadcrumb(data []dataBreadcrumb) string {
  142. result := ``
  143. result += `<nav aria-label="breadcrumb">`
  144. result += `<ol class="breadcrumb">`
  145. result += `<li class="breadcrumb-item"><a href="/cp/` + this.mmod + `/">` + html.EscapeString(this.module_get_name(this.mmod)) + `</a></li>`
  146. for _, item := range data {
  147. if item.link == "" {
  148. result += `<li class="breadcrumb-item active" aria-current="page">` + html.EscapeString(item.name) + `</li>`
  149. } else {
  150. result += `<li class="breadcrumb-item"><a href="` + item.link + `">` + html.EscapeString(item.name) + `</a></li>`
  151. }
  152. }
  153. result += `</ol>`
  154. result += `</nav>`
  155. return result
  156. }
  157. func (this *Module) data_table(table string, order_by string, order_way string, data []dataTableRow, action dataTableAction, pagination_url string) string {
  158. var num int
  159. err := this.db.QueryRow("SELECT COUNT(*) FROM `" + table + "`;").Scan(&num)
  160. if err != nil {
  161. return ""
  162. }
  163. pear_page := 10
  164. max_pages := int(math.Ceil(float64(num) / float64(pear_page)))
  165. curr_page := 1
  166. p := this.wrapper.R.URL.Query().Get("p")
  167. if p != "" {
  168. pi, err := strconv.Atoi(p)
  169. if err != nil {
  170. curr_page = 1
  171. } else {
  172. if pi < 1 {
  173. curr_page = 1
  174. } else if pi > max_pages {
  175. curr_page = max_pages
  176. } else {
  177. curr_page = pi
  178. }
  179. }
  180. }
  181. limit_offset := curr_page*pear_page - pear_page
  182. result := `<table class="table data-table table-striped table-bordered table-hover table_` + table + `">`
  183. result += `<thead>`
  184. result += `<tr>`
  185. sql := "SELECT"
  186. for i, column := range data {
  187. if column.nameInTable != "" {
  188. result += `<th scope="col" class="col_` + column.dbField + `">` + html.EscapeString(column.nameInTable) + `</th>`
  189. }
  190. sql += " `" + column.dbField + "`"
  191. if i+1 < len(data) {
  192. sql += ","
  193. }
  194. }
  195. sql += " FROM `" + table + "` ORDER BY `" + order_by + "` " + order_way + " LIMIT ?, ?;"
  196. if action != nil {
  197. result += `<th scope="col" class="col_action">&nbsp;</th>`
  198. }
  199. result += `</tr>`
  200. result += `</thead>`
  201. result += `<tbody>`
  202. rows, err := this.db.Query(sql, limit_offset, pear_page)
  203. if err == nil {
  204. values := make([]string, len(data))
  205. scan := make([]interface{}, len(values))
  206. for i := range values {
  207. scan[i] = &values[i]
  208. }
  209. for rows.Next() {
  210. err = rows.Scan(scan...)
  211. if err == nil {
  212. result += `<tr>`
  213. for i, val := range values {
  214. if data[i].nameInTable != "" {
  215. if data[i].display == nil {
  216. result += `<td class="col_` + data[i].dbField + `">` + html.EscapeString(string(val)) + `</td>`
  217. } else {
  218. result += `<td class="col_` + data[i].dbField + `">` + data[i].display(&values) + `</td>`
  219. }
  220. }
  221. }
  222. if action != nil {
  223. result += `<td class="col_action">` + action(&values) + `</td>`
  224. }
  225. result += `</tr>`
  226. }
  227. }
  228. }
  229. result += `</tbody></table>`
  230. result += `<nav>`
  231. result += `<ul class="pagination" style="margin-bottom:0px;">`
  232. class := ""
  233. if curr_page <= 1 {
  234. class = " disabled"
  235. }
  236. result += `<li class="page-item` + class + `">`
  237. result += `<a class="page-link" href="` + pagination_url + `?p=` + fmt.Sprintf("%d", curr_page-1) + `" aria-label="Previous">`
  238. result += `<span aria-hidden="true">&laquo;</span>`
  239. result += `<span class="sr-only">Previous</span>`
  240. result += `</a>`
  241. result += `</li>`
  242. for i := 1; i <= max_pages; i++ {
  243. class = ""
  244. if i == curr_page {
  245. class = " active"
  246. }
  247. result += `<li class="page-item` + class + `">`
  248. result += `<a class="page-link" href="` + pagination_url + `?p=` + fmt.Sprintf("%d", i) + `">` + fmt.Sprintf("%d", i) + `</a>`
  249. result += `</li>`
  250. }
  251. class = ""
  252. if curr_page >= max_pages {
  253. class = " disabled"
  254. }
  255. result += `<li class="page-item` + class + `">`
  256. result += `<a class="page-link" href="` + pagination_url + `?p=` + fmt.Sprintf("%d", curr_page+1) + `" aria-label="Next">`
  257. result += `<span aria-hidden="true">&raquo;</span>`
  258. result += `<span class="sr-only">Next</span>`
  259. result += `</a>`
  260. result += `</li>`
  261. result += `</ul>`
  262. result += `</nav>`
  263. return result
  264. }
  265. func (this *Module) data_form(data []dataFormField) string {
  266. result := `<form class="data-form" action="/cp/" method="post" autocomplete="off">`
  267. result += `<div class="hidden">`
  268. for _, field := range data {
  269. if field.kind == dfkHidden {
  270. if field.hook != nil {
  271. result += field.hook(&field)
  272. } else {
  273. result += `<input type="hidden" name="` + field.name + `" value="` + field.value + `">`
  274. }
  275. }
  276. }
  277. result += `</div>`
  278. for _, field := range data {
  279. if field.kind != dfkHidden && field.kind != dfkSubmit {
  280. if field.hook != nil {
  281. result += field.hook(&field)
  282. } else {
  283. result += `<div class="form-group">`
  284. result += `<div class="row">`
  285. result += `<div class="col-3">`
  286. result += `<label for="lbl_` + field.name + `">` + field.caption + `</label>`
  287. result += `</div>`
  288. result += `<div class="col-9">`
  289. result += `<div>`
  290. if field.kind == dfkText {
  291. result += `<input class="form-control" type="text" id="lbl_` + field.name + `" name="` + field.name + `" value="` + field.value + `" placeholder="` + field.placeholder + `" autocomplete="off">`
  292. } else if field.kind == dfkEmail {
  293. result += `<input class="form-control" type="email" id="lbl_` + field.name + `" name="` + field.name + `" value="` + field.value + `" placeholder="` + field.placeholder + `" autocomplete="off">`
  294. } else if field.kind == dfkPassword {
  295. result += `<input class="form-control" type="password" id="lbl_` + field.name + `" name="` + field.name + `" value="` + field.value + `" placeholder="` + field.placeholder + `" autocomplete="off">`
  296. }
  297. result += `</div>`
  298. if field.hint != "" {
  299. result += `<div><small>` + field.hint + `</small></div>`
  300. }
  301. result += `</div>`
  302. result += `</div>`
  303. result += `</div>`
  304. }
  305. }
  306. }
  307. for _, field := range data {
  308. if field.kind == dfkSubmit {
  309. if field.hook != nil {
  310. result += field.hook(&field)
  311. } else {
  312. result += `<div class="row hidden">`
  313. result += `<div class="col-3">`
  314. result += `&nbsp;`
  315. result += `</div>`
  316. result += `<div class="col-9">`
  317. result += `<button type="submit" class="btn btn-primary" data-target="` + field.target + `">` + field.value + `</button>`
  318. result += `</div>`
  319. result += `</div>`
  320. }
  321. }
  322. }
  323. result += `</form>`
  324. return result
  325. }
  326. func New(wrapper *wrapper.Wrapper, db *sql.DB, user *utils.MySql_user, url_args *[]string) *Module {
  327. mmod := "index"
  328. smod := "default"
  329. imod := 0
  330. if len(*url_args) >= 2 {
  331. mmod = (*url_args)[1]
  332. }
  333. if len(*url_args) >= 3 {
  334. smod = (*url_args)[2]
  335. }
  336. if len(*url_args) >= 4 {
  337. if val, err := strconv.Atoi((*url_args)[3]); err == nil {
  338. imod = val
  339. }
  340. }
  341. return &Module{wrapper, db, user, url_args, mmod, smod, imod, make([]ModuleItem, 0)}
  342. }
  343. func (this *Module) Run() bool {
  344. mname := "Module_" + this.mmod
  345. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  346. reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  347. return true
  348. }
  349. return false
  350. }
  351. func (this *Module) GetNavMenuModules() string {
  352. html := ""
  353. list := this.module_get_list_of_modules()
  354. for _, value := range *list {
  355. if value.Display {
  356. class := ""
  357. if value.Alias == this.mmod {
  358. class = " active"
  359. }
  360. html += `<a class="dropdown-item` + class + `" href="/cp/` + value.Alias + `/">` + value.Name + `</a>`
  361. }
  362. }
  363. return html
  364. }
  365. func (this *Module) GetNavMenuModulesSys() string {
  366. html := ""
  367. list := this.module_get_list_of_modules()
  368. for _, value := range *list {
  369. if !value.Display {
  370. class := ""
  371. if value.Alias == this.mmod {
  372. class = " active"
  373. }
  374. html += `<a class="dropdown-item` + class + `" href="/cp/` + value.Alias + `/">` + value.Name + `</a>`
  375. }
  376. }
  377. return html
  378. }
  379. func (this *Module) GetSidebarLeft() string {
  380. list := this.module_get_list_of_modules()
  381. modules_all := `<ul class="nav flex-column">`
  382. for _, value := range *list {
  383. if value.Display {
  384. class := ""
  385. submenu := ""
  386. if value.Alias == this.mmod {
  387. class = " active"
  388. submenu = this.module_get_submenu(value.Alias)
  389. }
  390. modules_all += `<li class="nav-item` + class + `"><a class="nav-link" href="/cp/` + value.Alias + `/">` + value.Icon + value.Name + `</a>` + submenu + `</li>`
  391. }
  392. }
  393. modules_all += `</ul>`
  394. modules_sys := `<ul class="nav flex-column">`
  395. for _, value := range *list {
  396. if !value.Display {
  397. class := ""
  398. submenu := ""
  399. if value.Alias == this.mmod {
  400. class = " active"
  401. submenu = this.module_get_submenu(value.Alias)
  402. }
  403. modules_sys += `<li class="nav-item` + class + `"><a class="nav-link" href="/cp/` + value.Alias + `/">` + value.Icon + value.Name + `</a>` + submenu + `</li>`
  404. }
  405. }
  406. modules_sys += `</ul>`
  407. return modules_all + `<div class="dropdown-divider"></div>` + modules_sys
  408. }
  409. func (this *Module) GetContent() string {
  410. mname := "Module_" + this.mmod + "_content"
  411. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  412. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  413. return result[0].String()
  414. }
  415. return ""
  416. }
  417. func (this *Module) GetSidebarRight() string {
  418. mname := "Module_" + this.mmod + "_sidebar"
  419. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  420. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  421. return result[0].String()
  422. }
  423. return ""
  424. }