gosora/routes/panel/pages.go

157 lines
4.2 KiB
Go

package panel
import (
"database/sql"
"net/http"
"strconv"
"../../common"
)
func Pages(w http.ResponseWriter, r *http.Request, user common.User) common.RouteError {
header, stats, ferr := common.PanelUserCheck(w, r, &user)
if ferr != nil {
return ferr
}
header.Title = common.GetTitlePhrase("panel_pages")
if r.FormValue("created") == "1" {
header.AddNotice("panel_page_created")
} else if r.FormValue("deleted") == "1" {
header.AddNotice("panel_page_deleted")
}
pageCount := common.Pages.GlobalCount()
page, _ := strconv.Atoi(r.FormValue("page"))
perPage := 10
offset, page, lastPage := common.PageOffset(pageCount, page, perPage)
cPages, err := common.Pages.GetOffset(offset, perPage)
if err != nil {
return common.InternalError(err, w, r)
}
pageList := common.Paginate(pageCount, perPage, 5)
pi := common.PanelCustomPagesPage{&common.BasePanelPage{header, stats, "pages", common.ReportForumID}, cPages, common.Paginator{pageList, page, lastPage}}
return panelRenderTemplate("panel_pages", w, r, user, &pi)
}
func PagesCreateSubmit(w http.ResponseWriter, r *http.Request, user common.User) common.RouteError {
_, ferr := common.SimplePanelUserCheck(w, r, &user)
if ferr != nil {
return ferr
}
pname := r.PostFormValue("name")
if pname == "" {
return common.LocalError("No name was provided for this page", w, r, user)
}
ptitle := r.PostFormValue("title")
if ptitle == "" {
return common.LocalError("No title was provided for this page", w, r, user)
}
pbody := r.PostFormValue("body")
if pbody == "" {
return common.LocalError("No body was provided for this page", w, r, user)
}
page := common.BlankCustomPage()
page.Name = pname
page.Title = ptitle
page.Body = pbody
_, err := page.Create()
if err != nil {
return common.InternalError(err, w, r)
}
http.Redirect(w, r, "/panel/pages/?created=1", http.StatusSeeOther)
return nil
}
func PagesEdit(w http.ResponseWriter, r *http.Request, user common.User, spid string) common.RouteError {
header, stats, ferr := common.PanelUserCheck(w, r, &user)
if ferr != nil {
return ferr
}
header.Title = common.GetTitlePhrase("panel_pages_edit")
if r.FormValue("updated") == "1" {
header.AddNotice("panel_page_updated")
}
pid, err := strconv.Atoi(spid)
if err != nil {
return common.LocalError("Page ID needs to be an integer", w, r, user)
}
page, err := common.Pages.Get(pid)
if err == sql.ErrNoRows {
return common.NotFound(w, r, header)
} else if err != nil {
return common.InternalError(err, w, r)
}
pi := common.PanelCustomPageEditPage{&common.BasePanelPage{header, stats, "pages", common.ReportForumID}, page}
return panelRenderTemplate("panel_pages_edit", w, r, user, &pi)
}
func PagesEditSubmit(w http.ResponseWriter, r *http.Request, user common.User, spid string) common.RouteError {
_, ferr := common.SimplePanelUserCheck(w, r, &user)
if ferr != nil {
return ferr
}
pid, err := strconv.Atoi(spid)
if err != nil {
return common.LocalError("Page ID needs to be an integer", w, r, user)
}
pname := r.PostFormValue("name")
if pname == "" {
return common.LocalError("No name was provided for this page", w, r, user)
}
ptitle := r.PostFormValue("title")
if ptitle == "" {
return common.LocalError("No title was provided for this page", w, r, user)
}
pbody := r.PostFormValue("body")
if pbody == "" {
return common.LocalError("No body was provided for this page", w, r, user)
}
page, err := common.Pages.Get(pid)
if err != nil {
return common.NotFound(w, r, nil)
}
page.Name = pname
page.Title = ptitle
page.Body = pbody
err = page.Commit()
if err != nil {
return common.InternalError(err, w, r)
}
http.Redirect(w, r, "/panel/pages/?updated=1", http.StatusSeeOther)
return nil
}
func PagesDeleteSubmit(w http.ResponseWriter, r *http.Request, user common.User, spid string) common.RouteError {
_, ferr := common.SimplePanelUserCheck(w, r, &user)
if ferr != nil {
return ferr
}
pid, err := strconv.Atoi(spid)
if err != nil {
return common.LocalError("Page ID needs to be an integer", w, r, user)
}
err = common.Pages.Delete(pid)
if err != nil {
return common.InternalError(err, w, r)
}
http.Redirect(w, r, "/panel/pages/?deleted=1", http.StatusSeeOther)
return nil
}