2018-06-06 00:21:22 +00:00
|
|
|
package panel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2018-06-06 00:21:22 +00:00
|
|
|
)
|
|
|
|
|
2020-03-18 09:21:34 +00:00
|
|
|
func Pages(w http.ResponseWriter, r *http.Request, user *c.User) c.RouteError {
|
|
|
|
basePage, ferr := buildBasePage(w, r, user, "pages", "pages")
|
2018-06-06 00:21:22 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if r.FormValue("created") == "1" {
|
2018-06-17 07:28:18 +00:00
|
|
|
basePage.AddNotice("panel_page_created")
|
2018-06-06 00:21:22 +00:00
|
|
|
} else if r.FormValue("deleted") == "1" {
|
2018-06-17 07:28:18 +00:00
|
|
|
basePage.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)
|
2019-04-19 08:20:10 +00:00
|
|
|
pi := c.PanelCustomPagesPage{basePage, cPages, c.Paginator{pageList, page, lastPage}}
|
2019-06-04 05:48:12 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "panel_page_list", "", "panel_pages", &pi})
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 09:21:34 +00:00
|
|
|
func PagesCreateSubmit(w http.ResponseWriter, r *http.Request, user *c.User) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, user)
|
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 == "" {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("No name was provided for this page", w, r, user)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2019-11-08 21:46:50 +00:00
|
|
|
title := c.SanitiseSingleLine(r.PostFormValue("title"))
|
|
|
|
if title == "" {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("No title was provided for this page", w, r, user)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2019-11-08 21:46:50 +00:00
|
|
|
body := r.PostFormValue("body")
|
|
|
|
if body == "" {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("No body was provided for this page", w, r, user)
|
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)
|
|
|
|
}
|
2019-12-31 21:57:54 +00:00
|
|
|
err = c.AdminLogs.Create("create", pid, "page", user.GetIP(), user.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-18 09:21:34 +00:00
|
|
|
func PagesEdit(w http.ResponseWriter, r *http.Request, user *c.User, spid string) c.RouteError {
|
|
|
|
basePage, ferr := buildBasePage(w, r, user, "pages_edit", "pages")
|
2018-06-06 00:21:22 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if r.FormValue("updated") == "1" {
|
2018-06-17 07:28:18 +00:00
|
|
|
basePage.AddNotice("panel_page_updated")
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pid, err := strconv.Atoi(spid)
|
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("Page ID needs to be an integer", w, r, user)
|
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 {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.NotFound(w, r, basePage.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
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
pi := c.PanelCustomPageEditPage{basePage, page}
|
2019-06-04 05:48:12 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "panel_page_edit", "", "panel_pages_edit", &pi})
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 09:21:34 +00:00
|
|
|
func PagesEditSubmit(w http.ResponseWriter, r *http.Request, user *c.User, spid string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, user)
|
2018-06-06 00:21:22 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
|
|
|
|
pid, err := strconv.Atoi(spid)
|
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("Page ID needs to be an integer", w, r, user)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2019-11-08 21:46:50 +00:00
|
|
|
name := c.SanitiseSingleLine(r.PostFormValue("name"))
|
|
|
|
if name == "" {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("No name was provided for this page", w, r, user)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2019-11-08 21:46:50 +00:00
|
|
|
title := c.SanitiseSingleLine(r.PostFormValue("title"))
|
|
|
|
if title == "" {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("No title was provided for this page", w, r, user)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2019-11-08 21:46:50 +00:00
|
|
|
body := r.PostFormValue("body")
|
|
|
|
if body == "" {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("No body was provided for this page", w, r, user)
|
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 != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.NotFound(w, r, nil)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2019-11-08 21:46:50 +00:00
|
|
|
page.Name = name
|
|
|
|
page.Title = title
|
|
|
|
page.Body = body
|
2018-06-06 00:21:22 +00:00
|
|
|
err = page.Commit()
|
|
|
|
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-12-31 21:57:54 +00:00
|
|
|
err = c.AdminLogs.Create("edit", pid, "page", user.GetIP(), user.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-18 09:21:34 +00:00
|
|
|
func PagesDeleteSubmit(w http.ResponseWriter, r *http.Request, user *c.User, spid string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, user)
|
2018-06-06 00:21:22 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
|
|
|
|
pid, err := strconv.Atoi(spid)
|
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("Page ID needs to be an integer", w, r, user)
|
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
|
|
|
}
|
2019-12-31 21:57:54 +00:00
|
|
|
err = c.AdminLogs.Create("delete", pid, "page", user.GetIP(), user.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
|
|
|
|
}
|