github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/routes/panel/pages.go (about) 1 package panel 2 3 import ( 4 "database/sql" 5 "net/http" 6 "strconv" 7 8 c "github.com/Azareal/Gosora/common" 9 ) 10 11 func Pages(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError { 12 bp, ferr := buildBasePage(w, r, u, "pages", "pages") 13 if ferr != nil { 14 return ferr 15 } 16 if r.FormValue("created") == "1" { 17 bp.AddNotice("panel_page_created") 18 } else if r.FormValue("deleted") == "1" { 19 bp.AddNotice("panel_page_deleted") 20 } 21 22 // TODO: Test the pagination here 23 pageCount := c.Pages.Count() 24 page, _ := strconv.Atoi(r.FormValue("page")) 25 perPage := 15 26 offset, page, lastPage := c.PageOffset(pageCount, page, perPage) 27 28 cPages, err := c.Pages.GetOffset(offset, perPage) 29 if err != nil { 30 return c.InternalError(err, w, r) 31 } 32 33 pageList := c.Paginate(page, lastPage, 5) 34 pi := c.PanelCustomPagesPage{bp, cPages, c.Paginator{pageList, page, lastPage}} 35 return renderTemplate("panel", w, r, bp.Header, c.Panel{bp, "panel_page_list", "", "panel_pages", &pi}) 36 } 37 38 func PagesCreateSubmit(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError { 39 _, ferr := c.SimplePanelUserCheck(w, r, u) 40 if ferr != nil { 41 return ferr 42 } 43 44 name := c.SanitiseSingleLine(r.PostFormValue("name")) 45 if name == "" { 46 return c.LocalError("No name was provided for this page", w, r, u) 47 } 48 title := c.SanitiseSingleLine(r.PostFormValue("title")) 49 if title == "" { 50 return c.LocalError("No title was provided for this page", w, r, u) 51 } 52 body := r.PostFormValue("body") 53 if body == "" { 54 return c.LocalError("No body was provided for this page", w, r, u) 55 } 56 57 page := c.BlankCustomPage() 58 page.Name = name 59 page.Title = title 60 page.Body = body 61 pid, err := page.Create() 62 if err != nil { 63 return c.InternalError(err, w, r) 64 } 65 err = c.AdminLogs.Create("create", pid, "page", u.GetIP(), u.ID) 66 if err != nil { 67 return c.InternalError(err, w, r) 68 } 69 70 http.Redirect(w, r, "/panel/pages/?created=1", http.StatusSeeOther) 71 return nil 72 } 73 74 func PagesEdit(w http.ResponseWriter, r *http.Request, u *c.User, spid string) c.RouteError { 75 bp, ferr := buildBasePage(w, r, u, "pages_edit", "pages") 76 if ferr != nil { 77 return ferr 78 } 79 if r.FormValue("updated") == "1" { 80 bp.AddNotice("panel_page_updated") 81 } 82 83 pid, err := strconv.Atoi(spid) 84 if err != nil { 85 return c.LocalError("Page ID needs to be an integer", w, r, u) 86 } 87 page, err := c.Pages.Get(pid) 88 if err == sql.ErrNoRows { 89 return c.NotFound(w, r, bp.Header) 90 } else if err != nil { 91 return c.InternalError(err, w, r) 92 } 93 94 pi := c.PanelCustomPageEditPage{bp, page} 95 return renderTemplate("panel", w, r, bp.Header, c.Panel{bp, "panel_page_edit", "", "panel_pages_edit", &pi}) 96 } 97 98 func PagesEditSubmit(w http.ResponseWriter, r *http.Request, u *c.User, spid string) c.RouteError { 99 _, ferr := c.SimplePanelUserCheck(w, r, u) 100 if ferr != nil { 101 return ferr 102 } 103 104 pid, err := strconv.Atoi(spid) 105 if err != nil { 106 return c.LocalError("Page ID needs to be an integer", w, r, u) 107 } 108 name := c.SanitiseSingleLine(r.PostFormValue("name")) 109 if name == "" { 110 return c.LocalError("No name was provided for this page", w, r, u) 111 } 112 title := c.SanitiseSingleLine(r.PostFormValue("title")) 113 if title == "" { 114 return c.LocalError("No title was provided for this page", w, r, u) 115 } 116 body := r.PostFormValue("body") 117 if body == "" { 118 return c.LocalError("No body was provided for this page", w, r, u) 119 } 120 121 p, err := c.Pages.Get(pid) 122 if err != nil { 123 return c.NotFound(w, r, nil) 124 } 125 p.Name = name 126 p.Title = title 127 p.Body = body 128 err = p.Commit() 129 if err != nil { 130 return c.InternalError(err, w, r) 131 } 132 err = c.AdminLogs.Create("edit", pid, "page", u.GetIP(), u.ID) 133 if err != nil { 134 return c.InternalError(err, w, r) 135 } 136 137 http.Redirect(w, r, "/panel/pages/?updated=1", http.StatusSeeOther) 138 return nil 139 } 140 141 func PagesDeleteSubmit(w http.ResponseWriter, r *http.Request, u *c.User, spid string) c.RouteError { 142 _, ferr := c.SimplePanelUserCheck(w, r, u) 143 if ferr != nil { 144 return ferr 145 } 146 147 pid, err := strconv.Atoi(spid) 148 if err != nil { 149 return c.LocalError("Page ID needs to be an integer", w, r, u) 150 } 151 err = c.Pages.Delete(pid) 152 if err != nil { 153 return c.InternalError(err, w, r) 154 } 155 err = c.AdminLogs.Create("delete", pid, "page", u.GetIP(), u.ID) 156 if err != nil { 157 return c.InternalError(err, w, r) 158 } 159 160 http.Redirect(w, r, "/panel/pages/?deleted=1", http.StatusSeeOther) 161 return nil 162 }