wp-go/internal/theme/wp/index.go

165 lines
4.1 KiB
Go
Raw Normal View History

2023-03-01 05:17:12 +00:00
package wp
import (
"database/sql"
"fmt"
"github.com/fthvgb1/wp-go/helper/number"
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/models"
2023-02-14 15:28:45 +00:00
"github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/model"
"github.com/fthvgb1/wp-go/plugin/pagination"
2023-03-06 12:53:51 +00:00
"strings"
)
2023-02-23 09:26:20 +00:00
type IndexHandle struct {
*Handle
Param *IndexParams
Posts []models.Posts
2023-02-28 15:59:17 +00:00
pageEle pagination.Elements
2023-02-23 09:26:20 +00:00
TotalRows int
2023-02-28 15:59:17 +00:00
postsPlugins map[string]Plugin[models.Posts, *Handle]
}
func (i *IndexHandle) PageEle() pagination.Elements {
return i.pageEle
}
func (i *IndexHandle) SetPageEle(pageEle pagination.Elements) {
i.pageEle = pageEle
}
func (i *IndexHandle) PostsPlugins() map[string]Plugin[models.Posts, *Handle] {
return i.postsPlugins
}
func (i *IndexHandle) SetPostsPlugins(postsPlugins map[string]Plugin[models.Posts, *Handle]) {
i.postsPlugins = postsPlugins
2023-02-23 09:26:20 +00:00
}
func NewIndexHandle(handle *Handle) *IndexHandle {
return &IndexHandle{Handle: handle}
}
func (i *IndexHandle) ParseIndex(parm *IndexParams) (err error) {
i.Param = parm
2023-02-28 15:38:23 +00:00
switch i.scene {
2023-03-17 10:26:08 +00:00
case constraints.Search:
i.Param.ParseSearch()
case constraints.Category:
err = i.Param.ParseCategory()
case constraints.Tag:
err = i.Param.ParseTag()
case constraints.Archive:
err = i.Param.ParseArchive()
case constraints.Author:
err = i.Param.ParseAuthor()
}
if err != nil {
i.Stats = constraints.ParamError
return
}
i.Param.ParseParams()
2023-02-14 15:44:48 +00:00
i.Param.CacheKey = i.Param.getSearchKey()
2023-02-28 15:38:23 +00:00
i.ginH["title"] = i.Param.getTitle()
i.ginH["search"] = i.Param.Search
i.ginH["header"] = i.Param.Header
return
}
func (i *IndexHandle) GetIndexData() (posts []models.Posts, totalRaw int, err error) {
2023-02-25 15:10:42 +00:00
q := model.QueryCondition{
Where: i.Param.Where,
Order: model.SqlBuilder{{i.Param.OrderBy, i.Param.Order}},
Join: i.Param.Join,
In: [][]any{i.Param.PostType, i.Param.PostStatus},
}
2023-02-28 15:38:23 +00:00
switch i.scene {
case constraints.Home, constraints.Category, constraints.Tag, constraints.Author:
2023-02-27 14:58:35 +00:00
posts, totalRaw, err = cache.PostLists(i.C, i.Param.CacheKey, i.C, q, i.Param.Page, i.Param.PageSize)
case constraints.Search:
2023-02-27 14:58:35 +00:00
posts, totalRaw, err = cache.SearchPost(i.C, i.Param.CacheKey, i.C, q, i.Param.Page, i.Param.PageSize)
case constraints.Archive:
2023-03-06 12:53:51 +00:00
i.ginH["archiveYear"] = i.Param.Year
i.ginH["archiveMonth"] = strings.TrimLeft(i.Param.Month, "0")
posts, totalRaw, err = cache.GetMonthPostIds(i.C, i.Param.Year, i.Param.Month, i.Param.Page, i.Param.PageSize, i.Param.Order)
}
return
}
func (i *IndexHandle) Pagination() {
2023-02-28 15:59:17 +00:00
if i.pageEle == nil {
i.pageEle = plugins.TwentyFifteenPagination()
2023-02-23 09:26:20 +00:00
}
q := i.C.Request.URL.Query().Encode()
if q != "" {
q = fmt.Sprintf("?%s", q)
}
paginations := pagination.NewParsePagination(i.TotalRows, i.Param.PageSize, i.Param.Page, i.Param.PaginationStep, q, i.C.Request.URL.Path)
2023-02-28 15:59:17 +00:00
i.ginH["pagination"] = pagination.Paginate(i.pageEle, paginations)
}
func (i *IndexHandle) BuildIndexData(parm *IndexParams) (err error) {
err = i.ParseIndex(parm)
if err != nil {
2023-02-27 14:58:35 +00:00
i.Stats = constraints.ParamError
return
}
posts, totalRows, err := i.GetIndexData()
if err != nil && err != sql.ErrNoRows {
2023-02-27 14:58:35 +00:00
i.Stats = constraints.Error404
return
}
i.Posts = posts
i.TotalRows = totalRows
2023-02-28 15:38:23 +00:00
i.ginH["totalPage"] = number.CalTotalPage(totalRows, i.Param.PageSize)
return
}
func (i *IndexHandle) ExecPostsPlugin(calls ...func(*models.Posts)) {
pluginConf := config.GetConfig().ListPagePlugins
2023-02-28 15:59:17 +00:00
postsPlugins := i.postsPlugins
if postsPlugins == nil {
postsPlugins = pluginFns
}
plugin := GetListPostPlugins(pluginConf, postsPlugins)
2023-03-02 13:16:43 +00:00
i.Posts = slice.Map(i.Posts, PluginFn[models.Posts](plugin, i.Handle, Defaults(calls...)))
}
2023-02-14 15:28:45 +00:00
func (i *IndexHandle) Render() {
2023-02-28 15:59:17 +00:00
i.PushHandleFn(constraints.Ok, NewHandleFn(func(h *Handle) {
i.ExecPostsPlugin()
i.Pagination()
2023-03-02 15:49:28 +00:00
i.ginH["posts"] = i.Posts
2023-02-28 15:59:17 +00:00
}, 10))
2023-03-03 15:09:39 +00:00
2023-02-23 09:26:20 +00:00
i.Handle.Render()
2023-02-14 15:28:45 +00:00
}
2023-03-17 05:35:22 +00:00
func Indexs(h *Handle) {
2023-03-17 10:26:08 +00:00
if h.Scene() == constraints.Detail {
return
2023-03-17 05:35:22 +00:00
}
2023-03-17 10:26:08 +00:00
i := h.Index
_ = i.BuildIndexData(NewIndexParams(i.C))
PreCodeAndStats(h)
PreTemplate(h)
2023-02-14 15:28:45 +00:00
}