2018-05-27 09:36:35 +00:00
package panel
import (
"database/sql"
"errors"
"net/http"
"strconv"
"strings"
2019-04-19 08:20:10 +00:00
c "github.com/Azareal/Gosora/common"
2019-10-06 11:32:00 +00:00
p "github.com/Azareal/Gosora/common/phrases"
2018-05-27 09:36:35 +00:00
)
2020-07-19 03:26:56 +00:00
func Forums ( w http . ResponseWriter , r * http . Request , u * c . User ) c . RouteError {
basePage , ferr := buildBasePage ( w , r , u , "forums" , "forums" )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
2020-07-19 03:26:56 +00:00
if ! u . Perms . ManageForums {
return c . NoPermissions ( w , r , u )
2018-05-27 09:36:35 +00:00
}
2019-04-27 06:32:26 +00:00
basePage . Header . AddScript ( "Sortable-1.4.0/Sortable.min.js" )
basePage . Header . AddScriptAsync ( "panel_forums.js" )
2018-05-27 09:36:35 +00:00
// TODO: Paginate this?
var forumList [ ] interface { }
2019-04-19 08:20:10 +00:00
forums , err := c . Forums . GetAll ( )
2018-05-27 09:36:35 +00:00
if err != nil {
2019-04-19 08:20:10 +00:00
return c . InternalError ( err , w , r )
2018-05-27 09:36:35 +00:00
}
// ? - Should we generate something similar to the forumView? It might be a little overkill for a page which is rarely loaded in comparison to /forums/
2019-09-29 05:10:05 +00:00
for _ , f := range forums {
if f . Name != "" && f . ParentID == 0 {
fadmin := c . ForumAdmin { f . ID , f . Name , f . Desc , f . Active , f . Preset , f . TopicCount , c . PresetToLang ( f . Preset ) }
2018-05-27 09:36:35 +00:00
if fadmin . Preset == "" {
fadmin . Preset = "custom"
}
forumList = append ( forumList , fadmin )
}
}
if r . FormValue ( "created" ) == "1" {
2018-06-17 07:28:18 +00:00
basePage . AddNotice ( "panel_forum_created" )
2018-05-27 09:36:35 +00:00
} else if r . FormValue ( "deleted" ) == "1" {
2018-06-17 07:28:18 +00:00
basePage . AddNotice ( "panel_forum_deleted" )
2018-05-27 09:36:35 +00:00
} else if r . FormValue ( "updated" ) == "1" {
2018-06-17 07:28:18 +00:00
basePage . AddNotice ( "panel_forum_updated" )
2018-05-27 09:36:35 +00:00
}
2019-04-19 08:20:10 +00:00
pi := c . PanelPage { basePage , forumList , nil }
2019-10-06 11:32:00 +00:00
return renderTemplate ( "panel" , w , r , basePage . Header , c . Panel { basePage , "" , "" , "panel_forums" , & pi } )
2018-05-27 09:36:35 +00:00
}
2020-07-19 03:26:56 +00:00
func ForumsCreateSubmit ( w http . ResponseWriter , r * http . Request , u * c . User ) c . RouteError {
_ , ferr := c . SimplePanelUserCheck ( w , r , u )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
2020-07-19 03:26:56 +00:00
if ! u . Perms . ManageForums {
return c . NoPermissions ( w , r , u )
2018-05-27 09:36:35 +00:00
}
2020-07-19 03:26:56 +00:00
name := r . PostFormValue ( "name" )
desc := r . PostFormValue ( "desc" )
preset := c . StripInvalidPreset ( r . PostFormValue ( "preset" ) )
2019-10-13 10:50:15 +00:00
factive := r . PostFormValue ( "active" )
2018-05-27 09:36:35 +00:00
active := ( factive == "on" || factive == "1" )
2020-07-19 03:26:56 +00:00
fid , err := c . Forums . Create ( name , desc , active , preset )
2019-11-08 10:08:37 +00:00
if err != nil {
return c . InternalError ( err , w , r )
}
2020-07-19 03:26:56 +00:00
err = c . AdminLogs . Create ( "create" , fid , "forum" , u . GetIP ( ) , u . ID )
2018-05-27 09:36:35 +00:00
if err != nil {
2019-04-19 08:20:10 +00:00
return c . InternalError ( err , w , r )
2018-05-27 09:36:35 +00:00
}
http . Redirect ( w , r , "/panel/forums/?created=1" , http . StatusSeeOther )
return nil
}
// TODO: Revamp this
2020-07-19 03:26:56 +00:00
func ForumsDelete ( w http . ResponseWriter , r * http . Request , u * c . User , sfid string ) c . RouteError {
basePage , ferr := buildBasePage ( w , r , u , "delete_forum" , "forums" )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
2020-07-19 03:26:56 +00:00
if ! u . Perms . ManageForums {
return c . NoPermissions ( w , r , u )
2018-05-27 09:36:35 +00:00
}
fid , err := strconv . Atoi ( sfid )
if err != nil {
2020-07-19 03:26:56 +00:00
return c . LocalError ( "The provided Forum ID is not a valid number." , w , r , u )
2018-05-27 09:36:35 +00:00
}
2019-04-19 08:20:10 +00:00
forum , err := c . Forums . Get ( fid )
2018-05-27 09:36:35 +00:00
if err == sql . ErrNoRows {
2020-07-19 03:26:56 +00:00
return c . LocalError ( "The forum you're trying to delete doesn't exist." , w , r , u )
2018-05-27 09:36:35 +00:00
} else if err != nil {
2019-04-19 08:20:10 +00:00
return c . InternalError ( err , w , r )
2018-05-27 09:36:35 +00:00
}
2019-10-06 11:32:00 +00:00
confirmMsg := p . GetTmplPhrasef ( "panel_forum_delete_are_you_sure" , forum . Name )
2020-03-18 09:21:34 +00:00
youSure := c . AreYouSure { "/panel/forums/delete/submit/" + strconv . Itoa ( fid ) , confirmMsg }
2018-05-27 09:36:35 +00:00
2020-03-18 09:21:34 +00:00
pi := c . PanelPage { basePage , tList , youSure }
2020-07-19 03:26:56 +00:00
if c . RunPreRenderHook ( "pre_render_panel_delete_forum" , w , r , u , & pi ) {
2018-05-27 09:36:35 +00:00
return nil
}
2019-02-10 05:52:26 +00:00
return renderTemplate ( "panel_are_you_sure" , w , r , basePage . Header , & pi )
2018-05-27 09:36:35 +00:00
}
2020-07-19 03:26:56 +00:00
func ForumsDeleteSubmit ( w http . ResponseWriter , r * http . Request , u * c . User , sfid string ) c . RouteError {
_ , ferr := c . SimplePanelUserCheck ( w , r , u )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
2020-07-19 03:26:56 +00:00
if ! u . Perms . ManageForums {
return c . NoPermissions ( w , r , u )
2018-05-27 09:36:35 +00:00
}
fid , err := strconv . Atoi ( sfid )
if err != nil {
2020-07-19 03:26:56 +00:00
return c . LocalError ( "The provided Forum ID is not a valid number." , w , r , u )
2018-05-27 09:36:35 +00:00
}
2019-04-19 08:20:10 +00:00
err = c . Forums . Delete ( fid )
2018-05-27 09:36:35 +00:00
if err == sql . ErrNoRows {
2020-07-19 03:26:56 +00:00
return c . LocalError ( "The forum you're trying to delete doesn't exist." , w , r , u )
2018-05-27 09:36:35 +00:00
} else if err != nil {
2019-04-19 08:20:10 +00:00
return c . InternalError ( err , w , r )
2018-05-27 09:36:35 +00:00
}
2020-07-19 03:26:56 +00:00
err = c . AdminLogs . Create ( "delete" , fid , "forum" , u . GetIP ( ) , u . ID )
2019-11-08 10:08:37 +00:00
if err != nil {
return c . InternalError ( err , w , r )
}
2018-05-27 09:36:35 +00:00
http . Redirect ( w , r , "/panel/forums/?deleted=1" , http . StatusSeeOther )
return nil
}
2020-07-19 03:26:56 +00:00
func ForumsOrderSubmit ( w http . ResponseWriter , r * http . Request , u * c . User ) c . RouteError {
_ , ferr := c . SimplePanelUserCheck ( w , r , u )
2019-04-27 06:32:26 +00:00
if ferr != nil {
return ferr
}
2021-04-07 14:23:11 +00:00
// TODO: Move this even earlier?
2019-10-06 22:20:37 +00:00
js := r . PostFormValue ( "js" ) == "1"
2020-07-19 03:26:56 +00:00
if ! u . Perms . ManageForums {
return c . NoPermissionsJSQ ( w , r , u , js )
2019-04-27 06:32:26 +00:00
}
sitems := strings . TrimSuffix ( strings . TrimPrefix ( r . PostFormValue ( "items" ) , "{" ) , "}" )
//fmt.Printf("sitems: %+v\n", sitems)
2019-08-20 08:44:37 +00:00
updateMap := make ( map [ int ] int )
2019-04-27 06:32:26 +00:00
for index , sfid := range strings . Split ( sitems , "," ) {
fid , err := strconv . Atoi ( sfid )
if err != nil {
2020-07-19 03:26:56 +00:00
return c . LocalErrorJSQ ( "Invalid integer in forum list" , w , r , u , js )
2019-04-27 06:32:26 +00:00
}
updateMap [ fid ] = index
}
2021-01-06 06:41:08 +00:00
err := c . Forums . UpdateOrder ( updateMap )
if err != nil {
return c . InternalErrorJSQ ( err , w , r , js )
}
2019-04-27 06:32:26 +00:00
2021-01-06 06:41:08 +00:00
err = c . AdminLogs . Create ( "reorder" , 0 , "forum" , u . GetIP ( ) , u . ID )
2019-11-08 10:08:37 +00:00
if err != nil {
return c . InternalErrorJSQ ( err , w , r , js )
}
2019-09-29 05:10:05 +00:00
return successRedirect ( "/panel/forums/" , w , r , js )
2019-04-27 06:32:26 +00:00
}
2020-07-19 03:26:56 +00:00
func ForumsEdit ( w http . ResponseWriter , r * http . Request , u * c . User , sfid string ) c . RouteError {
basePage , ferr := buildBasePage ( w , r , u , "edit_forum" , "forums" )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
2020-07-19 03:26:56 +00:00
if ! u . Perms . ManageForums {
return c . NoPermissions ( w , r , u )
2018-05-27 09:36:35 +00:00
}
2019-10-06 11:32:00 +00:00
2018-05-27 09:36:35 +00:00
fid , err := strconv . Atoi ( sfid )
if err != nil {
2019-10-06 11:32:00 +00:00
return c . SimpleError ( p . GetErrorPhrase ( "url_id_must_be_integer" ) , w , r , basePage . Header )
2018-05-27 09:36:35 +00:00
}
2019-07-28 10:42:30 +00:00
basePage . Header . AddScriptAsync ( "panel_forum_edit.js" )
2018-05-27 09:36:35 +00:00
2021-04-07 14:23:11 +00:00
f , err := c . Forums . Get ( fid )
2018-05-27 09:36:35 +00:00
if err == sql . ErrNoRows {
2020-07-19 03:26:56 +00:00
return c . LocalError ( "The forum you're trying to edit doesn't exist." , w , r , u )
2018-05-27 09:36:35 +00:00
} else if err != nil {
2019-04-19 08:20:10 +00:00
return c . InternalError ( err , w , r )
2018-05-27 09:36:35 +00:00
}
2021-04-07 14:23:11 +00:00
if f . Preset == "" {
f . Preset = "custom"
2018-05-27 09:36:35 +00:00
}
2019-04-19 08:20:10 +00:00
glist , err := c . Groups . GetAll ( )
2018-05-27 09:36:35 +00:00
if err != nil {
2019-04-19 08:20:10 +00:00
return c . InternalError ( err , w , r )
2018-05-27 09:36:35 +00:00
}
2019-04-19 08:20:10 +00:00
var gplist [ ] c . GroupForumPermPreset
2018-05-27 09:36:35 +00:00
for gid , group := range glist {
if gid == 0 {
continue
}
2019-04-19 08:20:10 +00:00
forumPerms , err := c . FPStore . Get ( fid , group . ID )
2018-05-27 09:36:35 +00:00
if err == sql . ErrNoRows {
2019-04-19 08:20:10 +00:00
forumPerms = c . BlankForumPerms ( )
2018-05-27 09:36:35 +00:00
} else if err != nil {
2019-04-19 08:20:10 +00:00
return c . InternalError ( err , w , r )
2018-05-27 09:36:35 +00:00
}
2019-04-19 08:20:10 +00:00
preset := c . ForumPermsToGroupForumPreset ( forumPerms )
gplist = append ( gplist , c . GroupForumPermPreset { group , preset , preset == "default" } )
2018-05-27 09:36:35 +00:00
}
if r . FormValue ( "updated" ) == "1" {
2018-06-17 07:28:18 +00:00
basePage . AddNotice ( "panel_forum_updated" )
2018-05-27 09:36:35 +00:00
}
2021-04-07 14:23:11 +00:00
falist , e := c . ForumActionStore . GetInForum ( f . ID )
if err != sql . ErrNoRows && e != nil {
return c . InternalError ( e , w , r )
}
afalist := make ( [ ] * c . ForumActionAction , len ( falist ) )
for i , faitem := range falist {
afalist [ i ] = & c . ForumActionAction { faitem , c . ConvActToString ( faitem . Action ) }
}
pi := c . PanelEditForumPage { basePage , f . ID , f . Name , f . Desc , f . Active , f . Preset , gplist , afalist }
2019-10-06 11:32:00 +00:00
return renderTemplate ( "panel" , w , r , basePage . Header , c . Panel { basePage , "" , "" , "panel_forum_edit" , & pi } )
2018-05-27 09:36:35 +00:00
}
2020-07-19 03:26:56 +00:00
func ForumsEditSubmit ( w http . ResponseWriter , r * http . Request , u * c . User , sfid string ) c . RouteError {
_ , ferr := c . SimplePanelUserCheck ( w , r , u )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
2020-07-19 03:26:56 +00:00
if ! u . Perms . ManageForums {
return c . NoPermissions ( w , r , u )
2018-05-27 09:36:35 +00:00
}
2019-10-06 22:20:37 +00:00
js := r . PostFormValue ( "js" ) == "1"
2018-05-27 09:36:35 +00:00
fid , err := strconv . Atoi ( sfid )
if err != nil {
2020-07-19 03:26:56 +00:00
return c . LocalErrorJSQ ( "The provided Forum ID is not a valid number." , w , r , u , js )
2018-05-27 09:36:35 +00:00
}
2019-04-19 08:20:10 +00:00
forum , err := c . Forums . Get ( fid )
2018-05-27 09:36:35 +00:00
if err == sql . ErrNoRows {
2020-07-19 03:26:56 +00:00
return c . LocalErrorJSQ ( "The forum you're trying to edit doesn't exist." , w , r , u , js )
2018-05-27 09:36:35 +00:00
} else if err != nil {
2019-09-29 05:10:05 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-05-27 09:36:35 +00:00
}
2020-07-19 03:26:56 +00:00
name := r . PostFormValue ( "forum_name" )
desc := r . PostFormValue ( "forum_desc" )
preset := c . StripInvalidPreset ( r . PostFormValue ( "forum_preset" ) )
2019-09-29 05:10:05 +00:00
factive := r . PostFormValue ( "forum_active" )
2018-05-27 09:36:35 +00:00
2019-08-20 08:44:37 +00:00
active := false
2019-09-29 05:10:05 +00:00
if factive == "" {
2018-05-27 09:36:35 +00:00
active = forum . Active
2019-09-29 05:10:05 +00:00
} else if factive == "1" || factive == "Show" {
2018-05-27 09:36:35 +00:00
active = true
}
2020-07-19 03:26:56 +00:00
err = forum . Update ( name , desc , active , preset )
2018-05-27 09:36:35 +00:00
if err != nil {
2019-09-29 05:10:05 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-05-27 09:36:35 +00:00
}
2020-07-19 03:26:56 +00:00
err = c . AdminLogs . Create ( "edit" , fid , "forum" , u . GetIP ( ) , u . ID )
2019-11-08 10:08:37 +00:00
if err != nil {
return c . InternalError ( err , w , r )
}
2018-05-27 09:36:35 +00:00
// ? Should we redirect to the forum editor instead?
2019-09-29 05:10:05 +00:00
return successRedirect ( "/panel/forums/" , w , r , js )
2018-05-27 09:36:35 +00:00
}
2020-07-19 03:26:56 +00:00
func ForumsEditPermsSubmit ( w http . ResponseWriter , r * http . Request , u * c . User , sfid string ) c . RouteError {
_ , ferr := c . SimplePanelUserCheck ( w , r , u )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
2020-07-19 03:26:56 +00:00
if ! u . Perms . ManageForums {
return c . NoPermissions ( w , r , u )
2018-05-27 09:36:35 +00:00
}
2019-10-06 22:20:37 +00:00
js := r . PostFormValue ( "js" ) == "1"
2018-05-27 09:36:35 +00:00
fid , err := strconv . Atoi ( sfid )
if err != nil {
2020-07-19 03:26:56 +00:00
return c . LocalErrorJSQ ( "The provided Forum ID is not a valid number." , w , r , u , js )
2018-05-27 09:36:35 +00:00
}
gid , err := strconv . Atoi ( r . PostFormValue ( "gid" ) )
if err != nil {
2020-07-19 03:26:56 +00:00
return c . LocalErrorJSQ ( "Invalid Group ID" , w , r , u , js )
2018-05-27 09:36:35 +00:00
}
2021-04-07 14:23:11 +00:00
f , err := c . Forums . Get ( fid )
2018-05-27 09:36:35 +00:00
if err == sql . ErrNoRows {
2020-07-19 03:26:56 +00:00
return c . LocalErrorJSQ ( "This forum doesn't exist" , w , r , u , js )
2018-05-27 09:36:35 +00:00
} else if err != nil {
2019-09-29 05:10:05 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-05-27 09:36:35 +00:00
}
2019-04-19 08:20:10 +00:00
permPreset := c . StripInvalidGroupForumPreset ( r . PostFormValue ( "perm_preset" ) )
2021-04-07 14:23:11 +00:00
err = f . SetPreset ( permPreset , gid )
2018-05-27 09:36:35 +00:00
if err != nil {
2020-07-19 03:26:56 +00:00
return c . LocalErrorJSQ ( err . Error ( ) , w , r , u , js )
2018-05-27 09:36:35 +00:00
}
2020-07-19 03:26:56 +00:00
err = c . AdminLogs . Create ( "edit" , fid , "forum" , u . GetIP ( ) , u . ID )
2019-11-08 10:08:37 +00:00
if err != nil {
return c . InternalError ( err , w , r )
}
2018-05-27 09:36:35 +00:00
2019-09-29 05:10:05 +00:00
return successRedirect ( "/panel/forums/edit/" + strconv . Itoa ( fid ) + "?updated=1" , w , r , js )
2018-05-27 09:36:35 +00:00
}
// A helper function for the Advanced portion of the Forum Perms Editor
2021-04-07 14:23:11 +00:00
func forumPermsExtractDash ( paramList string ) ( fid , gid int , e error ) {
2018-05-27 09:36:35 +00:00
params := strings . Split ( paramList , "-" )
if len ( params ) != 2 {
return fid , gid , errors . New ( "Parameter count mismatch" )
}
2021-04-07 14:23:11 +00:00
fid , e = strconv . Atoi ( params [ 0 ] )
if e != nil {
2018-05-27 09:36:35 +00:00
return fid , gid , errors . New ( "The provided Forum ID is not a valid number." )
}
2021-04-07 14:23:11 +00:00
gid , e = strconv . Atoi ( params [ 1 ] )
if e != nil {
e = errors . New ( "The provided Group ID is not a valid number." )
2018-05-27 09:36:35 +00:00
}
2021-04-07 14:23:11 +00:00
return fid , gid , e
2018-05-27 09:36:35 +00:00
}
2020-07-19 03:26:56 +00:00
func ForumsEditPermsAdvance ( w http . ResponseWriter , r * http . Request , u * c . User , paramList string ) c . RouteError {
basePage , ferr := buildBasePage ( w , r , u , "edit_forum" , "forums" )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
2020-07-19 03:26:56 +00:00
if ! u . Perms . ManageForums {
return c . NoPermissions ( w , r , u )
2018-05-27 09:36:35 +00:00
}
fid , gid , err := forumPermsExtractDash ( paramList )
if err != nil {
2020-07-19 03:26:56 +00:00
return c . LocalError ( err . Error ( ) , w , r , u )
2018-05-27 09:36:35 +00:00
}
2020-03-18 09:21:34 +00:00
f , err := c . Forums . Get ( fid )
2018-05-27 09:36:35 +00:00
if err == sql . ErrNoRows {
2020-07-19 03:26:56 +00:00
return c . LocalError ( "The forum you're trying to edit doesn't exist." , w , r , u )
2018-05-27 09:36:35 +00:00
} else if err != nil {
2019-04-19 08:20:10 +00:00
return c . InternalError ( err , w , r )
2018-05-27 09:36:35 +00:00
}
2020-03-18 09:21:34 +00:00
if f . Preset == "" {
f . Preset = "custom"
2018-05-27 09:36:35 +00:00
}
2019-09-29 05:10:05 +00:00
fp , err := c . FPStore . Get ( fid , gid )
2018-05-27 09:36:35 +00:00
if err == sql . ErrNoRows {
2019-09-29 05:10:05 +00:00
fp = c . BlankForumPerms ( )
2018-05-27 09:36:35 +00:00
} else if err != nil {
2019-04-19 08:20:10 +00:00
return c . InternalError ( err , w , r )
2018-05-27 09:36:35 +00:00
}
2019-04-19 08:20:10 +00:00
var formattedPermList [ ] c . NameLangToggle
2018-05-27 09:36:35 +00:00
// TODO: Load the phrases in bulk for efficiency?
// TODO: Reduce the amount of code duplication between this and the group editor. Also, can we grind this down into one line or use a code generator to stay current more easily?
2019-10-06 11:32:00 +00:00
addToggle := func ( permStr string , perm bool ) {
formattedPermList = append ( formattedPermList , c . NameLangToggle { permStr , p . GetPermPhrase ( permStr ) , perm } )
2018-05-27 09:36:35 +00:00
}
2019-10-06 11:32:00 +00:00
addToggle ( "ViewTopic" , fp . ViewTopic )
addToggle ( "LikeItem" , fp . LikeItem )
addToggle ( "CreateTopic" , fp . CreateTopic )
2018-05-27 09:36:35 +00:00
//<--
2019-10-06 11:32:00 +00:00
addToggle ( "EditTopic" , fp . EditTopic )
addToggle ( "DeleteTopic" , fp . DeleteTopic )
addToggle ( "CreateReply" , fp . CreateReply )
addToggle ( "EditReply" , fp . EditReply )
addToggle ( "DeleteReply" , fp . DeleteReply )
addToggle ( "PinTopic" , fp . PinTopic )
addToggle ( "CloseTopic" , fp . CloseTopic )
addToggle ( "MoveTopic" , fp . MoveTopic )
2018-05-27 09:36:35 +00:00
if r . FormValue ( "updated" ) == "1" {
2019-04-27 06:32:26 +00:00
basePage . AddNotice ( "panel_forum_perms_updated" )
2018-05-27 09:36:35 +00:00
}
2020-03-18 09:21:34 +00:00
pi := c . PanelEditForumGroupPage { basePage , f . ID , gid , f . Name , f . Desc , f . Active , f . Preset , formattedPermList }
2019-10-06 11:32:00 +00:00
return renderTemplate ( "panel" , w , r , basePage . Header , c . Panel { basePage , "" , "" , "panel_forum_edit_perms" , & pi } )
2018-05-27 09:36:35 +00:00
}
2020-07-19 03:26:56 +00:00
func ForumsEditPermsAdvanceSubmit ( w http . ResponseWriter , r * http . Request , u * c . User , paramList string ) c . RouteError {
_ , ferr := c . SimplePanelUserCheck ( w , r , u )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
2020-07-19 03:26:56 +00:00
if ! u . Perms . ManageForums {
return c . NoPermissions ( w , r , u )
2018-05-27 09:36:35 +00:00
}
2019-10-06 11:32:00 +00:00
js := r . PostFormValue ( "js" ) == "1"
2018-05-27 09:36:35 +00:00
fid , gid , err := forumPermsExtractDash ( paramList )
if err != nil {
2020-07-19 03:26:56 +00:00
return c . LocalError ( err . Error ( ) , w , r , u )
2018-05-27 09:36:35 +00:00
}
2021-04-07 14:23:11 +00:00
f , err := c . Forums . Get ( fid )
2018-05-27 09:36:35 +00:00
if err == sql . ErrNoRows {
2020-07-19 03:26:56 +00:00
return c . LocalError ( "The forum you're trying to edit doesn't exist." , w , r , u )
2018-05-27 09:36:35 +00:00
} else if err != nil {
2019-04-19 08:20:10 +00:00
return c . InternalError ( err , w , r )
2018-05-27 09:36:35 +00:00
}
2019-09-29 05:10:05 +00:00
fp , err := c . FPStore . GetCopy ( fid , gid )
2018-05-27 09:36:35 +00:00
if err == sql . ErrNoRows {
2019-09-29 05:10:05 +00:00
fp = * c . BlankForumPerms ( )
2018-05-27 09:36:35 +00:00
} else if err != nil {
2019-04-19 08:20:10 +00:00
return c . InternalError ( err , w , r )
2018-05-27 09:36:35 +00:00
}
2021-04-07 14:23:11 +00:00
ep := func ( name string ) bool {
2020-03-18 09:21:34 +00:00
pvalue := r . PostFormValue ( "perm-" + name )
2018-05-27 09:36:35 +00:00
return ( pvalue == "1" )
}
// TODO: Generate this code?
2021-04-07 14:23:11 +00:00
fp . ViewTopic = ep ( "ViewTopic" )
fp . LikeItem = ep ( "LikeItem" )
fp . CreateTopic = ep ( "CreateTopic" )
fp . EditTopic = ep ( "EditTopic" )
fp . DeleteTopic = ep ( "DeleteTopic" )
fp . CreateReply = ep ( "CreateReply" )
fp . EditReply = ep ( "EditReply" )
fp . DeleteReply = ep ( "DeleteReply" )
fp . PinTopic = ep ( "PinTopic" )
fp . CloseTopic = ep ( "CloseTopic" )
fp . MoveTopic = ep ( "MoveTopic" )
err = f . SetPerms ( & fp , "custom" , gid )
2018-05-27 09:36:35 +00:00
if err != nil {
2020-07-19 03:26:56 +00:00
return c . LocalErrorJSQ ( err . Error ( ) , w , r , u , js )
2018-05-27 09:36:35 +00:00
}
2020-07-19 03:26:56 +00:00
err = c . AdminLogs . Create ( "edit" , fid , "forum" , u . GetIP ( ) , u . ID )
2019-11-08 10:08:37 +00:00
if err != nil {
return c . InternalError ( err , w , r )
}
2018-05-27 09:36:35 +00:00
2019-09-29 05:10:05 +00:00
return successRedirect ( "/panel/forums/edit/perms/" + strconv . Itoa ( fid ) + "-" + strconv . Itoa ( gid ) + "?updated=1" , w , r , js )
2018-05-27 09:36:35 +00:00
}
2021-04-07 14:23:11 +00:00
func ForumsEditActionDeleteSubmit ( w http . ResponseWriter , r * http . Request , u * c . User , sfaid string ) c . RouteError {
_ , ferr := c . SimplePanelUserCheck ( w , r , u )
if ferr != nil {
return ferr
}
// TODO: Should we split this permission?
if ! u . Perms . ManageForums {
return c . NoPermissions ( w , r , u )
}
js := r . PostFormValue ( "js" ) == "1"
faid , e := strconv . Atoi ( sfaid )
if e != nil {
return c . LocalError ( "The forum action ID is not a valid integer." , w , r , u )
}
e = c . ForumActionStore . Delete ( faid )
if e != nil {
return c . InternalError ( e , w , r )
}
fid , e := strconv . Atoi ( r . FormValue ( "ret" ) )
if e != nil {
return c . LocalError ( "The forum action ID is not a valid integer." , w , r , u )
}
if ! c . Forums . Exists ( fid ) {
return c . LocalError ( "The target forum doesn't exist." , w , r , u )
}
return successRedirect ( "/panel/forums/edit/" + strconv . Itoa ( fid ) + "?updated=1" , w , r , js )
}
func ForumsEditActionCreateSubmit ( w http . ResponseWriter , r * http . Request , u * c . User , sfid string ) c . RouteError {
_ , ferr := c . SimplePanelUserCheck ( w , r , u )
if ferr != nil {
return ferr
}
// TODO: Should we split this permission?
if ! u . Perms . ManageForums {
return c . NoPermissions ( w , r , u )
}
js := r . PostFormValue ( "js" ) == "1"
fid , e := strconv . Atoi ( sfid )
if e != nil {
return c . LocalError ( "The provided Forum ID is not a valid number." , w , r , u )
}
if ! c . Forums . Exists ( fid ) {
return c . LocalError ( "This forum does not exist" , w , r , u )
}
runOnTopicCreation := r . PostFormValue ( "action_run_on_topic_creation" ) == "1"
f := func ( s string ) ( int , c . RouteError ) {
i , e := strconv . Atoi ( r . PostFormValue ( s ) )
if e != nil {
return i , c . LocalError ( s + " is not a valid integer." , w , r , u )
}
if i < 0 {
return i , c . LocalError ( s + " cannot be less than 0" , w , r , u )
}
return i , nil
}
runDaysAfterTopicCreation , re := f ( "action_run_days_after_topic_creation" )
if re != nil {
return re
}
runDaysAfterTopicLastReply , re := f ( "action_run_days_after_topic_last_reply" )
if re != nil {
return re
}
action := r . PostFormValue ( "action_action" )
aint := c . ConvStringToAct ( action )
if aint == - 1 {
return c . LocalError ( "invalid action" , w , r , u )
}
extra := r . PostFormValue ( "action_extra" )
switch aint {
case c . ForumActionMove :
conv , e := strconv . Atoi ( extra )
if e != nil {
return c . LocalError ( "action_extra is not a valid integer." , w , r , u )
}
extra = strconv . Itoa ( conv )
default :
extra = ""
}
_ , e = c . ForumActionStore . Add ( & c . ForumAction {
Forum : fid ,
RunOnTopicCreation : runOnTopicCreation ,
RunDaysAfterTopicCreation : runDaysAfterTopicCreation ,
RunDaysAfterTopicLastReply : runDaysAfterTopicLastReply ,
Action : aint ,
Extra : extra ,
} )
if e != nil {
return c . InternalError ( e , w , r )
}
return successRedirect ( "/panel/forums/edit/" + strconv . Itoa ( fid ) + "?updated=1" , w , r , js )
}