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"
2018-11-01 06:43:56 +00:00
"github.com/Azareal/Gosora/common/phrases"
2018-05-27 09:36:35 +00:00
)
2019-04-19 08:20:10 +00:00
func Forums ( 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 , "forums" , "forums" )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ManageForums {
2019-04-19 08:20:10 +00:00
return c . NoPermissions ( w , r , user )
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/
for _ , forum := range forums {
if forum . Name != "" && forum . ParentID == 0 {
2019-04-19 08:20:10 +00:00
fadmin := c . ForumAdmin { forum . ID , forum . Name , forum . Desc , forum . Active , forum . Preset , forum . TopicCount , c . PresetToLang ( forum . 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-04-29 08:17:12 +00:00
return renderTemplate ( "panel" , w , r , basePage . Header , c . Panel { basePage , "" , "" , "panel_forums" , & pi } )
2018-05-27 09:36:35 +00:00
}
2019-04-19 08:20:10 +00:00
func ForumsCreateSubmit ( w http . ResponseWriter , r * http . Request , user c . User ) c . RouteError {
_ , ferr := c . SimplePanelUserCheck ( w , r , & user )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ManageForums {
2019-04-19 08:20:10 +00:00
return c . NoPermissions ( w , r , user )
2018-05-27 09:36:35 +00:00
}
fname := r . PostFormValue ( "forum-name" )
fdesc := r . PostFormValue ( "forum-desc" )
2019-04-19 08:20:10 +00:00
fpreset := c . StripInvalidPreset ( r . PostFormValue ( "forum-preset" ) )
2018-05-27 09:36:35 +00:00
factive := r . PostFormValue ( "forum-active" )
active := ( factive == "on" || factive == "1" )
2019-04-19 08:20:10 +00:00
_ , err := c . Forums . Create ( fname , fdesc , active , fpreset )
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
2019-04-19 08:20:10 +00:00
func ForumsDelete ( w http . ResponseWriter , r * http . Request , user c . User , sfid string ) c . RouteError {
2018-06-17 07:28:18 +00:00
basePage , ferr := buildBasePage ( w , r , & user , "delete_forum" , "forums" )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ManageForums {
2019-04-19 08:20:10 +00:00
return c . NoPermissions ( w , r , user )
2018-05-27 09:36:35 +00:00
}
fid , err := strconv . Atoi ( sfid )
if err != nil {
2019-04-19 08:20:10 +00:00
return c . LocalError ( "The provided Forum ID is not a valid number." , w , r , user )
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 {
2019-04-19 08:20:10 +00:00
return c . LocalError ( "The forum you're trying to delete doesn't exist." , w , r , user )
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-02-10 05:52:26 +00:00
confirmMsg := phrases . GetTmplPhrasef ( "panel_forum_delete_are_you_sure" , forum . Name )
2019-04-19 08:20:10 +00:00
yousure := c . AreYouSure { "/panel/forums/delete/submit/" + strconv . Itoa ( fid ) , confirmMsg }
2018-05-27 09:36:35 +00:00
2019-04-19 08:20:10 +00:00
pi := c . PanelPage { basePage , tList , yousure }
if c . RunPreRenderHook ( "pre_render_panel_delete_forum" , w , r , & user , & 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
}
2019-04-19 08:20:10 +00:00
func ForumsDeleteSubmit ( w http . ResponseWriter , r * http . Request , user c . User , sfid string ) c . RouteError {
_ , ferr := c . SimplePanelUserCheck ( w , r , & user )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ManageForums {
2019-04-19 08:20:10 +00:00
return c . NoPermissions ( w , r , user )
2018-05-27 09:36:35 +00:00
}
fid , err := strconv . Atoi ( sfid )
if err != nil {
2019-04-19 08:20:10 +00:00
return c . LocalError ( "The provided Forum ID is not a valid number." , w , r , user )
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 {
2019-04-19 08:20:10 +00:00
return c . LocalError ( "The forum you're trying to delete doesn't exist." , w , r , user )
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
}
http . Redirect ( w , r , "/panel/forums/?deleted=1" , http . StatusSeeOther )
return nil
}
2019-04-27 06:32:26 +00:00
func ForumsOrderSubmit ( w http . ResponseWriter , r * http . Request , user c . User ) c . RouteError {
_ , ferr := c . SimplePanelUserCheck ( w , r , & user )
if ferr != nil {
return ferr
}
isJs := ( r . PostFormValue ( "js" ) == "1" )
if ! user . Perms . ManageForums {
return c . NoPermissionsJSQ ( w , r , user , isJs )
}
sitems := strings . TrimSuffix ( strings . TrimPrefix ( r . PostFormValue ( "items" ) , "{" ) , "}" )
//fmt.Printf("sitems: %+v\n", sitems)
var updateMap = make ( map [ int ] int )
for index , sfid := range strings . Split ( sitems , "," ) {
fid , err := strconv . Atoi ( sfid )
if err != nil {
return c . LocalErrorJSQ ( "Invalid integer in forum list" , w , r , user , isJs )
}
updateMap [ fid ] = index
}
c . Forums . UpdateOrder ( updateMap )
return successRedirect ( "/panel/forums/" , w , r , isJs )
}
2019-04-19 08:20:10 +00:00
func ForumsEdit ( w http . ResponseWriter , r * http . Request , user c . User , sfid string ) c . RouteError {
2018-06-17 07:28:18 +00:00
basePage , ferr := buildBasePage ( w , r , & user , "edit_forum" , "forums" )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ManageForums {
2019-04-19 08:20:10 +00:00
return c . NoPermissions ( w , r , user )
2018-05-27 09:36:35 +00:00
}
2019-07-28 10:42:30 +00:00
2018-05-27 09:36:35 +00:00
fid , err := strconv . Atoi ( sfid )
if err != nil {
2019-07-28 10:42:30 +00:00
return c . SimpleError ( phrases . 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
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 {
2019-04-19 08:20:10 +00:00
return c . LocalError ( "The forum you're trying to edit doesn't exist." , w , r , user )
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
}
if forum . Preset == "" {
forum . Preset = "custom"
}
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
}
2019-04-19 08:20:10 +00:00
pi := c . PanelEditForumPage { basePage , forum . ID , forum . Name , forum . Desc , forum . Active , forum . Preset , gplist }
2019-04-29 08:17:12 +00:00
return renderTemplate ( "panel" , w , r , basePage . Header , c . Panel { basePage , "" , "" , "panel_forum_edit" , & pi } )
2018-05-27 09:36:35 +00:00
}
2019-04-19 08:20:10 +00:00
func ForumsEditSubmit ( w http . ResponseWriter , r * http . Request , user c . User , sfid string ) c . RouteError {
_ , ferr := c . SimplePanelUserCheck ( w , r , & user )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ManageForums {
2019-04-19 08:20:10 +00:00
return c . NoPermissions ( w , r , user )
2018-05-27 09:36:35 +00:00
}
isJs := ( r . PostFormValue ( "js" ) == "1" )
fid , err := strconv . Atoi ( sfid )
if err != nil {
2019-04-19 08:20:10 +00:00
return c . LocalErrorJSQ ( "The provided Forum ID is not a valid number." , w , r , user , isJs )
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 {
2019-04-19 08:20:10 +00:00
return c . LocalErrorJSQ ( "The forum you're trying to edit doesn't exist." , w , r , user , isJs )
2018-05-27 09:36:35 +00:00
} else if err != nil {
2019-04-19 08:20:10 +00:00
return c . InternalErrorJSQ ( err , w , r , isJs )
2018-05-27 09:36:35 +00:00
}
forumName := r . PostFormValue ( "forum_name" )
forumDesc := r . PostFormValue ( "forum_desc" )
2019-04-19 08:20:10 +00:00
forumPreset := c . StripInvalidPreset ( r . PostFormValue ( "forum_preset" ) )
2018-05-27 09:36:35 +00:00
forumActive := r . PostFormValue ( "forum_active" )
var active = false
if forumActive == "" {
active = forum . Active
} else if forumActive == "1" || forumActive == "Show" {
active = true
}
err = forum . Update ( forumName , forumDesc , active , forumPreset )
if err != nil {
2019-04-19 08:20:10 +00:00
return c . InternalErrorJSQ ( err , w , r , isJs )
2018-05-27 09:36:35 +00:00
}
// ? Should we redirect to the forum editor instead?
2018-10-02 04:09:17 +00:00
return successRedirect ( "/panel/forums/" , w , r , isJs )
2018-05-27 09:36:35 +00:00
}
2019-04-19 08:20:10 +00:00
func ForumsEditPermsSubmit ( w http . ResponseWriter , r * http . Request , user c . User , sfid string ) c . RouteError {
_ , ferr := c . SimplePanelUserCheck ( w , r , & user )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ManageForums {
2019-04-19 08:20:10 +00:00
return c . NoPermissions ( w , r , user )
2018-05-27 09:36:35 +00:00
}
isJs := ( r . PostFormValue ( "js" ) == "1" )
fid , err := strconv . Atoi ( sfid )
if err != nil {
2019-04-19 08:20:10 +00:00
return c . LocalErrorJSQ ( "The provided Forum ID is not a valid number." , w , r , user , isJs )
2018-05-27 09:36:35 +00:00
}
gid , err := strconv . Atoi ( r . PostFormValue ( "gid" ) )
if err != nil {
2019-04-19 08:20:10 +00:00
return c . LocalErrorJSQ ( "Invalid Group ID" , w , r , user , isJs )
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 {
2019-04-19 08:20:10 +00:00
return c . LocalErrorJSQ ( "This forum doesn't exist" , w , r , user , isJs )
2018-05-27 09:36:35 +00:00
} else if err != nil {
2019-04-19 08:20:10 +00:00
return c . InternalErrorJSQ ( err , w , r , isJs )
2018-05-27 09:36:35 +00:00
}
2019-04-19 08:20:10 +00:00
permPreset := c . StripInvalidGroupForumPreset ( r . PostFormValue ( "perm_preset" ) )
2018-05-27 09:36:35 +00:00
err = forum . SetPreset ( permPreset , gid )
if err != nil {
2019-04-19 08:20:10 +00:00
return c . LocalErrorJSQ ( err . Error ( ) , w , r , user , isJs )
2018-05-27 09:36:35 +00:00
}
2018-10-02 04:09:17 +00:00
return successRedirect ( "/panel/forums/edit/" + strconv . Itoa ( fid ) + "?updated=1" , w , r , isJs )
2018-05-27 09:36:35 +00:00
}
// A helper function for the Advanced portion of the Forum Perms Editor
func forumPermsExtractDash ( paramList string ) ( fid int , gid int , err error ) {
params := strings . Split ( paramList , "-" )
if len ( params ) != 2 {
return fid , gid , errors . New ( "Parameter count mismatch" )
}
fid , err = strconv . Atoi ( params [ 0 ] )
if err != nil {
return fid , gid , errors . New ( "The provided Forum ID is not a valid number." )
}
gid , err = strconv . Atoi ( params [ 1 ] )
if err != nil {
err = errors . New ( "The provided Group ID is not a valid number." )
}
return fid , gid , err
}
2019-04-19 08:20:10 +00:00
func ForumsEditPermsAdvance ( w http . ResponseWriter , r * http . Request , user c . User , paramList string ) c . RouteError {
2018-06-17 07:28:18 +00:00
basePage , ferr := buildBasePage ( w , r , & user , "edit_forum" , "forums" )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ManageForums {
2019-04-19 08:20:10 +00:00
return c . NoPermissions ( w , r , user )
2018-05-27 09:36:35 +00:00
}
fid , gid , err := forumPermsExtractDash ( paramList )
if err != nil {
2019-04-19 08:20:10 +00:00
return c . LocalError ( err . Error ( ) , w , r , user )
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 {
2019-04-19 08:20:10 +00:00
return c . LocalError ( "The forum you're trying to edit doesn't exist." , w , r , user )
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
}
if forum . Preset == "" {
forum . Preset = "custom"
}
2019-04-19 08:20:10 +00:00
forumPerms , err := c . FPStore . Get ( fid , gid )
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
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?
var addNameLangToggle = func ( permStr string , perm bool ) {
2019-04-19 08:20:10 +00:00
formattedPermList = append ( formattedPermList , c . NameLangToggle { permStr , phrases . GetLocalPermPhrase ( permStr ) , perm } )
2018-05-27 09:36:35 +00:00
}
addNameLangToggle ( "ViewTopic" , forumPerms . ViewTopic )
addNameLangToggle ( "LikeItem" , forumPerms . LikeItem )
addNameLangToggle ( "CreateTopic" , forumPerms . CreateTopic )
//<--
addNameLangToggle ( "EditTopic" , forumPerms . EditTopic )
addNameLangToggle ( "DeleteTopic" , forumPerms . DeleteTopic )
addNameLangToggle ( "CreateReply" , forumPerms . CreateReply )
addNameLangToggle ( "EditReply" , forumPerms . EditReply )
addNameLangToggle ( "DeleteReply" , forumPerms . DeleteReply )
addNameLangToggle ( "PinTopic" , forumPerms . PinTopic )
addNameLangToggle ( "CloseTopic" , forumPerms . CloseTopic )
addNameLangToggle ( "MoveTopic" , forumPerms . MoveTopic )
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
}
2019-04-19 08:20:10 +00:00
pi := c . PanelEditForumGroupPage { basePage , forum . ID , gid , forum . Name , forum . Desc , forum . Active , forum . Preset , formattedPermList }
2019-04-29 08:17:12 +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
}
2019-04-19 08:20:10 +00:00
func ForumsEditPermsAdvanceSubmit ( w http . ResponseWriter , r * http . Request , user c . User , paramList string ) c . RouteError {
_ , ferr := c . SimplePanelUserCheck ( w , r , & user )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ManageForums {
2019-04-19 08:20:10 +00:00
return c . NoPermissions ( w , r , user )
2018-05-27 09:36:35 +00:00
}
isJs := ( r . PostFormValue ( "js" ) == "1" )
fid , gid , err := forumPermsExtractDash ( paramList )
if err != nil {
2019-04-19 08:20:10 +00:00
return c . LocalError ( err . Error ( ) , w , r , user )
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 {
2019-04-19 08:20:10 +00:00
return c . LocalError ( "The forum you're trying to edit doesn't exist." , w , r , user )
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
forumPerms , err := c . FPStore . GetCopy ( fid , gid )
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
}
var extractPerm = func ( name string ) bool {
pvalue := r . PostFormValue ( "forum-perm-" + name )
return ( pvalue == "1" )
}
// TODO: Generate this code?
forumPerms . ViewTopic = extractPerm ( "ViewTopic" )
forumPerms . LikeItem = extractPerm ( "LikeItem" )
forumPerms . CreateTopic = extractPerm ( "CreateTopic" )
forumPerms . EditTopic = extractPerm ( "EditTopic" )
forumPerms . DeleteTopic = extractPerm ( "DeleteTopic" )
forumPerms . CreateReply = extractPerm ( "CreateReply" )
forumPerms . EditReply = extractPerm ( "EditReply" )
forumPerms . DeleteReply = extractPerm ( "DeleteReply" )
forumPerms . PinTopic = extractPerm ( "PinTopic" )
forumPerms . CloseTopic = extractPerm ( "CloseTopic" )
forumPerms . MoveTopic = extractPerm ( "MoveTopic" )
err = forum . SetPerms ( & forumPerms , "custom" , gid )
if err != nil {
2019-04-19 08:20:10 +00:00
return c . LocalErrorJSQ ( err . Error ( ) , w , r , user , isJs )
2018-05-27 09:36:35 +00:00
}
2018-10-02 04:09:17 +00:00
return successRedirect ( "/panel/forums/edit/perms/" + strconv . Itoa ( fid ) + "-" + strconv . Itoa ( gid ) + "?updated=1" , w , r , isJs )
2018-05-27 09:36:35 +00:00
}