2018-09-30 14:43:46 +00:00
|
|
|
package panel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2019-01-21 12:27:59 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2018-09-30 14:43:46 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2019-09-29 05:10:05 +00:00
|
|
|
p "github.com/Azareal/Gosora/common/phrases"
|
2018-09-30 14:43:46 +00:00
|
|
|
)
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
func Themes(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError {
|
|
|
|
basePage, ferr := buildBasePage(w, r, u, "themes", "themes")
|
2018-09-30 14:43:46 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManageThemes {
|
|
|
|
return c.NoPermissions(w, r, u)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
var pThemeList, vThemeList []*c.Theme
|
|
|
|
for _, theme := range c.Themes {
|
2018-09-30 14:43:46 +00:00
|
|
|
if theme.HideFromThemes {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if theme.ForkOf == "" {
|
|
|
|
pThemeList = append(pThemeList, theme)
|
|
|
|
} else {
|
|
|
|
vThemeList = append(vThemeList, theme)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
pi := c.PanelThemesPage{basePage, pThemeList, vThemeList}
|
2019-10-06 00:34:09 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "panel_themes", "", "panel_themes", &pi})
|
2018-10-02 04:09:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
func ThemesSetDefault(w http.ResponseWriter, r *http.Request, u *c.User, uname string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, u)
|
2018-10-02 04:09:17 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManageThemes {
|
|
|
|
return c.NoPermissions(w, r, u)
|
2018-10-02 04:09:17 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
theme, ok := c.Themes[uname]
|
2018-10-02 04:09:17 +00:00
|
|
|
if !ok {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError("The theme isn't registered in the system", w, r, u)
|
2018-10-02 04:09:17 +00:00
|
|
|
}
|
|
|
|
if theme.Disabled {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError("You must not enable this theme", w, r, u)
|
2018-10-02 04:09:17 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
err := c.UpdateDefaultTheme(theme)
|
2018-10-02 04:09:17 +00:00
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-10-02 04:09:17 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
err = c.AdminLogs.CreateExtra("set_default", 0, "theme", u.GetIP(), u.ID, c.SanitiseSingleLine(theme.Name))
|
2019-11-10 02:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2018-10-02 04:09:17 +00:00
|
|
|
|
|
|
|
http.Redirect(w, r, "/panel/themes/", http.StatusSeeOther)
|
|
|
|
return nil
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
func ThemesMenus(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError {
|
|
|
|
basePage, ferr := buildBasePage(w, r, u, "themes_menus", "themes")
|
2018-09-30 14:43:46 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManageThemes {
|
|
|
|
return c.NoPermissions(w, r, u)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
var menuList []c.PanelMenuListItem
|
|
|
|
for mid, list := range c.Menus.GetAllMap() {
|
2019-10-06 00:34:09 +00:00
|
|
|
name := ""
|
2018-09-30 14:43:46 +00:00
|
|
|
if mid == 1 {
|
2019-09-29 05:10:05 +00:00
|
|
|
name = p.GetTmplPhrase("panel_themes_menus_main")
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
menuList = append(menuList, c.PanelMenuListItem{
|
2018-09-30 14:43:46 +00:00
|
|
|
Name: name,
|
|
|
|
ID: mid,
|
|
|
|
ItemCount: len(list.List),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-06 00:34:09 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "", "", "panel_themes_menus", &c.PanelMenuListPage{basePage, menuList}})
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
func ThemesMenusEdit(w http.ResponseWriter, r *http.Request, u *c.User, smid string) c.RouteError {
|
2018-09-30 14:43:46 +00:00
|
|
|
// TODO: Something like Menu #1 for the title?
|
2020-07-14 21:50:29 +00:00
|
|
|
basePage, ferr := buildBasePage(w, r, u, "themes_menus_edit", "themes")
|
2018-09-30 14:43:46 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManageThemes {
|
|
|
|
return c.NoPermissions(w, r, u)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
basePage.Header.AddScript("Sortable-1.4.0/Sortable.min.js")
|
2019-04-28 22:17:08 +00:00
|
|
|
basePage.Header.AddScriptAsync("panel_menu_items.js")
|
2018-09-30 14:43:46 +00:00
|
|
|
|
|
|
|
mid, err := strconv.Atoi(smid)
|
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError(p.GetErrorPhrase("url_id_must_be_integer"), w, r, u)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
menuHold, err := c.Menus.Get(mid)
|
2018-09-30 14:43:46 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.NotFound(w, r, basePage.Header)
|
2018-09-30 14:43:46 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
var menuList []c.MenuItem
|
2018-09-30 14:43:46 +00:00
|
|
|
for _, item := range menuHold.List {
|
2019-09-29 05:10:05 +00:00
|
|
|
menuTmpls := map[string]c.MenuTmpl{
|
2018-09-30 14:43:46 +00:00
|
|
|
item.TmplName: menuHold.Parse(item.Name, []byte("{{.Name}}")),
|
|
|
|
}
|
|
|
|
var renderBuffer [][]byte
|
|
|
|
var variableIndices []int
|
|
|
|
renderBuffer, _ = menuHold.ScanItem(menuTmpls, item, renderBuffer, variableIndices)
|
|
|
|
|
|
|
|
var out string
|
|
|
|
for _, renderItem := range renderBuffer {
|
|
|
|
out += string(renderItem)
|
|
|
|
}
|
|
|
|
item.Name = out
|
|
|
|
if item.Name == "" {
|
|
|
|
item.Name = "???"
|
|
|
|
}
|
|
|
|
menuList = append(menuList, item)
|
|
|
|
}
|
|
|
|
|
2019-10-06 00:34:09 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "", "", "panel_themes_menus_items", &c.PanelMenuPage{basePage, mid, menuList}})
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
func ThemesMenuItemEdit(w http.ResponseWriter, r *http.Request, u *c.User, sitemID string) c.RouteError {
|
2018-09-30 14:43:46 +00:00
|
|
|
// TODO: Something like Menu #1 for the title?
|
2020-07-14 21:50:29 +00:00
|
|
|
basePage, ferr := buildBasePage(w, r, u, "themes_menus_edit", "themes")
|
2018-09-30 14:43:46 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManageThemes {
|
|
|
|
return c.NoPermissions(w, r, u)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
itemID, err := strconv.Atoi(sitemID)
|
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError(p.GetErrorPhrase("url_id_must_be_integer"), w, r, u)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
menuItem, err := c.Menus.ItemStore().Get(itemID)
|
2018-09-30 14:43:46 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.NotFound(w, r, basePage.Header)
|
2018-09-30 14:43:46 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 00:34:09 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "", "", "panel_themes_menus_item_edit", &c.PanelMenuItemPage{basePage, menuItem}})
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 00:34:09 +00:00
|
|
|
func themesMenuItemSetters(r *http.Request, i c.MenuItem) c.MenuItem {
|
2019-09-29 05:10:05 +00:00
|
|
|
getItem := func(name string) string {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.SanitiseSingleLine(r.PostFormValue("item-" + name))
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
2019-10-06 00:34:09 +00:00
|
|
|
i.Name = getItem("name")
|
|
|
|
i.HTMLID = getItem("htmlid")
|
|
|
|
i.CSSClass = getItem("cssclass")
|
|
|
|
i.Position = getItem("position")
|
|
|
|
if i.Position != "left" && i.Position != "right" {
|
|
|
|
i.Position = "left"
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
2019-10-06 00:34:09 +00:00
|
|
|
i.Path = getItem("path")
|
|
|
|
i.Aria = getItem("aria")
|
|
|
|
i.Tooltip = getItem("tooltip")
|
|
|
|
i.TmplName = getItem("tmplname")
|
2019-11-10 02:37:53 +00:00
|
|
|
i.GuestOnly = false
|
2018-09-30 14:43:46 +00:00
|
|
|
|
|
|
|
switch getItem("permissions") {
|
|
|
|
case "everyone":
|
2019-10-06 00:34:09 +00:00
|
|
|
i.MemberOnly = false
|
|
|
|
i.SuperModOnly = false
|
|
|
|
i.AdminOnly = false
|
2018-09-30 14:43:46 +00:00
|
|
|
case "guest-only":
|
2019-10-06 00:34:09 +00:00
|
|
|
i.GuestOnly = true
|
|
|
|
i.MemberOnly = false
|
|
|
|
i.SuperModOnly = false
|
|
|
|
i.AdminOnly = false
|
2018-09-30 14:43:46 +00:00
|
|
|
case "member-only":
|
2019-10-06 00:34:09 +00:00
|
|
|
i.MemberOnly = true
|
|
|
|
i.SuperModOnly = false
|
|
|
|
i.AdminOnly = false
|
2018-09-30 14:43:46 +00:00
|
|
|
case "supermod-only":
|
2019-10-06 00:34:09 +00:00
|
|
|
i.MemberOnly = true
|
|
|
|
i.SuperModOnly = true
|
|
|
|
i.AdminOnly = false
|
2018-09-30 14:43:46 +00:00
|
|
|
case "admin-only":
|
2019-10-06 00:34:09 +00:00
|
|
|
i.MemberOnly = true
|
|
|
|
i.SuperModOnly = true
|
|
|
|
i.AdminOnly = true
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
2019-10-06 00:34:09 +00:00
|
|
|
return i
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
func ThemesMenuItemEditSubmit(w http.ResponseWriter, r *http.Request, u *c.User, sitemID string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, u)
|
2018-09-30 14:43:46 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2019-10-06 00:34:09 +00:00
|
|
|
js := r.PostFormValue("js") == "1"
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManageThemes {
|
|
|
|
return c.NoPermissionsJSQ(w, r, u, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
itemID, err := strconv.Atoi(sitemID)
|
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalErrorJSQ(p.GetErrorPhrase("id_must_be_integer"), w, r, u, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
menuItem, err := c.Menus.ItemStore().Get(itemID)
|
2018-09-30 14:43:46 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalErrorJSQ("This item doesn't exist.", w, r, u, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
} else if err != nil {
|
2019-09-29 05:10:05 +00:00
|
|
|
return c.InternalErrorJSQ(err, w, r, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
//menuItem = menuItem.Copy() // If we switch this for a pointer, we might need this as a scratchpad
|
|
|
|
menuItem = themesMenuItemSetters(r, menuItem)
|
|
|
|
|
|
|
|
err = menuItem.Commit()
|
|
|
|
if err != nil {
|
2019-09-29 05:10:05 +00:00
|
|
|
return c.InternalErrorJSQ(err, w, r, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
err = c.AdminLogs.Create("edit", menuItem.ID, "menu_item", u.GetIP(), u.ID)
|
2019-11-10 02:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
2019-09-29 05:10:05 +00:00
|
|
|
return successRedirect("/panel/themes/menus/item/edit/"+strconv.Itoa(itemID), w, r, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
func ThemesMenuItemCreateSubmit(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, u)
|
2018-09-30 14:43:46 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2019-10-06 00:34:09 +00:00
|
|
|
js := r.PostFormValue("js") == "1"
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManageThemes {
|
|
|
|
return c.NoPermissionsJSQ(w, r, u, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
2019-11-10 02:37:53 +00:00
|
|
|
|
2018-09-30 14:43:46 +00:00
|
|
|
smenuID := r.PostFormValue("mid")
|
|
|
|
if smenuID == "" {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalErrorJSQ("No menuID provided", w, r, u, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
menuID, err := strconv.Atoi(smenuID)
|
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalErrorJSQ(p.GetErrorPhrase("id_must_be_integer"), w, r, u, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
menuItem := c.MenuItem{MenuID: menuID}
|
2018-09-30 14:43:46 +00:00
|
|
|
menuItem = themesMenuItemSetters(r, menuItem)
|
|
|
|
itemID, err := menuItem.Create()
|
|
|
|
if err != nil {
|
2019-09-29 05:10:05 +00:00
|
|
|
return c.InternalErrorJSQ(err, w, r, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
err = c.AdminLogs.Create("create", itemID, "menu_item", u.GetIP(), u.ID)
|
2019-11-10 02:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
2019-09-29 05:10:05 +00:00
|
|
|
return successRedirect("/panel/themes/menus/item/edit/"+strconv.Itoa(itemID), w, r, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
func ThemesMenuItemDeleteSubmit(w http.ResponseWriter, r *http.Request, u *c.User, sitemID string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, u)
|
2018-09-30 14:43:46 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2019-10-06 00:34:09 +00:00
|
|
|
js := r.PostFormValue("js") == "1"
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManageThemes {
|
|
|
|
return c.NoPermissionsJSQ(w, r, u, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
itemID, err := strconv.Atoi(sitemID)
|
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalErrorJSQ(p.GetErrorPhrase("id_must_be_integer"), w, r, u, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
menuItem, err := c.Menus.ItemStore().Get(itemID)
|
2018-09-30 14:43:46 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalErrorJSQ("This item doesn't exist.", w, r, u, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
} else if err != nil {
|
2019-09-29 05:10:05 +00:00
|
|
|
return c.InternalErrorJSQ(err, w, r, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
//menuItem = menuItem.Copy() // If we switch this for a pointer, we might need this as a scratchpad
|
|
|
|
|
|
|
|
err = menuItem.Delete()
|
|
|
|
if err != nil {
|
2019-09-29 05:10:05 +00:00
|
|
|
return c.InternalErrorJSQ(err, w, r, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
err = c.AdminLogs.Create("delete", menuItem.ID, "menu_item", u.GetIP(), u.ID)
|
2019-11-10 02:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
2019-09-29 05:10:05 +00:00
|
|
|
return successRedirect("/panel/themes/menus/", w, r, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
func ThemesMenuItemOrderSubmit(w http.ResponseWriter, r *http.Request, u *c.User, smid string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, u)
|
2018-09-30 14:43:46 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2019-10-06 00:34:09 +00:00
|
|
|
js := r.PostFormValue("js") == "1"
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManageThemes {
|
|
|
|
return c.NoPermissionsJSQ(w, r, u, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mid, err := strconv.Atoi(smid)
|
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalErrorJSQ(p.GetErrorPhrase("id_must_be_integer"), w, r, u, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
menuHold, err := c.Menus.Get(mid)
|
2018-09-30 14:43:46 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalErrorJSQ("Can't find menu", w, r, u, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
} else if err != nil {
|
2019-09-29 05:10:05 +00:00
|
|
|
return c.InternalErrorJSQ(err, w, r, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sitems := strings.TrimSuffix(strings.TrimPrefix(r.PostFormValue("items"), "{"), "}")
|
|
|
|
//fmt.Printf("sitems: %+v\n", sitems)
|
|
|
|
|
2019-09-29 05:10:05 +00:00
|
|
|
updateMap := make(map[int]int)
|
2018-09-30 14:43:46 +00:00
|
|
|
for index, smiid := range strings.Split(sitems, ",") {
|
|
|
|
miid, err := strconv.Atoi(smiid)
|
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalErrorJSQ("Invalid integer in menu item list", w, r, u, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
|
|
|
updateMap[miid] = index
|
|
|
|
}
|
|
|
|
menuHold.UpdateOrder(updateMap)
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
err = c.AdminLogs.Create("suborder", menuHold.MenuID, "menu", u.GetIP(), u.ID)
|
2019-11-10 02:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
2019-09-29 05:10:05 +00:00
|
|
|
return successRedirect("/panel/themes/menus/edit/"+strconv.Itoa(mid), w, r, js)
|
2018-09-30 14:43:46 +00:00
|
|
|
}
|
2019-01-21 12:27:59 +00:00
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
func ThemesWidgets(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError {
|
|
|
|
basePage, ferr := buildBasePage(w, r, u, "themes_widgets", "themes")
|
2019-01-21 12:27:59 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManageThemes {
|
|
|
|
return c.NoPermissions(w, r, u)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
|
|
|
basePage.Header.AddScript("widgets.js")
|
|
|
|
|
2019-10-06 00:34:09 +00:00
|
|
|
docks := make(map[string][]c.WidgetEdit)
|
2019-04-19 06:36:26 +00:00
|
|
|
for _, name := range c.GetDockList() {
|
2019-01-22 12:42:46 +00:00
|
|
|
if name == "leftOfNav" || name == "rightOfNav" {
|
|
|
|
continue
|
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
var widgets []c.WidgetEdit
|
|
|
|
for _, widget := range c.GetDock(name) {
|
2019-10-06 00:34:09 +00:00
|
|
|
data := make(map[string]string)
|
2019-01-21 12:27:59 +00:00
|
|
|
err := json.Unmarshal([]byte(widget.RawBody), &data)
|
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
widgets = append(widgets, c.WidgetEdit{widget, data})
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
|
|
|
docks[name] = widgets
|
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
pi := c.PanelWidgetListPage{basePage, docks, c.WidgetEdit{&c.Widget{ID: 0, Type: "simple"}, make(map[string]string)}}
|
2019-10-06 00:34:09 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "", "", "panel_themes_widgets", pi})
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
func widgetsParseInputs(r *http.Request, widget *c.Widget) (*c.WidgetEdit, error) {
|
2019-10-06 00:34:09 +00:00
|
|
|
data := make(map[string]string)
|
|
|
|
widget.Enabled = r.FormValue("wenabled") == "1"
|
2019-01-21 12:27:59 +00:00
|
|
|
widget.Location = r.FormValue("wlocation")
|
|
|
|
if widget.Location == "" {
|
|
|
|
return nil, errors.New("You need to specify a location for this widget.")
|
|
|
|
}
|
|
|
|
widget.Side = r.FormValue("wside")
|
2019-04-19 06:36:26 +00:00
|
|
|
if !c.HasDock(widget.Side) {
|
2019-01-21 12:27:59 +00:00
|
|
|
return nil, errors.New("The widget dock you specified doesn't exist.")
|
|
|
|
}
|
|
|
|
|
2019-10-06 00:34:09 +00:00
|
|
|
wtype := r.FormValue("wtype")
|
2019-01-21 12:27:59 +00:00
|
|
|
switch wtype {
|
|
|
|
case "simple", "about":
|
|
|
|
data["Name"] = r.FormValue("wname")
|
|
|
|
if data["Name"] == "" {
|
|
|
|
return nil, errors.New("You need to specify a title for this widget.")
|
|
|
|
}
|
|
|
|
data["Text"] = r.FormValue("wtext")
|
|
|
|
if data["Text"] == "" {
|
|
|
|
return nil, errors.New("You need to fill in the body for this widget.")
|
|
|
|
}
|
|
|
|
widget.Type = wtype // ? - Are we sure we should be directly assigning user provided data even if it's validated?
|
2019-01-22 12:06:55 +00:00
|
|
|
case "wol", "wol_context", "search_and_filter":
|
2019-01-21 12:27:59 +00:00
|
|
|
widget.Type = wtype // ? - Are we sure we should be directly assigning user provided data even if it's validated?
|
|
|
|
default:
|
|
|
|
return nil, errors.New("Unknown widget type")
|
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
return &c.WidgetEdit{widget, data}, nil
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ThemesWidgetsEditSubmit is an action which is triggered when someone sends an update request for a widget
|
2020-07-14 21:50:29 +00:00
|
|
|
func ThemesWidgetsEditSubmit(w http.ResponseWriter, r *http.Request, u *c.User, swid string) c.RouteError {
|
2019-01-22 12:06:55 +00:00
|
|
|
//fmt.Println("in ThemesWidgetsEditSubmit")
|
2020-07-14 21:50:29 +00:00
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, u)
|
2019-01-21 12:27:59 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2019-10-06 00:34:09 +00:00
|
|
|
js := r.PostFormValue("js") == "1"
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManageThemes {
|
|
|
|
return c.NoPermissionsJSQ(w, r, u, js)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wid, err := strconv.Atoi(swid)
|
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalErrorJSQ(p.GetErrorPhrase("id_must_be_integer"), w, r, u, js)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
widget, err := c.Widgets.Get(wid)
|
2019-01-21 12:27:59 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-09-29 05:10:05 +00:00
|
|
|
return c.NotFoundJSQ(w, r, nil, js)
|
2019-01-21 12:27:59 +00:00
|
|
|
} else if err != nil {
|
2019-09-29 05:10:05 +00:00
|
|
|
return c.InternalErrorJSQ(err, w, r, js)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ewidget, err := widgetsParseInputs(r, widget.Copy())
|
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalErrorJSQ(err.Error(), w, r, u, js)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ewidget.Commit()
|
|
|
|
if err != nil {
|
2019-09-29 05:10:05 +00:00
|
|
|
return c.InternalErrorJSQ(err, w, r, js)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
err = c.AdminLogs.Create("edit", widget.ID, "widget", u.GetIP(), u.ID)
|
2019-11-10 02:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2019-01-21 12:27:59 +00:00
|
|
|
|
2019-09-29 05:10:05 +00:00
|
|
|
return successRedirect("/panel/themes/widgets/", w, r, js)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ThemesWidgetsCreateSubmit is an action which is triggered when someone sends a create request for a widget
|
2020-07-14 21:50:29 +00:00
|
|
|
func ThemesWidgetsCreateSubmit(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError {
|
2019-10-06 00:34:09 +00:00
|
|
|
js := r.PostFormValue("js") == "1"
|
2020-07-14 21:50:29 +00:00
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, u)
|
2019-01-21 12:27:59 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManageThemes {
|
|
|
|
return c.NoPermissionsJSQ(w, r, u, js)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
ewidget, err := widgetsParseInputs(r, &c.Widget{})
|
2019-01-21 12:27:59 +00:00
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalErrorJSQ(err.Error(), w, r, u, js)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
2019-11-10 02:37:53 +00:00
|
|
|
wid, err := ewidget.Create()
|
2019-01-21 12:27:59 +00:00
|
|
|
if err != nil {
|
2019-09-29 05:10:05 +00:00
|
|
|
return c.InternalErrorJSQ(err, w, r, js)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
err = c.AdminLogs.Create("create", wid, "widget", u.GetIP(), u.ID)
|
2019-11-10 02:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2019-01-21 12:27:59 +00:00
|
|
|
|
2019-09-29 05:10:05 +00:00
|
|
|
return successRedirect("/panel/themes/widgets/", w, r, js)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
func ThemesWidgetsDeleteSubmit(w http.ResponseWriter, r *http.Request, u *c.User, swid string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, u)
|
2019-01-21 12:27:59 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2019-10-06 00:34:09 +00:00
|
|
|
js := r.PostFormValue("js") == "1"
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManageThemes {
|
|
|
|
return c.NoPermissionsJSQ(w, r, u, js)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wid, err := strconv.Atoi(swid)
|
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalErrorJSQ(p.GetErrorPhrase("id_must_be_integer"), w, r, u, js)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
widget, err := c.Widgets.Get(wid)
|
2019-01-21 12:27:59 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.NotFound(w, r, nil)
|
2019-01-21 12:27:59 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
|
|
|
err = widget.Delete()
|
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
err = c.AdminLogs.Create("delete", widget.ID, "widget", u.GetIP(), u.ID)
|
2019-11-10 02:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2019-01-21 12:27:59 +00:00
|
|
|
|
2019-09-29 05:10:05 +00:00
|
|
|
return successRedirect("/panel/themes/widgets/", w, r, js)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|