miniflux/server/ui/controller/settings.go

97 lines
2.5 KiB
Go
Raw Normal View History

2017-11-20 06:10:04 +01:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package controller
import (
2017-11-28 06:30:04 +01:00
"log"
2017-11-20 06:10:04 +01:00
"github.com/miniflux/miniflux2/locale"
"github.com/miniflux/miniflux2/model"
"github.com/miniflux/miniflux2/server/core"
"github.com/miniflux/miniflux2/server/ui/form"
)
2017-11-28 06:30:04 +01:00
// ShowSettings shows the settings page.
2017-11-20 06:10:04 +01:00
func (c *Controller) ShowSettings(ctx *core.Context, request *core.Request, response *core.Response) {
2017-11-22 03:37:08 +01:00
user := ctx.LoggedUser()
2017-11-20 06:10:04 +01:00
args, err := c.getSettingsFormTemplateArgs(ctx, user, nil)
if err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().ServerError(err)
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.HTML().Render("settings", args)
2017-11-20 06:10:04 +01:00
}
2017-11-28 06:30:04 +01:00
// UpdateSettings update the settings.
2017-11-20 06:10:04 +01:00
func (c *Controller) UpdateSettings(ctx *core.Context, request *core.Request, response *core.Response) {
2017-11-22 03:37:08 +01:00
user := ctx.LoggedUser()
2017-11-20 06:10:04 +01:00
2017-11-22 03:14:45 +01:00
settingsForm := form.NewSettingsForm(request.Request())
2017-11-20 06:10:04 +01:00
args, err := c.getSettingsFormTemplateArgs(ctx, user, settingsForm)
if err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().ServerError(err)
2017-11-20 06:10:04 +01:00
return
}
if err := settingsForm.Validate(); err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().Render("settings", args.Merge(tplParams{
2017-11-20 06:10:04 +01:00
"form": settingsForm,
"errorMessage": err.Error(),
}))
return
}
if c.store.AnotherUserExists(user.ID, settingsForm.Username) {
2017-11-22 03:30:16 +01:00
response.HTML().Render("settings", args.Merge(tplParams{
2017-11-20 06:10:04 +01:00
"form": settingsForm,
"errorMessage": "This user already exists.",
}))
return
}
err = c.store.UpdateUser(settingsForm.Merge(user))
if err != nil {
log.Println(err)
2017-11-22 03:30:16 +01:00
response.HTML().Render("settings", args.Merge(tplParams{
2017-11-20 06:10:04 +01:00
"form": settingsForm,
"errorMessage": "Unable to update this user.",
}))
return
}
2017-11-22 03:37:08 +01:00
response.Redirect(ctx.Route("settings"))
2017-11-20 06:10:04 +01:00
}
func (c *Controller) getSettingsFormTemplateArgs(ctx *core.Context, user *model.User, settingsForm *form.SettingsForm) (tplParams, error) {
args, err := c.getCommonTemplateArgs(ctx)
if err != nil {
return args, err
}
if settingsForm == nil {
args["form"] = form.SettingsForm{
2017-12-03 02:04:01 +01:00
Username: user.Username,
Theme: user.Theme,
Language: user.Language,
Timezone: user.Timezone,
EntryDirection: user.EntryDirection,
2017-11-20 06:10:04 +01:00
}
} else {
args["form"] = settingsForm
}
args["menu"] = "settings"
2017-11-28 06:30:04 +01:00
args["themes"] = model.Themes()
args["languages"] = locale.AvailableLanguages()
args["timezones"], err = c.store.Timezones()
2017-11-20 06:10:04 +01:00
if err != nil {
return args, err
}
return args, nil
}