notice.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. "net/http"
  7. "github.com/unknwon/com"
  8. "github.com/unknwon/paginater"
  9. log "unknwon.dev/clog/v2"
  10. "gogs.io/gogs/internal/conf"
  11. "gogs.io/gogs/internal/context"
  12. "gogs.io/gogs/internal/db"
  13. )
  14. const (
  15. NOTICES = "admin/notice"
  16. )
  17. func Notices(c *context.Context) {
  18. c.Title("admin.notices")
  19. c.Data["PageIsAdmin"] = true
  20. c.Data["PageIsAdminNotices"] = true
  21. total := db.CountNotices()
  22. page := c.QueryInt("page")
  23. if page <= 1 {
  24. page = 1
  25. }
  26. c.Data["Page"] = paginater.New(int(total), conf.UI.Admin.NoticePagingNum, page, 5)
  27. notices, err := db.Notices(page, conf.UI.Admin.NoticePagingNum)
  28. if err != nil {
  29. c.Error(err, "list notices")
  30. return
  31. }
  32. c.Data["Notices"] = notices
  33. c.Data["Total"] = total
  34. c.Success(NOTICES)
  35. }
  36. func DeleteNotices(c *context.Context) {
  37. strs := c.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 := db.DeleteNoticesByIDs(ids); err != nil {
  46. c.Flash.Error("DeleteNoticesByIDs: " + err.Error())
  47. c.Status(http.StatusInternalServerError)
  48. } else {
  49. c.Flash.Success(c.Tr("admin.notices.delete_success"))
  50. c.Status(http.StatusOK)
  51. }
  52. }
  53. func EmptyNotices(c *context.Context) {
  54. if err := db.DeleteNotices(0, 0); err != nil {
  55. c.Error(err,"delete notices")
  56. return
  57. }
  58. log.Trace("System notices deleted by admin (%s): [start: %d]", c.User.Name, 0)
  59. c.Flash.Success(c.Tr("admin.notices.delete_success"))
  60. c.Redirect(conf.Server.Subpath + "/admin/notices")
  61. }