admin.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package activity
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "reflect"
  8. "strings"
  9. "github.com/qor5/admin/presets"
  10. "github.com/qor5/ui/vuetify"
  11. "github.com/qor5/ui/vuetifyx"
  12. "github.com/qor5/web"
  13. "github.com/qor5/x/i18n"
  14. h "github.com/theplant/htmlgo"
  15. "golang.org/x/text/language"
  16. )
  17. const (
  18. I18nActivityKey i18n.ModuleKey = "I18nActivityKey"
  19. )
  20. func (ab *ActivityBuilder) configureAdmin(b *presets.Builder) {
  21. b.I18n().
  22. RegisterForModule(language.English, I18nActivityKey, Messages_en_US).
  23. RegisterForModule(language.SimplifiedChinese, I18nActivityKey, Messages_zh_CN)
  24. if permB := b.GetPermission(); permB != nil {
  25. permB.CreatePolicies(permPolicy)
  26. }
  27. var (
  28. mb = b.Model(ab.logModel).MenuIcon("receipt_long")
  29. listing = mb.Listing("CreatedAt", "Creator", "Action", "ModelKeys", "ModelLabel", "ModelName")
  30. detailing = mb.Detailing("ModelDiffs")
  31. )
  32. ab.lmb = mb
  33. listing.Field("CreatedAt").Label(Messages_en_US.ModelCreatedAt).ComponentFunc(
  34. func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  35. return h.Td(h.Text(obj.(*ActivityLog).CreatedAt.Format("2006-01-02 15:04:05 MST")))
  36. },
  37. )
  38. listing.Field("ModelKeys").Label(Messages_en_US.ModelKeys)
  39. listing.Field("ModelName").Label(Messages_en_US.ModelName)
  40. listing.Field("ModelLabel").Label(Messages_en_US.ModelLabel).ComponentFunc(
  41. func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  42. if obj.(*ActivityLog).ModelLabel == "" {
  43. return h.Td(h.Text("-"))
  44. }
  45. return h.Td(h.Text(obj.(*ActivityLog).ModelLabel))
  46. },
  47. )
  48. listing.FilterDataFunc(func(ctx *web.EventContext) vuetifyx.FilterData {
  49. var (
  50. msgr = i18n.MustGetModuleMessages(ctx.R, I18nActivityKey, Messages_en_US).(*Messages)
  51. contextDB = ab.getDBFromContext(ctx.R.Context())
  52. )
  53. creatorGroups := ab.NewLogModelSlice()
  54. contextDB.Select("creator").Group("creator").Find(creatorGroups)
  55. creatorGroupsValues := reflect.Indirect(reflect.ValueOf(creatorGroups))
  56. var creatorOptions []*vuetifyx.SelectItem
  57. for i := 0; i < creatorGroupsValues.Len(); i++ {
  58. creator := reflect.Indirect(creatorGroupsValues.Index(i)).FieldByName("Creator").String()
  59. creatorOptions = append(creatorOptions, &vuetifyx.SelectItem{
  60. Text: creator,
  61. Value: creator,
  62. })
  63. }
  64. actionGroups := ab.NewLogModelSlice()
  65. contextDB.Select("action").Group("action").Order("action").Find(actionGroups)
  66. actionGroupsValues := reflect.Indirect(reflect.ValueOf(actionGroups))
  67. var actionOptions []*vuetifyx.SelectItem
  68. for i := 0; i < actionGroupsValues.Len(); i++ {
  69. creator := reflect.Indirect(actionGroupsValues.Index(i)).FieldByName("Action").String()
  70. actionOptions = append(actionOptions, &vuetifyx.SelectItem{
  71. Text: creator,
  72. Value: creator,
  73. })
  74. }
  75. var modelOptions []*vuetifyx.SelectItem
  76. for _, m := range ab.models {
  77. modelOptions = append(modelOptions, &vuetifyx.SelectItem{
  78. Text: m.typ.Name(),
  79. Value: m.typ.Name(),
  80. })
  81. }
  82. return []*vuetifyx.FilterItem{
  83. {
  84. Key: "action",
  85. Label: msgr.FilterAction,
  86. ItemType: vuetifyx.ItemTypeSelect,
  87. SQLCondition: `action %s ?`,
  88. Options: actionOptions,
  89. },
  90. {
  91. Key: "created",
  92. Label: msgr.FilterCreatedAt,
  93. ItemType: vuetifyx.ItemTypeDatetimeRange,
  94. SQLCondition: `created_at %s ?`,
  95. },
  96. {
  97. Key: "creator",
  98. Label: msgr.FilterCreator,
  99. ItemType: vuetifyx.ItemTypeSelect,
  100. SQLCondition: `creator %s ?`,
  101. Options: creatorOptions,
  102. },
  103. {
  104. Key: "model",
  105. Label: msgr.FilterModel,
  106. ItemType: vuetifyx.ItemTypeSelect,
  107. SQLCondition: `model_name %s ?`,
  108. Options: modelOptions,
  109. },
  110. }
  111. })
  112. listing.FilterTabsFunc(func(ctx *web.EventContext) []*presets.FilterTab {
  113. msgr := i18n.MustGetModuleMessages(ctx.R, I18nActivityKey, Messages_en_US).(*Messages)
  114. return []*presets.FilterTab{
  115. {
  116. Label: msgr.ActionAll,
  117. Query: url.Values{"action": []string{}},
  118. },
  119. {
  120. Label: msgr.ActionEdit,
  121. Query: url.Values{"action": []string{ActivityEdit}},
  122. },
  123. {
  124. Label: msgr.ActionCreate,
  125. Query: url.Values{"action": []string{ActivityCreate}},
  126. },
  127. {
  128. Label: msgr.ActionDelete,
  129. Query: url.Values{"action": []string{ActivityDelete}},
  130. },
  131. }
  132. })
  133. detailing.Field("ModelDiffs").Label("Detail").ComponentFunc(
  134. func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) (r h.HTMLComponent) {
  135. var (
  136. record = obj.(ActivityLogInterface)
  137. msgr = i18n.MustGetModuleMessages(ctx.R, I18nActivityKey, Messages_en_US).(*Messages)
  138. )
  139. var detailElems []h.HTMLComponent
  140. detailElems = append(detailElems, vuetify.VCard(
  141. vuetify.VCardTitle(
  142. vuetify.VBtn("").Children(
  143. vuetify.VIcon("arrow_back").Class("pr-2").Small(true),
  144. ).Icon(true).Attr("@click", "window.history.back()"),
  145. h.Text(msgr.DiffDetail),
  146. ),
  147. vuetify.VSimpleTable(
  148. h.Tbody(
  149. h.Tr(h.Td(h.Text(msgr.ModelCreator)), h.Td(h.Text(record.GetCreator()))),
  150. h.Tr(h.Td(h.Text(msgr.ModelUserID)), h.Td(h.Text(fmt.Sprintf("%v", record.GetUserID())))),
  151. h.Tr(h.Td(h.Text(msgr.ModelAction)), h.Td(h.Text(record.GetAction()))),
  152. h.Tr(h.Td(h.Text(msgr.ModelName)), h.Td(h.Text(record.GetModelName()))),
  153. h.Tr(h.Td(h.Text(msgr.ModelLabel)), h.Td(h.Text(record.GetModelLabel()))),
  154. h.Tr(h.Td(h.Text(msgr.ModelKeys)), h.Td(h.Text(record.GetModelKeys()))),
  155. h.If(record.GetModelLink() != "", h.Tr(h.Td(h.Text(msgr.ModelLink)), h.Td(h.Text(record.GetModelLink())))),
  156. h.Tr(h.Td(h.Text(msgr.ModelCreatedAt)), h.Td(h.Text(record.GetCreatedAt().Format("2006-01-02 15:04:05 MST")))),
  157. ),
  158. ),
  159. ).Attr("style", "margin-top:15px;margin-bottom:15px;"))
  160. if d := field.Value(obj).(string); d != "" {
  161. detailElems = append(detailElems, DiffComponent(d, ctx.R))
  162. }
  163. return h.Components(detailElems...)
  164. },
  165. )
  166. }
  167. func fixSpecialChars(str string) string {
  168. str = strings.Replace(str, "{", "[", -1)
  169. str = strings.Replace(str, "}", "]", -1)
  170. return str
  171. }
  172. func DiffComponent(diffstr string, req *http.Request) h.HTMLComponent {
  173. var diffs []Diff
  174. err := json.Unmarshal([]byte(diffstr), &diffs)
  175. if err != nil {
  176. return nil
  177. }
  178. if len(diffs) == 0 {
  179. return nil
  180. }
  181. var (
  182. newdiffs []Diff
  183. changediffs []Diff
  184. deletediffs []Diff
  185. )
  186. for _, diff := range diffs {
  187. if diff.Now == "" && diff.Old != "" {
  188. deletediffs = append(deletediffs, diff)
  189. continue
  190. }
  191. if diff.Now != "" && diff.Old == "" {
  192. newdiffs = append(newdiffs, diff)
  193. continue
  194. }
  195. if diff.Now != "" && diff.Old != "" {
  196. changediffs = append(changediffs, diff)
  197. continue
  198. }
  199. }
  200. var diffsElems []h.HTMLComponent
  201. msgr := i18n.MustGetModuleMessages(req, I18nActivityKey, Messages_en_US).(*Messages)
  202. if len(newdiffs) > 0 {
  203. var elems []h.HTMLComponent
  204. for _, d := range newdiffs {
  205. elems = append(elems, h.Tr(h.Td(h.Text(d.Field)), h.Td(h.Text(fixSpecialChars(d.Now)))))
  206. }
  207. diffsElems = append(diffsElems,
  208. vuetify.VCard(
  209. vuetify.VCardTitle(h.Text(msgr.DiffNew)),
  210. vuetify.VSimpleTable(
  211. h.Thead(h.Tr(h.Th(msgr.DiffField), h.Th(msgr.DiffValue))),
  212. h.Tbody(elems...),
  213. ),
  214. ).Attr("style", "margin-top:15px;margin-bottom:15px;"))
  215. }
  216. if len(deletediffs) > 0 {
  217. var elems []h.HTMLComponent
  218. for _, d := range deletediffs {
  219. elems = append(elems, h.Tr(h.Td(h.Text(d.Field)), h.Td(h.Text(fixSpecialChars(d.Old)))))
  220. }
  221. diffsElems = append(diffsElems,
  222. vuetify.VCard(
  223. vuetify.VCardTitle(h.Text(msgr.DiffDelete)),
  224. vuetify.VSimpleTable(
  225. h.Thead(h.Tr(h.Th(msgr.DiffField), h.Th(msgr.DiffValue))),
  226. h.Tbody(elems...),
  227. ),
  228. ).Attr("style", "margin-top:15px;margin-bottom:15px;"))
  229. }
  230. if len(changediffs) > 0 {
  231. var elems []h.HTMLComponent
  232. for _, d := range changediffs {
  233. elems = append(elems, h.Tr(h.Td(h.Text(d.Field)), h.Td(h.Text(fixSpecialChars(d.Old)), h.Td(h.Text(fixSpecialChars(d.Now))))))
  234. }
  235. diffsElems = append(diffsElems,
  236. vuetify.VCard(
  237. vuetify.VCardTitle(h.Text(msgr.DiffChanges)),
  238. vuetify.VSimpleTable(
  239. h.Thead(h.Tr(h.Th(msgr.DiffField), h.Th(msgr.DiffOld), h.Th(msgr.DiffNow))),
  240. h.Tbody(elems...),
  241. ),
  242. ).Attr("style", "margin-top:15px;margin-bottom:15px;"))
  243. }
  244. return h.Components(diffsElems...)
  245. }