notice.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package admin
  5. import (
  6. "github.com/Unknwon/com"
  7. "github.com/Unknwon/paginater"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/log"
  11. "github.com/gogits/gogs/modules/middleware"
  12. "github.com/gogits/gogs/modules/setting"
  13. )
  14. const (
  15. NOTICES base.TplName = "admin/notice"
  16. )
  17. func Notices(ctx *middleware.Context) {
  18. ctx.Data["Title"] = ctx.Tr("admin.notices")
  19. ctx.Data["PageIsAdmin"] = true
  20. ctx.Data["PageIsAdminNotices"] = true
  21. total := models.CountNotices()
  22. page := ctx.QueryInt("page")
  23. if page <= 1 {
  24. page = 1
  25. }
  26. ctx.Data["Page"] = paginater.New(int(total), setting.AdminNoticePagingNum, page, 5)
  27. notices, err := models.Notices(page, setting.AdminNoticePagingNum)
  28. if err != nil {
  29. ctx.Handle(500, "Notices", err)
  30. return
  31. }
  32. ctx.Data["Notices"] = notices
  33. ctx.Data["Total"] = total
  34. ctx.HTML(200, NOTICES)
  35. }
  36. func DeleteNotices(ctx *middleware.Context) {
  37. strs := ctx.QueryStrings("ids[]")
  38. ids := make([]int64, 0, len(strs))
  39. for i := range strs {
  40. id := com.StrTo(strs[i]).MustInt64()
  41. if id > 0 {
  42. ids = append(ids, id)
  43. }
  44. }
  45. if err := models.DeleteNoticesByIDs(ids); err != nil {
  46. ctx.Flash.Error("DeleteNoticesByIDs: " + err.Error())
  47. ctx.Status(500)
  48. } else {
  49. ctx.Flash.Success(ctx.Tr("admin.notices.delete_success"))
  50. ctx.Status(200)
  51. }
  52. }
  53. func EmptyNotices(ctx *middleware.Context) {
  54. if err := models.DeleteNotices(0, 0); err != nil {
  55. ctx.Handle(500, "DeleteNotices", err)
  56. return
  57. }
  58. log.Trace("System notices deleted by admin (%s): [start: %d]", ctx.User.Name, 0)
  59. ctx.Flash.Success(ctx.Tr("admin.notices.delete_success"))
  60. ctx.Redirect(setting.AppSubUrl + "/admin/notices")
  61. }