diff --git a/forum.go b/forum.go index f697a3b0..a1fdf735 100644 --- a/forum.go +++ b/forum.go @@ -1,8 +1,12 @@ package main //import "fmt" -import "strconv" -import _ "github.com/go-sql-driver/mysql" +import ( + "strconv" + "strings" + + _ "github.com/go-sql-driver/mysql" +) type ForumAdmin struct { ID int @@ -39,6 +43,21 @@ type ForumSimple struct { Preset string } +func (forum *Forum) Update(name string, desc string, active bool, preset string) error { + if name == "" { + name = forum.Name + } + preset = strings.TrimSpace(preset) + _, err := updateForumStmt.Exec(name, desc, active, preset, forum.ID) + if err != nil { + return err + } + if forum.Preset != preset || preset == "custom" || preset == "" { + permmapToQuery(presetToPermmap(preset), forum.ID) + } + _ = fstore.Reload(forum.ID) +} + // TODO: Replace this sorting mechanism with something a lot more efficient // ? - Use sort.Slice instead? type SortForum []*Forum diff --git a/forum_store.go b/forum_store.go index 6a36bcd5..73abbf68 100644 --- a/forum_store.go +++ b/forum_store.go @@ -8,6 +8,7 @@ package main import ( "database/sql" + "errors" "log" "sort" "sync" @@ -28,7 +29,7 @@ type ForumStore interface { Get(id int) (*Forum, error) GetCopy(id int) (Forum, error) BypassGet(id int) (*Forum, error) - Reload(id int) error // ? - Should we move this to TopicCache? Might require us to do a lot more casting in Gosora though... + Reload(id int) error // ? - Should we move this to ForumCache? It might require us to do some unnecessary casting though //Update(Forum) error Delete(id int) error IncrementTopicCount(id int) error @@ -216,9 +217,6 @@ func (mfs *MemoryForumStore) Reload(id int) error { } func (mfs *MemoryForumStore) CacheSet(forum *Forum) error { - if !mfs.Exists(forum.ID) { - return ErrNoRows - } mfs.forums.Store(forum.ID, forum) mfs.rebuildView() return nil @@ -278,7 +276,11 @@ func (mfs *MemoryForumStore) CacheDelete(id int) { mfs.rebuildView() } +// TODO: Add a hook to allow plugin_socialgroups to detect when one of it's forums has just been deleted? func (mfs *MemoryForumStore) Delete(id int) error { + if id == 1 { + return errors.New("You cannot delete the Reports forum") + } forumUpdateMutex.Lock() defer forumUpdateMutex.Unlock() _, err := mfs.delete.Exec(id) diff --git a/panel_routes.go b/panel_routes.go index 4f26c602..9e2edac7 100644 --- a/panel_routes.go +++ b/panel_routes.go @@ -417,11 +417,6 @@ func routePanelForumsEditSubmit(w http.ResponseWriter, r *http.Request, user Use return } - forumName := r.PostFormValue("forum_name") - forumDesc := r.PostFormValue("forum_desc") - forumPreset := stripInvalidPreset(r.PostFormValue("forum_preset")) - forumActive := r.PostFormValue("forum_active") - forum, err := fstore.Get(fid) if err == ErrNoRows { LocalErrorJSQ("The forum you're trying to edit doesn't exist.", w, r, user, isJs) @@ -431,40 +426,23 @@ func routePanelForumsEditSubmit(w http.ResponseWriter, r *http.Request, user Use return } - if forumName == "" { - forumName = forum.Name - } + forumName := r.PostFormValue("forum_name") + forumDesc := r.PostFormValue("forum_desc") + forumPreset := stripInvalidPreset(r.PostFormValue("forum_preset")) + forumActive := r.PostFormValue("forum_active") - var active bool + var active = false if forumActive == "" { active = forum.Active } else if forumActive == "1" || forumActive == "Show" { active = true - } else { - active = false } - forumUpdateMutex.Lock() - _, err = updateForumStmt.Exec(forumName, forumDesc, active, forumPreset, fid) + err = forum.Update(forumName, forumDesc, active, forumPreset) if err != nil { InternalErrorJSQ(err, w, r, isJs) return } - if forum.Name != forumName { - forum.Name = forumName - } - if forum.Desc != forumDesc { - forum.Desc = forumDesc - } - if forum.Active != active { - forum.Active = active - } - if forum.Preset != forumPreset { - forum.Preset = forumPreset - } - forumUpdateMutex.Unlock() - - permmapToQuery(presetToPermmap(forumPreset), fid) if !isJs { http.Redirect(w, r, "/panel/forums/", http.StatusSeeOther) @@ -473,6 +451,7 @@ func routePanelForumsEditSubmit(w http.ResponseWriter, r *http.Request, user Use } } +// ! This probably misses the forumView cache func routePanelForumsEditPermsSubmit(w http.ResponseWriter, r *http.Request, user User, sfid string) { _, ok := SimplePanelUserCheck(w, r, &user) if !ok { diff --git a/public/global.js b/public/global.js index a32e7ebc..96deddb6 100644 --- a/public/global.js +++ b/public/global.js @@ -305,11 +305,12 @@ $(document).ready(function(){ //console.log("Field Type",field_type) //console.log("Field Value '" + field_value + "'") for (var i = 0; i < itLen; i++) { + var sel = ""; if(field_value == i || field_value == it[i]) { sel = "selected "; this.classList.remove(field_name + '_' + it[i]); this.innerHTML = ""; - } else sel = ""; + } out += ""; } this.innerHTML = ""; diff --git a/templates/panel-forum-edit.html b/templates/panel-forum-edit.html index 6bc08d11..9eb78459 100644 --- a/templates/panel-forum-edit.html +++ b/templates/panel-forum-edit.html @@ -21,8 +21,8 @@ var form_vars = {'perm_preset': ['can_moderate','can_post','read_only','no_acces