1
0

notice.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package admin
  2. import (
  3. "net/http"
  4. "strconv"
  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 := max(c.QueryInt("page"), 1)
  20. c.Data["Page"] = paginater.New(int(total), conf.UI.Admin.NoticePagingNum, page, 5)
  21. notices, err := database.Handle.Notices().List(c.Req.Context(), page, conf.UI.Admin.NoticePagingNum)
  22. if err != nil {
  23. c.Error(err, "list notices")
  24. return
  25. }
  26. c.Data["Notices"] = notices
  27. c.Data["Total"] = total
  28. c.Success(NOTICES)
  29. }
  30. func DeleteNotices(c *context.Context) {
  31. strs := c.QueryStrings("ids[]")
  32. ids := make([]int64, 0, len(strs))
  33. for i := range strs {
  34. id, _ := strconv.ParseInt(strs[i], 10, 64)
  35. if id > 0 {
  36. ids = append(ids, id)
  37. }
  38. }
  39. if err := database.Handle.Notices().DeleteByIDs(c.Req.Context(), ids...); err != nil {
  40. c.Flash.Error("DeleteNoticesByIDs: " + err.Error())
  41. c.Status(http.StatusInternalServerError)
  42. } else {
  43. c.Flash.Success(c.Tr("admin.notices.delete_success"))
  44. c.Status(http.StatusOK)
  45. }
  46. }
  47. func EmptyNotices(c *context.Context) {
  48. if err := database.Handle.Notices().DeleteAll(c.Req.Context()); err != nil {
  49. c.Error(err, "delete notices")
  50. return
  51. }
  52. log.Trace("System notices deleted by admin (%s): [start: %d]", c.User.Name, 0)
  53. c.Flash.Success(c.Tr("admin.notices.delete_success"))
  54. c.Redirect(conf.Server.Subpath + "/admin/notices")
  55. }