modules.go 13 KB

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