2018-02-10 15:07:21 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
"github.com/Azareal/Gosora/common"
|
2018-11-01 06:43:56 +00:00
|
|
|
"github.com/Azareal/Gosora/common/phrases"
|
2018-02-10 15:07:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TopicList(w http.ResponseWriter, r *http.Request, user common.User) common.RouteError {
|
2018-04-22 12:33:56 +00:00
|
|
|
header, ferr := common.UserCheck(w, r, &user)
|
2018-02-10 15:07:21 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2018-11-01 06:43:56 +00:00
|
|
|
header.Title = phrases.GetTitlePhrase("topics")
|
2018-04-22 12:33:56 +00:00
|
|
|
header.Zone = "topics"
|
2018-10-14 05:08:44 +00:00
|
|
|
header.Path = "/topics/"
|
2018-04-22 12:33:56 +00:00
|
|
|
header.MetaDesc = header.Settings["meta_desc"].(string)
|
2018-02-10 15:07:21 +00:00
|
|
|
|
|
|
|
group, err := common.Groups.Get(user.Group)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Group #%d doesn't exist despite being used by common.User #%d", user.Group, user.ID)
|
|
|
|
return common.LocalError("Something weird happened", w, r, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current page
|
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
|
|
|
|
|
|
|
// TODO: Pass a struct back rather than passing back so many variables
|
|
|
|
var topicList []*common.TopicsRow
|
|
|
|
var forumList []common.Forum
|
2018-04-22 12:33:56 +00:00
|
|
|
var paginator common.Paginator
|
2018-02-10 15:07:21 +00:00
|
|
|
if user.IsSuperAdmin {
|
2018-09-26 07:46:30 +00:00
|
|
|
topicList, forumList, paginator, err = common.TopicList.GetList(page, "")
|
2018-02-10 15:07:21 +00:00
|
|
|
} else {
|
2018-09-26 07:46:30 +00:00
|
|
|
topicList, forumList, paginator, err = common.TopicList.GetListByGroup(group, page, "")
|
2018-02-10 15:07:21 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ! Need an inline error not a page level error
|
|
|
|
if len(topicList) == 0 {
|
2018-04-22 12:33:56 +00:00
|
|
|
return common.NotFound(w, r, header)
|
2018-02-10 15:07:21 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 08:56:50 +00:00
|
|
|
pi := common.TopicListPage{header, topicList, forumList, common.Config.DefaultForum, common.TopicListSort{"lastupdated", false}, paginator}
|
2018-02-19 04:26:01 +00:00
|
|
|
if common.RunPreRenderHook("pre_render_topic_list", w, r, &user, &pi) {
|
2018-04-22 12:33:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = common.RunThemeTemplate(header.Theme.Name, "topics", pi, w)
|
2018-02-10 15:07:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-26 07:46:30 +00:00
|
|
|
|
|
|
|
func TopicListMostViewed(w http.ResponseWriter, r *http.Request, user common.User) common.RouteError {
|
|
|
|
header, ferr := common.UserCheck(w, r, &user)
|
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2018-11-01 06:43:56 +00:00
|
|
|
header.Title = phrases.GetTitlePhrase("topics")
|
2018-09-26 07:46:30 +00:00
|
|
|
header.Zone = "topics"
|
2018-10-14 05:08:44 +00:00
|
|
|
header.Path = "/topics/"
|
2018-09-26 07:46:30 +00:00
|
|
|
header.MetaDesc = header.Settings["meta_desc"].(string)
|
|
|
|
|
|
|
|
group, err := common.Groups.Get(user.Group)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Group #%d doesn't exist despite being used by common.User #%d", user.Group, user.ID)
|
|
|
|
return common.LocalError("Something weird happened", w, r, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current page
|
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
|
|
|
|
|
|
|
// TODO: Pass a struct back rather than passing back so many variables
|
|
|
|
var topicList []*common.TopicsRow
|
|
|
|
var forumList []common.Forum
|
|
|
|
var paginator common.Paginator
|
|
|
|
if user.IsSuperAdmin {
|
|
|
|
topicList, forumList, paginator, err = common.TopicList.GetList(page, "most-viewed")
|
|
|
|
} else {
|
|
|
|
topicList, forumList, paginator, err = common.TopicList.GetListByGroup(group, page, "most-viewed")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ! Need an inline error not a page level error
|
|
|
|
if len(topicList) == 0 {
|
|
|
|
return common.NotFound(w, r, header)
|
|
|
|
}
|
|
|
|
|
|
|
|
pi := common.TopicListPage{header, topicList, forumList, common.Config.DefaultForum, common.TopicListSort{"mostviewed", false}, paginator}
|
|
|
|
if common.RunPreRenderHook("pre_render_topic_list", w, r, &user, &pi) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = common.RunThemeTemplate(header.Theme.Name, "topics", pi, w)
|
|
|
|
if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|