module_templates_act_create_theme_file.go 999 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package modules
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "golang-fave/engine/utils"
  6. "golang-fave/engine/wrapper"
  7. )
  8. func (this *Modules) RegisterAction_TemplatesCreateThemeFile() *Action {
  9. return this.newAction(AInfo{
  10. Mount: "templates-create-theme-file",
  11. WantAdmin: true,
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_name := utils.Trim(wrap.R.FormValue("name"))
  14. pf_content := wrap.R.FormValue("content")
  15. if pf_name == "" {
  16. wrap.MsgError(`Please specify file name`)
  17. return
  18. }
  19. // Check normal file name here
  20. template_file := wrap.DTemplate + string(os.PathSeparator) + pf_name + ".html"
  21. if utils.IsFileExists(template_file) {
  22. wrap.MsgError(`File is already exists`)
  23. return
  24. }
  25. // Save content to file
  26. err := ioutil.WriteFile(template_file, []byte(pf_content), 0664)
  27. if err != nil {
  28. wrap.MsgError(err.Error())
  29. return
  30. }
  31. wrap.ResetCacheBlocks()
  32. // Redirect to created file in editor
  33. // Reload current page
  34. wrap.Write(`window.location.reload(false);`)
  35. })
  36. }