2018-06-06 00:21:22 +00:00
|
|
|
package panel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2022-02-21 03:53:13 +00:00
|
|
|
c "git.tuxpa.in/a/gosora/common"
|
2018-06-06 00:21:22 +00:00
|
|
|
)
|
|
|
|
|
2020-03-21 07:44:33 +00:00
|
|
|
func Pages(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError {
|
2021-04-28 08:39:08 +00:00
|
|
|
bp, ferr := buildBasePage(w, r, u, "pages", "pages")
|
2018-06-06 00:21:22 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if r.FormValue("created") == "1" {
|
2021-04-28 08:39:08 +00:00
|
|
|
bp.AddNotice("panel_page_created")
|
2018-06-06 00:21:22 +00:00
|
|
|
} else if r.FormValue("deleted") == "1" {
|
2021-04-28 08:39:08 +00:00
|
|
|
bp.AddNotice("panel_page_deleted")
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:09:10 +00:00
|
|
|
// TODO: Test the pagination here
|
2019-06-01 12:31:48 +00:00
|
|
|
pageCount := c.Pages.Count()
|
2018-06-06 00:21:22 +00:00
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
2018-12-06 11:09:10 +00:00
|
|
|
perPage := 15
|
2019-04-19 08:20:10 +00:00
|
|
|
offset, page, lastPage := c.PageOffset(pageCount, page, perPage)
|
2018-06-06 00:21:22 +00:00
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
cPages, err := c.Pages.GetOffset(offset, perPage)
|
2018-06-06 00:21:22 +00:00
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 05:48:12 +00:00
|
|
|
pageList := c.Paginate(page, lastPage, 5)
|
2021-04-28 08:39:08 +00:00
|
|
|
pi := c.PanelCustomPagesPage{bp, cPages, c.Paginator{pageList, page, lastPage}}
|
|
|
|
return renderTemplate("panel", w, r, bp.Header, c.Panel{bp, "panel_page_list", "", "panel_pages", &pi})
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
func PagesCreateSubmit(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, u)
|
2018-06-06 00:21:22 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
|
2019-11-08 21:46:50 +00:00
|
|
|
name := c.SanitiseSingleLine(r.PostFormValue("name"))
|
|
|
|
if name == "" {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError("No name was provided for this page", w, r, u)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2019-11-08 21:46:50 +00:00
|
|
|
title := c.SanitiseSingleLine(r.PostFormValue("title"))
|
|
|
|
if title == "" {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError("No title was provided for this page", w, r, u)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2019-11-08 21:46:50 +00:00
|
|
|
body := r.PostFormValue("body")
|
|
|
|
if body == "" {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError("No body was provided for this page", w, r, u)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
page := c.BlankCustomPage()
|
2019-11-08 21:46:50 +00:00
|
|
|
page.Name = name
|
|
|
|
page.Title = title
|
|
|
|
page.Body = body
|
|
|
|
pid, err := page.Create()
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
err = c.AdminLogs.Create("create", pid, "page", u.GetIP(), u.ID)
|
2018-06-06 00:21:22 +00:00
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/panel/pages/?created=1", http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-21 07:44:33 +00:00
|
|
|
func PagesEdit(w http.ResponseWriter, r *http.Request, u *c.User, spid string) c.RouteError {
|
2021-04-28 08:39:08 +00:00
|
|
|
bp, ferr := buildBasePage(w, r, u, "pages_edit", "pages")
|
2018-06-06 00:21:22 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if r.FormValue("updated") == "1" {
|
2021-04-28 08:39:08 +00:00
|
|
|
bp.AddNotice("panel_page_updated")
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pid, err := strconv.Atoi(spid)
|
|
|
|
if err != nil {
|
2020-03-21 07:44:33 +00:00
|
|
|
return c.LocalError("Page ID needs to be an integer", w, r, u)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2019-04-19 08:20:10 +00:00
|
|
|
page, err := c.Pages.Get(pid)
|
2018-06-06 00:21:22 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2021-04-28 08:39:08 +00:00
|
|
|
return c.NotFound(w, r, bp.Header)
|
2018-06-06 00:21:22 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 08:39:08 +00:00
|
|
|
pi := c.PanelCustomPageEditPage{bp, page}
|
|
|
|
return renderTemplate("panel", w, r, bp.Header, c.Panel{bp, "panel_page_edit", "", "panel_pages_edit", &pi})
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
func PagesEditSubmit(w http.ResponseWriter, r *http.Request, u *c.User, spid string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, u)
|
2018-06-06 00:21:22 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
|
|
|
|
pid, err := strconv.Atoi(spid)
|
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError("Page ID needs to be an integer", w, r, u)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2019-11-08 21:46:50 +00:00
|
|
|
name := c.SanitiseSingleLine(r.PostFormValue("name"))
|
|
|
|
if name == "" {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError("No name was provided for this page", w, r, u)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2019-11-08 21:46:50 +00:00
|
|
|
title := c.SanitiseSingleLine(r.PostFormValue("title"))
|
|
|
|
if title == "" {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError("No title was provided for this page", w, r, u)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2019-11-08 21:46:50 +00:00
|
|
|
body := r.PostFormValue("body")
|
|
|
|
if body == "" {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError("No body was provided for this page", w, r, u)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 07:43:38 +00:00
|
|
|
p, err := c.Pages.Get(pid)
|
2018-06-06 00:21:22 +00:00
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.NotFound(w, r, nil)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2021-07-28 07:43:38 +00:00
|
|
|
p.Name = name
|
|
|
|
p.Title = title
|
|
|
|
p.Body = body
|
|
|
|
err = p.Commit()
|
2018-06-06 00:21:22 +00:00
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
err = c.AdminLogs.Create("edit", pid, "page", u.GetIP(), u.ID)
|
2019-11-08 21:46:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2018-06-06 00:21:22 +00:00
|
|
|
|
|
|
|
http.Redirect(w, r, "/panel/pages/?updated=1", http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-21 07:44:33 +00:00
|
|
|
func PagesDeleteSubmit(w http.ResponseWriter, r *http.Request, u *c.User, spid string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, u)
|
2018-06-06 00:21:22 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
|
|
|
|
pid, err := strconv.Atoi(spid)
|
|
|
|
if err != nil {
|
2020-03-21 07:44:33 +00:00
|
|
|
return c.LocalError("Page ID needs to be an integer", w, r, u)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2019-04-19 08:20:10 +00:00
|
|
|
err = c.Pages.Delete(pid)
|
2018-06-06 00:21:22 +00:00
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2020-03-21 07:44:33 +00:00
|
|
|
err = c.AdminLogs.Create("delete", pid, "page", u.GetIP(), u.ID)
|
2019-11-08 21:46:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2018-06-06 00:21:22 +00:00
|
|
|
|
|
|
|
http.Redirect(w, r, "/panel/pages/?deleted=1", http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|