2023-03-01 05:17:12 +00:00
|
|
|
package wp
|
2023-02-10 16:32:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2023-10-27 15:57:15 +00:00
|
|
|
"errors"
|
2023-02-10 16:32:46 +00:00
|
|
|
"fmt"
|
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-11-12 13:39:04 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache/reload"
|
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
|
2023-12-23 12:17:42 +00:00
|
|
|
pageEle pagination.Render
|
2023-04-09 13:51:12 +00:00
|
|
|
TotalRows int
|
|
|
|
postsPlugin PostsPlugin
|
2023-02-28 15:59:17 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
func (h *Handle) GetIndexHandle() *IndexHandle {
|
|
|
|
v, ok := h.C.Get("indexHandle")
|
|
|
|
if !ok {
|
|
|
|
vv := NewIndexHandle(h)
|
|
|
|
h.C.Set("indexHandle", vv)
|
|
|
|
return vv
|
|
|
|
}
|
|
|
|
return v.(*IndexHandle)
|
|
|
|
}
|
|
|
|
|
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-12-23 12:17:42 +00:00
|
|
|
func (i *IndexHandle) PageEle() pagination.Render {
|
2023-04-09 13:51:12 +00:00
|
|
|
return i.pageEle
|
2023-02-28 15:59:17 +00:00
|
|
|
}
|
|
|
|
|
2023-12-23 12:17:42 +00:00
|
|
|
func (i *IndexHandle) SetPageEle(pageEle pagination.Render) {
|
2023-04-09 13:51:12 +00:00
|
|
|
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-05-18 14:27:28 +00:00
|
|
|
q := &model.QueryCondition{
|
2023-02-25 15:10:42 +00:00
|
|
|
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-11-25 09:49:20 +00:00
|
|
|
posts, totalRaw, err = cache.PostLists(i.C, i.Param.CacheKey, 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-11-25 09:49:20 +00:00
|
|
|
posts, totalRaw, err = cache.SearchPost(i.C, i.Param.CacheKey, 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)
|
|
|
|
}
|
2023-12-23 12:17:42 +00:00
|
|
|
i.ginH["pagination"] = pagination.Paginate(i.pageEle, i.TotalRows, i.Param.PageSize, i.Param.Page, i.Param.PaginationStep, *i.C.Request.URL, i.IsHttps())
|
2023-02-11 15:51:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
func (i *IndexHandle) BuildIndexData() (err error) {
|
|
|
|
if i.Param == nil {
|
|
|
|
i.Param = NewIndexParams(i.C)
|
|
|
|
}
|
|
|
|
err = i.ParseIndex(i.Param)
|
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-10-27 15:57:15 +00:00
|
|
|
if err != nil && !errors.Is(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-12-03 14:42:44 +00:00
|
|
|
i.ginH["totalPage"] = number.DivideCeil(totalRows, i.Param.PageSize)
|
2023-02-10 16:32:46 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-11 15:51:07 +00:00
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
var GetPostsPlugin = reload.BuildValFnWithAnyParams("postPlugins", UsePostsPlugins)
|
|
|
|
|
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 {
|
2024-01-18 15:29:50 +00:00
|
|
|
fn = GetPostsPlugin()
|
2023-05-09 05:44:20 +00:00
|
|
|
}
|
|
|
|
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) {
|
2024-01-18 15:29:50 +00:00
|
|
|
i := h.GetIndexHandle()
|
2023-04-04 15:02:54 +00:00
|
|
|
i.ExecPostsPlugin()
|
|
|
|
i.Pagination()
|
|
|
|
i.ginH["posts"] = i.Posts
|
|
|
|
}
|
|
|
|
|
2023-05-05 12:45:20 +00:00
|
|
|
func Index(h *Handle) {
|
2024-01-18 15:29:50 +00:00
|
|
|
i := h.GetIndexHandle()
|
|
|
|
err := i.BuildIndexData()
|
2023-05-05 12:45:20 +00:00
|
|
|
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) {
|
2024-01-18 15:29:50 +00:00
|
|
|
a := GetStickPosts(i.Handle)
|
2023-03-29 13:58:39 +00:00
|
|
|
if len(a) < 1 {
|
|
|
|
return
|
|
|
|
}
|
2024-01-18 15:29:50 +00:00
|
|
|
m := GetStickMapPosts(i.Handle)
|
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
|
|
|
})...)
|
|
|
|
}
|