miniflux/ui/settings.go

97 lines
2.6 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 ui
2017-11-20 06:10:04 +01:00
import (
"github.com/miniflux/miniflux/http/handler"
2017-12-13 06:48:13 +01:00
"github.com/miniflux/miniflux/locale"
2017-12-16 03:55:57 +01:00
"github.com/miniflux/miniflux/logger"
2017-12-13 06:48:13 +01:00
"github.com/miniflux/miniflux/model"
"github.com/miniflux/miniflux/ui/form"
2017-11-20 06:10:04 +01:00
)
2017-11-28 06:30:04 +01:00
// ShowSettings shows the settings page.
func (c *Controller) ShowSettings(ctx *handler.Context, request *handler.Request, response *handler.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.
func (c *Controller) UpdateSettings(ctx *handler.Context, request *handler.Request, response *handler.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 {
2017-12-16 03:55:57 +01:00
logger.Error("[Controller:UpdateSettings] %v", 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-12-17 03:07:53 +01:00
ctx.SetFlashMessage(ctx.Translate("Preferences saved!"))
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 *handler.Context, user *model.User, settingsForm *form.SettingsForm) (tplParams, error) {
2017-11-20 06:10:04 +01:00
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
}