@@ -331,3 +331,64 @@ func ViewBadgeUsers(ctx *context.Context) {
331331 ctx .Data ["Pages" ] = context .NewPagination (int (count ), setting .UI .Admin .UserPagingNum , page , 5 )
332332 ctx .HTML (http .StatusOK , tplBadgeUsers )
333333}
334+
335+ func RenderBadgeSearch (ctx * context.Context , opts * user_model.SearchBadgeOptions , tplName templates.TplName ) {
336+ // Sitemap index for sitemap paths
337+ opts .Page = int (ctx .PathParamInt64 ("idx" ))
338+ if opts .Page <= 1 {
339+ opts .Page = ctx .FormInt ("page" )
340+ }
341+ if opts .Page <= 1 {
342+ opts .Page = 1
343+ }
344+
345+ var (
346+ badges []* user_model.Badge
347+ count int64
348+ err error
349+ orderBy db.SearchOrderBy
350+ )
351+
352+ // we can not set orderBy to `models.SearchOrderByXxx`, because there may be a JOIN in the statement, different tables may have the same name columns
353+
354+ sortOrder := ctx .FormString ("sort" )
355+ if sortOrder == "" {
356+ sortOrder = setting .UI .ExploreDefaultSort
357+ }
358+ ctx .Data ["SortType" ] = sortOrder
359+
360+ switch sortOrder {
361+ case "newest" :
362+ orderBy = "`badge`.id DESC"
363+ case "oldest" :
364+ orderBy = "`badge`.id ASC"
365+ case "reversealphabetically" :
366+ orderBy = "`badge`.slug DESC"
367+ case "alphabetically" :
368+ orderBy = "`badge`.slug ASC"
369+ default :
370+ // in case the sortType is not valid, we set it to recent update
371+ ctx .Data ["SortType" ] = "oldest"
372+ orderBy = "`badge`.id ASC"
373+ }
374+
375+ opts .Keyword = ctx .FormTrim ("q" )
376+ opts .OrderBy = orderBy
377+ if len (opts .Keyword ) == 0 || isKeywordValid (opts .Keyword ) {
378+ badges , count , err = user_model .SearchBadges (ctx , opts )
379+ if err != nil {
380+ ctx .ServerError ("SearchBadges" , err )
381+ return
382+ }
383+ }
384+
385+ ctx .Data ["Keyword" ] = opts .Keyword
386+ ctx .Data ["Total" ] = count
387+ ctx .Data ["Badges" ] = badges
388+
389+ pager := context .NewPagination (int (count ), opts .PageSize , opts .Page , 5 )
390+ pager .AddParamFromRequest (ctx .Req )
391+ ctx .Data ["Page" ] = pager
392+
393+ ctx .HTML (http .StatusOK , tplName )
394+ }
0 commit comments