github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/web/manage/setting/setting.go (about) 1 package managesetting 2 3 import ( 4 "fmt" 5 6 "github.com/ngocphuongnb/tetua/app/config" 7 "github.com/ngocphuongnb/tetua/app/entities" 8 "github.com/ngocphuongnb/tetua/app/repositories" 9 "github.com/ngocphuongnb/tetua/app/server" 10 "github.com/ngocphuongnb/tetua/app/services" 11 "github.com/ngocphuongnb/tetua/app/utils" 12 "github.com/ngocphuongnb/tetua/views" 13 ) 14 15 func Settings(c server.Context) (err error) { 16 c.Meta().Title = "Settings" 17 overrideSettings := []*config.SettingItem{} 18 if config.Setting("app_base_url") == "" { 19 overrideSettings = append(overrideSettings, &config.SettingItem{ 20 Type: "input", 21 Name: "app_base_url", 22 Value: c.BaseUrl(), 23 }) 24 } 25 if config.Setting("file_base_url") == "" { 26 overrideSettings = append(overrideSettings, &config.SettingItem{ 27 Type: "input", 28 Name: "file_base_url", 29 Value: c.BaseUrl(), 30 }) 31 } 32 if config.Setting("footer_content") == "" { 33 overrideSettings = append(overrideSettings, &config.SettingItem{ 34 Type: "textarea", 35 Name: "footer_content", 36 Value: fmt.Sprintf( 37 `%s - %s.<br/>Built on Tetua - An open source CMS platform for Blogging.<br/>%s © 2021 - 2022.`, 38 config.Setting("app_name"), 39 config.Setting("app_desc"), 40 config.Setting("app_name"), 41 ), 42 }) 43 } 44 45 if len(overrideSettings) > 0 { 46 config.Settings(overrideSettings) 47 } 48 49 return c.Render(views.ManageSettings(config.AllSettings())) 50 } 51 52 func Save(c server.Context) (err error) { 53 data := &config.SettingsMutation{} 54 if err = c.BodyParser(data); err != nil { 55 c.WithError("Error parsing body", err) 56 } 57 58 var settingItems = utils.SliceFilter(data.Settings, func(s *config.SettingItem) bool { 59 return s != nil 60 }) 61 62 for _, s := range config.AllSettings() { 63 if s.Type != "image" { 64 continue 65 } 66 67 if image, err := services.SaveFile(c, s.Name); err != nil { 68 c.WithError("Error saving setting image: "+s.Name, err) 69 } else if image != nil { 70 settingItems = append(settingItems, &config.SettingItem{ 71 Type: "image", 72 Name: s.Name, 73 Value: image.Url(), 74 }) 75 } 76 } 77 78 var settings = utils.SliceMap(settingItems, func(s *config.SettingItem) *entities.Setting { 79 return &entities.Setting{ 80 Name: s.Name, 81 Value: s.Value, 82 Type: s.Type, 83 } 84 }) 85 86 if err = repositories.Setting.Save(c.Context(), settings); err != nil { 87 c.WithError("Error saving settings", err) 88 } 89 90 config.Settings(settingItems) 91 92 return c.Redirect("/manage/settings") 93 }