notice.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package admin
  2. import (
  3. "net/http"
  4. "github.com/unknwon/com"
  5. "github.com/unknwon/paginater"
  6. log "unknwon.dev/clog/v2"
  7. "gogs.io/gogs/internal/conf"
  8. "gogs.io/gogs/internal/context"
  9. "gogs.io/gogs/internal/database"
  10. )
  11. const (
  12. NOTICES = "admin/notice"
  13. )
  14. func Notices(c *context.Context) {
  15. c.Title("admin.notices")
  16. c.Data["PageIsAdmin"] = true
  17. c.Data["PageIsAdminNotices"] = true
  18. total := database.Handle.Notices().Count(c.Req.Context())
  19. page := c.QueryInt("page")
  20. if page <= 1 {
  21. page = 1
  22. }
  23. c.Data["Page"] = paginater.New(int(total), conf.UI.Admin.NoticePagingNum, page, 5)
  24. notices, err := database.Handle.Notices().List(c.Req.Context(), page, conf.UI.Admin.NoticePagingNum)
  25. if err != nil {
  26. c.Error(err, "list notices")
  27. return
  28. }
  29. c.Data["Notices"] = notices
  30. c.Data["Total"] = total
  31. c.Success(NOTICES)
  32. }
  33. func DeleteNotices(c *context.Context) {
  34. strs := c.QueryStrings("ids[]")
  35. ids := make([]int64, 0, len(strs))
  36. for i := range strs {
  37. id := com.StrTo(strs[i]).MustInt64()
  38. if id > 0 {
  39. ids = append(ids, id)
  40. }
  41. }
  42. if err := database.Handle.Notices().DeleteByIDs(c.Req.Context(), ids...); err != nil {
  43. c.Flash.Error("DeleteNoticesByIDs: " + err.Error())
  44. c.Status(http.StatusInternalServerError)
  45. } else {
  46. c.Flash.Success(c.Tr("admin.notices.delete_success"))
  47. c.Status(http.StatusOK)
  48. }
  49. }
  50. func EmptyNotices(c *context.Context) {
  51. if err := database.Handle.Notices().DeleteAll(c.Req.Context()); err != nil {
  52. c.Error(err, "delete notices")
  53. return
  54. }
  55. log.Trace("System notices deleted by admin (%s): [start: %d]", c.User.Name, 0)
  56. c.Flash.Success(c.Tr("admin.notices.delete_success"))
  57. c.Redirect(conf.Server.Subpath + "/admin/notices")
  58. }