gosora/routes/panel/pages.go
Azareal d9acf27c5b The Search and Filter Widget is now partly implemented. Just Search to go in the basic implementation.
Added AJAX Pagination for the Topic List and Forum Page.
A new log file pair is now created every-time Gosora starts up.
Added proper per-theme template overrides.

Added EasyJSON to make JSON serialisation faster.
Moved a bit of boilerplate into paginator.html
Improved paginator.html with a richer template with first, last and symbols instead of text.
Phased out direct access to Templates.ExecuteTemplate across the software.
Fixed the Live Topic List so it should work again.
Added MicroAvatar to WsJSONUser for topic list JSON requests.
An instance of the plugin is now passed to plugin handlers rather than having the plugins manipulate the globals directly.
Added the pre_render_panel_forum_edit and pre_render_panel_forum_edit_perms hooks to replace pre_render_panel_edit_forum.
Renamed the pre_render_panel_edit_user hook to pre_render_panel_user_edit
Reduced the amount of noise from fsnotify.
Added RawPrepare() to qgen.Accumulator.
Added a temporary phrase whitelist to the phrase endpoint.
Moved the location of the zone data assignments in the topic list to reduce the chances of security issues in the future.
Changed the signature of routes/panel/renderTemplate() requiring some changes across the panel routes.
Removed bits of boilerplate in some of the panel routes with renderTemplate()
Added a BenchmarkTopicsGuestJSRouteParallelWithRouter benchmark.
Removed a fair bit of boilerplate for each page struct by generating a couple of interface casts for each template file instead.
Added the profile_comments_row_alt template.
Added the topics_quick_topic template to reuse part of the quick topic logic for both the topic list and forum page.
Tweaked the CSS for the Online Users Widget.
Tweaked the CSS for Widgets in every theme with a sidebar.
Refactored the template initialisers to hopefully reduce the amount of boilerplate and make things easier to maintain and follow.
Add genIntTmpl in the template initialiser file to reduce the amount of boilerplate needed for the fallback template bindings.

Removed the topics_head phrase.
Moved the paginator_ phrases into the paginator. namespace and renamed them accordingly.
Added the paginator.first_page phrase.
Added the paginator.first_page_aria phrase.
Added the paginator.last_page phrase.
Added the paginator.last_page_aria phrase.
Added the panel_forum_delete_are_you_sure phrase.

Fixed a data race in LogWarning()
2019-02-10 15:52:26 +10:00

155 lines
4.0 KiB
Go

package panel
import (
"database/sql"
"net/http"
"strconv"
"github.com/Azareal/Gosora/common"
)
func Pages(w http.ResponseWriter, r *http.Request, user common.User) common.RouteError {
basePage, ferr := buildBasePage(w, r, &user, "pages", "pages")
if ferr != nil {
return ferr
}
if r.FormValue("created") == "1" {
basePage.AddNotice("panel_page_created")
} else if r.FormValue("deleted") == "1" {
basePage.AddNotice("panel_page_deleted")
}
// TODO: Test the pagination here
pageCount := common.Pages.GlobalCount()
page, _ := strconv.Atoi(r.FormValue("page"))
perPage := 15
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{basePage, cPages, common.Paginator{pageList, page, lastPage}}
return renderTemplate("panel_pages", w, r, basePage.Header, &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 {
basePage, ferr := buildBasePage(w, r, &user, "pages_edit", "pages")
if ferr != nil {
return ferr
}
if r.FormValue("updated") == "1" {
basePage.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, basePage.Header)
} else if err != nil {
return common.InternalError(err, w, r)
}
pi := common.PanelCustomPageEditPage{basePage, page}
return renderTemplate("panel_pages_edit", w, r, basePage.Header, &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
}