modules.go 13 KB

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