2018-06-06 00:21:22 +00:00
|
|
|
package panel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
"github.com/Azareal/Gosora/common"
|
2018-06-06 00:21:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Pages(w http.ResponseWriter, r *http.Request, user common.User) common.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
|
2018-06-06 00:21:22 +00:00
|
|
|
pageCount := common.Pages.GlobalCount()
|
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
2018-12-06 11:09:10 +00:00
|
|
|
perPage := 15
|
2018-06-06 00:21:22 +00:00
|
|
|
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)
|
2018-06-17 07:28:18 +00:00
|
|
|
pi := common.PanelCustomPagesPage{basePage, cPages, common.Paginator{pageList, page, lastPage}}
|
2018-10-02 04:09:17 +00:00
|
|
|
return renderTemplate("panel_pages", w, r, user, &pi)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
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 {
|
|
|
|
return common.LocalError("Page ID needs to be an integer", w, r, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
page, err := common.Pages.Get(pid)
|
|
|
|
if err == sql.ErrNoRows {
|
2018-06-17 07:28:18 +00:00
|
|
|
return common.NotFound(w, r, basePage.Header)
|
2018-06-06 00:21:22 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
2018-06-17 07:28:18 +00:00
|
|
|
pi := common.PanelCustomPageEditPage{basePage, page}
|
2018-10-02 04:09:17 +00:00
|
|
|
return renderTemplate("panel_pages_edit", w, r, user, &pi)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|