module_template.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package modules
  2. import (
  3. "html"
  4. "io/ioutil"
  5. "net/url"
  6. "os"
  7. "strings"
  8. "golang-fave/assets"
  9. "golang-fave/consts"
  10. "golang-fave/engine/builder"
  11. "golang-fave/engine/wrapper"
  12. "golang-fave/utils"
  13. )
  14. func (this *Modules) template_GetThemeFiles(wrap *wrapper.Wrapper) []string {
  15. var result []string
  16. files, err := ioutil.ReadDir(wrap.DTemplate)
  17. if err == nil {
  18. for _, file := range files {
  19. if len(file.Name()) > 0 && file.Name()[0] == '.' {
  20. continue
  21. }
  22. if len(file.Name()) > 0 && strings.ToLower(file.Name()) == "robots.txt" {
  23. continue
  24. }
  25. result = append(result, file.Name())
  26. }
  27. }
  28. return result
  29. }
  30. func (this *Modules) RegisterModule_Template() *Module {
  31. return this.newModule(MInfo{
  32. WantDB: false,
  33. Mount: "template",
  34. Name: "Template",
  35. Order: 802,
  36. System: true,
  37. Icon: assets.SysSvgIconGear,
  38. Sub: &[]MISub{
  39. {Mount: "default", Name: "Theme", Show: true, Icon: assets.SysSvgIconGear},
  40. },
  41. }, nil, func(wrap *wrapper.Wrapper) (string, string, string) {
  42. content := ""
  43. sidebar := ""
  44. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  45. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  46. {Name: "Theme"},
  47. })
  48. files := this.template_GetThemeFiles(wrap)
  49. if len(files) > 0 {
  50. selected_file, _ := url.QueryUnescape(wrap.R.URL.Query().Get("file"))
  51. if !(selected_file != "" && utils.InArrayString(files, selected_file)) {
  52. selected_file = files[0]
  53. }
  54. list_of_files := ``
  55. for _, file := range files {
  56. selected := ""
  57. if file == selected_file {
  58. selected = " selected"
  59. }
  60. list_of_files += `<option value="` + html.EscapeString(file) +
  61. `"` + selected + `>` + html.EscapeString(file) + `</option>`
  62. }
  63. fcont := []byte(``)
  64. fcont, _ = ioutil.ReadFile(wrap.DTemplate + string(os.PathSeparator) + selected_file)
  65. content += builder.DataForm(wrap, []builder.DataFormField{
  66. {
  67. Kind: builder.DFKHidden,
  68. Name: "action",
  69. Value: "template-edit-theme-file",
  70. },
  71. {
  72. Kind: builder.DFKText,
  73. Caption: "Theme file",
  74. Name: "file",
  75. Value: "0",
  76. CallBack: func(field *builder.DataFormField) string {
  77. return `<div class="form-group n1">` +
  78. `<div class="row">` +
  79. `<div class="col-md-12">` +
  80. `<div>` +
  81. `<select class="form-control ignore-lost-data" id="lbl_file" name="file" onchange="setTimeout(function(){$('#lbl_file').val('` + selected_file + `')},500);document.location='/cp/` + wrap.CurrModule + `/?file='+encodeURI(this.value);">` +
  82. list_of_files +
  83. `</select>` +
  84. `</div>` +
  85. `</div>` +
  86. `</div>` +
  87. `</div>`
  88. },
  89. },
  90. {
  91. Kind: builder.DFKText,
  92. CallBack: func(field *builder.DataFormField) string {
  93. return `<div class="form-group last"><div class="row"><div class="col-12"><textarea class="form-control autosize use-tab-key" id="lbl_content" name="content" placeholder="" autocomplete="off">` + html.EscapeString(string(fcont)) + `</textarea></div></div></div>`
  94. },
  95. },
  96. {
  97. Kind: builder.DFKMessage,
  98. CallBack: func(field *builder.DataFormField) string {
  99. return `<div class="row"><div class="col-12"><div class="sys-messages"></div></div></div>`
  100. },
  101. },
  102. {
  103. Kind: builder.DFKSubmit,
  104. CallBack: func(field *builder.DataFormField) string {
  105. return `<div class="row d-lg-none"><div class="col-12"><button type="submit" class="btn btn-primary" data-target="add-edit-button">Save</button></div></div>`
  106. },
  107. },
  108. })
  109. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  110. } else {
  111. content += `<div class="sys-messages">
  112. <div class="alert alert-warning" role="alert">
  113. <strong>Error!</strong> No any file found in theme folder
  114. </div>
  115. </div>`
  116. }
  117. }
  118. return this.getSidebarModules(wrap), content, sidebar
  119. })
  120. }