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
|
|
|
)
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
func Pages(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError {
|
2018-06-17 07:28:18 +00:00
|
|
|
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-04-19 08:20:10 +00:00
|
|
|
pageList := c.Paginate(pageCount, perPage, 5)
|
|
|
|
pi := c.PanelCustomPagesPage{basePage, cPages, c.Paginator{pageList, page, lastPage}}
|
2019-05-01 23:14:07 +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
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
pname := r.PostFormValue("name")
|
|
|
|
if pname == "" {
|
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
|
|
|
}
|
|
|
|
ptitle := r.PostFormValue("title")
|
|
|
|
if ptitle == "" {
|
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
|
|
|
}
|
|
|
|
pbody := r.PostFormValue("body")
|
|
|
|
if pbody == "" {
|
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()
|
2018-06-06 00:21:22 +00:00
|
|
|
page.Name = pname
|
|
|
|
page.Title = ptitle
|
|
|
|
page.Body = pbody
|
|
|
|
_, err := page.Create()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
func PagesEdit(w http.ResponseWriter, r *http.Request, user c.User, spid string) c.RouteError {
|
2018-06-17 07:28:18 +00:00
|
|
|
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-05-01 23:14:07 +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
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +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
|
|
|
}
|
|
|
|
|
|
|
|
pname := r.PostFormValue("name")
|
|
|
|
if pname == "" {
|
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
|
|
|
}
|
|
|
|
ptitle := r.PostFormValue("title")
|
|
|
|
if ptitle == "" {
|
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
|
|
|
}
|
|
|
|
pbody := r.PostFormValue("body")
|
|
|
|
if pbody == "" {
|
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
|
|
|
}
|
|
|
|
page.Name = pname
|
|
|
|
page.Title = ptitle
|
|
|
|
page.Body = pbody
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/panel/pages/?updated=1", http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +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
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/panel/pages/?deleted=1", http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|