2023-03-01 05:17:12 +00:00
|
|
|
package wp
|
2023-02-10 16:32:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2023-05-09 05:44:20 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/cmd/reload"
|
2023-05-04 12:37:06 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/cache"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/constraints"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
|
|
|
"github.com/fthvgb1/wp-go/app/plugins"
|
2023-02-10 16:32:46 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/number"
|
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
|
|
|
"github.com/fthvgb1/wp-go/model"
|
|
|
|
"github.com/fthvgb1/wp-go/plugin/pagination"
|
2023-03-06 12:53:51 +00:00
|
|
|
"strings"
|
2023-02-10 16:32:46 +00:00
|
|
|
)
|
|
|
|
|
2023-02-23 09:26:20 +00:00
|
|
|
type IndexHandle struct {
|
|
|
|
*Handle
|
2023-04-09 13:51:12 +00:00
|
|
|
Param *IndexParams
|
|
|
|
Posts []models.Posts
|
|
|
|
pageEle pagination.Elements
|
|
|
|
TotalRows int
|
|
|
|
postsPlugin PostsPlugin
|
2023-02-28 15:59:17 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 13:51:12 +00:00
|
|
|
func (i *IndexHandle) ListPlugin() func(*Handle, *models.Posts) {
|
|
|
|
return i.postsPlugin
|
2023-02-28 15:59:17 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 13:51:12 +00:00
|
|
|
func (i *IndexHandle) SetListPlugin(listPlugin func(*Handle, *models.Posts)) {
|
|
|
|
i.postsPlugin = listPlugin
|
2023-02-28 15:59:17 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 13:51:12 +00:00
|
|
|
func (i *IndexHandle) PageEle() pagination.Elements {
|
|
|
|
return i.pageEle
|
2023-02-28 15:59:17 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 13:51:12 +00:00
|
|
|
func (i *IndexHandle) SetPageEle(pageEle pagination.Elements) {
|
|
|
|
i.pageEle = pageEle
|
2023-02-23 09:26:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIndexHandle(handle *Handle) *IndexHandle {
|
|
|
|
return &IndexHandle{Handle: handle}
|
|
|
|
}
|
|
|
|
|
2023-05-05 12:45:20 +00:00
|
|
|
func PushIndexHandler(pipeScene string, h *Handle, call HandleCall) {
|
|
|
|
h.PushHandlers(pipeScene, call, constraints.Home,
|
|
|
|
constraints.Category, constraints.Search, constraints.Tag,
|
|
|
|
constraints.Archive, constraints.Author,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:51:07 +00:00
|
|
|
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:
|
2023-02-11 15:51:07 +00:00
|
|
|
i.Param.ParseSearch()
|
2023-02-10 16:32:46 +00:00
|
|
|
case constraints.Category:
|
2023-02-11 15:51:07 +00:00
|
|
|
err = i.Param.ParseCategory()
|
2023-02-10 16:32:46 +00:00
|
|
|
case constraints.Tag:
|
2023-02-11 15:51:07 +00:00
|
|
|
err = i.Param.ParseTag()
|
2023-02-10 16:32:46 +00:00
|
|
|
case constraints.Archive:
|
2023-02-11 15:51:07 +00:00
|
|
|
err = i.Param.ParseArchive()
|
2023-02-10 16:32:46 +00:00
|
|
|
case constraints.Author:
|
2023-02-11 15:51:07 +00:00
|
|
|
err = i.Param.ParseAuthor()
|
2023-02-10 16:32:46 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2023-02-11 15:51:07 +00:00
|
|
|
i.Stats = constraints.ParamError
|
2023-02-10 16:32:46 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-11 15:51:07 +00:00
|
|
|
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
|
2023-02-10 16:32:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:51:07 +00:00
|
|
|
func (i *IndexHandle) GetIndexData() (posts []models.Posts, totalRaw int, err error) {
|
2023-02-10 16:32:46 +00:00
|
|
|
|
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 {
|
2023-02-10 16:32:46 +00:00
|
|
|
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)
|
2023-03-29 13:58:39 +00:00
|
|
|
if i.scene == constraints.Home && i.Param.Page == 1 {
|
|
|
|
i.MarkSticky(&posts)
|
|
|
|
}
|
2023-02-10 16:32:46 +00:00
|
|
|
|
|
|
|
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)
|
2023-02-10 16:32:46 +00:00
|
|
|
|
|
|
|
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")
|
2023-02-11 15:51:07 +00:00
|
|
|
posts, totalRaw, err = cache.GetMonthPostIds(i.C, i.Param.Year, i.Param.Month, i.Param.Page, i.Param.PageSize, i.Param.Order)
|
2023-02-10 16:32:46 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:51:07 +00:00
|
|
|
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
|
|
|
}
|
2023-02-11 15:51:07 +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)
|
2023-02-11 15:51:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IndexHandle) BuildIndexData(parm *IndexParams) (err error) {
|
|
|
|
err = i.ParseIndex(parm)
|
2023-02-10 16:32:46 +00:00
|
|
|
if err != nil {
|
2023-02-27 14:58:35 +00:00
|
|
|
i.Stats = constraints.ParamError
|
2023-02-10 16:32:46 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-11 15:51:07 +00:00
|
|
|
posts, totalRows, err := i.GetIndexData()
|
2023-02-10 16:32:46 +00:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2023-02-27 14:58:35 +00:00
|
|
|
i.Stats = constraints.Error404
|
2023-02-10 16:32:46 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-11 15:51:07 +00:00
|
|
|
i.Posts = posts
|
|
|
|
i.TotalRows = totalRows
|
2023-02-28 15:38:23 +00:00
|
|
|
i.ginH["totalPage"] = number.CalTotalPage(totalRows, i.Param.PageSize)
|
2023-02-10 16:32:46 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-11 15:51:07 +00:00
|
|
|
|
2023-04-09 13:51:12 +00:00
|
|
|
func (i *IndexHandle) ExecPostsPlugin() {
|
2023-05-09 05:44:20 +00:00
|
|
|
fn := i.postsPlugin
|
|
|
|
if fn == nil {
|
|
|
|
fn = reload.GetAnyValBys("postPlugins", i, func(a *IndexHandle) PostsPlugin {
|
|
|
|
return UsePostsPlugins()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for j := range i.Posts {
|
|
|
|
fn(i.Handle, &i.Posts[j])
|
2023-02-11 15:51:07 +00:00
|
|
|
}
|
2023-02-14 15:28:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 15:02:54 +00:00
|
|
|
func IndexRender(h *Handle) {
|
|
|
|
i := h.Index
|
|
|
|
i.ExecPostsPlugin()
|
|
|
|
i.Pagination()
|
|
|
|
i.ginH["posts"] = i.Posts
|
|
|
|
}
|
|
|
|
|
2023-05-05 12:45:20 +00:00
|
|
|
func Index(h *Handle) {
|
2023-03-17 10:26:08 +00:00
|
|
|
i := h.Index
|
2023-05-05 12:45:20 +00:00
|
|
|
err := i.BuildIndexData(NewIndexParams(i.C))
|
|
|
|
if err != nil {
|
|
|
|
i.SetErr(err)
|
|
|
|
}
|
|
|
|
h.SetData("scene", h.Scene())
|
2023-02-14 15:28:45 +00:00
|
|
|
}
|
2023-03-29 13:58:39 +00:00
|
|
|
|
|
|
|
func (i *IndexHandle) MarkSticky(posts *[]models.Posts) {
|
|
|
|
a := i.StickPosts()
|
|
|
|
if len(a) < 1 {
|
|
|
|
return
|
|
|
|
}
|
2023-04-04 15:02:54 +00:00
|
|
|
m := i.StickMapPosts()
|
2023-03-29 13:58:39 +00:00
|
|
|
*posts = append(a, slice.Filter(*posts, func(post models.Posts, _ int) bool {
|
2023-04-04 15:02:54 +00:00
|
|
|
_, ok := m[post.Id]
|
|
|
|
return !ok
|
2023-03-29 13:58:39 +00:00
|
|
|
})...)
|
|
|
|
}
|