2018-05-27 09:36:35 +00:00
|
|
|
package panel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
"github.com/Azareal/Gosora/common"
|
2018-11-01 06:43:56 +00:00
|
|
|
"github.com/Azareal/Gosora/common/phrases"
|
2018-05-27 09:36:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Settings(w http.ResponseWriter, r *http.Request, user common.User) common.RouteError {
|
2018-06-17 07:28:18 +00:00
|
|
|
basePage, ferr := buildBasePage(w, r, &user, "settings", "settings")
|
2018-05-27 09:36:35 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if !user.Perms.EditSettings {
|
|
|
|
return common.NoPermissions(w, r, user)
|
|
|
|
}
|
|
|
|
|
2018-06-17 07:28:18 +00:00
|
|
|
settings, err := basePage.Settings.BypassGetAll()
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
2018-11-01 06:43:56 +00:00
|
|
|
settingPhrases := phrases.GetAllSettingPhrases()
|
2018-05-27 09:36:35 +00:00
|
|
|
|
|
|
|
var settingList []*common.PanelSetting
|
|
|
|
for _, settingPtr := range settings {
|
|
|
|
setting := settingPtr.Copy()
|
|
|
|
if setting.Type == "list" {
|
|
|
|
llist := settingPhrases[setting.Name+"_label"]
|
|
|
|
labels := strings.Split(llist, ",")
|
|
|
|
conv, err := strconv.Atoi(setting.Content)
|
|
|
|
if err != nil {
|
|
|
|
return common.LocalError("The setting '"+setting.Name+"' can't be converted to an integer", w, r, user)
|
|
|
|
}
|
|
|
|
setting.Content = labels[conv-1]
|
|
|
|
} else if setting.Type == "bool" {
|
|
|
|
if setting.Content == "1" {
|
|
|
|
setting.Content = "Yes"
|
|
|
|
} else {
|
|
|
|
setting.Content = "No"
|
|
|
|
}
|
|
|
|
} else if setting.Type == "html-attribute" {
|
|
|
|
setting.Type = "textarea"
|
|
|
|
}
|
2018-11-01 06:43:56 +00:00
|
|
|
settingList = append(settingList, &common.PanelSetting{setting, phrases.GetSettingPhrase(setting.Name)})
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 07:28:18 +00:00
|
|
|
pi := common.PanelPage{basePage, tList, settingList}
|
2018-10-02 04:09:17 +00:00
|
|
|
return renderTemplate("panel_settings", w, r, user, &pi)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SettingEdit(w http.ResponseWriter, r *http.Request, user common.User, sname string) common.RouteError {
|
2018-06-17 07:28:18 +00:00
|
|
|
basePage, ferr := buildBasePage(w, r, &user, "edit_setting", "settings")
|
2018-05-27 09:36:35 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if !user.Perms.EditSettings {
|
|
|
|
return common.NoPermissions(w, r, user)
|
|
|
|
}
|
|
|
|
|
2018-06-17 07:28:18 +00:00
|
|
|
setting, err := basePage.Settings.BypassGet(sname)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return common.LocalError("The setting you want to edit doesn't exist.", w, r, user)
|
|
|
|
} else if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
var itemList []common.OptionLabel
|
|
|
|
if setting.Type == "list" {
|
2018-11-01 06:43:56 +00:00
|
|
|
llist := phrases.GetSettingPhrase(setting.Name + "_label")
|
2018-05-27 09:36:35 +00:00
|
|
|
conv, err := strconv.Atoi(setting.Content)
|
|
|
|
if err != nil {
|
|
|
|
return common.LocalError("The value of this setting couldn't be converted to an integer", w, r, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
for index, label := range strings.Split(llist, ",") {
|
|
|
|
itemList = append(itemList, common.OptionLabel{
|
|
|
|
Label: label,
|
|
|
|
Value: index + 1,
|
|
|
|
Selected: conv == (index + 1),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else if setting.Type == "html-attribute" {
|
|
|
|
setting.Type = "textarea"
|
|
|
|
}
|
|
|
|
|
2018-11-01 06:43:56 +00:00
|
|
|
pSetting := &common.PanelSetting{setting, phrases.GetSettingPhrase(setting.Name)}
|
2018-06-17 07:28:18 +00:00
|
|
|
pi := common.PanelSettingPage{basePage, itemList, pSetting}
|
2018-10-02 04:09:17 +00:00
|
|
|
return renderTemplate("panel_setting", w, r, user, &pi)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SettingEditSubmit(w http.ResponseWriter, r *http.Request, user common.User, sname string) common.RouteError {
|
|
|
|
headerLite, ferr := common.SimplePanelUserCheck(w, r, &user)
|
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if !user.Perms.EditSettings {
|
|
|
|
return common.NoPermissions(w, r, user)
|
|
|
|
}
|
|
|
|
|
2018-05-31 06:51:31 +00:00
|
|
|
scontent := common.SanitiseBody(r.PostFormValue("setting-value"))
|
2018-11-13 06:51:34 +00:00
|
|
|
rerr := headerLite.Settings.Update(sname, scontent)
|
|
|
|
if rerr != nil {
|
|
|
|
return rerr
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/panel/settings/", http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|