package controllers
import (
"github.com/astaxie/beego"
"github.com/lifei6671/mindoc/conf"
"github.com/lifei6671/mindoc/models"
"github.com/lifei6671/mindoc/utils/pagination"
"regexp"
"strconv"
"strings"
)
type SearchController struct {
BaseController
}
//搜索首页
func (c *SearchController) Index() {
c.Prepare()
c.TplName = "search/index.tpl"
//如果没有开启你们访问则跳转到登录
if !c.EnableAnonymous && c.Member == nil {
c.Redirect(beego.URLFor("AccountController.Login"), 302)
return
}
keyword := c.GetString("keyword")
pageIndex, _ := c.GetInt("page", 1)
c.Data["BaseUrl"] = c.BaseUrl()
if keyword != "" {
c.Data["Keyword"] = keyword
member_id := 0
if c.Member != nil {
member_id = c.Member.MemberId
}
search_result, totalCount, err := models.NewDocumentSearchResult().FindToPager(keyword, pageIndex, conf.PageSize, member_id)
if err != nil {
beego.Error(err)
return
}
if totalCount > 0 {
pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize)
c.Data["PageHtml"] = pager.HtmlPages()
} else {
c.Data["PageHtml"] = ""
}
if len(search_result) > 0 {
for _, item := range search_result {
item.DocumentName = strings.Replace(item.DocumentName, keyword, ""+keyword+"", -1)
if item.Description != "" {
src := item.Description
//将HTML标签全转换成小写
re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
src = re.ReplaceAllStringFunc(src, strings.ToLower)
//去除STYLE
re, _ = regexp.Compile("\\