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.
|
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
package ui
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
2018-01-03 07:04:48 +01:00
|
|
|
"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"
|
2018-01-03 07:04:48 +01:00
|
|
|
"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.
|
2018-01-03 07:04:48 +01:00
|
|
|
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.
|
2018-01-03 07:04:48 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-03 07:04:48 +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
|
|
|
|
}
|